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
Add support for babel.config.mjs and .babelrc.mjs #10903
Conversation
c762cdd
to
53c74e8
Compare
fb4d8b7
to
c9816b6
Compare
@@ -121,6 +121,10 @@ module.exports = function(api) { | |||
["@babel/plugin-proposal-nullish-coalescing-operator", { loose: true }], | |||
|
|||
convertESM ? "@babel/transform-modules-commonjs" : null, | |||
// Until Jest supports native mjs, we must simulate it 🤷 | |||
env === "test" || env === "development" | |||
? "@babel/plugin-proposal-dynamic-import" |
There was a problem hiding this comment.
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 😢
c9816b6
to
e7a1d14
Compare
53c74e8
to
7d82ffa
Compare
e7a1d14
to
108fb56
Compare
7d82ffa
to
f71a3d6
Compare
108fb56
to
f4b9d50
Compare
f71a3d6
to
35388e9
Compare
f4b9d50
to
098992c
Compare
098992c
to
f04c249
Compare
|
||
function loadCjsDefault(filepath: string) { | ||
const module = (require(filepath): mixed); | ||
return module?.__esModule ? module.default || undefined : module; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
babel/packages/babel-core/src/config/files/configuration.js
Lines 181 to 185 in 5b907e9
const configModule = (require(filepath): mixed); | |
options = | |
configModule && configModule.__esModule | |
? configModule.default || undefined | |
: configModule; |
There was a problem hiding this comment.
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? 🤔
There was a problem hiding this comment.
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/, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
f04c249
to
bac13ab
Compare
bac13ab
to
ed00952
Compare
} | ||
|
||
function guessJSModuleType(path: string): "cjs" | "mjs" | "unknown" { | ||
switch (path.slice(-4)) { |
There was a problem hiding this comment.
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?
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.