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

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

Merged
merged 2 commits into from Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion Gulpfile.js
Expand Up @@ -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.
Expand Down
97 changes: 73 additions & 24 deletions 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";

Expand Down Expand Up @@ -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
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
JLHwung marked this conversation as resolved.
Show resolved Hide 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++) {
Expand All @@ -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 };
}

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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({
Expand Down
13 changes: 13 additions & 0 deletions 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 }))
);
})();
@@ -0,0 +1,3 @@
export default {
comments: true,
};
@@ -0,0 +1,3 @@
export default {
comments: true,
};