perf(electron): prune authoring docs from packages (#10359)

This commit is contained in:
backryun
2026-08-16 12:15:42 +09:00
committed by GitHub
parent 4b76d3b76f
commit 684ea70fb3
3 changed files with 143 additions and 1 deletions

View File

@@ -0,0 +1,65 @@
import { existsSync, lstatSync, readdirSync, rmSync } from "node:fs";
import { join, relative, resolve, sep } from "node:path";
export const ELECTRON_RUNTIME_DOC_PRUNE_RULES = Object.freeze({
localeRootFiles: Object.freeze(["CHANGELOG.md"]),
authoringDirectories: Object.freeze(["docs/research", "docs/superpowers"]),
});
function payloadSize(targetPath) {
const stat = lstatSync(targetPath);
if (!stat.isDirectory()) {
return { files: 1, bytes: stat.size };
}
return readdirSync(targetPath).reduce(
(total, entry) => {
const payload = payloadSize(join(targetPath, entry));
total.files += payload.files;
total.bytes += payload.bytes;
return total;
},
{ files: 0, bytes: 0 }
);
}
function removePayload(bundleRoot, relativePath, summary) {
const root = resolve(bundleRoot);
const targetPath = resolve(root, relativePath);
if (targetPath !== root && !targetPath.startsWith(`${root}${sep}`)) {
throw new Error(`[electron-docs] refusing to prune outside bundle root: ${relativePath}`);
}
if (!existsSync(targetPath)) return;
const payload = payloadSize(targetPath);
rmSync(targetPath, { recursive: true, force: true });
summary.removedFiles += payload.files;
summary.removedBytes += payload.bytes;
summary.removedPaths.push(relative(root, targetPath).split(sep).join("/"));
}
/**
* Remove docs that are useful while authoring OmniRoute but are never read by
* the packaged desktop runtime. Canonical docs remain untouched; bundleRoot is
* the disposable Electron staging directory.
*/
export function pruneElectronRuntimeDocs(bundleRoot) {
const summary = { removedFiles: 0, removedBytes: 0, removedPaths: [] };
const localesRoot = join(bundleRoot, "docs", "i18n");
if (existsSync(localesRoot)) {
for (const locale of readdirSync(localesRoot, { withFileTypes: true })) {
if (!locale.isDirectory()) continue;
for (const fileName of ELECTRON_RUNTIME_DOC_PRUNE_RULES.localeRootFiles) {
removePayload(bundleRoot, join("docs", "i18n", locale.name, fileName), summary);
}
}
}
for (const relativePath of ELECTRON_RUNTIME_DOC_PRUNE_RULES.authoringDirectories) {
removePayload(bundleRoot, relativePath, summary);
}
summary.removedPaths.sort();
return summary;
}

View File

@@ -6,6 +6,7 @@ import { fileURLToPath } from "node:url";
import { spawnSync } from "node:child_process";
import { assembleStandalone } from "./assembleStandalone.mjs";
import { buildRebuildSpawnPlan } from "./electronRebuildPlan.mjs";
import { pruneElectronRuntimeDocs } from "./electronRuntimeDocs.mjs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@@ -205,6 +206,14 @@ assembleStandalone({
materializeSymlinks: true,
});
const docsPrune = pruneElectronRuntimeDocs(ELECTRON_STANDALONE_DIR);
if (docsPrune.removedFiles > 0) {
console.log(
`[electron] pruned ${docsPrune.removedFiles} authoring doc file(s) ` +
`(${docsPrune.removedBytes} bytes) from the staging bundle`
);
}
// Electron-UNIQUE post-assembly steps
removeGeneratedElectronArtifacts();