Files
deerflow-code/frontend-web/scripts/check-firefox52-js.mjs
T

49 lines
1.5 KiB
JavaScript

import fs from "node:fs";
import path from "node:path";
import { parse } from "acorn";
const assetsDir = path.resolve("dist-firefox52/assets");
if (!fs.existsSync(assetsDir)) {
throw new Error("dist-firefox52/assets 不存在,请先运行 pnpm build:firefox52");
}
const jsFiles = fs.readdirSync(assetsDir).filter((file) => file.endsWith(".js"));
if (jsFiles.length === 0) {
throw new Error("dist-firefox52/assets 中没有 JavaScript 构建产物");
}
const failures = [];
let totalBytes = 0;
for (const file of jsFiles) {
const source = fs.readFileSync(path.join(assetsDir, file), "utf8");
totalBytes += Buffer.byteLength(source);
try {
// Firefox 52 implements ES2017 parsing, but not later syntax such as
// optional chaining, BigInt literals, object rest, or dynamic import.
parse(source, {
ecmaVersion: 2017,
sourceType: "script",
allowHashBang: false,
});
} catch (error) {
const location = error.loc ? `${error.loc.line}:${error.loc.column}` : "unknown";
failures.push(`${file}:${location} ${error.message}`);
}
}
if (failures.length > 0) {
console.error("Firefox 52 JavaScript 语法检查失败:\n");
console.error(failures.slice(0, 50).join("\n"));
if (failures.length > 50) console.error(`... 及另外 ${failures.length - 50} 处`);
process.exit(1);
}
console.log(
`Firefox 52 JavaScript 语法检查通过(${jsFiles.length} 个文件,${(
totalBytes /
1024 /
1024
).toFixed(1)} MiB)。`,
);