/
esm.js
79 lines (65 loc) 路 2.32 KB
/
esm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import cp from "child_process";
import util from "util";
import path from "path";
import { createRequire } from "module";
import * as babel from "../../lib";
const require = createRequire(import.meta.url);
const dirname = new URL(".", import.meta.url).pathname;
// "minNodeVersion": "10.0.0" <-- For Ctrl+F when dropping node 10
const nodeSupportsESM = parseInt(process.versions.node) >= 12;
const isWindows = process.platform === "win32";
export const supportsESM = nodeSupportsESM && !isWindows;
export const isMJS = file => path.extname(file) === ".mjs";
export const itESM = supportsESM ? it : it.skip;
export function skipUnsupportedESM(esm, name) {
if (esm && !nodeSupportsESM) {
console.warn(
`Skipping "${name}" because native ECMAScript modules are not supported.`,
);
return true;
}
// This can be removed when loadOptionsAsyncInSpawedProcess is removed.
if (esm && isWindows) {
console.warn(
`Skipping "${name}" because the ESM runner cannot be spawned on Windows.`,
);
return true;
}
return false;
}
export function loadOptionsAsync({ filename, cwd = dirname }, mjs) {
if (mjs) {
// import() crashes with jest
return spawn("load-options-async", filename, cwd);
}
return babel.loadOptionsAsync({ filename, cwd });
}
export function spawnTransformAsync() {
// import() crashes with jest
return spawn("compile-async");
}
export function spawnTransformSync() {
// import() crashes with jest
return spawn("compile-sync");
}
// !!!! hack is coming !!!!
// Remove this function when https://github.com/nodejs/node/issues/35889 is resolved.
// Jest supports dynamic import(), but Node.js segfaults when using it in our tests.
async function spawn(runner, filename, cwd = process.cwd()) {
const { stdout, stderr } = await util.promisify(cp.execFile)(
require.resolve(`../fixtures/babel-${runner}.mjs`),
// pass `cwd` as params as `process.cwd()` will normalize `cwd` on macOS
[filename, cwd],
{ cwd, env: process.env },
);
const EXPERIMENTAL_WARNING = /\(node:\d+\) ExperimentalWarning: The ESM module loader is experimental\./;
if (stderr.replace(EXPERIMENTAL_WARNING, "").trim()) {
throw new Error(
`error is thrown in babel-${runner}.mjs: stdout\n` +
stdout +
"\nstderr:\n" +
stderr,
);
}
return JSON.parse(stdout);
}