-
Notifications
You must be signed in to change notification settings - Fork 3
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
CJS support #7
Comments
I found a better way is to rely on the original format through const {format, source} = await defaultLoad(url, context, defaultLoad); This solves my use-case but is still not a solution to getting the output format through babel. Although I am not sure it makes sense for babel to transform from ESM to CJS in this particular case because the load hook cannot emit source for the |
This is expected, since commonjs modules do not provide named exports. Recently, Guy Bedford added a lexer to node that attempts to provide named exports for commonjs modules, but this is not guaranteed to work in all cases. See https://nodejs.org/dist/latest-v17.x/docs/api/esm.html#import-statements, which says "When importing CommonJS modules, the module.exports object is provided as the default export. Named exports may be available, provided by static analysis as a convenience for better ecosystem compatibility." Is the problem you're encountering that the static analysis of that lexer is skipped when a loader is applied? If so, I think that the best way to solve that would be to allow the user of node-loader-babel to customize the useLoader function. |
OK. I thought I had pinpointed it. See explanation in #8 (comment). I will double-check and come back to you. PS: BTW @joeldenning, thanks for your great work on this software. Other than this minor "issue", it has worked wonders so far. |
I created a reproduction repository at https:/bisectifiate/node-loader-babel-commonjs. I tested the following (:heavy_check_mark: means it works, :heavy_multiplication_x: means it does not work):
It seems To rule out static analysis as the culprit, there is this PR which imports the commonjs source with a default import (
Note that this use case works without a loader or with a patched babel loader. What I think is happening with
Just for your information, importing the default export from a |
I was able to replicate |
Fix released in https:/node-loader/node-loader-babel/releases/tag/v2.0.1 |
Added a bit of documentation that's available at https:/node-loader/node-loader-babel#commonjs-modules |
I found at least one offensive line here:
node-loader-babel/lib/node-loader-babel.js
Lines 46 to 48 in 4d53080
If I replace the format definition with
format: /\.cjs$/.test(filename) ? "commonjs" : "module",
it works for me so I assume something was overlooked here. This may not be the best way to handle this but at least it shows I can fix it to work for my use case.If I do not do that, I get tons of errors similar to
SyntaxError: The requested module '#foo' does not provide an export named bar
when importing some commonjs module. I am also using@node-loader/import-maps
so maybe there is some interdependence there?The text was updated successfully, but these errors were encountered: