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

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

Merged
merged 2 commits into from Jan 10, 2020

Conversation

@nicolo-ribaudo
Copy link
Member

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 this to the v7.8.0 milestone Dec 21, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the babel:core-async branch from c762cdd to 53c74e8 Dec 21, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:config-mjs branch from fb4d8b7 to c9816b6 Dec 21, 2019
@@ -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"

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Dec 21, 2019

Author Member

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

@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:config-mjs branch from c9816b6 to e7a1d14 Dec 21, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the babel:core-async branch from 53c74e8 to 7d82ffa Dec 26, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:config-mjs branch from e7a1d14 to 108fb56 Dec 26, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the babel:core-async branch from 7d82ffa to f71a3d6 Dec 27, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:config-mjs branch from 108fb56 to f4b9d50 Dec 27, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the babel:core-async branch from f71a3d6 to 35388e9 Dec 27, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:config-mjs branch from f4b9d50 to 098992c Dec 27, 2019
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:config-mjs branch from 098992c to f04c249 Jan 1, 2020
@nicolo-ribaudo nicolo-ribaudo changed the base branch from core-async to feat-7.8.0/core-async Jan 1, 2020
@nicolo-ribaudo nicolo-ribaudo requested a review from JLHwung Jan 9, 2020

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

This comment has been minimized.

Copy link
@JLHwung

JLHwung Jan 10, 2020

Contributor

Why undefined is offered as fallback here?

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Jan 10, 2020

Author Member

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

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

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Jan 10, 2020

Author Member

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

This comment has been minimized.

Copy link
@JLHwung

JLHwung Jan 10, 2020

Contributor

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/,

This comment has been minimized.

Copy link
@JLHwung

JLHwung Jan 10, 2020

Contributor

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

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Jan 10, 2020

Author Member

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 force-pushed the nicolo-ribaudo:config-mjs branch from f04c249 to bac13ab Jan 10, 2020
@nicolo-ribaudo nicolo-ribaudo changed the base branch from feat-7.8.0/core-async to core-async Jan 10, 2020
@nicolo-ribaudo nicolo-ribaudo force-pushed the nicolo-ribaudo:config-mjs branch from bac13ab to ed00952 Jan 10, 2020
@nicolo-ribaudo nicolo-ribaudo changed the base branch from core-async to master Jan 10, 2020
}

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

This comment has been minimized.

Copy link
@kaicataldo

kaicataldo Jan 10, 2020

Member

Is it worth using path.extname() here?

@nicolo-ribaudo nicolo-ribaudo merged commit ae06baf into babel:master Jan 10, 2020
3 of 5 checks passed
3 of 5 checks passed
build (13.x)
Details
Travis CI - Pull Request Build Errored
Details
test262 Workflow: test262
Details
build-standalone Workflow: build-standalone
Details
e2e Workflow: e2e
Details
@nicolo-ribaudo nicolo-ribaudo deleted the nicolo-ribaudo:config-mjs branch Jan 10, 2020
@xiaoxiangmoe xiaoxiangmoe mentioned this pull request Jan 12, 2020
1 of 4 tasks complete
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jan 13, 2020
I really don't like this commit, but import() is currently breaking
our publish script.
When "normal" tests we are transpiling import() so that it works
with Jest. We can't do it while publishing because we need to
publish the untranspiled import() so that it can load real .mjs files.

Follow up to babel#10903
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jan 13, 2020
I really don't like this commit, but import() is currently breaking
our publish script.
When "normal" tests we are transpiling import() so that it works
with Jest. We can't do it while publishing because we need to
publish the untranspiled import() so that it can load real .mjs files.

Follow up to babel#10903
nicolo-ribaudo added a commit that referenced this pull request Jan 15, 2020
I really don't like this commit, but import() is currently breaking
our publish script.
When "normal" tests we are transpiling import() so that it works
with Jest. We can't do it while publishing because we need to
publish the untranspiled import() so that it can load real .mjs files.

Follow up to #10903
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants
You can’t perform that action at this time.