commit
e092cb029c
252 files changed
+35037
No files matched your search
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env node
|
||||
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// The assembled site's index.html, from the one in the repository: scripts/assemble.sh runs this on site/.
|
||||
// node scripts/assemble-page.mjs <site-dir>
|
||||
// It adds two things a checkout served as it is (pixi run dev) does without:
|
||||
// - <meta name="pplus-version">, the commit in site/VERSION, which the footer shows (no request needed);
|
||||
// - a <link rel="modulepreload"> for every module the page's static import graph reaches. Without a bundler the
|
||||
// browser finds the modules one level of imports at a time, so a first visit waits for a chain of round trips
|
||||
// (six levels today); with the list it asks for them all at once. The links go after the import map, which a
|
||||
// browser must have read before it loads any module.
|
||||
// Nothing else in the page changes, so the hashes of its inline scripts (docs/Caddyfile's CSP) still hold.
|
||||
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
|
||||
import { resolve, dirname, relative, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const IMPORT = /(?:^|[;\n}])\s*(?:import|export)\s*(?:[\w*{}\s,$]*?\sfrom\s*)?['"]([^'"]+)['"]/g;
|
||||
|
||||
/**
|
||||
* Every module the page's entry imports, directly or through others, as hrefs relative to the page ('./dist/…',
|
||||
* './core/dist/…'), shallowest first; the entry itself is not listed (its own <script> loads it).
|
||||
* @param {string} root the directory that holds index.html, dist/ and core/dist/
|
||||
* @param {string} [entry] the entry module, relative to root
|
||||
* @returns {string[]}
|
||||
*/
|
||||
export function modulePreloads(root, entry = 'dist/main.js') {
|
||||
const coreDist = resolve(root, 'core/dist');
|
||||
const target = (from, spec) => spec.startsWith('#core/') ? join(coreDist, spec.slice('#core/'.length)) : spec.startsWith('.') ? resolve(dirname(from), spec) : null;
|
||||
const start = resolve(root, entry);
|
||||
const depth = new Map([[start, 0]]);
|
||||
const queue = [start];
|
||||
while (queue.length) {
|
||||
const file = queue.shift();
|
||||
const src = readFileSync(file, 'utf8');
|
||||
for (const m of src.matchAll(IMPORT)) {
|
||||
const t = target(file, m[1]);
|
||||
if (!t || depth.has(t)) continue;
|
||||
if (!existsSync(t)) throw new Error(`${relative(root, file)} imports ${m[1]}, which is not in ${root}`);
|
||||
depth.set(t, depth.get(file) + 1);
|
||||
queue.push(t);
|
||||
}
|
||||
}
|
||||
return [...depth].filter(([f]) => f !== start).sort((a, b) => a[1] - b[1] || (a[0] < b[0] ? -1 : 1)).map(([f]) => `./${relative(root, f).split('\\').join('/')}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* The page with the version and the preloads in it.
|
||||
* @param {string} html index.html as the repository has it
|
||||
* @param {{ version?: string | null, preloads?: string[] }} parts
|
||||
* @returns {string}
|
||||
*/
|
||||
export function assemblePage(html, { version = null, preloads = [] } = {}) {
|
||||
let out = html;
|
||||
if (version && /^[0-9a-f]{7,40}$/.test(version)) out = out.replace(/\n<\/head>/, `\n <meta name="pplus-version" content="${version}">\n</head>`);
|
||||
const map = /<script type="importmap">[^<]*<\/script>\n/.exec(out);
|
||||
if (!map) throw new Error('index.html has no import map: the preloads go after it');
|
||||
const links = preloads.map((h) => ` <link rel="modulepreload" href="${h}">\n`).join('');
|
||||
return out.slice(0, map.index + map[0].length) + links + out.slice(map.index + map[0].length);
|
||||
}
|
||||
|
||||
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||||
const site = resolve(process.argv[2] ?? 'site');
|
||||
const page = join(site, 'index.html');
|
||||
const versionFile = join(site, 'VERSION');
|
||||
const version = existsSync(versionFile) ? readFileSync(versionFile, 'utf8').trim() : null;
|
||||
const preloads = modulePreloads(site);
|
||||
writeFileSync(page, assemblePage(readFileSync(page, 'utf8'), { version, preloads }));
|
||||
console.log(`index.html: ${preloads.length} module preloads${version ? `, version ${version.slice(0, 12)}` : ''}`);
|
||||
}
|
||||
Reference in new issue
Block a user