Browser execution and parity
The compiler in a page: @pcbjs/compile/browser runs the stage graph in a Web Worker on files held in memory and streams the facets as they end, equal to Node's compile byte for byte. Advanced. Read it to compile a board in a browser, to know what of the compiler runs there and what cannot, and how the equality with Node is kept; design.md has the platform beneath it.
import { compileInBrowser, type BrowserModule } from '@pcbjs/compile/browser'
declare const board: BrowserModule
// compile-worker.ts is one line: import '@pcbjs/export/browser-worker'
const worker = () => new Worker(new URL('./compile-worker.ts', import.meta.url), { type: 'module' })
compileInBrowser(board, { worker, want: ['layers', 'findings'], facet: f => console.log(f.board, f.facet, f.hash) }).then(r => console.log(r.ms, r.check.ok))What runs and what cannot
- Runs: evaluating the design and its config, every stage through
checkswithwantand a cancel, the facetsmeta,sch,layers,modelandfindings, DRC and ERC,model-missingincluded, outline fonts, KiCad footprints,reference()of a board the module carries, the route memo of a board. - Cannot: the
exportsstage, so no fab set, SVG file, GLB or raster, which need Node's zlib, sharp and a child process; STEP to mesh, which is OpenCascade in Node; the scope and source range transforms and the typecheck, which run when the module is built; areference()the module does not carry, which compiles another entry in a child process; fetching akicad:,lcsc:or https model; discovering KiCad libraries beyond the files given; the user model cache. - A read of a file the module does not name finds nothing, as a missing file in Node;
reference()of an entry it does not carry fails withnot available in the browser compile: <path>.
The module
BrowserModule is one entry ready for a browser compile, its paths virtual under root, /project:
| Field | What it holds |
|---|---|
format | 1 |
root | the virtual project root |
project | names the project, every entry of it alike; one worker keeps each project's parts, bound routes and defaults apart by it, as inRealm does in Node; default the config's URL, else the module's |
entry | the entry relative to the root, as the CLI names it |
config | the config as browser ESM, evaluated first for its defaults, plugins and kicad folders |
module | the design as browser ESM, its default export the board |
patches | the entry's .patches.ts as browser ESM, when it has one |
modules | every built module of the project's own sources, whose stack frames map back through their source maps |
sources | the sha1 of each source module, which keys the stage cache |
files | fonts, KiCad footprints and model files by virtual path: a URL fetched before the compile, or null for a file that only has to exist |
routes | route memos by board name, URLs of .pcbjs/cache/route/<board>.json |
references | each referenced board by absolute virtual entry, URLs of its ReferencedBoard JSON |
meshes | the mesh hash Node gave each STEP model of files, by virtual path, so a model whose content the module leaves out names the mesh beside the site; a fetched model resolves to its file under .pcbjs/models/fetched as the CLI keeps it |
env | what the compile reads of the environment |
generatedAt | the one timestamp of the facets |
expect | checkHash of each facet the Node compile gave, its root written /project, by board/target/facet |
math | how the Node that built the module rounds its Math, plain or fused |
?pcb:moduleof@pcbjs/cli/vitebuilds one, ui.md, and so does the equality test, both through the CLI's module build. The modules carry the CLI's transforms: the scope injection and source ranges before esbuild, as Vite's SSR applies them, so parts take their names and items their source ranges as in Node.- They are bundled with esbuild and
keepNames, one build per project, config and entries sharing chunks. A bundler that renames a function expression shadowing its own constant, as Rollup does withconst TestPad = defineFootprint(function TestPad…, changes the name a part is labelled by. The transform of each file renames such a function the way Vite's does, and keeping names then keeps that. - The build defines
process.envas an empty object, so a design reading the environment reads none in the browser. The site build ofboards: 'compile'leaves the pcbjs packages out of each project and imports them from one shared build underpcb/lib/. - A source map names its sources by virtual path:
/project/…for the project,/node_modules/@pcbjs/…for a package, whose frames are never a call site.
The worker
@pcbjs/export/browser-workeris the module Web Worker;serveCompile(port, { stream })of@pcbjs/compile/browseris its runtime, and export'sfacetStreamthe facets. One worker serves a page, one compile at a time.- To the worker:
{ t: 'compile', id, module, want?, boards? }, and once{ t: 'init', flag }with aSharedArrayBufferundercrossOriginIsolated. - From it:
{ t: 'ready', check }on load, then per compile{ t: 'stage', id, s }as each stage starts, ends or hits,{ t: 'facet', id, f }as each facet is made,{ t: 'board', id, board, target?, json }per target whenboardsasks, and{ t: 'done', id, ms, mismatch?, error? }.s,fanderrorhave the session worker's shapes. - Facets come in stage order:
meta,layers,shapesandmodelafterplace,schafterschematic,layersandshapesaftertuneandfinish, andfindingsafterchecks. compileInBrowser(module, { worker, want, boards, facet, stage, signal })keeps one worker per factory and resolves with every facet, the boards asked for, the self-check and any mismatch. A cancel throughsignalraises the shared flag, or without one ends the worker; the next compile starts another, the runtime coming from the HTTP cache.- The worker keeps its own stage cache and route memos in memory, keyed by its own hashes; it never reads or writes Node's cache. A board's route memo seeds it, so no leg routes cold.
Math
- Chromium builds V8 with another libm than Node does:
Math.sin,exp,log,atan2and the rest differ from Node's in the last bit for a few inputs in a hundred, and a board's arcs, rotations and pours with them. - The worker installs the fdlibm of V8's
base/ieee754.ccin JavaScript, so its Math is Node's bit for bit. Node built by clang for arm64 fusesa*b + cinto one rounding where the source writes it, and x86-64 builds do not;mathKind()in Node names which, and the module'smathpicks the same in the worker. Math.powandMath.hypotare V8's own in both and need nothing.
Self-check
- On load the worker compiles a small fixture, a circle board with rotated parts, a route and a pour, with the plain Math, and compares its hash with
SELF_CHECK_HASH.plain, an x86-64 Node's. A mismatch logs one warning; the compile still runs and shows the browser's result. - A module with
expectis checked facet by facet withcheckHash, the facet's hash without the router's own countscached,rippedandrounds, which tell a warm compile from a cold one; the facets that differ come back inmismatch, with one warning naming them. - Firefox and Safari are not gated. Their Math is fdlibm too once the worker installs it, so a mismatch there means another engine difference, which the self-check reports.
The equality test
node scripts/browser-equality.mjs [--port <n>] [--devtools <n>] [--dump <dir>] [project …] compiles the demo boards and highspeed in Node through the CLI and in a headless Chromium through the worker, and compares each target's board JSON, finding ids and inspect --lengths byte for byte, Node's project root written as /project. The router's own counts, cached, ripped and rounds, tell a warm compile from a cold one and are left out, as stage-equality leaves them out.
- Chromium is
$PCBJS_CHROME, else Chrome, Chromium or Edge in the usual places, else a Playwright download; one that exits at once gives way to the next. Without one the script prints a notice and exits 0. - It drives the browser over the DevTools protocol with Node's own WebSocket, no Playwright or Puppeteer.
- The Node side runs from a folder named
node_modules, so no route takes the test as its caller, as none takes the CLI. - CI's dedicated
browserjob runs this check once on Linux with Chromium set up. It is not repeated inside the package test suite.