// Build the browser runtime from source; no prebuilt local WASM is required. import { spawnSync } from 'node:child_process'; import { mkdirSync, readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; const site = fileURLToPath(new URL('../', import.meta.url)); const rust = fileURLToPath(new URL('../../../wip-rs/', import.meta.url)); const source = JSON.parse(readFileSync(new URL('../rust-source.json', import.meta.url), 'utf8')); const revision = spawnSync('git', ['rev-parse', 'HEAD'], { cwd: rust, encoding: 'utf8' }); const status = spawnSync('git', ['status', '--porcelain'], { cwd: rust, encoding: 'utf8' }); if (revision.status !== 0 || revision.stdout.trim() !== source.revision || status.status !== 0 || status.stdout.trim()) { if (process.env.WIP_ALLOW_DIRTY_SOURCE !== '1') { throw new Error(`Expected clean sibling wip-rs at ${source.revision}. See rust-source.json; use WIP_ALLOW_DIRTY_SOURCE=1 only for local Rust development.`); } console.warn('Building with an explicitly unpinned local Rust source; this is not a reproducible release build.'); } function run(command, args) { const result = spawnSync(command, args, { cwd: site, stdio: 'inherit' }); if (result.error) throw result.error; if (result.status !== 0) process.exit(result.status ?? 1); } function available(command) { return spawnSync(command, ['--version'], { stdio: 'ignore' }).status === 0; } const testProbes = process.argv.includes('--test-probes'); const output = testProbes ? 'test-assets/wip-wasm' : 'public/wip-wasm'; const cargo = ['build', '--locked', '--manifest-path', 'browser/Cargo.toml', '--target', 'wasm32-unknown-unknown', '--release', ...(testProbes ? ['--features', 'test-probes'] : [])]; if (available('cargo')) run('cargo', cargo); else run('nix', ['shell', '--inputs-from', rust, 'nixpkgs#lld', '--command', 'nix', 'develop', rust, '--no-write-lock-file', '--command', 'cargo', ...cargo]); mkdirSync(new URL(`../${output}/`, import.meta.url), { recursive: true }); const bindgen = ['target/wasm32-unknown-unknown/release/wip_browser.wasm', '--target', 'web', '--out-dir', output, '--out-name', 'wip_browser']; if (available('wasm-bindgen')) run('wasm-bindgen', bindgen); else run('nix', ['shell', '--inputs-from', rust, 'nixpkgs#wasm-bindgen-cli', '--command', 'wasm-bindgen', ...bindgen]);