Files
test-repo/scripts/nodejs-14-test-loader.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

26 lines
964 B
JavaScript
Raw Normal View History

2025-09-24 04:31:22 +02:00
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import esbuild from "esbuild";
// `@babel/code-frame` and `@babel/highlight` use syntax that is not available in Node.js 14,
// such as `??=`. When bundled into dist/ they are compiled to be compatible with Node.js 14,
// however we also need to load them at runtime from node_modules to run our tests.
// We can use an `--experimental-loader` to compile them on-the-fly with esbuild.
export async function getSource(url, context, defaultGetSource) {
if (
!/node_modules\/@babel\/(?:code-frame|highlight)/u.test(url) ||
context.format !== "module"
) {
return defaultGetSource(url, context, defaultGetSource);
}
const raw = await readFile(fileURLToPath(url));
try {
const result = await esbuild.transform(raw, { target: "node14" });
return { source: result.code };
} catch (error) {
error.message += ` (in ${url})`;
throw error;
}
}