rollup-plugin-worker
raw JSON → 3.0.4 verified Mon Apr 27 auth: no javascript
Rollup plugin to bundle Web Workers using a custom prefix import syntax (e.g., `worker!./worker.js`). v3.0.4 supports Rollup 3.x. It allows specifying worker-specific plugins and enables uglification. Provides TypeScript types. Differentiator: uses a prefix syntax rather than URL or inline blob, enabling easy bundling.
Common errors
error Error: Cannot find module 'rollup-plugin-worker' ↓
cause Package not installed or incorrect import path.
fix
npm install rollup-plugin-worker --save-dev
error TypeError: worker is not a function ↓
cause Using default import incorrectly (e.g., import { worker } instead of import worker).
fix
Use default import: import worker from 'rollup-plugin-worker'
error Error: Could not resolve 'worker!./worker.js' ↓
cause Plugin not properly configured or prefix mismatch.
fix
Check that rollup-plugin-worker is in plugins array and prefix matches import path (e.g., 'worker!').
Warnings
breaking Plugin requires Rollup ^3.0.0; does not work with Rollup 2.x. ↓
fix Upgrade to Rollup 3.x or use rollup-plugin-worker@2.x for Rollup 2 compatibility.
gotcha The prefix 'worker!' is required by default; if changed, ensure import statements match. ↓
fix Use the same prefix in both plugin options and import paths.
gotcha Plugins passed in the `plugins` option are used only for worker bundle; do not include the main bundle plugins unintentionally. ↓
fix Define separate plugins array for worker processing if needed.
deprecated uglify option is boolean; consider using terser or other minifier plugins instead. ↓
fix Set uglify: false and use @rollup/plugin-terser for minification.
Install
npm install rollup-plugin-worker yarn add rollup-plugin-worker pnpm add rollup-plugin-worker Imports
- default wrong
const worker = require('rollup-plugin-worker');correctimport worker from 'rollup-plugin-worker' - Worker wrong
import { Worker } from 'worker!./workers/worker.js'correctimport Worker from 'worker!./workers/worker.js' - WebWorker (TypeScript declaration) wrong
declare module 'worker!*' { export class WebWorker extends Worker { constructor(); } }correctdeclare module 'worker!*' { class WebWorker extends Worker { constructor(); } export default WebWorker; }
Quickstart
// rollup.config.js
import worker from 'rollup-plugin-worker';
export default {
input: 'src/main.js',
plugins: [
worker({
prefix: 'worker!',
plugins: [],
uglify: false,
}),
],
output: {
file: 'dist/bundle.js',
format: 'iife',
},
};
// src/main.js
import MyWorker from 'worker!./worker.js';
const w = new MyWorker();
w.postMessage('hello');