From 4b5bf77ab461dbe60aae8bcff1d34d174a990687 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 21:51:35 -0700 Subject: [PATCH] fix(js): bundle worklets in-process so no esbuild service outlives the build 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 | 12 +++++----- demo/boy/package.json | 2 +- demo/web/package.json | 2 +- js/common/vite-plugin-worklet.ts | 26 ++++++++++++---------- js/common/worklet.test.ts | 23 +++++++++++++++++++ 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 +- 9 files changed, 49 insertions(+), 24 deletions(-) diff --git a/bun.lock b/bun.lock index 60b672d8524..f71d5a7f9d7 100644 --- a/bun.lock +++ b/bun.lock @@ -29,7 +29,7 @@ "@moq/signals": "workspace:^", }, "devDependencies": { - "esbuild": "^0.28.2", + "rolldown": "^1.2.12", "typescript": "7.0.2", "vite": "^8.3.2", "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.12", "solid-element": "^1.9.2", "solid-js": "^1.9.15", "tailwindcss": "^4.1.13", @@ -258,8 +258,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.12", "typescript": "7.0.2", "vite": "^8.3.2", }, @@ -339,8 +339,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.12", "typescript": "7.0.2", "vite": "^8.3.2", }, @@ -356,8 +356,8 @@ }, "devDependencies": { "@types/audioworklet": "^0.0.100", - "esbuild": "^0.28.2", "playwright": "^1.63.0", + "rolldown": "^1.2.12", "vite": "^8.3.2", }, }, @@ -376,8 +376,8 @@ "@moq/watch": "workspace:*", }, "devDependencies": { - "esbuild": "^0.28.2", "playwright": "^1.63.0", + "rolldown": "^1.2.12", "vite": "^8.3.2", }, }, diff --git a/demo/boy/package.json b/demo/boy/package.json index 8e818afa508..787224d27cf 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.12", "typescript": "7.0.2", "vite": "^8.3.2", "vite-plugin-solid": "^2.11.14" diff --git a/demo/web/package.json b/demo/web/package.json index ab27dfbb5d5..2e685abcea7 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.12", "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 5cc48c9b4f7..f9153fb65cc 100644 --- a/js/common/vite-plugin-worklet.ts +++ b/js/common/vite-plugin-worklet.ts @@ -1,5 +1,5 @@ import { basename } from "node:path"; -import { build } from "esbuild"; +import { rolldown } from "rolldown"; import type { Plugin } from "vite"; const SUFFIX = "?worklet"; @@ -18,18 +18,20 @@ const BLOB = "?worklet-blob"; export function worklet(alias?: Record): Plugin { let production = false; + // Rolldown is Vite's own bundler and runs in-process, so nothing outlives the build. const compile = async (path: string) => { - const result = await build({ - entryPoints: [path], - bundle: true, - write: false, - format: "esm", - target: "esnext", - // A consumer can't minify code inlined as a string, so builds do it here; dev stays readable. - minify: production, - alias: alias, - }); - return result.outputFiles[0].text; + const bundle = await rolldown({ input: path, platform: "browser", resolve: { alias } }); + try { + const { output } = await bundle.generate({ + format: "es", + codeSplitting: false, + // A consumer can't minify code inlined as a string, so builds do it here; dev stays readable. + minify: production, + }); + return output[0].code; + } finally { + await bundle.close(); + } }; return { diff --git a/js/common/worklet.test.ts b/js/common/worklet.test.ts index 6817ef8ab0d..d5801f81fbb 100644 --- a/js/common/worklet.test.ts +++ b/js/common/worklet.test.ts @@ -1,4 +1,5 @@ import { expect, test } from "bun:test"; +import { spawnSync } from "node:child_process"; import { mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; @@ -41,3 +42,25 @@ test("library builds load worklets from a lazy blob or a hosted base", async () rmSync(outDir, { recursive: true, force: true }); } }); + +// 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: [worklet()], + build: { + outDir, + lib: { entry: resolve(import.meta.dir, "../publish/src/video/processor.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/publish/package.json b/js/publish/package.json index 464a151bed6..2aa08e981a5 100644 --- a/js/publish/package.json +++ b/js/publish/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.12", "typescript": "7.0.2", "vite": "^8.3.2" } diff --git a/js/watch/package.json b/js/watch/package.json index 704494d0e2b..d8d4b3b8bfa 100644 --- a/js/watch/package.json +++ b/js/watch/package.json @@ -41,8 +41,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.12", "typescript": "7.0.2", "vite": "^8.3.2" } diff --git a/test/audio-quality/clients/js/package.json b/test/audio-quality/clients/js/package.json index 3494ac34e60..b456d6c7183 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.12", "vite": "^8.3.2" } } diff --git a/test/interop/clients/js/package.json b/test/interop/clients/js/package.json index ad04a58efe6..9a6aac0ce99 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.12", "vite": "^8.3.2" } }