@doenet/prefigure
v0.6.7
Published
Standalone PreFigure WASM compiler for browser and CDN usage
Downloads
254
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/[email protected]/prefigure.js';
await prefigure.initPrefigure();
const result = await prefigure.compilePrefigure(diagramXml, { mode: 'svg' });
</script>Pin a specific CDN version instead of using @latest so the browser runtime,
vendored wheel, and downstream Doenet defaults stay reproducible.
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.
Runtime Version Sync
Builds vendor wheels from pyodide_packages/, and the browser runtime is only
consistent when all pinned PreFigure pieces move together.
Before publishing or bumping the runtime, keep these in sync:
packages/prefigure/package.jsonversion for the published@doenet/prefiguretagsrc/worker/compiler-metadata.tsPREFIG_VERSION/PREFIG_WHEEL_FILENAMEpyodide_packages/prefig-<version>-py3-none-any.whlvianpm run setup -w @doenet/prefigurepackages/doenetml/src/Viewer/renderers/utils/prefigureConfig.tsdefault CDN URLs for@doenet/prefigureanddiagcess
At runtime, initPrefigure() defaults to loading wheel assets from an assets/
directory relative to the loaded prefigure.js module URL. Pass indexURL to
initPrefigure(indexURL) to override this default.
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 PreFigure runtime version
@doenet/prefigure is not managed by Changesets. Its npm version is manually
pinned to match the bundled prefig Python wheel so consumers can immediately
see which upstream version they are running. Follow these steps when bumping:
- Update
versioninpackages/prefigure/package.jsonto match the new wheel version. - Update
PREFIG_VERSIONinsrc/worker/compiler-metadata.ts. - Update the default CDN module URL in
packages/doenetml/src/Viewer/renderers/utils/prefigureConfig.ts. - If the bundled accessibility runtime changed, update the default diagcess CDN URL there as well.
- Update
prefigure.doenet.orgoutside this repository so the fallback build-service path matches the new runtime behavior before local WASM compilation warms up. - Update the hardcoded wheel path in
packages/doenetml-print/src/ptx-compiler.ts— the Vite?uint8array&base64asset import must be a static literal string, so it cannot referencePREFIG_WHEEL_FILENAMEdirectly. Search for the old version string and replace it. - Run
npm run setup -w @doenet/prefigureto fetch the matchingprefig-<version>-py3-none-any.whl. - 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.
