Add support for babel.config.mjs and .babelrc.mjs#10903
Add support for babel.config.mjs and .babelrc.mjs#10903nicolo-ribaudo merged 2 commits intobabel:masterfrom
Conversation
c762cdd to
53c74e8
Compare
fb4d8b7 to
c9816b6
Compare
| 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.
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.
Why undefined is offered as fallback here?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Maybe in case someone exports false as their default config? 🤔
There was a problem hiding this comment.
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.
You can use toThrowErrorMatchingSnapshot() here and don't have to write the same string again.
There was a problem hiding this comment.
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.
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
*.mjsconfig 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,
.mjsfiles can only be used when calling Babel with.parseAsync,.transformFileAsync,.transformAsync,.loadPartialConfigAsyncor.loadOptionsAsync. Synchronous calls will throw an error. This is a problem for@babel/eslint-parserwhich will still only support synchronous configuration files, but the ESLint team is working on allowing asynchronous ESLint calls.