Split the game page into partials under src/, assembled into one index.html at build
site/index.html (900 KB of HTML, CSS and JS in one file) becomes src/: a shell (src/index.html), styles/ and game/, concatenated in path order by scripts/lib/page.js. The assembled page is byte-identical to the old file, so load time and in-game performance are unchanged. - scripts/build.js replaces build.sh (same env vars, same output) - npm run dev: a zero-dependency dev server that assembles on every request - tests assemble the page in memory; npm test runs every tests/*.test.js - perf-bench takes --base-ref to build a git ref's page - scripts/legacy/split-index-html.js is the splitter, kept for older branches
This commit is contained in:
parent
295e68cc46
commit
67a40942b4
113 changed files with 5584 additions and 5195 deletions
103
scripts/lib/page.js
Normal file
103
scripts/lib/page.js
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
// Assembles the game page from src/: src/index.html is the shell, and every line that is exactly
|
||||
// `<!-- @include DIR -->` is replaced by the files under src/DIR, concatenated in path order.
|
||||
// The result is the same single, self-contained index.html the game has always shipped: one request,
|
||||
// no module loader, no runtime cost. See docs/ARCHITECTURE.md ("Source layout") and docs/adr/.
|
||||
//
|
||||
// node scripts/lib/page.js [-o FILE] assemble this checkout (stdout by default)
|
||||
// node scripts/lib/page.js --ref origin/main -o FILE assemble the page as it is at a git ref
|
||||
//
|
||||
// --ref uses that commit's own assembler, so it keeps working when this file changes; refs from before
|
||||
// the split (when the page was site/index.html) are read as they are.
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { execFileSync } = require('node:child_process');
|
||||
|
||||
const ROOT = path.join(__dirname, '..', '..');
|
||||
const INCLUDE = /^<!-- @include ([\w./-]+) -->$/;
|
||||
|
||||
// Every file under dir, as paths relative to dir with '/' separators, in a stable order
|
||||
// (plain code-unit comparison, so the order never depends on the machine's locale).
|
||||
function listFiles(dir, rel = '') {
|
||||
const out = [];
|
||||
for (const e of fs.readdirSync(path.join(dir, rel), { withFileTypes: true })) {
|
||||
if (e.name.startsWith('.')) continue;
|
||||
const r = rel ? rel + '/' + e.name : e.name;
|
||||
if (e.isDirectory()) out.push(...listFiles(dir, r));
|
||||
else out.push(r);
|
||||
}
|
||||
return out.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
||||
}
|
||||
|
||||
// The partials that make up one include, in order: [{file, text}].
|
||||
function partials(src, dir) {
|
||||
const base = path.join(src, dir);
|
||||
if (!fs.existsSync(base)) throw new Error(`@include ${dir}: no such directory under ${path.relative(ROOT, src) || '.'}`);
|
||||
return listFiles(base).map((r) => {
|
||||
const file = path.join(base, r);
|
||||
const text = fs.readFileSync(file, 'utf8');
|
||||
if (text.includes('\r')) throw new Error(`${path.relative(ROOT, file)}: use LF line endings`);
|
||||
if (text && !text.endsWith('\n')) throw new Error(`${path.relative(ROOT, file)}: must end with a newline`);
|
||||
if (/^<\/(script|style)/im.test(text)) throw new Error(`${path.relative(ROOT, file)}: must not close its <script>/<style>`);
|
||||
return { file: dir + '/' + r, text };
|
||||
});
|
||||
}
|
||||
|
||||
// The assembled page as a string, plus a map of which output line came from which source file.
|
||||
function assemble({ src = path.join(ROOT, 'src') } = {}) {
|
||||
const shell = fs.readFileSync(path.join(src, 'index.html'), 'utf8');
|
||||
const out = [], map = [];
|
||||
let line = 1;
|
||||
const lines = shell.split('\n');
|
||||
lines.forEach((l, i) => {
|
||||
const m = INCLUDE.exec(l);
|
||||
if (!m) {
|
||||
out.push(l + (i < lines.length - 1 ? '\n' : ''));
|
||||
line++;
|
||||
return;
|
||||
}
|
||||
for (const p of partials(src, m[1])) {
|
||||
const n = p.text.split('\n').length - 1;
|
||||
map.push({ file: p.file, from: line, to: line + n - 1 });
|
||||
out.push(p.text);
|
||||
line += n;
|
||||
}
|
||||
});
|
||||
return { html: out.join(''), map };
|
||||
}
|
||||
|
||||
// Which source file (and line in it) an assembled-page line comes from: for stack traces and errors.
|
||||
function locate(map, line) {
|
||||
const m = map.find((x) => line >= x.from && line <= x.to);
|
||||
return m ? { file: m.file, line: line - m.from + 1 } : { file: 'index.html (shell)', line };
|
||||
}
|
||||
|
||||
// The page at a git ref: that commit's src/ through its own assembler, or site/index.html before the split.
|
||||
function assembleRef(ref, cwd = ROOT) {
|
||||
const git = (...a) => execFileSync('git', a, { cwd, encoding: 'utf8', maxBuffer: 1 << 28 });
|
||||
const has = (p) => { try { git('cat-file', '-e', `${ref}:${p}`); return true; } catch { return false; } };
|
||||
if (!has('src/index.html')) return git('show', `${ref}:site/index.html`);
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'tubes-ref-'));
|
||||
try {
|
||||
const tar = execFileSync('git', ['archive', '--format=tar', ref, 'src', 'scripts'], { cwd, maxBuffer: 1 << 28 });
|
||||
execFileSync('tar', ['-x', '-C', tmp], { input: tar });
|
||||
return execFileSync(process.execPath, [path.join(tmp, 'scripts', 'lib', 'page.js')], { cwd: tmp, encoding: 'utf8', maxBuffer: 1 << 28 });
|
||||
} finally { fs.rmSync(tmp, { recursive: true, force: true }); }
|
||||
}
|
||||
|
||||
// The assembled page written to a file, for tools that need a path (perf in a real browser, base vs head).
|
||||
function pageFile(file = path.join(os.tmpdir(), `tubes-page-${process.pid}.html`)) {
|
||||
fs.writeFileSync(file, assemble().html);
|
||||
return file;
|
||||
}
|
||||
|
||||
module.exports = { assemble, assembleRef, pageFile, locate, listFiles, ROOT };
|
||||
|
||||
if (require.main === module) {
|
||||
const args = process.argv.slice(2);
|
||||
const opt = (k) => { const i = args.indexOf(k); return i >= 0 ? args[i + 1] : null; };
|
||||
const html = opt('--ref') ? assembleRef(opt('--ref')) : assemble().html;
|
||||
const out = opt('-o');
|
||||
if (out) { fs.mkdirSync(path.dirname(path.resolve(out)), { recursive: true }); fs.writeFileSync(out, html); }
|
||||
else process.stdout.write(html);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue