From 9effd5489c1b2db9c0ee627fcced8e6a34fcecac Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 21:58:49 -0700 Subject: [PATCH] fix(js): bundle worklets in-process so no esbuild service outlives the build Backport of fix/worklet-inline-esbuild to release, adapted to workletInline. The worklet Vite plugin bundled with esbuild's build(), which starts esbuild's long-lived service process. Nothing stopped it, so it outlived `vite build` and was reaped by init, taking its CPU out of the build's accounting. Bundle with rolldown, the bundler Vite 8 already runs, which works in-process and leaves no child behind. The worklet consumers swap their esbuild devDependency for rolldown at Vite's resolved version. Co-Authored-By: Claude Opus 5.5 --- bun.lock | 14 +++++------ demo/boy/package.json | 2 +- demo/web/package.json | 2 +- js/common/vite-plugin-worklet.ts | 23 ++++++++--------- js/common/worklet.test.ts | 29 ++++++++++++++++++++++ js/justfile | 2 +- js/moq-boy/package.json | 2 +- js/publish/package.json | 2 +- js/watch/package.json | 2 +- test/audio-quality/clients/js/package.json | 2 +- test/interop/clients/js/package.json | 2 +- 11 files changed, 55 insertions(+), 27 deletions(-) create mode 100644 js/common/worklet.test.ts diff --git a/bun.lock b/bun.lock index 29aefc3bc30..2d3de9f68e4 100644 --- a/bun.lock +++ b/bun.lock @@ -29,7 +29,7 @@ "@moq/signals": "workspace:^", }, "devDependencies": { - "esbuild": "^0.28.2", + "rolldown": "^1.2.6", "typescript": "7.0.2", "vite": "^8.3.0", "vite-plugin-solid": "^2.11.14", @@ -48,9 +48,9 @@ "devDependencies": { "@tailwindcss/typography": "^0.5.20", "@tailwindcss/vite": "^4.3.2", - "esbuild": "^0.28.2", "highlight.js": "^11.12.0", "open": "^11.0.4", + "rolldown": "^1.2.6", "solid-element": "^1.9.2", "solid-js": "^1.9.15", "tailwindcss": "^4.1.13", @@ -203,8 +203,8 @@ }, "devDependencies": { "@types/bun": "^1.4.2", - "esbuild": "^0.28.2", "rimraf": "^6.1.3", + "rolldown": "^1.2.6", "solid-js": "^1.9.15", "typescript": "7.0.2", "vite": "^8.3.0", @@ -270,8 +270,8 @@ "@types/audioworklet": "^0.0.100", "@types/bun": "^1.4.2", "@typescript/lib-dom": "npm:@types/web@^0.0.350", - "esbuild": "^0.28.2", "rimraf": "^6.1.3", + "rolldown": "^1.2.6", "typescript": "7.0.2", "vite": "^8.3.0", }, @@ -337,8 +337,8 @@ "@types/audioworklet": "^0.0.100", "@types/bun": "^1.4.2", "@typescript/lib-dom": "npm:@types/web@^0.0.350", - "esbuild": "^0.28.2", "rimraf": "^6.1.3", + "rolldown": "^1.2.6", "typescript": "7.0.2", "vite": "^8.3.0", }, @@ -354,8 +354,8 @@ }, "devDependencies": { "@types/audioworklet": "^0.0.100", - "esbuild": "^0.28.2", "playwright": "^1.63.0", + "rolldown": "^1.2.6", "vite": "^8.3.0", }, }, @@ -374,8 +374,8 @@ "@moq/watch": "workspace:*", }, "devDependencies": { - "esbuild": "^0.28.2", "playwright": "^1.63.0", + "rolldown": "^1.2.6", "vite": "^8.3.0", }, }, diff --git a/demo/boy/package.json b/demo/boy/package.json index 24777f20fca..0322e5342ae 100644 --- a/demo/boy/package.json +++ b/demo/boy/package.json @@ -13,7 +13,7 @@ "@moq/signals": "workspace:^" }, "devDependencies": { - "esbuild": "^0.28.2", + "rolldown": "^1.2.6", "typescript": "7.0.2", "vite": "^8.3.0", "vite-plugin-solid": "^2.11.14" diff --git a/demo/web/package.json b/demo/web/package.json index bb3dfb9a2fe..c714d242af5 100644 --- a/demo/web/package.json +++ b/demo/web/package.json @@ -22,9 +22,9 @@ "devDependencies": { "@tailwindcss/typography": "^0.5.20", "@tailwindcss/vite": "^4.3.2", - "esbuild": "^0.28.2", "highlight.js": "^11.12.0", "open": "^11.0.4", + "rolldown": "^1.2.6", "solid-element": "^1.9.2", "solid-js": "^1.9.15", "tailwindcss": "^4.1.13", diff --git a/js/common/vite-plugin-worklet.ts b/js/common/vite-plugin-worklet.ts index f4a9029e7c7..d85bca4ac46 100644 --- a/js/common/vite-plugin-worklet.ts +++ b/js/common/vite-plugin-worklet.ts @@ -1,4 +1,4 @@ -import { build } from "esbuild"; +import { rolldown } from "rolldown"; import type { Plugin } from "vite"; const SUFFIX = "?worklet"; @@ -8,7 +8,7 @@ const SUFFIX = "?worklet"; * * Usage: import workletUrl from "./my-worklet.ts?worklet" * - * The worklet file is compiled to JS with all dependencies bundled via esbuild, + * The worklet file is compiled to JS with all dependencies bundled via rolldown, * then inlined as a string. At runtime, a blob URL is created and exported. * Pass the URL to audioWorklet.addModule(). */ @@ -36,16 +36,15 @@ export function workletInline(alias?: Record): Plugin { this.addWatchFile(filePath); } - const result = await build({ - entryPoints: [filePath], - bundle: true, - write: false, - format: "esm", - target: "esnext", - alias: alias, - }); - - const compiled = result.outputFiles[0].text; + // Rolldown is Vite's own bundler and runs in-process, so nothing outlives the build. + const bundle = await rolldown({ input: filePath, platform: "browser", resolve: { alias } }); + let compiled: string; + try { + const { output } = await bundle.generate({ format: "es", codeSplitting: false }); + compiled = output[0].code; + } finally { + await bundle.close(); + } return [ `const code = ${JSON.stringify(compiled)};`, diff --git a/js/common/worklet.test.ts b/js/common/worklet.test.ts new file mode 100644 index 00000000000..49b35dc1e19 --- /dev/null +++ b/js/common/worklet.test.ts @@ -0,0 +1,29 @@ +import { expect, test } from "bun:test"; +import { spawnSync } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { build } from "vite"; +import { workletInline } from "./vite-plugin-worklet"; + +// A bundler child process would outlive `vite build` and be reaped by init, so its CPU escapes the +// build's accounting and it lingers after the build. +test("builds leave no child process behind", async () => { + const outDir = mkdtempSync(join(tmpdir(), "worklet-")); + try { + await build({ + configFile: false, + logLevel: "silent", + plugins: [workletInline()], + build: { + outDir, + lib: { entry: resolve(import.meta.dir, "../watch/src/audio/decoder.ts"), formats: ["es"] }, + rollupOptions: { external: (id) => id.startsWith("@moq/") }, + }, + }); + } finally { + rmSync(outDir, { recursive: true, force: true }); + } + + expect(spawnSync("pgrep", ["-l", "-P", String(process.pid)], { encoding: "utf8" }).stdout).toBe(""); +}); diff --git a/js/justfile b/js/justfile index 7118f097734..65944996eb3 100644 --- a/js/justfile +++ b/js/justfile @@ -40,7 +40,7 @@ fix: # Run all unit tests. test: bun install --frozen-lockfile - bun test common/declarations.test.ts common/deps.test.ts + bun test common/declarations.test.ts common/deps.test.ts common/worklet.test.ts bun run --filter='*' --elide-lines=0 test # Run one net benchmark: broadcasts, forward, reader, frames, track, varint, or lite-varint. diff --git a/js/moq-boy/package.json b/js/moq-boy/package.json index 51ad777bff2..a2688536b06 100644 --- a/js/moq-boy/package.json +++ b/js/moq-boy/package.json @@ -34,8 +34,8 @@ "//devDependencies": "These are only needed for local development with workspace packages. They are NOT needed when using the published npm packages.", "devDependencies": { "@types/bun": "^1.4.2", - "esbuild": "^0.28.2", "rimraf": "^6.1.3", + "rolldown": "^1.2.6", "solid-js": "^1.9.15", "typescript": "7.0.2", "vite": "^8.3.0", diff --git a/js/publish/package.json b/js/publish/package.json index 50fbec9c36c..09a79bde5d3 100644 --- a/js/publish/package.json +++ b/js/publish/package.json @@ -39,8 +39,8 @@ "@types/audioworklet": "^0.0.100", "@types/bun": "^1.4.2", "@typescript/lib-dom": "npm:@types/web@^0.0.350", - "esbuild": "^0.28.2", "rimraf": "^6.1.3", + "rolldown": "^1.2.6", "typescript": "7.0.2", "vite": "^8.3.0" } diff --git a/js/watch/package.json b/js/watch/package.json index 960b5dedee2..363bd74dd76 100644 --- a/js/watch/package.json +++ b/js/watch/package.json @@ -40,8 +40,8 @@ "@types/audioworklet": "^0.0.100", "@types/bun": "^1.4.2", "@typescript/lib-dom": "npm:@types/web@^0.0.350", - "esbuild": "^0.28.2", "rimraf": "^6.1.3", + "rolldown": "^1.2.6", "typescript": "7.0.2", "vite": "^8.3.0" } diff --git a/test/audio-quality/clients/js/package.json b/test/audio-quality/clients/js/package.json index dd2918b1669..148ac1ee7c7 100644 --- a/test/audio-quality/clients/js/package.json +++ b/test/audio-quality/clients/js/package.json @@ -15,8 +15,8 @@ }, "devDependencies": { "@types/audioworklet": "^0.0.100", - "esbuild": "^0.28.2", "playwright": "^1.63.0", + "rolldown": "^1.2.6", "vite": "^8.3.0" } } diff --git a/test/interop/clients/js/package.json b/test/interop/clients/js/package.json index 484cee7861e..eaeaea56510 100644 --- a/test/interop/clients/js/package.json +++ b/test/interop/clients/js/package.json @@ -13,8 +13,8 @@ "@moq/watch": "workspace:*" }, "devDependencies": { - "esbuild": "^0.28.2", "playwright": "^1.63.0", + "rolldown": "^1.2.6", "vite": "^8.3.0" } }