perf(web): skip unchanged incremental web builds
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled

This commit is contained in:
mes123456
2026-09-06 10:46:53 +08:00
parent 6090995999
commit ed347098b4
5 changed files with 107 additions and 2 deletions

View File

@@ -0,0 +1,23 @@
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import { fingerprint, readState, root, writeState } from "./web-build-fingerprint.mjs";
const currentFingerprint = fingerprint();
const state = readState("web-build-success.json");
const index = `${root}/web/dist/index.html`;
if (state?.fingerprint === currentFingerprint && fs.existsSync(index)) {
process.stdout.write(`web-build-reused fingerprint=${currentFingerprint}\n`);
process.exit(0);
}
const typecheck = spawnSync(process.execPath, ["tools/web/run-incremental-typecheck.mjs"], {
cwd: root,
stdio: "inherit",
});
if (typecheck.status !== 0) process.exit(typecheck.status ?? 1);
const vite = spawnSync(process.execPath, ["./node_modules/vite/bin/vite.js", "build", "--config", "app/vite.config.ts"], {
cwd: `${root}/web`,
stdio: "inherit",
});
if (vite.status !== 0) process.exit(vite.status ?? 1);
writeState("web-build-success.json", { schemaVersion: 1, fingerprint: currentFingerprint });

View File

@@ -0,0 +1,18 @@
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import { fingerprint, readState, root, statePath, writeState } from "./web-build-fingerprint.mjs";
const currentFingerprint = fingerprint();
const state = readState("typecheck-success.json");
const buildInfo = statePath("tsconfig.tsbuildinfo");
if (state?.fingerprint === currentFingerprint && fs.existsSync(buildInfo)) {
process.stdout.write(`web-typecheck-reused fingerprint=${currentFingerprint}\n`);
process.exit(0);
}
const result = spawnSync(process.execPath, ["./node_modules/typescript/bin/tsc", "-p", "tsconfig.json"], {
cwd: root + "/web",
stdio: "inherit",
});
if (result.status !== 0) process.exit(result.status ?? 1);
writeState("typecheck-success.json", { schemaVersion: 1, fingerprint: currentFingerprint });

View File

@@ -0,0 +1,61 @@
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
export const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const webRoot = path.join(root, "web");
function walk(directory, output) {
if (!fs.existsSync(directory)) return;
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
if (entry.name === "node_modules" || entry.name === "dist" || entry.name === ".cache" || entry.name === ".vite") continue;
const file = path.join(directory, entry.name);
if (entry.isDirectory()) walk(file, output);
else if (entry.isFile()) output.push(file);
}
}
export function inputFiles() {
const files = [
path.join(webRoot, "package.json"),
path.join(webRoot, "package-lock.json"),
path.join(webRoot, "tsconfig.json"),
path.join(webRoot, "app/vite.config.ts"),
];
walk(path.join(webRoot, "app/src"), files);
walk(path.join(webRoot, "protocol"), files);
walk(path.join(webRoot, "app/public"), files);
return [...new Set(files)].filter((file) => fs.existsSync(file)).sort();
}
export function fingerprint() {
const hash = crypto.createHash("sha256");
for (const file of inputFiles()) {
const stat = fs.statSync(file);
hash.update(path.relative(root, file));
hash.update("\0");
hash.update(String(stat.size));
hash.update("\0");
hash.update(String(stat.mtimeNs));
hash.update("\n");
}
return hash.digest("hex");
}
export function statePath(name) {
return path.join(webRoot, ".cache", name);
}
export function readState(name) {
try { return JSON.parse(fs.readFileSync(statePath(name), "utf8")); }
catch { return null; }
}
export function writeState(name, value) {
const file = statePath(name);
fs.mkdirSync(path.dirname(file), { recursive: true });
const temporary = `${file}.tmp-${process.pid}`;
fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`);
fs.renameSync(temporary, file);
}