-
Notifications
You must be signed in to change notification settings - Fork 27
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
CommonJS import from a Module package too inflexible #57
Comments
Thanks for reporting the issue, and as you can likely tell there is quite a bit of complex reasoning for how we got here. The short answer is that the cases you're talking about supporting like The restriction of needing to work out what a CommonJS module will export before we can execute it is one that was imposed by a combination of requirements from the ECMAScript modules spec itself to Node.js requirements, that were all fought and argued extensively. While it is possible in theory to support these cases via static analysis, the other constraint this project has is to extract the list of exports incredibly quickly so there is no execution slowdown. More advanced code analysis to determine There are likely simple cases we can improve - eg we don't currently support Let me know if that answers your question and sorry we can't do better than this. |
I think it answers my questions and makes sense why an eval is completely out of the question. Further I wondered if we could use the *.d.ts if available to try aid in the populating of exports. I think this could be more efficient than running eval on the js file itself, however still goes against your goals and probably would have perf impact as it's still not just a regex expression. Cutting that as an option as a whole then. Below you'll find some notes I had per package and why they didn't work based on your answers. This is mostly for anyone who comes to see this issue later. On this, I definitely think there is a bug with the At this point, feel free to close this issue or keep it open to fix the enzyme example I provided. However I think you've answered all my questions so thank you! chalkunpkg link to package contents Reason this library is unable to parse the commonjs contents is because the exports are defined as such However, this package does provide a index.d.ts... which is very complicated and would not be useful. That idea's out the door too. commanderunpkg link to package contents Reason this library is unable to parse is because the exports line is as such: globunpkg link to package contents Same as chalk and commander enzymeunpkg link to package contents This one actually should be parseable. The exports are as follows:
I created a unit test for this particular file and found that it's only picking up the first key
tslibunpkg link to package contents Seems to work as expected which is great! |
Agreed it would be nice to parse the enzyme one. There's actually a way we could support this in the parser I think, although it would be an interesting new parser approach... are you interested in working on the parser? |
As much as I'd love to I simply wont have time. This is only one of multiple issues I'm running into while trying to move my org to be able to use ESM. Currently hunting and debugging each one to open tickets in all the respective projects (as you can see from the investigation above I'm going package by package) is very time consuming. I'll try to loop back when time opens up in case it hasn't been tackled but I'd estimate months out at this point. |
Ok I've posted #58 which we can leave open to track the specific support for whenever whomever has time! Thanks again for the comments. |
I originally opened this over in nodejs/node#39052 and was asked to move this here. Using the same structure since it has quite a bit of detail already.
What steps will reproduce the bug?
Whenever a consuming npm package is
type: module
it seems to be much picker about importing packages of which distribute cjs style code viaimport { bar } from "../lib/factory.js";
. This limitation prevents consuming libraries from usingtype: module
until all the dependent libraries are fixed and corrected which slows adoption.To give an example I've created a repository https://github.com/k2snowman69/bug-nodejs-import-cjs which has two folders
How often does it reproduce? Is there a required condition?
Every time across multiple versions of node
What is the expected behavior?
When running any of the following:
All three succeed in executing. This would mean that even though a dependency of my project might not be written in ESM or be rewritten using the Object.defineProperty style of CJS, that I'm able to use ESM style for my particular project though lose on some of the major benefits if that dependency were written in ESM.
What do you see instead?
If the commonjs library dist output uses the Object.defineProperty to build it's output, it works successfully, however the other two solutions seem to break.
Additional information
This is my first bug in such a public space and I'm a bit nervous, feel free to ask me to reorganize or cleanup the original description to make it easier to understand. Any suggestions would be great, I'm trying to get better at writing issues!
The text was updated successfully, but these errors were encountered: