-
Notifications
You must be signed in to change notification settings - Fork 1
convert inline worker generation to a post-processing step #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Devin T. Currie (DTCurrie)
wants to merge
18
commits into
main
Choose a base branch
from
make-worker-transform-post-process
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
aa9ebc5
convert inline worker generation to a post-processing step
DTCurrie 9767af3
lint
DTCurrie f35b17e
Merge branch 'main' into make-worker-transform-post-process
DTCurrie 788dc83
Merge branch 'make-worker-transform-post-process' of github.com:viam-…
DTCurrie a5928c2
Merge branch 'main' of github.com:viam-labs/motion-tools into make-wo…
DTCurrie c21c0c8
Merge branch 'main' of github.com:viam-labs/motion-tools into make-wo…
DTCurrie 0a0d8fe
Merge branch 'main' into make-worker-transform-post-process
DTCurrie 364db4f
Merge branch 'main' into make-worker-transform-post-process
DTCurrie 8546a1e
Merge branch 'main' into make-worker-transform-post-process
DTCurrie 78c1ef1
Merge branch 'main' into make-worker-transform-post-process
DTCurrie 5e8ff89
Merge branch 'main' into make-worker-transform-post-process
DTCurrie 2764dac
Merge branch 'main' of github.com:viam-labs/motion-tools into make-wo…
DTCurrie 37dd415
fix wireit ordering, replaceAll, and ./worker.js path
github-actions[bot] 6f127d5
lint
DTCurrie b3c8c12
Merge branch 'main' into make-worker-transform-post-process
DTCurrie 8852e4b
fix vite config
DTCurrie 8ce8ae0
Merge branch 'make-worker-transform-post-process' of github.com:viam-…
DTCurrie d9aa21f
Merge branch 'main' of github.com:viam-labs/motion-tools into make-wo…
DTCurrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # scripts/ | ||
|
|
||
| Build and code generation scripts that run as part of the development workflow or publish pipeline. | ||
|
|
||
| --- | ||
|
|
||
| ## `post-process-workers.js` | ||
|
|
||
| **Run automatically as part of `pnpm run prepack`** (`svelte-package && node scripts/post-process-workers.js && publint`). | ||
|
|
||
| ### What it does | ||
|
|
||
| After `svelte-package` copies `src/lib` into `dist/`, this script rewrites web worker references so the published package works correctly in consumer projects. | ||
|
|
||
| It scans `dist/` for files containing `new Worker(new URL(..., import.meta.url))`, bundles each referenced worker file into a self-contained IIFE using esbuild (inlining all dependencies like Three.js PCDLoader), replaces the `new Worker(new URL(...))` call with a Blob URL equivalent, and deletes the now-unnecessary worker `.js` files from `dist/`. | ||
|
|
||
| ### Why we need it | ||
|
|
||
| This is a workaround for a [known open Vite bug](https://github.com/vitejs/vite/issues/21422). When a library uses `new Worker(new URL('./worker.js', import.meta.url))`, a consumer project's Vite dep optimizer moves the library to `.vite/deps/` but the worker file doesn't follow -- causing a runtime error. | ||
|
|
||
| `svelte-package` copies `.ts` files as-is (transpiled to JS), so there is no point in the library packaging pipeline where Vite's bundler runs and can resolve worker imports. The fix is to post-process `dist/` and inline the worker code so that consumers load it from a Blob URL with no file path dependency. | ||
|
|
||
| In dev mode within this project, Vite handles `new Worker(new URL(...))` natively with full HMR support -- no post-processing is needed. | ||
|
|
||
| --- | ||
|
|
||
| ## `model-pipeline.js` | ||
|
|
||
| **Run manually via `pnpm run model-pipeline:run`** when new 3D models need to be added to the project. | ||
|
|
||
| ### What it does | ||
|
|
||
| Converts `.glb` and `.gltf` 3D model files into typed Threlte/Svelte components using [`@threlte/gltf`](https://threlte.xyz/docs/reference/gltf/getting-started). | ||
|
|
||
| Place model files in `static/models/`, then run the script. It: | ||
|
|
||
| 1. Finds all `.glb`/`.gltf` files in `static/models/` (skipping already-transformed files) | ||
| 2. Runs `@threlte/gltf` on each file to generate a typed Svelte component | ||
| 3. Moves the generated `.svelte` files to `src/lib/components/models/` | ||
| 4. Cleans up the intermediate files from `static/models/` | ||
|
|
||
| Configuration at the top of the file controls output options (TypeScript types, Draco compression, mesh simplification, etc.). By default `overwrite: false` -- existing components are not replaced. | ||
|
|
||
| ### Why we need it | ||
|
|
||
| Hand-writing Three.js scene graphs for complex GLTF models is tedious and error-prone. `@threlte/gltf` generates Svelte components that exactly mirror the scene hierarchy of a model, including typed props for materials and geometry. This script automates the full pipeline from raw model file to usable Svelte component. |
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /** | ||
| * Post-processes dist/ after svelte-package to inline web workers. | ||
| * | ||
| * svelte-package copies worker.ts -> worker.js into dist/ alongside the files | ||
| * that reference them. But dist/index.js files still contain | ||
| * `new Worker(new URL('./worker.js', import.meta.url))` which breaks when | ||
| * consumed by another Vite project's dep optimizer. | ||
| * | ||
| * This script: | ||
| * 1. Scans dist/ for JS files containing the `new Worker(new URL(...))` pattern | ||
| * 2. Bundles the referenced worker file with esbuild into a self-contained IIFE | ||
| * 3. Rewrites the worker instantiation to use a Blob URL instead | ||
| * 4. Deletes the now-unnecessary worker.js file from dist/ | ||
| */ | ||
|
|
||
| import { build } from 'esbuild' | ||
| import { globSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs' | ||
| import path from 'node:path' | ||
|
|
||
| const WORKER_PATTERN = | ||
| /new Worker\(new URL\(['"]([^'"]+)['"]\s*,\s*import\.meta\.url\)\s*(?:,\s*\{[^}]*\})?\)/g | ||
|
DTCurrie marked this conversation as resolved.
|
||
|
|
||
| const distFiles = globSync('dist/**/*.js') | ||
| const deleted = new Set() | ||
|
|
||
| for (const file of distFiles) { | ||
| if (deleted.has(path.resolve(file))) continue | ||
|
|
||
| const code = readFileSync(file, 'utf8') | ||
| const matches = [...code.matchAll(WORKER_PATTERN)] | ||
|
|
||
| if (matches.length === 0) continue | ||
|
|
||
| let result = code | ||
|
|
||
| for (const match of matches) { | ||
| const [fullMatch, workerRelPath] = match | ||
| const workerAbsPath = path.resolve(path.dirname(file), workerRelPath) | ||
|
|
||
| console.log(`Inlining worker ${workerRelPath} referenced in ${file}`) | ||
|
|
||
| const bundle = await build({ | ||
| entryPoints: [workerAbsPath], | ||
| bundle: true, | ||
| format: 'iife', | ||
| write: false, | ||
| minify: true, | ||
| }) | ||
|
|
||
| const bundledCode = bundle.outputFiles[0].text | ||
| const escaped = bundledCode | ||
| .replaceAll('\\', '\\\\') | ||
| .replaceAll('`', '\\`') | ||
| .replaceAll('$', String.raw`\$`) | ||
|
|
||
| const replacement = [ | ||
| `(function() {`, | ||
| ` const __workerCode = \`${escaped}\``, | ||
| ` const __blob = new Blob([__workerCode], { type: 'text/javascript' })`, | ||
| ` return new Worker(URL.createObjectURL(__blob))`, | ||
| `})()`, | ||
| ].join('\n') | ||
|
|
||
| result = result.replaceAll(fullMatch, replacement) | ||
|
|
||
| try { | ||
| unlinkSync(workerAbsPath) | ||
| deleted.add(workerAbsPath) | ||
| console.log(`Deleted ${workerAbsPath}`) | ||
| } catch { | ||
| console.warn(`Could not delete ${workerAbsPath} -- may already be removed`) | ||
| } | ||
| } | ||
|
|
||
| writeFileSync(file, result) | ||
| console.log(`Rewrote ${file}`) | ||
| } | ||
|
|
||
| console.log('Post-processed all workers in dist/') | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.