Ensure import() is not transpiled in babel-core published source (#11974)

* fix: Ensure `import()` is not transpiled when published

* Update packages/babel-core/test/config-chain.js [skip ci]
This commit is contained in:
Huáng Jùnliàng 2020-08-20 13:31:26 -04:00 committed by GitHub
parent 84ea6e4501
commit 4be67fb19a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 25 deletions

View File

@ -69,7 +69,14 @@ function buildBabel(exclude, sourcesGlob = defaultSourcesGlob) {
.pipe(errorsLogger()) .pipe(errorsLogger())
.pipe(newer({ dest: base, map: swapSrcWithLib })) .pipe(newer({ dest: base, map: swapSrcWithLib }))
.pipe(compilationLogger()) .pipe(compilationLogger())
.pipe(babel()) .pipe(
babel({
caller: {
// We have wrapped packages/babel-core/src/config/files/configuration.js with feature detection
supportsDynamicImport: true,
},
})
)
.pipe( .pipe(
// Passing 'file.relative' because newer() above uses a relative // Passing 'file.relative' because newer() above uses a relative
// path and this keeps it consistent. // path and this keeps it consistent.

View File

@ -1,6 +1,8 @@
import cp from "child_process";
import fs from "fs"; import fs from "fs";
import os from "os"; import os from "os";
import path from "path"; import path from "path";
import util from "util";
import escapeRegExp from "lodash/escapeRegExp"; import escapeRegExp from "lodash/escapeRegExp";
import * as babel from "../lib"; import * as babel from "../lib";
@ -51,6 +53,31 @@ function loadOptionsAsync(opts) {
return babel.loadOptionsAsync({ cwd: __dirname, ...opts }); return babel.loadOptionsAsync({ cwd: __dirname, ...opts });
} }
async function loadOptionsAsyncInSpawedProcess({ filename, cwd }) {
// !!!! hack is coming !!!!
// todo(Babel 8): 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) { function pairs(items) {
const pairs = []; const pairs = [];
for (let i = 0; i < items.length - 1; i++) { for (let i = 0; i < items.length - 1; i++) {
@ -61,11 +88,10 @@ function pairs(items) {
return pairs; 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 cwd = await pfs.mkdtemp(os.tmpdir() + path.sep + name);
const tmp = name => path.join(cwd, name); const tmp = name => path.join(cwd, name);
const config = name => const config = name => pfs.copyFile(fixture(fixtureFolder, name), tmp(name));
pfs.copyFile(fixture("config-files-templates", name), tmp(name));
return { cwd, tmp, config }; return { cwd, tmp, config };
} }
@ -1034,21 +1060,21 @@ describe("buildConfigChain", function () {
); );
}); });
test.each([ test.each(
"babel.config.json", [
"babel.config.js", "babel.config.json",
"babel.config.cjs", "babel.config.js",
"babel.config.mjs", "babel.config.cjs",
])("should load %s asynchronously", async name => { // 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( const { cwd, tmp, config } = await getTemp(
`babel-test-load-config-async-${name}`, `babel-test-load-config-async-${name}`,
); );
const filename = tmp("src.js"); 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); await config(name);
expect(await loadOptionsAsync({ filename, cwd })).toEqual({ expect(await loadOptionsAsync({ filename, cwd })).toEqual({
@ -1087,6 +1113,29 @@ describe("buildConfigChain", function () {
loadOptionsAsync({ filename: tmp("src.js"), cwd }), loadOptionsAsync({ filename: tmp("src.js"), cwd }),
).rejects.toThrow(/Multiple configuration files found/); ).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", () => { describe("relative", () => {
@ -1126,22 +1175,22 @@ describe("buildConfigChain", function () {
); );
}); });
test.each([ test.each(
"package.json", [
".babelrc", "package.json",
".babelrc.js", ".babelrc",
".babelrc.cjs", ".babelrc.js",
".babelrc.mjs", ".babelrc.cjs",
])("should load %s asynchronously", async name => { // 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( const { cwd, tmp, config } = await getTemp(
`babel-test-load-config-${name}`, `babel-test-load-config-${name}`,
); );
const filename = tmp("src.js"); 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); await config(name);
expect(await loadOptionsAsync({ filename, cwd })).toEqual({ expect(await loadOptionsAsync({ filename, cwd })).toEqual({

View File

@ -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 }))
);
})();

View File

@ -0,0 +1,3 @@
export default {
comments: true,
};

View File

@ -0,0 +1,3 @@
export default {
comments: true,
};