Skip to content
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: transform commonjs to esm #2

Open
hyrious opened this issue Feb 21, 2022 · 2 comments
Open

feat: transform commonjs to esm #2

hyrious opened this issue Feb 21, 2022 · 2 comments

Comments

@hyrious
Copy link
Owner

hyrious commented Feb 21, 2022

I came up with a super easy way of doing this instead of going "syntheticNamedExports". To be clear I just want to replicate the node17 behavior: use cjs-module-lexer to support partial of the cjs modules. The transformed module will be like:

var exports = {}, module = {exports}
// have to use { block } to prevent names conflict
{
  // original code
}
export default module.exports
// analyzed possible named exports
export var useState = module.exports.useState
...

The downside is obvious: some modules will cause error at runtime when it doesn't follow the pattern. But the advantage is we can reduce some verbosing code about __commonJS. I can make this transformation optional.

Those requires still need the trick in this repo. But I want to add additional info here:

try { require("x") } catch {}

Code like this won't be transformed to import statement because esbuild allowed this pattern and it won't warn about it. Frankly speaking, even "require is undefined" won't cause error at runtime when it was caught like this.

@hyrious
Copy link
Owner Author

hyrious commented Feb 21, 2022

Preview Link if the proposal of this issue come true.

@hyrious
Copy link
Owner Author

hyrious commented Feb 22, 2022

🚢 0.2.0
I'll keep this issue open if there be any improvements to the conversion or strong reasons to remove this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant