Files
SlpSoftware/scripts/htmlBaseline.mjs

104 lines
3.8 KiB
JavaScript

// Safety net for moving texts into the language files: saves what the prerendered page says
// (text, links, labels, meta tags, structured data and the CMS data block, if present) and later
// checks that a new build says exactly the same. Styling, scripts, asset hashes and generated class
// names are left out, so only a change of wording or content shows up.
//
// Usage (after `pnpm build`, or `vite build && node scripts/prerender.mjs`):
// node scripts/htmlBaseline.mjs save writes .baseline/index.html (ignored by git)
// node scripts/htmlBaseline.mjs check compares dist/index.html with .baseline (exit code 1 on a difference)
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
const DIST_FILE = join('dist', 'index.html');
const BASELINE_DIR = '.baseline';
const BASELINE_FILE = join(BASELINE_DIR, 'index.html');
// What stays of the scripts: structured data and data blocks, not code
const KEEP_SCRIPT = /type="application\/(?:ld\+json|json)"/;
export function normalizeHtml(html) {
return (
html
.replace(/<!--(?!\/?CMS:)[\s\S]*?-->/g, '')
.replace(/<style[\s\S]*?<\/style>/g, '')
.replace(/<script\b([^>]*)>([\s\S]*?)<\/script>/g, (all, attrs) =>
KEEP_SCRIPT.test(attrs) ? all : '',
)
.replace(/<noscript>[\s\S]*?<\/noscript>/g, '<noscript></noscript>')
// Only the canonical link is content; the others point to hashed files
.replace(/<link\b[^>]*>/g, (tag) => (/rel="canonical"/.test(tag) ? tag : ''))
// Look, not content
.replace(/\s(?:class|style|data-emotion|data-reveal)="[^"]*"/g, '')
// The hash in /assets/name-Hash.ext
.replace(/(\/assets\/[\w.-]*?)-[A-Za-z0-9_-]{8}(\.[a-z0-9]+)/g, '$1$2')
// Line breaks and indentation inside a tag are formatting
.replace(/<[a-zA-Z][^>]*>/g, (tag) => tag.replace(/\s+/g, ' ').replace(/\s+(\/?>)$/, '$1'))
.replace(/>\s+</g, '><')
// One tag per line, so a difference is readable
.replace(/></g, '>\n<')
.trim() + '\n'
);
}
// The first lines that differ, for the message
export function firstDifferences(a, b, max = 6) {
const left = a.split('\n');
const right = b.split('\n');
const out = [];
for (let i = 0; i < Math.max(left.length, right.length) && out.length < max * 3; i++) {
if (left[i] !== right[i]) {
out.push(
` regel ${i + 1}:`,
` basislijn: ${left[i] ?? '(geen)'}`,
` nu: ${right[i] ?? '(geen)'}`,
);
}
}
return out;
}
function readDist() {
if (!existsSync(DIST_FILE)) {
console.error(`${DIST_FILE} bestaat niet; draai eerst \`pnpm build\`.`);
return null;
}
return normalizeHtml(readFileSync(DIST_FILE, 'utf8'));
}
function save() {
const now = readDist();
if (now === null) return false;
mkdirSync(BASELINE_DIR, { recursive: true });
writeFileSync(BASELINE_FILE, now);
console.log(`Basislijn opgeslagen in ${BASELINE_FILE}`);
return true;
}
function check() {
if (!existsSync(BASELINE_FILE)) {
console.error('Geen basislijn; draai eerst `pnpm baseline:save`.');
return false;
}
const now = readDist();
if (now === null) return false;
const was = readFileSync(BASELINE_FILE, 'utf8');
if (now !== was) {
console.error('index.html: tekst verschilt van de basislijn');
console.error(firstDifferences(was, now).join('\n'));
return false;
}
console.log('Gelijk aan de basislijn: index.html');
return true;
}
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
const command = process.argv[2];
if (command === 'save') process.exitCode = save() ? 0 : 1;
else if (command === 'check') process.exitCode = check() ? 0 : 1;
else {
console.error('Gebruik: node scripts/htmlBaseline.mjs save|check');
process.exitCode = 2;
}
}