| 1 | #!/usr/bin/env node |
| 2 | // Builds the level 2 vendor bundle for Elffuss Code. |
| 3 | // |
| 4 | // npm ci --ignore-scripts (once; npm never runs anywhere else in the repo) |
| 5 | // node build.mjs → web/vendor/x2/<buildId>/ + web/js/ext2/vendor-manifest.json |
| 6 | // node build.mjs --out <dir> → <dir>/vendor/x2/<buildId>/ + <dir>/js/ext2/vendor-manifest.json |
| 7 | // |
| 8 | // Steps: refuse to run on another Node version or with installed packages that |
| 9 | // differ from the pins; one Vite build per entry (one ES module file each, every |
| 10 | // asset inlined, no source map); then postbuild.mjs (gzip, hashes, buildId, |
| 11 | // licences, manifest) and sweep.mjs (no machine paths). Any failure stops the |
| 12 | // build before anything is written to the output folder. |
| 13 | import fs from 'node:fs'; |
| 14 | import path from 'node:path'; |
| 15 | import { fileURLToPath } from 'node:url'; |
| 16 | |
| 17 | const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 18 | const REPO = path.resolve(HERE, '../..'); |
| 19 | |
| 20 | export function checkToolchain() { |
| 21 | const want = fs.readFileSync(path.join(HERE, '.node-version'), 'utf8').trim(); |
| 22 | if (process.versions.node !== want) { |
| 23 | throw new Error(`This build only runs on Node ${want} (this is ${process.versions.node}): the output must be byte-identical everywhere.`); |
| 24 | } |
| 25 | const pkg = JSON.parse(fs.readFileSync(path.join(HERE, 'package.json'), 'utf8')); |
| 26 | const lock = JSON.parse(fs.readFileSync(path.join(HERE, 'package-lock.json'), 'utf8')); |
| 27 | const bad = []; |
| 28 | for (const [name, spec] of Object.entries({ ...pkg.dependencies, ...pkg.devDependencies })) { |
| 29 | const alias = /^npm:(@?[^@]+)@(.+)$/.exec(spec); |
| 30 | const realName = alias ? alias[1] : name; |
| 31 | const version = alias ? alias[2] : spec; |
| 32 | if (!/^\d+\.\d+\.\d+$/.test(version)) bad.push(`${name}: "${spec}" is not an exact version`); |
| 33 | let installed = null; |
| 34 | try { installed = JSON.parse(fs.readFileSync(path.join(HERE, 'node_modules', name, 'package.json'), 'utf8')); } catch { /* missing */ } |
| 35 | if (!installed) { bad.push(`${name} is not installed (run npm ci --ignore-scripts)`); continue; } |
| 36 | if (installed.name !== realName || installed.version !== version) bad.push(`${name}: installed ${installed.name}@${installed.version}, pinned ${realName}@${version}`); |
| 37 | } |
| 38 | // every package npm put in node_modules must be the one the lockfile names |
| 39 | for (const [key, entry] of Object.entries(lock.packages || {})) { |
| 40 | if (!key || entry.optional) continue; |
| 41 | let installed = null; |
| 42 | try { installed = JSON.parse(fs.readFileSync(path.join(HERE, key, 'package.json'), 'utf8')); } catch { /* missing */ } |
| 43 | if (!installed) bad.push(`${key} is missing (run npm ci --ignore-scripts)`); |
| 44 | else if (installed.version !== entry.version) bad.push(`${key}: installed ${installed.version}, lockfile ${entry.version}`); |
| 45 | } |
| 46 | if (bad.length) throw new Error('Installed packages do not match the pins:\n ' + bad.join('\n ')); |
| 47 | } |
| 48 | |
| 49 | export async function buildRaw(rawDir) { |
| 50 | const { build } = await import('vite'); |
| 51 | const { ENTRIES, configFor } = await import('./vite.config.mjs'); |
| 52 | fs.rmSync(rawDir, { recursive: true, force: true }); |
| 53 | const out = {}; |
| 54 | for (const entry of ENTRIES) { |
| 55 | const dir = path.join(rawDir, entry); |
| 56 | await build(configFor(entry, { outDir: dir })); |
| 57 | const files = fs.readdirSync(dir).sort(); |
| 58 | const js = files.filter(f => f.endsWith('.js')); |
| 59 | const extra = files.filter(f => !f.endsWith('.js') && !f.endsWith('.licenses.json')); |
| 60 | if (js.length !== 1 || js[0] !== entry + '.js' || extra.length) { |
| 61 | throw new Error(`${entry}: expected exactly one file ${entry}.js, got ${files.join(', ')}`); |
| 62 | } |
| 63 | out[entry] = { file: path.join(dir, entry + '.js'), licenses: JSON.parse(fs.readFileSync(path.join(dir, entry + '.licenses.json'), 'utf8')) }; |
| 64 | } |
| 65 | return out; |
| 66 | } |
| 67 | |
| 68 | async function main() { |
| 69 | const args = process.argv.slice(2); |
| 70 | const outIdx = args.indexOf('--out'); |
| 71 | const outRoot = outIdx >= 0 ? path.resolve(args[outIdx + 1]) : path.join(REPO, 'web'); |
| 72 | checkToolchain(); |
| 73 | const rawDir = path.join(HERE, '.build', 'raw'); |
| 74 | const raw = await buildRaw(rawDir); |
| 75 | const { postbuild } = await import('./postbuild.mjs'); |
| 76 | const result = await postbuild(raw, { outRoot, buildRoot: HERE }); |
| 77 | console.log(`buildId ${result.manifest.buildId}: ${result.manifest.files.map(f => `${f.name} ${f.raw} raw, ${f.gz} gz`).join('; ')}; total ${result.totalGz} gz`); |
| 78 | console.log('manifest: ' + path.relative(process.cwd(), result.manifestPath)); |
| 79 | } |
| 80 | |
| 81 | if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 82 | main().catch(e => { console.error('BUILD FAILED: ' + (e?.message || e)); process.exit(1); }); |
| 83 | } |