40 lines
1.9 KiB
JavaScript
40 lines
1.9 KiB
JavaScript
import { spawnSync } from "node:child_process";
|
|
import { mkdirSync, mkdtempSync } from "node:fs";
|
|
import { createRequire } from "node:module";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const { build } = await import(pathToFileURL(require.resolve("esbuild", {
|
|
paths: [dirname(require.resolve("vite"))],
|
|
})).href);
|
|
const output = join(root, ".runtime", "ui-boundary-check");
|
|
mkdirSync(output, { recursive: true });
|
|
await build({
|
|
absWorkingDir: root,
|
|
entryPoints: ["src/components/react-ui-error-boundary.browser-check.tsx"],
|
|
bundle: true,
|
|
platform: "browser",
|
|
format: "iife",
|
|
define: { "process.env.NODE_ENV": JSON.stringify("production") },
|
|
outfile: join(output, "check.js"),
|
|
});
|
|
const browser = process.env.CHROME_PATH || (process.platform === "win32"
|
|
? "C:/Program Files/Google/Chrome/Application/chrome.exe"
|
|
: "/usr/bin/chromium");
|
|
const response = spawnSync(browser, [
|
|
"--headless=new", "--disable-gpu", "--disable-background-networking", "--no-first-run",
|
|
`--user-data-dir=${mkdtempSync(join(output, "browser-profile-"))}`,
|
|
"--virtual-time-budget=3000", "--dump-dom",
|
|
pathToFileURL(join(root, "scripts", "react-ui-error-boundary-check.html")).href,
|
|
], { encoding: "utf8", windowsHide: true, timeout: 30_000 });
|
|
if (response.error) throw response.error;
|
|
const payload = response.stdout?.match(/<pre id="boundary-check-result">([^<]+)<\/pre>/)?.[1];
|
|
if (!payload || response.status !== 0) {
|
|
throw new Error(`Browser check did not finish: exit=${response.status}; ${response.stderr?.slice(-500)}`);
|
|
}
|
|
const result = JSON.parse(payload);
|
|
if (result.passed !== true) throw new Error(`Browser check failed: ${payload}`);
|
|
console.log("React production boundary browser check:", result);
|