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
module: CJS exports detection for all CJS modules #35249
Conversation
|
Review requested: |
|
what do the numbers look like on this one? |
|
As someone less familiar with the code here, but interested in improved ecosystem interop, I really like this approach and think it would greatly help a lot of the ecosystem. |
This comment has been hidden.
This comment has been hidden.
|
@guybedford looks like this needs a rebase |
|
Those numbers aren't looking too good. |
|
I made several updates to my test suite. I made the test run in a child process, thereby working around the bug that was previously causing crashes. I also installed the packages to test in separate subfolders for each set of 200 packages, to work around how The upshot of all of this is that now I can install packages so much faster that I changed “top 3,000” to “top 20,000,” though there are still many packages that get excluded from testing for various reasons (expecting browser or Windows environments, etc.). The other thing I noticed is that the dataset I’m using for “top npm packages” hasn’t been updated since 2016, even though its readme says it gets updated nightly. I think this isn’t as bad as it sounds: it’s just a list of package names, not names with versions, and I’ve been installing the latest versions of all these packages. The point of this PR is to improve compatibility with older npm packages that are still popular, so a dataset of packages that were popular a few years ago (but the latest code for those packages) is arguaby more appropriate than today’s most popular. Of all 16,107 packages installed and tested:
Of 364 packages that had a readme encouraging the use of named exports:
Of 700 packages generated via transpilation:
I also found a data source for the top 1,000 packages as of August 2019. About half of these packages are the same as the 2016 top 1,000. Here are the results for this list: Of all 921 packages installed and tested:
Of 40 packages that had a readme encouraging the use of named exports:
Of 43 packages generated via transpilation:
|
|
(approval on the approach, haven't reviewed the code yet) |
|
@nodejs/tsc @nodejs/collaborators @nodejs/modules Assuming that @guybedford get the updated implementation in time I plan to land this on Monday evening (after sun down because it's Yom Kippur) so I can kick off a 14.x release that includes this on Tuesday. If anyone has an objection please voice it ASAP. |
89770b0
to
d14b089
Compare
|
Feedback added. Finally finished updating this PR to the latest cjs-module-lexer JS conversion at https://github.com/guybedford/cjs-module-lexer. |
|
It doesn't have to be done in this PR, but I think it's important that we have a section in the Node.js documentation that explains to module authors which CommonJS export patterns are supported and which are not. Linking to https://github.com/guybedford/cjs-module-lexer is not enough because the "head" version of the module will evolve faster than what is actually released in Node. |
|
@targos Alternatively I'd be happy to move the grammar into something here, I'm just weary of putting it into the esm.md file as there's already a lot of info. I actually recently wanted to create a PR to even "unhide" the resolver spec already because a lot of people aren't seeing that when it's important info too. We are at risk of losing info in the thick of it all if we don't actively whittle these things down from time to time. |
|
@guybedford What I'm concerned about is that the contents of https://github.com/guybedford/cjs-module-lexer can change at any time. Even if Node.js quickly follows the changes, previous versions of the documentation would link to the latest version of the lexer, and that would not correspond to the behavior in the released Node.js version. One solution could be to link to the README at a specific commit SHA (the one that corresponds to the code in this PR), but I'd still prefer we have a minimal description in the Node.js docs about the supported patterns (Linking to a commit SHA has drawbacks. For example we could not enhance the documentation). It doesn't have to describe the grammar precisely, but could briefly explain with some code examples the patterns that are definitely supported and the ones that aren't. |
|
@targos these are great points. I spent some time today working to heavily clarify the readme of cjs-module-lexer to properly inform the exact semantics, and making sure this was in sync with the code. I've released a 0.3.1 and set the docs link to this exact version readme. I'm not sure it is possible to summarize the points more simply than what is there. I do think treating that as the primary source here may be best. I've posted nodejs/admin#557 to follow-up on transferring the repo to the Node.js org. It doesn't have to happen immediately but I do feel it might be the better structure here as I do think of this as Node.js core code effectively now. I've then also gone through the docs here and reworked it quite a bit too to very clearly focus on the namespace interop and how it behaves exactly. I think it's important to make this the focus over the exact heuristical detection process, which can for most users be considered a sort of "black box". Specifically on that point, users writing CommonJS shouldn't be "designing" their CommonJS to support this detection. In those cases they should rather just ship an ES module wrapper. What we are more concerned about is how users import CommonJS from ESM and their understanding of this process, and there specifically for CommonJS projects that aren't necessarily well-maintained or being updated with new ESM support. I know it's not exactly what you've requested, but I hope these clarifications make sense? The new exports detection section is at https://github.com/guybedford/cjs-module-lexer#parsing-examples, and just feels a bit too long to inline into this page to me. |
Please update tools/license-builder.sh for the new dependency and regenerate the LICENSE.
`npx` / `node` in prior versions fail with:
```sh
$ npx buschtoens
npx: installed 88 in 3.063s
file:///Users/jan/.npm/_npx/22231/lib/node_modules/buschtoens/ui.js:1
import { Box, Text, Spacer, Newline } from 'ink';
^^^
SyntaxError: The requested module 'ink' does not provide an export named 'Box'
at ModuleJob._instantiate (internal/modules/esm/module_job.js:92:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:107:20)
at async Loader.import (internal/modules/esm/loader.js:179:24)
```
nodejs/node#35249
Node ^12.20 || >=14.13 is now required because there are MJS modules in the project that have named imports from CJS modules. Support for doing this was only added in 14.13.0 and backported to 12.20.0: nodejs/node#35249 (comment)
Node ^12.20 || >=14.13 is now required because there are MJS modules in the project that have named imports from CJS modules. Support for doing this was only added in 14.13.0 and backported to 12.20.0: nodejs/node#35249 (comment)
Which added support for named exports for CJS via static analysis: nodejs/node#35249 nodejs/node@1e8cb08edc for v15 nodejs/node@f551f52f83 for v14.13 nodejs/node@9eb1fa1924 for v12.20 This is also necessary for `node:` scheme support, checked by the `unicorn/prefer-node-protocol` ESLint rule (currently disabled pending support in eslint-plugin-node): nodejs/node#35387 nodejs/node@ee9e3e75aa for v15 nodejs/node@91b820e939 for v14.13.1 nodejs/node@0f757bc2df for v12.20 Signed-off-by: Kevin Locke <kevin@kevinlocke.name>


This is another PR for CommonJS named exports identical to #33416 but without the restriction to modules with the
__esModuleflag.This PR is made to separate the consensus process for this PR from the PR at #33416, which strictly speaking could land given that it now has consensus.
If all those involved could review this PR with their opinions on relaxing the
__esModulerestriction that would be a help to determine which if any approach might ship. This way we can clearly choose between the three choices.//cc @nodejs/modules-active-members