Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rollup-plugin-web-worker-loader

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rollup-plugin-web-worker-loader - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

src/WorkerLoaderHelper.js

4

package.json
{
"name": "rollup-plugin-web-worker-loader",
"version": "0.1.3",
"description": "Rollup plugin to bundle web worker code to later be instantiated as a Worker",
"version": "0.2.0",
"description": "Rollup plugin to handle Web Workers",
"main": "src/index.js",

@@ -6,0 +6,0 @@ "repository": "https://github.com/darionco/rollup-plugin-web-worker-loader",

# rollup-plugin-web-worker-loader
Rollup plugin to bundle web worker code to later be instantiated as a Worker.
Rollup plugin to handle Web Workers.
Preserves the code so other plugins can modify it and/or transpile it.
Can be
configured to inline a sourcemap.
Can inline the worker code or emit a script file using code-splitting.
Handles Worker dependencies and can emit source maps.
Worker dependencies are added to Rollup's watch list.

@@ -23,3 +23,3 @@ ### Getting started

plugins: [
webWorkerLoader(),
webWorkerLoader(/* configuration */),
],

@@ -43,6 +43,16 @@ format: 'esm',

webWorkerLoader({
sourcemap: boolean, // should a source map be included in the final output
sourcemap?: boolean, // when inlined, should a source map be included in the final output. Default: false
inline?: boolean, // should the worker code be inlined (Base64). Default: true
loadPath?: string // this options is useful when the worker scripts need to be loaded from another folder. Default: ''
})
```
### Notes
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.
The `sourcemap` configuration option is ignored when `inline` is set to `false`, in that case the project's sourcemap configuration is inherited.
`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.
### Roadmap

@@ -52,2 +62,3 @@ - [x] Bundle file as web worker blob

- [x] Include source map
- [x] Configuration options to inline or code-split workers
- [ ] Provide capability checks and fallbacks

@@ -54,0 +65,0 @@ - [ ] Avoid code duplication

@@ -11,2 +11,4 @@ const path = require('path');

const sourcemap = (config && config.sourcemap) || false;
const loadPath = config && config.hasOwnProperty('loadPath') ? config.loadPath : '';
let inline = config && config.hasOwnProperty('inline') ? config.inline : true;

@@ -17,5 +19,6 @@ const idMap = new Map();

let basePath = null;
let configuredFileName = null;
return {
name: 'worker-loader',
name: 'web-worker-loader',

@@ -42,3 +45,3 @@ options(options ) {

if (importee === 'rollup-plugin-web-worker-loader-helper') {
return path.resolve(__dirname, 'createWorkerFactory.js');
return path.resolve(__dirname, 'WorkerLoaderHelper.js');
} else if (importee.indexOf('web-worker:') === 0) {

@@ -52,6 +55,12 @@ const name = importee.split(':')[1];

if (target && !idMap.has(importee)) {
idMap.set(target, Object.assign({}, projectOptions, {
const inputOptions = Object.assign({}, projectOptions, {
input: target,
}));
});
idMap.set(target, {
workerID: `web-worker-${idMap.size}.js`,
chunk: null,
inputOptions,
});
return target;

@@ -66,3 +75,27 @@ }

if (idMap.has(id) && !exclude.has(id)) {
const inputOptions = idMap.get(id);
if (!inline) {
/* inline requires rollup version 1.9.2 or higher */
const version = this.meta.rollupVersion.split('.');
if (version.length !== 3) {
this.warn('Unknown rollup version');
inline = true;
} else {
const major = parseInt(version[0], 10);
const minor = parseInt(version[1], 10);
const patch = parseInt(version[2], 10);
if (
isNaN(major) ||
isNaN(minor) ||
isNaN(patch) ||
major < 1 ||
minor < 9 ||
patch < 2
) {
this.warn(`Rollup version 1.9.2 or higher is required for emitting a worker file (current version:${this.meta.rollupVersion}). See https://github.com/rollup/rollup/issues/2801`);
inline = true;
}
}
}
const {inputOptions, workerID} = idMap.get(id);
exclude.set(id, true);

@@ -87,8 +120,16 @@ rollup.rollup(inputOptions).then(bundle => {

let source = utils.extractSource(chunk.code, chunk.exports);
let map = null;
if (sourcemap) {
map = utils.fixMapSources(chunk, basePath);
let source;
if (inline) {
source = utils.extractSource(chunk.code, chunk.exports);
map = null;
if (sourcemap) {
map = utils.fixMapSources(chunk, basePath);
}
} else {
source = path.join(loadPath, workerID);
chunk.fileName = workerID;
idMap.get(id).chunk = chunk;
}
resolve({code: utils.buildWorkerCode(source, map)});
resolve({code: utils.buildWorkerCode(source, map, inline)});
} else {

@@ -110,3 +151,3 @@ resolve(null);

if (idMap.has(id) && !exclude.has(id)) {
const inputOptions = idMap.get(id);
const {inputOptions} = idMap.get(id);
return { code, map: `{"version":3,"file":"${path.basename(inputOptions.input)}","sources":[],"sourcesContent":[],"names":[],"mappings":""}` };

@@ -116,3 +157,27 @@ }

},
outputOptions(options) {
if (!inline && options.file && !options.dir) {
configuredFileName = path.basename(options.file);
return Object.assign({}, options, {
file: null,
dir: path.dirname(options.file),
});
}
return null;
},
generateBundle(options, bundle, isWrite) {
if (!inline && isWrite) {
if (configuredFileName && Object.keys(bundle).length === 1) {
bundle[Object.keys(bundle)[0]].fileName = configuredFileName;
}
for (const worker of idMap) {
if (worker[1].chunk && !bundle[worker[1].workerID]) {
bundle[worker[1].workerID] = worker[1].chunk;
}
}
}
},
};
};

@@ -30,7 +30,16 @@ const path = require('path');

function buildWorkerCode(code, sourcemap = null) {
function buildWorkerCode(source, sourcemap = null, inline = true) {
if (inline) {
return `\
/* eslint-disable */\n\
import {createInlineWorkerFactory} from 'rollup-plugin-web-worker-loader-helper';\n\
const WorkerFactory = createInlineWorkerFactory(${source.substring(0, source.length - 1)}, ${sourcemap ? `'${sourcemap.toUrl()}'` : 'null'});\n\
export default WorkerFactory;\n\
/* eslint-enable */\n`;
}
return `\
/* eslint-disable */\n\
import {createWorkerFactory} from 'rollup-plugin-web-worker-loader-helper';\n\
const WorkerFactory = createWorkerFactory(${code.substring(0, code.length - 1)}, ${sourcemap ? `'${sourcemap.toUrl()}'` : 'null'});\n\
import {createURLWorkerFactory} from 'rollup-plugin-web-worker-loader-helper';\n\
const WorkerFactory = createURLWorkerFactory('${source}');\n\
export default WorkerFactory;\n\

@@ -37,0 +46,0 @@ /* eslint-enable */\n`;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc