esbuild-plugin-worker
Collect and transpile Web Workers with esbuild.
How it works
The plugin looks for new Worker('./path/to/worker.js') statements in JavaScript and TypeScript files and instructs esbuild to treat that references as entrypoints. Final output will be used to correctly update the statement.
For example, the following script:
const worker = new Worker('./path/to/worker.js');will be transformed to:
const worker = new Worker(new URL('./path/to/worker.js', import.meta.url));and then resolved by the @chialab/esbuild-plugin-meta-url plugin.
Please note that RNA does not generate a Worker class to instantiate like webpack does, but it will just correctly update the import reference. If you need a Worker class, you have to wrap it yourself:
const workerClass = function () {
return new Worker('./path/to/worker.js');
};WARNING
At the moment this plugin does not collect importScript() statements and does treat workers as modules, but we have plan to support the { type: "module" } option in the near future.
Install
npm i -D @chialab/esbuild-plugin-workeryarn add -D @chialab/esbuild-plugin-workerpnpm add -D @chialab/esbuild-plugin-workerUsage
import workerPlugin from '@chialab/esbuild-plugin-worker';
import esbuild from 'esbuild';
await esbuild.build({
plugins: [workerPlugin()],
});You can also define a list of Worker constructors to use (default it will look for Worker and SharedWorker):
workerPlugin({ constructors: ['Worker', 'MyWorker'] });