@doenet/prefigure
v0.5.13
Published
Standalone PreFigure WASM compiler for browser and CDN usage
Downloads
867
Readme
@doenet/prefigure
Standalone PreFigure compiler package for browser and CDN usage.
Run the PreFigure Python compiler in the browser via Pyodide + Web Worker. This package can be loaded from NPM or from a CDN (jsDelivr) and provides a full-featured PreFigure XML → SVG compiler without server dependencies.
Attribution
This package includes code adapted from the PreFigure project and its browser playground runtime:
- Upstream repository: https://github.com/davidaustinm/prefigure
- Project website: https://prefigure.org
The upstream-derived worker/runtime pieces remain available under the GNU
Affero General Public License, version 3 or later. This package is distributed
under the same AGPL-3.0-or-later license and preserves attribution to the
upstream PreFigure work. See NOTICE.md for package-specific provenance.
Installation
npm install @doenet/prefigureOr load from CDN:
<script type="module">
import * as prefigure from 'https://cdn.jsdelivr.net/npm/@doenet/prefigure@latest';
await prefigure.initPrefigure();
const result = await prefigure.compilePrefigure(diagramXml, { mode: 'svg' });
</script>Global API
When loaded in a browser, this package registers:
window.initPrefigure(indexURL?)window.prefigure(source, { mode, indexURL })
Both methods return promises.
Example
<script type="module" src="./prefigure.js"></script>
<script type="module">
await window.initPrefigure();
const result = await window.prefigure(`<diagram dimensions="(100,100)"></diagram>`);
console.log(result.svg, result.annotationsXml);
</script>Browser Runtime Check
Run a local browser runtime check harness that compiles a tiny diagram and checks for non-empty SVG + annotations output:
npm run browser-runtime -w @doenet/prefigureThen open http://127.0.0.1:4175/.
The page shows PASS when compilePrefigure(...) returns both:
- SVG containing an
<svg>element - annotations XML containing
<annotations>or<annotation>
Automated Tests
Run package tests:
npm run test -w @doenet/prefigureCurrent coverage includes API-level behavior tests for:
- default index URL resolution
initPrefigure()idempotency and conflicting-URL guardcompilePrefigure()delegation/result mapping- dedicated-worker API wiring and compile delegation behavior
The browser runtime check remains useful as a manual runtime check for real Pyodide+WASM execution.
Wheel Requirement
Builds currently vendor wheels from pyodide_packages/. Before publishing,
ensure a matching prefig-<version>-py3-none-any.whl is present there.
At runtime, initPrefigure() defaults to loading from ./assets/.
Repository Hygiene
These directories/files in packages/prefigure are generated and safe to
delete locally:
dist/pyodide_packages/.wireit/src/worker/liblouis/generated/build-no-tables-utf32.jssrc/worker/liblouis/generated/*.ctisrc/worker/liblouis/generated/*.ctbsrc/worker/liblouis/generated/*.utisrc/worker/liblouis/generated/*.dis
They are re-created by build/setup scripts and are already ignored by the
repository .gitignore.
Maintenance Procedures
First-time setup
npm run setup -w @doenet/prefigureThis downloads:
- runtime Pyodide wheels and the pinned
prefigwheel intopyodide_packages/ - liblouis JS/tables into
src/worker/liblouis/generated/
Normal build + publish checks
npm run build -w @doenet/prefigure
npm run verify-wheel-sync -w @doenet/prefigureverify-wheel-sync ensures src/worker/compiler-metadata.ts and
pyodide_packages/ reference the same prefig wheel.
Upgrade prefig wheel version
- Update
PREFIG_WHEEL_FILENAMEinsrc/worker/compiler-metadata.ts. - Run
npm run setup -w @doenet/prefigure. - Run
npm run verify-wheel-sync -w @doenet/prefigure. - Build and run the browser runtime check:
npm run build -w @doenet/prefigure
npm run browser-runtime -w @doenet/prefigureUpgrade Pyodide runtime packages
- Bump
pyodideinpackages/prefigure/package.json. - Run
npm installat repo root. - Run
npm run setup -w @doenet/prefigure(reads the newpyodide-lock.json). - Run build + the browser runtime check.
Upgrade liblouis assets
- Update
LIBLOUIS_REFand hashes inscripts/fetch-liblouis.ts. - Run
npm run setup -w @doenet/prefigure. - Run build + the browser runtime check.
