| 1 | // Builds the Blitz renderer prototype for the web dev app: renderer-blitz/ → |
| 2 | // wasm32-unknown-unknown (cargo, `wasm-release` profile) → wasm-bindgen glue |
| 3 | // (`--target web`; only web-time needs it, the twb_* exports are plain C ABI) |
| 4 | // → renderer-blitz/pkg-web/ (gitignored; the Vite plugin serves it at /blitz/, |
| 5 |
| 6 | // docs/blitz-experiment.md. Needs rustup's cargo with the wasm32 target and |
| 7 | // wasm-bindgen-cli at the version Cargo.lock pins for the wasm-bindgen crate. |
| 8 | // |
| 9 | // corepack pnpm build:blitz-web |
| 10 |
| 11 | import { spawnSync } from 'node:child_process'; |
| 12 | import fs from 'node:fs'; |
| 13 | import os from 'node:os'; |
| 14 | import path from 'node:path'; |
| 15 | import { fileURLToPath } from 'node:url'; |
| 16 |
| 17 | const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 18 | const crate = path.join(root, 'apps', 'renderer-blitz'); |
| 19 | const outDir = path.join(crate, 'pkg-web'); |
| 20 | const target = 'wasm32-unknown-unknown'; |
| 21 | const wasm = path.join(crate, 'target', target, 'wasm-release', 'twidget_blitz.wasm'); |
| 22 | const env = { ...process.env, PATH: ['/opt/homebrew/opt/rustup/bin', path.join(os.homedir(), '.cargo', 'bin'), process.env.PATH ?? ''].join(path.delimiter) }; |
| 23 |
| 24 | function run(cmd: string, args: string[], cwd: string) { |
| 25 | console.log(`$ ${cmd} ${args.join(' ')}`); |
| 26 | const r = spawnSync(cmd, args, { cwd, env, stdio: 'inherit' }); |
| 27 | if (r.error) throw new Error(`${cmd}: ${r.error.message}${r.error.message.includes('ENOENT') ? ' (install it: rustup / cargo install wasm-bindgen-cli --version <Cargo.lock>)' : ''}`); |
| 28 | if (r.status !== 0) process.exit(r.status ?? 1); |
| 29 | } |
| 30 |
| 31 | const lock = fs.readFileSync(path.join(crate, 'Cargo.lock'), 'utf8'); |
| 32 | const bindgenVersion = /name = "wasm-bindgen"\nversion = "([^"]+)"/.exec(lock)?.[1]; |
| 33 | const installed = spawnSync('wasm-bindgen', ['--version'], { env, encoding: 'utf8' }).stdout?.trim(); |
| 34 | if (bindgenVersion && installed && !installed.endsWith(bindgenVersion)) { |
| 35 | console.error(`wasm-bindgen-cli is ${installed}, Cargo.lock pins wasm-bindgen ${bindgenVersion}: cargo install wasm-bindgen-cli --version ${bindgenVersion} --locked`); |
| 36 | process.exit(1); |
| 37 | } |
| 38 |
| 39 | const targets = spawnSync('rustup', ['target', 'list', '--installed'], { env, encoding: 'utf8' }).stdout ?? ''; |
| 40 | if (!targets.split('\n').includes(target)) run('rustup', ['target', 'add', target], crate); |
| 41 | run('cargo', ['build', '--profile', 'wasm-release', '--lib', '--target', target], crate); |
| 42 | fs.mkdirSync(outDir, { recursive: true }); |
| 43 | run('wasm-bindgen', ['--target', 'web', '--no-typescript', '--out-dir', outDir, '--out-name', 'twidget_blitz', wasm], crate); |
| 44 | for (const f of fs.readdirSync(outDir).sort()) console.log(`${f}: ${(fs.statSync(path.join(outDir, f)).size / 1024 / 1024).toFixed(2)} MB`); |
| 45 |