Make worker_threads dynamic imports direct and keep them in the bundle (externalized), & mark them as ignored for browser builds (fixes #306)

This commit is contained in:
Vanilagy
2026-02-24 13:11:16 +01:00
parent 31147d6e96
commit 89afa1e2a6
3 changed files with 11 additions and 3 deletions
+5 -3
View File
@@ -39,11 +39,10 @@ export default async function inlineWorker(scriptText) {
// Node, Bun (Bun's Worker is flaky, worker_threads works much better)
let Worker;
const workerModule = 'node:worker_threads';
try {
Worker = (await import(workerModule)).Worker;
Worker = (await import('worker_threads')).Worker;
} catch {
Worker = require(workerModule).Worker;
Worker = require('worker_threads').Worker;
}
const worker = new Worker(scriptText, { eval: true });
@@ -56,6 +55,9 @@ export default async function inlineWorker(scriptText) {
build.onResolve({ filter: /^__inline-worker$/ }, ({ path }) => {
return { path, namespace: 'inline-worker' };
});
build.onResolve({ filter: /^worker_threads$/ }, ({ path }) => {
return { path, external: true }; // Keep it in the bundle
});
build.onLoad({ filter: /.*/, namespace: 'inline-worker' }, () => {
return { contents: inlineWorkerFunctionCode, loader: 'js' };
});