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:
parent
84ea6e4501
commit
4be67fb19a
@ -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.
|
||||
|
||||
@ -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 !!!!
|
||||
// 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) {
|
||||
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([
|
||||
test.each(
|
||||
[
|
||||
"babel.config.json",
|
||||
"babel.config.js",
|
||||
"babel.config.cjs",
|
||||
"babel.config.mjs",
|
||||
])("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(
|
||||
`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([
|
||||
test.each(
|
||||
[
|
||||
"package.json",
|
||||
".babelrc",
|
||||
".babelrc.js",
|
||||
".babelrc.cjs",
|
||||
".babelrc.mjs",
|
||||
])("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(
|
||||
`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({
|
||||
|
||||
13
packages/babel-core/test/fixtures/babel-load-options-async.mjs
vendored
Executable file
13
packages/babel-core/test/fixtures/babel-load-options-async.mjs
vendored
Executable 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 }))
|
||||
);
|
||||
})();
|
||||
3
packages/babel-core/test/fixtures/config/config-files-templates-prepublish/.babelrc.mjs
vendored
Normal file
3
packages/babel-core/test/fixtures/config/config-files-templates-prepublish/.babelrc.mjs
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export default {
|
||||
comments: true,
|
||||
};
|
||||
3
packages/babel-core/test/fixtures/config/config-files-templates-prepublish/babel.config.mjs
vendored
Normal file
3
packages/babel-core/test/fixtures/config/config-files-templates-prepublish/babel.config.mjs
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export default {
|
||||
comments: true,
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user