Skip to content

Add support for babel.config.mjs and .babelrc.mjs#10903

Merged
nicolo-ribaudo merged 2 commits intobabel:masterfrom
nicolo-ribaudo:config-mjs
Jan 10, 2020
Merged

Add support for babel.config.mjs and .babelrc.mjs#10903
nicolo-ribaudo merged 2 commits intobabel:masterfrom
nicolo-ribaudo:config-mjs

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo commented Dec 21, 2019

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

NOTE: This PR is a diff on top of #10507, but they can be reviewed in any order.

This PR adds support for *.mjs config files: this is one of the last steps (with #10783) to support as configuration files all the extensions natively supported by nodejs.

Since native ECMAScript modules can only be loaded asynchronously, .mjs files can only be used when calling Babel with .parseAsync, .transformFileAsync, .transformAsync, .loadPartialConfigAsync or .loadOptionsAsync. Synchronous calls will throw an error. This is a problem for @babel/eslint-parser which will still only support synchronous configuration files, but the ESLint team is working on allowing asynchronous ESLint calls.

@nicolo-ribaudo nicolo-ribaudo added PR: New Feature 🚀 A type of pull request used for our changelog categories pkg: core area: config labels Dec 21, 2019
@nicolo-ribaudo nicolo-ribaudo added this to the v7.8.0 milestone Dec 21, 2019
convertESM ? "@babel/transform-modules-commonjs" : null,
// Until Jest supports native mjs, we must simulate it 🤷
env === "test" || env === "development"
? "@babel/plugin-proposal-dynamic-import"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I hate this, but this is the best way we have so far to test it 😢


function loadCjsDefault(filepath: string) {
const module = (require(filepath): mixed);
return module?.__esModule ? module.default || undefined : module;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why undefined is offered as fallback here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure; I only moved this code here from

const configModule = (require(filepath): mixed);
options =
configModule && configModule.__esModule
? configModule.default || undefined
: configModule;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Maybe in case someone exports false as their default config? 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Alright, we should investigate this in later PRs, or just remove it in Babel 8 because it looks suspicious.

await config("babel.config.mjs");

expect(() => loadOptions({ filename, cwd })).toThrow(
/is only supported when running Babel asynchronously/,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can use toThrowErrorMatchingSnapshot() here and don't have to write the same string again.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I can't use it because the error contains the path of the file in the tmp folder, which might change.

@nicolo-ribaudo nicolo-ribaudo changed the base branch from feat-7.8.0/core-async to core-async January 10, 2020 20:52
@nicolo-ribaudo nicolo-ribaudo changed the base branch from core-async to master January 10, 2020 21:45
}

function guessJSModuleType(path: string): "cjs" | "mjs" | "unknown" {
switch (path.slice(-4)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it worth using path.extname() here?

@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 Apr 11, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: config outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: core PR: New Feature 🚀 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for .mjs config files

3 participants