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

fix(es/module): Improve compatibility with cjs-module-lexer #5835

Merged
merged 6 commits into from
Sep 13, 2022

Conversation

magic-akari
Copy link
Member

Description:

Support reexport by emitting TS __export function call.

BREAKING CHANGE:

Related issue (if exists):

@magic-akari magic-akari marked this pull request as ready for review September 12, 2022 18:06
@kdy1
Copy link
Member

kdy1 commented Sep 13, 2022

Why is this required? Do you have any real-world usecase related to this?

@kdy1 kdy1 added this to the Planned milestone Sep 13, 2022
@magic-akari
Copy link
Member Author

magic-akari commented Sep 13, 2022

Sometimes you need CommonJS while allowing them to be imported into node esm.
Node supports interoperability features by detecting CJS exports through the cjs-module-lexer library.


There are two issues about compatibility with cjs-module-lexer that need to be fixed now.

  1. reexport

The following code will output app page in native esm.
The page is reexport from page.js to app.js.

// page.js
export const page = "page";
// app.js
export * from "./page.js";

export const app = "app";
// index.js
import { app, page } from "./app.js";

console.log(app, page);

When compiling app.js and page.js into CommonJS, we expect the same result.
Therefore, we must tell node that we have re-export the page.js from app.js

We emit 0 && __export(require("foo")) to hint we reexport "foo".

This is similar to the way used by esbuild.


  1. arrow getter

Take the same example as above, we will get the following transformed code.

Object.defineProperty(exports, "page", {
    enumerable: true,
    get: ()=>page
});

Once cjs-module-lexer detects the page, but not in the format it expects,
cjs-module-lexer adds it to unsafe getter export and never exports it.

https://github.com/nodejs/cjs-module-lexer/blob/4641ae5419634deeefa41f943080f2eb7b2c925e/src/lexer.c#L442-L444
https://github.com/nodejs/cjs-module-lexer/blob/4641ae5419634deeefa41f943080f2eb7b2c925e/src/lexer.js#L31-L35

We need to change the output to a method/function getter until cjs-module-lexer supports arrow getters.

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense, thanks!


swc-bump:

  • swc_ecma_transforms_module

@kdy1 kdy1 enabled auto-merge (squash) September 13, 2022 06:59
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review comment generated by auto-rebase script

@kdy1 kdy1 merged commit bf75981 into swc-project:main Sep 13, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.3.1 Sep 14, 2022
@magic-akari magic-akari deleted the cjs-module-lexer branch September 15, 2022 03:18
@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants