{"id":13774482,"url":"https://github.com/darionco/rollup-plugin-web-worker-loader","last_synced_at":"2025-04-09T10:00:16.810Z","repository":{"id":34306290,"uuid":"176850554","full_name":"darionco/rollup-plugin-web-worker-loader","owner":"darionco","description":"Rollup plugin to load Workers. Supports inlining, dependencies, source maps, NodeJS and browsers.","archived":false,"fork":false,"pushed_at":"2025-02-20T23:46:36.000Z","size":213,"stargazers_count":114,"open_issues_count":18,"forks_count":35,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-01T13:41:43.111Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/darionco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-21T02:03:26.000Z","updated_at":"2025-01-31T16:19:55.000Z","dependencies_parsed_at":"2024-01-13T12:21:57.800Z","dependency_job_id":"d6444cba-7061-4765-a735-29129de049ba","html_url":"https://github.com/darionco/rollup-plugin-web-worker-loader","commit_stats":{"total_commits":130,"total_committers":19,"mean_commits":6.842105263157895,"dds":0.2615384615384615,"last_synced_commit":"48df45336b9d2527adae5dd50f4bbfd7c19557dc"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darionco%2Frollup-plugin-web-worker-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darionco%2Frollup-plugin-web-worker-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darionco%2Frollup-plugin-web-worker-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darionco%2Frollup-plugin-web-worker-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darionco","download_url":"https://codeload.github.com/darionco/rollup-plugin-web-worker-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248018032,"owners_count":21034045,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T17:01:27.180Z","updated_at":"2025-04-09T10:00:16.741Z","avatar_url":"https://github.com/darionco.png","language":"JavaScript","funding_links":[],"categories":["Plugins"],"sub_categories":["Modules"],"readme":"# rollup-plugin-web-worker-loader\n\nRollup plugin to handle Web Workers, Service Workers, Shared Workers,\nAudio Worklets, and Paint Worklets. Support for Animation Worklets and\nLayout Worklets is in consideration for when implementations are available\nin browsers.\n\nWeb Workers are available in Node JS as well as in browsers. All the other\nworklets and workers are available in browsers only, and will throw a runtime\nerror if used in Node JS.\n\nCan inline the worker code or emit a script file using code-splitting.\nHandles Worker dependencies and can emit source maps.\nWorker dependencies are added to Rollup's watch list.\nSupports bundling workers for Node.js environments\n\n### Getting started\n\n```\nyarn add rollup-plugin-web-worker-loader --dev\n```\n\nAdd the plugin to your rollup configuration:\n\n```javascript\nimport webWorkerLoader from 'rollup-plugin-web-worker-loader';\n\nexport default {\n    entry: 'src/index.js',\n    plugins: [\n        webWorkerLoader(/* configuration */),\n    ],\n    format: 'esm',\n};\n```\n\n#### Web Worker Example\n\nBundle the worker code using the RegEx pattern specified in the plugin's configuration.\nBy default you can add the prefix `web-worker:` to your imports:\n\n```javascript\n// here we use the default pattern but any RegEx can be configured\nimport DataWorker from 'web-worker:./DataWorker';\n\nconst dataWorker = new DataWorker();\ndataWorker.postMessage('Hello World!');\n```\n\n#### Shared Worker Example\n\n```javascript\nimport SharedWorker from 'shared-worker:./SharedWorker';\n\nconst sharedWorker = new SharedWorker();\nsharedWorker.port.postMessage('Hello World!');\n```\n\n#### Service Worker Example\n\n```javascript\nimport ServiceWorker from 'service-worker:./ServiceWorker';\n\nServiceWorker.then(function(registration) {\n    console.log('Registration successful, scope is: ', registration.scope);\n})\n.catch(function(error) {\n    console.log('Service worker registration failed, error: ', error);\n}\n```\n\n#### Audio Worklet Example\n\nAudio Worklets require an audio context at instantiation. When you use\nrollup-plugin-web-worker-loader in a browser environment, your import will\nreturn a constructor to which you can pass an audio context.\n##### Worklet Processor\n\n```javascript\nclass MyAudioWorkletProcessor extends AudioWorkletProcessor {\n}\n\nregisterProcessor(\"my-audio-worklet\", MyAudioWorkletProcessor);\n```\n\n##### Worklet Consumer\n\n```javascript\nimport registerMyAudioWorklet from 'audio-worklet:./MyAudioWorkletFactory';\n\nconst audioContext = new AudioContext();\nregisterMyAudioWorklet(audioContext);\n\nclass MyAudioWorklet extends AudioWorkletNode {\n    constructor(audioContext) {\n        super(audioContext, \"my-audio-worklet\"));\n    }\n}\n```\n\n#### Paint Worklet Example\n\n##### Worklet Processor\n\n```javascript\nclass MyPaintWorklet {\n    ...\n}\n\nregisterPaint('my-paint-worklet', MyPaintWorklet);\n```\n\n##### Worklet Consumer\n\n```javascript\nimport registerMyPaintWorklet from 'paint-worklet:./MyPaintWorkletFactory';\nregisterMyPaintWorklet();\n```\n\n```css\nhtml {\n    background: paint(my-paint-worklet);\n}\n```\n\n### Configuration\nThe plugin responds to the following configuration options:\n```javascript\nwebWorkerLoader({\n    targetPlatform?: string,        // The platform workers should be built for, can be 'auto', 'browser', 'node' or 'base64'.\n                                    // specifying a target platform other than 'auto' reduces the amount of loader code.\n                                    // The `base64` options forces inline and the import results on a base64 string that\n                                    // encodes the worker's source code. NOTE: The string does not include a mime type.\n                                    // 'auto' detectes the target platform and selects between 'browser` and 'node'.\n                                    // Default: 'auto'\n\n    web-worker?: RegEx,             // A RegEx instance describing the pattern that matches the files to import as\n                                    // web workers. If capturing groups are present, the plugin uses the contents of the\n                                    // last capturing group as the path to the worker script. Default: /web-worker:(.+)/\n\n    shared-worker?: RegEx,          // A RegEx instance describing the pattern that matches the files to import as\n                                    // shared workers. If capturing groups are present, the plugin uses the contents of the\n                                    // last capturing group as the path to the worker script. Default: /shared-worker:(.+)/\n\n    service-worker?: RegEx,         // A RegEx instance describing the pattern that matches the files to import as\n                                    // service workers. If capturing groups are present, the plugin uses the contents of the\n                                    // last capturing group as the path to the worker script. Default: /service-worker:(.+)/\n\n    audio-worklet?: RegEx,          // A RegEx instance describing the pattern that matches the files to import as\n                                    // audio worklets. If capturing groups are present, the plugin uses the contents of the\n                                    // last capturing group as the path to the worker script. Default: /audio-worklet:(.+)/\n\n    paint-worklet?: RegEx,          // A RegEx instance describing the pattern that matches the files to import as\n                                    // paint worklets. If capturing groups are present, the plugin uses the contents of the\n                                    // last capturing group as the path to the worker script. Default: /paint-worklet:(.+)/\n\n    \n    extensions?: string[],          // An array of strings to use as extensions when resolving worker files.\n                                    // Default: ['.js']\n\n\n    sourcemap?: boolean,            // When inlined, should a source map be included in the final output. Default: false\n\n    inline?: boolean,               // Should the worker code be inlined (Base64). Default: true\n\n    forceInline?: boolean,          // *EXPERIMENTAL* when inlined, forces the code to be included every time it is imported\n                                    // useful when using code splitting: Default: false\n\n    external?: string[],            // *EXPERIMENTAL* override rollup resolution of external module IDs\n                                    // useful to inline external dependencies in a worker blob. Default: undefined\n\n    preserveSource?: boolean,       // When inlined and this option is enabled, the full source code is included in the\n                                    // built file, otherwise it's embedded as a base64 string. Default: false\n    \n    preserveFileNames?: boolean,    // When code splitting is used (`inline === false`) the input worker file names are\n                                    // preserved, if duplicates are found `-n` is appended to the file names.\n                                    // Default: false\n\n    enableUnicodeSupport?: boolean, // When inlined in Base64 format, this option enables unicode support (UTF16). This\n                                    // flag is disabled by default because supporting UTF16 doubles the size of the final\n                                    // payload. Default: false\n\n    outputFolder?: string,          // When code splitting is used (`inline: false`), folder in which the worker scripts\n                                    // should be written to. Default: '' (same as build output folder)\n\n    loadPath?: string,              // This option is useful when the worker scripts need to be loaded from another folder.\n                                    // Default: ''\n\n    skipPlugins?: Array             // Plugin names to skip for web worker build\n                                    // Default: [ 'liveServer', 'serve', 'livereload' ]\n})\n```\n\n### TypeScript\nAn example project that uses this plugin with TypeScript can be found [here](https://github.com/darionco/rollup-typescript-webworkers)\n\n**WARNING:** `@rollup/plugin-typescript` is NOT compatible with this plugin, use `rollup-plugin-typescript2` instead (see [#38](https://github.com/darionco/rollup-plugin-web-worker-loader/issues/38)).\n\n### Notes\n**WARNING:** To use code-splitting for the worker scripts, Rollup v1.9.2 or higher is required. See https://github.com/rollup/rollup/issues/2801 for more details.\n\nThe `sourcemap` configuration option is ignored when `inline` is set to `false`, in that case the project's sourcemap configuration is inherited.\n\n`loadPath` is meant to be used in situations where code-splitting is used (`inline = false`) and the entry script is hosted in a different folder than the worker code.\n\nSetting `targetPlatform` to `'base64'` will ignore the `inline` option and will always inline the resulting code.\n\n\n### Roadmap\n- [x] Bundle file as web worker blob\n- [x] Support for dependencies using `import`\n- [x] Include source map\n- [x] Configuration options to inline or code-split workers\n- [ ] ~~Provide capability checks and fallbacks~~ DROPPED (all modern environments support workers) \n- [ ] ~~Avoid code duplication~~ DROPPED (there are better solutions for this purpose)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarionco%2Frollup-plugin-web-worker-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarionco%2Frollup-plugin-web-worker-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarionco%2Frollup-plugin-web-worker-loader/lists"}