Repository navigation
Conversation
…on builds
TTFLoader calls `new MSDF()` with no arguments, so @zappar/msdf-generator
falls back to its own defaults for the worker and the wasm. Neither
survives a bundler.
The worker is shipped as unbundled ESM containing a bare
`import { expose, transfer } from 'comlink'`, and the package builds its
worker URL in the MSDF constructor, separate from the
`new Worker(url, { type: 'module' })` that consumes it. Bundlers only
bundle a worker when both halves are visible in one expression, so the
file is copied through verbatim instead — and a module worker cannot
resolve a bare specifier, so it fails to load a few milliseconds after
construction. The wasm is the same problem in a second form: Emscripten
names it with a runtime string through locateFile, so no bundler can see
that dependency and it is never emitted.
Both failures are silent. A worker whose module graph fails to resolve
fires an error event with an empty message, no filename and no line, so
nothing reaches the console; the generator simply never initialises and
the caller's promise never settles. Downstream that reads as a blank
canvas with a clean console, which is a hard thing to chase.
No consumer configuration fixes this. Building a consuming app with the
settings our own ttf examples use — optimizeDeps.exclude on
@zappar/msdf-generator plus assetsInclude for wasm — still emits the
worker with its bare import and still omits the wasm. The examples do not
catch it because they only have a dev script; nothing here is ever
production-built.
So bundle the worker into this package and ship the wasm beside it, then
point TTFLoader at copies it can address relatively, which every bundler
understands. The assets are written to src/loaders as well as
dist/loaders because this package resolves to src/index.ts inside the
monorepo and only to dist/index.js once published, so import.meta.url in
ttf.ts resolves against whichever is in play.
Verified against a WebXR app that reproduced the hang: with this change
and no build-side workaround, its World.create() promise resolves in
1432ms and the scene renders at 617 draw calls per frame, with the wasm
served once per font weight and no 404s. Before it never resolved at all.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
TTFLoadercallsnew MSDF()with no arguments, so@zappar/msdf-generatorfalls back to its own defaults for both the worker and the wasm. Neither survives a bundler.The worker. It ships as unbundled ESM containing a bare
import { expose, transfer } from 'comlink', and the package builds its worker URL in theMSDFconstructor — separate from thenew Worker(url, { type: 'module' })that consumes it:Bundlers only bundle a worker when both halves are visible in a single expression. Split like this, the file is copied through verbatim instead — and a module worker cannot resolve a bare specifier, so it fails to load a few milliseconds after construction.
The wasm. Same problem in a second form: Emscripten names it with a runtime string through
locateFile, so no bundler can see the dependency and it is never emitted.Why it is hard to diagnose
Both failures are silent. A worker whose module graph fails to resolve fires an
errorevent with an empty message, no filename and no line, and nothing reaches the console. The generator never initialises, so the caller's promise never settles. Downstream that reads as a blank canvas with a completely clean console.No consumer configuration fixes it
I built a consuming app using the exact settings our own ttf examples carry —
optimizeDeps.exclude: ['@zappar/msdf-generator']plusassetsInclude: ['**/*.wasm']— and the emitted worker still containedfrom "comlink"and the wasm was still not emitted.Our examples do not catch this because they only have a
devscript; nothing inexamples/is ever production-built.The change
scripts/build-msdf-assets.mjsbundles the worker (inlining comlink) and copies the wasm.TTFLoaderpoints at those copies vianew URL('./…', import.meta.url).href, the bundler-agnostic idiom.src/loadersanddist/loaders, because this package resolves tosrc/index.tsinside the monorepo and only todist/index.jsonce published (publishConfig), soimport.meta.urlinttf.tsresolves against whichever is in play.Verification
Measured against a WebXR app that reproduced the hang, with no build-side workaround in the app:
World.create()msdfgen_wasm.wasmNotes for reviewers
esbuilddevDependency needs apnpm install, but my pnpm reformatted the entirepnpm-lock.yaml(~6250 lines of churn for ~4 real ones), so I left it out rather than bury the diff. Needs regenerating.preparescript is how thesrc/loaderscopies come to exist after install, which the examples need in dev. Happy to move it if you would rather it sat elsewhere in the pipeline.packages/uikitcurrently has 57 pre-existingtscerrors onmain, unrelated to this change;tscstill emits, so the build completes.@zappar/msdf-generator(inline thenew Worker(new URL(...))and defaultwasmUrl), but that package publishes to npm with norepository,homepageorbugsfield and no public source I could find, so this seemed like the right layer.