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.

36 lines
1.3 KiB
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) {
2026-01-15 23:53:14 +08:00
if (context.format === "module") {
if (url.includes("/node_modules/js-tokens/")) {
const text = await readFile(fileURLToPath(url), "utf8");
const commonJsExport = 'export { jsTokens as "module.exports" };';
if (text.includes(commonJsExport)) {
return { source: text.replace(commonJsExport, "") };
}
}
if (
url.includes("/node_modules/@babel/code-frame/") ||
url.includes("/node_modules/@babel/highlight/")
) {
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;
}
}
2025-09-24 04:31:22 +02:00
}
2026-01-15 23:53:14 +08:00
return defaultGetSource(url, context, defaultGetSource);
2025-09-24 04:31:22 +02:00
}