Skip to content
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

Open
TristanCacqueray opened this issue May 14, 2024 · 10 comments
Open

@rescript/core package doesn't have the javascript file #6754

TristanCacqueray opened this issue May 14, 2024 · 10 comments

Comments

@TristanCacqueray
Copy link
Contributor

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:

Module not found: Can't resolve '@rescript/core/src/Core__List.res.js'

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 from rescript, some modules from @rescript/core are not readily available as JavaScript. Is there something I am missing?

Thanks in advance,
-Tristan

@fhammerschmidt
Copy link
Contributor

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",
  ...
}

@TristanCacqueray
Copy link
Contributor Author

TristanCacqueray commented May 14, 2024

@fhammerschmidt Thanks, using .res.mjs still failed with:

Module not found: Can't resolve '@rescript/core/src/Core__List.res.mjs' 

Using .mjs directly worked, but now the build is failing with:

Module not found: Can't resolve 'react/jsx-runtime'

Re-running yarn install in the downstream project fixed that error, but that results in:

./node_modules/@softwarefactory-project/re-ansi/src/Ansi.mjs
Can't import the named export 'jsx' from non EcmaScript module (only default export is available)

Finally, it seems like rescript is still a required dependencies to fix that other error:

./node_modules/@rescript/core/src/Core__Option.mjs
Cannot find module: 'rescript/lib/es6/caml_option.js'. Make sure this package is installed.

@TristanCacqueray
Copy link
Contributor Author

Also switching to "jsx": {"version": 4, "mode": "classic"} results in:

./node_modules/@softwarefactory-project/re-ansi/src/Ansi.mjs
Can't import the named export 'createElement' from non EcmaScript module (only default export is available)

Note that this happens with a regular cra react-app-rewired project, not nextjs.

@TristanCacqueray
Copy link
Contributor Author

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?

@fhammerschmidt
Copy link
Contributor

Ah yes, just .mjs is what I meant.

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.

TristanCacqueray added a commit to TristanCacqueray/rescript-lang.org that referenced this issue May 14, 2024
@TristanCacqueray
Copy link
Contributor Author

Well I'm not familiar with react/vite/next, and my issue is that using mjs is a breaking change for CRA users.

It looks like .mjs is the way to go nowadays, and for what it's worth, here is the necessary change for react-app-rewired users:

// 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 .

TristanCacqueray added a commit to TristanCacqueray/rescript-lang.org that referenced this issue May 14, 2024
@fhammerschmidt
Copy link
Contributor

Don't you still have this issue?

Finally, it seems like rescript is still a required dependencies to fix that other error:

./node_modules/@rescript/core/src/Core__Option.mjs
Cannot find module: 'rescript/lib/es6/caml_option.js'. Make sure this package is installed.

@TristanCacqueray
Copy link
Contributor Author

Don't you still have this issue?

Finally, it seems like rescript is still a required dependencies to fix that other error:

./node_modules/@rescript/core/src/Core__Option.mjs
Cannot find module: 'rescript/lib/es6/caml_option.js'. Make sure this package is installed.

No, this is gone after adding rescript to the dependencies instead of devDependencies.

TristanCacqueray added a commit to TristanCacqueray/rescript-lang.org that referenced this issue May 14, 2024
@fhammerschmidt
Copy link
Contributor

On second thought, you should really really use a bundler for publishing the library as well.
Check out nanobundle for instance:
https://forum.rescript-lang.org/t/bundle-rescript-libraries-with-dual-package-exports/4058

And I suggest we mention that on the rescript-std page on rescript-lang.org but I will do that myself.

@TristanCacqueray
Copy link
Contributor Author

Sounds great to me, thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants