-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat: add CJS named export annotations for Transform API #2248
Comments
There was already a hint made for nodejs when you enabled esbuild/internal/bundler/linker.go Line 4177 in a599d45
|
Thanks @hyrious. That's exactly what I was looking for! Glad it's already been implemented. We're using the Transform API so I guess the feature request would be to make |
I investigated further and I think I may have misinterpreted the problem. Closing in favor of #2253 For reference, I was misinterpreting Node.js behavior when a CJS file uses index.js (gets transformed to CJS) import('./esm.js').then(m => console.log(m)); esm.js (gets transformed to CJS) export const a = 1; In this case, Node.js reads the file source directly from disk so cjs-module-lexer can't detect the transformed CJS exports because it would be checking pre-transformed ESM code. Therefore, adding named export annotations didn't actually make a difference. |
Request
Node.js uses cjs-module-lexer to parse ComonJS named exports to interop with ESM imports.
Since esbuild compiles ESM exports to CJS using getters on
module.exports
, it is currently not parsable.I would like to request a minor syntax hint to help the Node.js detect named exports in the Transform API.
Current behavior
ESM Input
CJS Output
Importing the output
The named export
a
is not detected by Node.js, and is grouped together behind thedefault
export.Proposal
Add a purely syntactical hint to help the lexer detect the named exports.
In this example, I add
exports.<export-name> = void 0
at the end of the transformed code. This doesn't do anything becausemodule.exports
is overwritten to another object prior.Importing the output
The named export is successfully detected by Node.js:
Context
We're developing a robust esbuild enhanced Node.js runtime called tsx using the transform API. And there is an edge-case for
import()
ing an CJS module (originally ESM, transpiled by esbuild on-demand) from a pure ESM file, but the named exports are consolidated in adefault
export.Currently, I'm considering using some regex to detect the export getters and injecting the hints at the bottom of the transformed code. However, this could be slow or frail and is not an ideal long-term solution.
I believe adding these hints in esbuild would benefit all use-cases that compile ESM/TS to CJS, and also other tools that are targeted for Node.
Alternative ideas
I would also appreciate a list of exports from the transform API, similar to what
es-module-lexer
does. That way, the hints can be added on our end reliably.Using
es-module-lexer
to detect exports is how we detect whether a module is ESM so it would also help remove double-parsing.The text was updated successfully, but these errors were encountered: