-
Notifications
You must be signed in to change notification settings - Fork 17
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
bug: CJS exports analysis reads directly from disk #175
Comments
Well this seems indeed to be a duplicate of nodejs/node#50649, and is hardly related to loaders, right? If you use the loaders API, it might help for this specific issue – but of course it has some limitations and would need some work (PRs welcome). Closing as duplicate |
The bug is with the ESM loader not properly detecting CJS exports (so I think it's kinda related to loaders...). Given the Loaders milestones mentions improving CJS exports discovery, I felt like it may be worth moving the issue here. Didn't mean to open a duplicate issue. Anyway, I trust your judgement in triaging this. Thanks. |
All bug reports go to the main Node repo; this repo is for feature discussion and design. And if the bug occurs when no hooks are registered, it's a core Node issue and not something related to this. |
Supporting interception of the exports analysis as part of the CJS hooking sounds to me like a sensible design. Such hooking should use the CJS source load hook to do the exports analysis against that CJS source, and this hooking could also permit modifying the CJS exports detection algorithm. |
Maybe the |
That’s already what’s happening |
FYI I confirmed CJS exports analysis is correctly detecting from the It would still nice to see this bug resolved in the ESM loader though. Open issue: nodejs/node#50649 |
I originally filed in nodejs/node#50649 but upon reading the Loaders Roadmap, I think it may be more appropriate to be filed here. This is a bug report that will likely be fixed by the following Milestone 3 item:
Reproduction
https://stackblitz.com/edit/stackblitz-starters-j9tcmc?file=index.mjs
The reproduction calls
node --require ./require-hook.cjs index.mjs
.For the sake of the reproduction,
require-hook.cjs
completely overwrites the code offile.js
. (In practice, tsx transforms the ESM export syntax to CJS exports)But despite that, when importing
file.js
fromindex.mjs
, the removed export is still present.This is happening because Node's CJS exports parser bypasses the CJS loader and directly analyzes the disk file. Seems this bug is acknowledged here.
How often does it reproduce? Is there a required condition?
Utilizing a CommonJS require hook to compile ESM syntax in a .js file in a CommonJS context, then importing the file from a .mjs file
What is the expected behavior? Why is that the expected behavior?
For Node to parse the named exports from the source retrieved by the loader
What do you see instead?
Node should parse the named exports from the source retrieved by the loader.
Additional information
This limitation is acknowledged in the source code as a TODO here:
https://github.com/nodejs/node/blob/a9b2535/lib/internal/modules/esm/translators.js#L421-L424
While it might be considered an edge case, I'm filing this issue to highlight its impact on tsx users:
The text was updated successfully, but these errors were encountered: