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