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

Prepare for compiling Babel to native ESM #13414

Merged
merged 15 commits into from Jul 11, 2022

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jun 2, 2021

Q                       A
Fixed Issues? Fixes #11701
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Having a dual "compiled to CJS"-"compiled to ESM" codebase is not as easy as having a dual "Babel 7"-"Babel 8" codebase, because it must be determined at compile time and cannot rely on a potentially-runtime flag. This PR adds two new commands, make use-esm and make use-cjs, to toggle between the two modes: the current mode is stored in the .module-type file, and it affects both the output type and the compile-time removal of a new USE_ESM flag.

This PR also adds two new GitHub action tests: a "Test ESM" CI job that runs yarn jest on the ESM build, and an ESM e2e test (only with React Native, which is the only one working so far) to make sure that the publishing process works.

The ESM migration is not finished yet, but I have updated the "To Do" column in https://github.com/babel/babel/projects/16 to track the next steps that I'll do. This PR can be merged, and I suggest reviewing it commit-by-commit since I tried to keep them as much self-contained as possible.

@nicolo-ribaudo nicolo-ribaudo added the PR: Internal 🏠 A type of pull request used for our changelog categories label Jun 2, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 2, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f0cc5a4:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@nicolo-ribaudo
Copy link
Member Author

This is now blocked on #14110, since the babel/babel-polyfills packages use package.json#exports.

@nicolo-ribaudo nicolo-ribaudo force-pushed the native-esm branch 2 times, most recently from 6e3344c to 57b208c Compare January 16, 2022 14:49
@babel-bot
Copy link
Collaborator

babel-bot commented Jan 16, 2022

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/52480/

@nicolo-ribaudo nicolo-ribaudo force-pushed the native-esm branch 2 times, most recently from 2c61f00 to 79a7664 Compare March 9, 2022 18:02
@nicolo-ribaudo nicolo-ribaudo force-pushed the native-esm branch 4 times, most recently from 952a629 to 9f151d4 Compare March 20, 2022 13:56
@nicolo-ribaudo nicolo-ribaudo changed the title Convert Babel to native ESM [babel 8] Convert Babel to native ESM Jun 11, 2022
Makefile Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
@@ -480,10 +488,15 @@ const libBundles = [
"packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression",
].map(src => ({
src,
format: "cjs",
format: USE_ESM ? "esm" : "cjs",
Copy link
Contributor

Choose a reason for hiding this comment

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

Note to myself: I should revive #14179.

Gulpfile.mjs Outdated Show resolved Hide resolved
Copy link
Member Author

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

I don't like self-reviewing, but

  • this doesn't affect published code, except for the very small change that now packages/babel-core/src/config/files/import.cjs is authored as CJS rather than as ESM compiled to CJS.
  • I re-checked all my changes, and I didn't find possible problems (it's been three weeks since I worked on this PR)

✔️

@liuxingbaoyu
Copy link
Member

https://github.com/babel/babel/compare/672effeb695c5d85f0360f756a9044b72372019f..8f36cb5b0358c38f58a3b6ed242ba5df0eb96cc9

Did you revert these changes in this force push for some reason?
I tested it locally (Windows) and the .js file is executed by JScript.

@nicolo-ribaudo
Copy link
Member Author

@liuxingbaoyu I had to revert them because yarn node ... doesn't work before yarn install. I'll update them to just use node ....

@liuxingbaoyu
Copy link
Member

Disclaimer: This looks good to me, but I'm really completely new to ESM😰 (which is why I haven't reviewed this PR for a long time).

@nicolo-ribaudo
Copy link
Member Author

This looks good to me, but I'm really completely new to ESM

You have already used ESM for a while, since our tests are not transpiled 😉 (jokes aside, with this PR you won't probably notice any difference when developing with use-esm or use-cjs).

@nicolo-ribaudo nicolo-ribaudo merged commit 2b163d8 into babel:main Jul 11, 2022
Move to native ES modules automation moved this from In progress to Done Jul 11, 2022
@nicolo-ribaudo nicolo-ribaudo deleted the native-esm branch July 11, 2022 14:44
dest: "lib",
}));

const cjsBundles = [
// This is used by Prettier, @babel/register and @babel/eslint-parser
{ src: "packages/babel-parser" },
Copy link
Contributor

Choose a reason for hiding this comment

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

We(Prettier) don't need cjs, our code base has migrate to esm, and we can do both static and dynamic import in parser, we support async parser now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I'll update the comment!

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 20, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Internal 🏠 A type of pull request used for our changelog categories
Development

Successfully merging this pull request may close these issues.

Publish as modules (instead of compiling to cjs)
5 participants