RuneStone 1.0.0

This commit is contained in:
Knotty Wyvern
2026-11-16 04:21:21 -07:00
commit b52eeb14a1
827 changed files with 241421 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
// Check the build output for the native features (src/lib/features/).
// Each feature module must load only through its dynamic import in `src/lib/features/loader.ts`.
// The check fails when a feature folder, or a library that only a feature needs, is in an entry chunk
// or in a chunk that an entry chunk imports statically. It also reports the size of each feature chunk.
//
// Usage: node scripts/featureChunks.mjs (also `pnpm check:features`)
// It runs a full build with Vite, so it takes about a minute.
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
/** The feature folders. The runtime files directly in `src/lib/features/` stay in the main bundle. */
export const FEATURE_FOLDERS = ["git", "background", "terminal", "pdf", "pm"];
/** Libraries that only features need. */
export const FEATURE_LIBRARIES = ["@xterm", "pdfjs-dist", "yaml"];
const normal = id => id.replaceAll("\\", "/");
/** The chunk names that the entry chunks load, directly or through static imports. */
export function staticClosure(chunks) {
const byName = new Map(chunks.map(chunk => [chunk.fileName, chunk]));
const seen = new Set();
const queue = chunks.filter(chunk => chunk.isEntry).map(chunk => chunk.fileName);
while (queue.length) {
const name = queue.pop();
if (seen.has(name)) continue;
seen.add(name);
for (const next of byName.get(name)?.imports ?? []) queue.push(next);
}
return seen;
}
/** True when a module belongs to a feature folder or to a feature library. */
export function featureModule(id, folders = FEATURE_FOLDERS, libraries = FEATURE_LIBRARIES) {
const path = normal(id);
return folders.some(folder => path.includes(`/src/lib/features/${folder}/`))
|| libraries.some(library => path.includes(`/node_modules/${library}/`) || path.includes(`/node_modules/.pnpm/${library.replace("/", "+")}`));
}
/**
* Check the chunks of one client build. `chunks` has the Rollup fields `fileName`, `isEntry`, `isDynamicEntry`,
* `facadeModuleId`, `imports`, `moduleIds`, and `size`. Returns the problems and the size of each feature chunk.
*/
export function checkChunks(chunks, folders = FEATURE_FOLDERS) {
const problems = [];
const closure = staticClosure(chunks);
for (const chunk of chunks) {
if (!closure.has(chunk.fileName)) continue;
for (const id of chunk.moduleIds ?? []) {
if (featureModule(id, folders)) problems.push(`${chunk.fileName} loads at start and contains ${normal(id).replace(/^.*?\/(src|node_modules)\//, "$1/")}`);
}
}
const sizes = {};
for (const folder of folders) {
const entry = chunks.find(chunk => chunk.facadeModuleId && normal(chunk.facadeModuleId).endsWith(`/src/lib/features/${folder}/index.ts`));
if (!entry) { problems.push(`No chunk starts at src/lib/features/${folder}/index.ts.`); continue; }
if (!entry.isDynamicEntry || entry.isEntry) problems.push(`src/lib/features/${folder}/index.ts must be a dynamic entry only.`);
sizes[folder] = entry.size;
}
return { problems, sizes };
}
async function main() {
// The client build of SvelteKit runs as a nested Vite build, so the report comes from a plugin in vite.config.js.
const report = path.join(mkdtempSync(path.join(tmpdir(), "runestone-chunks-")), "chunks.json");
const vite = fileURLToPath(new URL("../node_modules/vite/bin/vite.js", import.meta.url));
execFileSync(process.execPath, [vite, "build", "--logLevel", "warn"], { stdio: ["ignore", "ignore", "inherit"], env: { ...process.env, RUNESTONE_FEATURE_CHUNKS: report } });
const chunks = JSON.parse(readFileSync(report, "utf8"));
rmSync(path.dirname(report), { recursive: true, force: true });
const { problems, sizes } = checkChunks(chunks);
for (const [folder, size] of Object.entries(sizes)) console.log(`${folder}: ${(size / 1024).toFixed(1)} KiB in its own chunk`);
if (problems.length) {
for (const problem of problems) console.error(`FAIL: ${problem}`);
process.exitCode = 1;
} else console.log(`PASS: ${FEATURE_FOLDERS.length} feature module(s) load only at first need.`);
}
if (process.argv[1] === fileURLToPath(import.meta.url)) await main();