-
Notifications
You must be signed in to change notification settings - Fork 458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@rescript/core package doesn't have the javascript file #6754
Comments
rescript-core only ships with mjs files. You could try to change your rescript.json to {
...
"package-specs": [
{
- "module": "commonjs",
+ "module": "esmodule",
"in-source": true
}
],
- "module": ".res.js",
+ "module": ".res.mjs",
...
} |
@fhammerschmidt Thanks, using
Using
Re-running
Finally, it seems like
|
Also switching to
Note that this happens with a regular cra |
Ok sorry about the noise, but if I understand correctly, create-react-app does not support mjs files out of the box according to facebook/create-react-app#10356 . It would be better if we don't have to update downstream build script just for that. Would it be possible to publish the javascript version of the std libs so that we can upgrade to rescript seamlessly? |
Ah yes, just Sorry, I guess you need to still ship the whole runtime for now since ReScriptCore did not really take into account that feature yet. It will be seamlessly when Core is part of the compiler. Also, just a recommendation: Consider switching to vite, it's so much better and easier to maintain than create-react-app and esmodule should be the default over commonjs already IMO. And when you use esmodule your end-users should be able to tree-shake all unused code away. |
Well I'm not familiar with react/vite/next, and my issue is that using It looks like // In config-overrides.js add:
function supportMJS(config) {
config.module.rules.push({
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto"
});
return config;
}
const { override } = require("customize-cra");
module.exports = override(supportMJS, rewiredEsbuild()); That will do, thanks for the explanation @fhammerschmidt . |
Don't you still have this issue?
|
No, this is gone after adding |
On second thought, you should really really use a bundler for publishing the library as well. And I suggest we mention that on the rescript-std page on rescript-lang.org but I will do that myself. |
Sounds great to me, thanks for your help! |
@fhammerschmidt where you able to update the rescript-lang doc on how to do that? Is it possible to not bundle react and let the downstream project link with the version it uses? |
I fiddled around with nanobundle on your repo. I think for JS consumers the most viable path is to bundle all ReScript dependencies and only require react to be installed. But that is pretty specific to your use-case. I don't want to create a guide without really having some practical experiences from the community. Here's roughly what to do:
Edit: this would at least support the ESM users, I think. You can also create bundles for both esm and cjs if you need to. See "dual-package exports" section: https://github.com/cometkim/espub/tree/main/packages/nanobundle |
@fhammerschmidt That's amazing, thank you so much for helping me out here! The procedure works great and the bundle command makes a standalone 25kb ReAnsi.res.js file. However, updating my downstream app still failed because it couldn't resolve Thanks again for your help, it's much appreciated! |
Actually you don't need Also all It's glad to hear that nanobundle has helped the problem, but this is not an ideal for ReScript. I'm focusing on improving the library authoring experience on both espub and ReScript. ReScript should be enough to build a JavaScript (and TypeScript) libraries without relying on external tools. |
While the create-react-app build worked when using
According to facebook/create-react-app#9938 , adding
When using ".mjs" extension, the tests pass (though with this warning
|
How this works is beyond my comprehension, I guess the culprit is lack of esmodule support from node/create-react-app/jest. I'm not sure why adding the For the reference, here is the change that integrates the latest re-ansi library in the downstream javascript project: https://review.opendev.org/c/zuul/zuul/+/921474 |
Jest has supported ESM since v29.7, but it is still experimental and never stabilized. The latest release of create-react-app was 2 years ago, with Jest v27. Seriously, it's a graveyard technology. I'd recommend migrating to a modern toolchain like Vite + Vitest. |
Yes, I made the same suggestion as well: #6754 (comment) |
compiler libraries should not be embedded by bundlers. It works since it's side-effect-free modules, but it makes a lot of duplication anyway.
So the minimal configuration would be: {
"type": "module",
"main": "./dist/Ansi.res.js",
"exports": {
".": {
"import": "./dist/Ansi.res.js",
"require": "./dist/Ansi.res.cjs"
},
"./package.json": "./package.json"
},
"scripts": {
"test": "npm run build && node tests/Spec.res.mjs",
"bundle": "nanobundle build --clean --out-dir dist"
},
"peerDependencies": {
"@rescript/core": "^1.0.0",
"@rescript/react": "^0.12.0",
"react": "^17.0.0 || ^18.0.0 || ^19.0.0-0",
"react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0-0",
"rescript": "^11.0.0"
},
"devDependencies": {
"@rescript/core": "^1.3.0",
"@rescript/react": "^0.12.1",
"nanobundle": "^2.1.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"rescript": "^11.1.0"
}
} Unfortunately, this doesn't work without an importmap. The {
"imports": {
"@rescript/core/src/Core__List.res.js": {
"import": "@rescript/core/src/Core__List.res.js",
"require": "@rescript/core/src/Core__List.res.cjs"
},
"@rescript/core/src/Core__Option.res.js": {
"import": "@rescript/core/src/Core__Option.res.js",
"require": "@rescript/core/src/Core__Option.res.cjs"
},
"rescript/lib/es6/": {
"import": "rescript/lib/es6/",
"require": "rescript/lib/js/"
}
},
} This also doesn't work. Because the Honestly this is a nightmare to library authors and will never be fixed until the dual package support is implemented. |
@cometkim thank you for your suggestions and for your work to improve the library authoring experience. I don't own the downstream app and migrating to Vitest just for this does not sound reasonable. I picked rescript to avoid dealing with such systems in the first place, so it's a bit unfortunate that regular scripts are no longer working for javascript user. Though I understand the need to move on to esmodules and I'm glad we can work around them with the transformIgnorePatterns trick. As you can probably tell, I'm not familiar with the javascript stack, and it's surprising to read that create-react-app is already deprecated, which makes me wonder how long until Vite also become graveyard :) |
Hello folks,
I'm trying to update one of my package (repo) from bs-platform 8.2 to rescript 11, and I get the following error when importing it in a regular javascript project:
According to https://rescript-lang.org/docs/manual/latest/build-external-stdlib we need to use
@rescript/std
, and even though that removes the requirements fromrescript
, some modules from@rescript/core
are not readily available as JavaScript. Is there something I am missing?Thanks in advance,
-Tristan
The text was updated successfully, but these errors were encountered: