Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 2,148 Bytes
816fa8a | 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 | // The ONE CommonJS register for a node test that loads app source (W-8547).
// node -r ./scripts/node_register.cjs <runner.cjs> or require("./scripts/node_register.cjs")
// A bare node loads no `src/customer-grid/*.tsx`. This makes three things loadable:
// .ts/.tsx transpiled by the project's own typescript (react-jsx, CommonJS); `import.meta` gets the
// D-312 stand-in, or node re-reads the module as ESM ("exports is not defined").
// .css an empty module: Vite bundles stylesheets, and node parses one as JavaScript.
// cjs .js a dependency that ships its CommonJS build as `.js` in a "type": "module" package
// (glide-data-grid `dist/cjs/`, phosphor-icons `dist/index.cjs.js`): node reads that build
// as ESM ("exports is not defined in ES module scope"). It is compiled as CommonJS.
// Proved by `python aios-web/web/_gate_node.py --selftest`.
"use strict";
const fs = require("fs");
const Module = require("module");
const META_STANDIN = '({ env: { BASE_URL: "/", MODE: "test", DEV: false, PROD: true, SSR: true }, '
+ 'url: require("url").pathToFileURL(__filename).href, glob: () => ({}) })'; // glob: a Vite build feature
const CJS_AS_JS = /[\\/]node_modules[\\/](?:.*[\\/]cjs[\\/].*|.*\.cjs)\.js$/;
let ts = null;
function transpile(source, filename) {
ts = ts || require("typescript"); // lazy: a runner that loads no .ts needs no node_modules
const out = ts.transpileModule(source, { fileName: filename, compilerOptions: {
module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX,
esModuleInterop: true } }).outputText;
return out.replace(/\bimport\.meta\b/g, META_STANDIN);
}
for (const ext of [".ts", ".tsx"]) {
Module._extensions[ext] = (mod, filename) =>
mod._compile(transpile(fs.readFileSync(filename, "utf8"), filename), filename, "commonjs");
}
Module._extensions[".css"] = () => {};
const loadJs = Module._extensions[".js"];
Module._extensions[".js"] = (mod, filename) => CJS_AS_JS.test(filename)
? mod._compile(fs.readFileSync(filename, "utf8"), filename, "commonjs")
: loadJs(mod, filename);
|