diff --git a/Gulpfile.js b/Gulpfile.js index 0a015ae73155..06ab66d44307 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -69,7 +69,14 @@ function buildBabel(exclude, sourcesGlob = defaultSourcesGlob) { .pipe(errorsLogger()) .pipe(newer({ dest: base, map: swapSrcWithLib })) .pipe(compilationLogger()) - .pipe(babel()) + .pipe( + babel({ + caller: { + // We have wrapped packages/babel-core/src/config/files/configuration.js with feature detection + supportsDynamicImport: true, + }, + }) + ) .pipe( // Passing 'file.relative' because newer() above uses a relative // path and this keeps it consistent. diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js index d78bd6a3fc4a..54dba7a3daad 100644 --- a/packages/babel-core/test/config-chain.js +++ b/packages/babel-core/test/config-chain.js @@ -1,6 +1,8 @@ +import cp from "child_process"; import fs from "fs"; import os from "os"; import path from "path"; +import util from "util"; import escapeRegExp from "lodash/escapeRegExp"; import * as babel from "../lib"; @@ -51,6 +53,31 @@ function loadOptionsAsync(opts) { return babel.loadOptionsAsync({ cwd: __dirname, ...opts }); } +async function loadOptionsAsyncInSpawedProcess({ filename, cwd }) { + // !!!! hack is coming !!!! + // remove this section when https://github.com/facebook/jest/issues/9430 is resolved + // We don't check process.versions.node here, it will fail if node does not support esm + // please publish Babel on a modernized node :) + const { stdout, stderr } = await util.promisify(cp.execFile)( + require.resolve("./fixtures/babel-load-options-async.mjs"), + // pass `cwd` as params as `process.cwd()` will normalize `cwd` on macOS + [filename, cwd], + { + cwd, + env: process.env, + }, + ); + if (stderr) { + throw new Error( + "error is thrown in babel-load-options-async.mjs: stdout\n" + + stdout + + "\nstderr:\n" + + stderr, + ); + } + return JSON.parse(stdout); +} + function pairs(items) { const pairs = []; for (let i = 0; i < items.length - 1; i++) { @@ -61,11 +88,10 @@ function pairs(items) { return pairs; } -async function getTemp(name) { +async function getTemp(name, fixtureFolder = "config-files-templates") { const cwd = await pfs.mkdtemp(os.tmpdir() + path.sep + name); const tmp = name => path.join(cwd, name); - const config = name => - pfs.copyFile(fixture("config-files-templates", name), tmp(name)); + const config = name => pfs.copyFile(fixture(fixtureFolder, name), tmp(name)); return { cwd, tmp, config }; } @@ -1034,21 +1060,21 @@ describe("buildConfigChain", function () { ); }); - test.each([ - "babel.config.json", - "babel.config.js", - "babel.config.cjs", - "babel.config.mjs", - ])("should load %s asynchronously", async name => { + test.each( + [ + "babel.config.json", + "babel.config.js", + "babel.config.cjs", + // We can't transpile import() while publishing, and it isn't supported + // by jest. + process.env.IS_PUBLISH ? "" : "babel.config.mjs", + ].filter(Boolean), + )("should load %s asynchronously", async name => { const { cwd, tmp, config } = await getTemp( `babel-test-load-config-async-${name}`, ); const filename = tmp("src.js"); - // We can't transpile import() while publishing, and it isn't supported - // by jest. - if (process.env.IS_PUBLISH && name === "babel.config.mjs") return; - await config(name); expect(await loadOptionsAsync({ filename, cwd })).toEqual({ @@ -1087,6 +1113,29 @@ describe("buildConfigChain", function () { loadOptionsAsync({ filename: tmp("src.js"), cwd }), ).rejects.toThrow(/Multiple configuration files found/); }); + + if (process.env.IS_PUBLISH) { + test.each(["babel.config.mjs", ".babelrc.mjs"])( + "should load %s asynchronously", + async name => { + const { cwd, tmp, config } = await getTemp( + `babel-test-load-config-async-prepublish-${name}`, + "config-files-templates-prepublish", + ); + const filename = tmp("src.js"); + await config(name); + expect( + await loadOptionsAsyncInSpawedProcess({ filename, cwd }), + ).toEqual({ + ...getDefaults(), + filename, + cwd, + root: cwd, + comments: true, + }); + }, + ); + } }); describe("relative", () => { @@ -1126,22 +1175,22 @@ describe("buildConfigChain", function () { ); }); - test.each([ - "package.json", - ".babelrc", - ".babelrc.js", - ".babelrc.cjs", - ".babelrc.mjs", - ])("should load %s asynchronously", async name => { + test.each( + [ + "package.json", + ".babelrc", + ".babelrc.js", + ".babelrc.cjs", + // We can't transpile import() while publishing, and it isn't supported + // by jest. + process.env.IS_PUBLISH ? "" : "babel.config.mjs", + ].filter(Boolean), + )("should load %s asynchronously", async name => { const { cwd, tmp, config } = await getTemp( `babel-test-load-config-${name}`, ); const filename = tmp("src.js"); - // We can't transpile import() while publishing, and it isn't supported - // by jest. - if (process.env.IS_PUBLISH && name === ".babelrc.mjs") return; - await config(name); expect(await loadOptionsAsync({ filename, cwd })).toEqual({ diff --git a/packages/babel-core/test/fixtures/babel-load-options-async.mjs b/packages/babel-core/test/fixtures/babel-load-options-async.mjs new file mode 100755 index 000000000000..51954e56b3aa --- /dev/null +++ b/packages/babel-core/test/fixtures/babel-load-options-async.mjs @@ -0,0 +1,13 @@ +#!/usr/bin/env node + +// Usage: +// babel-load-options-async.js [filename] +import babel from "@babel/core"; + +const [, , filename, cwd] = process.argv; + +(async () => { + process.stdout.write( + JSON.stringify(await babel.loadOptionsAsync({ filename, cwd })) + ); +})(); diff --git a/packages/babel-core/test/fixtures/config/config-files-templates-prepublish/.babelrc.mjs b/packages/babel-core/test/fixtures/config/config-files-templates-prepublish/.babelrc.mjs new file mode 100644 index 000000000000..813cbb0f7962 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/config-files-templates-prepublish/.babelrc.mjs @@ -0,0 +1,3 @@ +export default { + comments: true, +}; diff --git a/packages/babel-core/test/fixtures/config/config-files-templates-prepublish/babel.config.mjs b/packages/babel-core/test/fixtures/config/config-files-templates-prepublish/babel.config.mjs new file mode 100644 index 000000000000..813cbb0f7962 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/config-files-templates-prepublish/babel.config.mjs @@ -0,0 +1,3 @@ +export default { + comments: true, +};