rollup-plugin-web-worker-loader
Advanced tools
Comparing version 0.4.1 to 0.5.0
{ | ||
"name": "rollup-plugin-web-worker-loader", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Rollup plugin to handle Web Workers", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -43,5 +43,11 @@ # rollup-plugin-web-worker-loader | ||
webWorkerLoader({ | ||
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: '' | ||
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 | ||
preserveSource?: boolean, // when inlined and this option is enabled, the full source code is included in the | ||
// built file, otherwise it's embedded as a base64 string. Default: false | ||
loadPath?: string // this options is useful when the worker scripts need to be loaded from another folder. | ||
// Default: '' | ||
}) | ||
@@ -48,0 +54,0 @@ ``` |
@@ -12,2 +12,3 @@ const path = require('path'); | ||
const loadPath = config && config.hasOwnProperty('loadPath') ? config.loadPath : ''; | ||
const preserveSource = config && config.hasOwnProperty('preserveSource') ? config.preserveSource : false; | ||
let inline = config && config.hasOwnProperty('inline') ? config.inline : true; | ||
@@ -52,13 +53,14 @@ | ||
const target = require.resolve(name, { paths }); | ||
if (target && !idMap.has(importee)) { | ||
const inputOptions = Object.assign({}, projectOptions, { | ||
input: target, | ||
}); | ||
if (target) { | ||
if (!idMap.has(target)) { | ||
const inputOptions = Object.assign({}, projectOptions, { | ||
input: target, | ||
}); | ||
idMap.set(target, { | ||
workerID: `web-worker-${idMap.size}.js`, | ||
chunk: null, | ||
inputOptions, | ||
}); | ||
idMap.set(target, { | ||
workerID: `web-worker-${idMap.size}.js`, | ||
chunk: null, | ||
inputOptions, | ||
}); | ||
} | ||
return target; | ||
@@ -120,3 +122,3 @@ } | ||
if (inline) { | ||
source = utils.extractSource(chunk.code, chunk.exports); | ||
source = utils.extractSource(chunk.code, chunk.exports, preserveSource); | ||
map = null; | ||
@@ -131,3 +133,3 @@ if (sourcemap) { | ||
} | ||
resolve({code: utils.buildWorkerCode(source, map, inline)}); | ||
resolve({code: utils.buildWorkerCode(source, map, inline, preserveSource)}); | ||
} else { | ||
@@ -134,0 +136,0 @@ resolve(null); |
const path = require('path'); | ||
function extractSource(code, exports) { | ||
function extractSource(code, exports, asFunction = true) { | ||
let source; | ||
@@ -27,8 +27,12 @@ if (exports.length) { | ||
return `/* rollup-plugin-web-worker-loader */function () {\n${source}}\n`; | ||
if (asFunction) { | ||
return `/* rollup-plugin-web-worker-loader */function () {\n${source}}\n`; | ||
} | ||
return `/* rollup-plugin-web-worker-loader */\n${source}\n`; | ||
} | ||
function buildWorkerCode(source, sourcemap = null, inline = true) { | ||
function buildWorkerCode(source, sourcemap = null, inline = true, preserveSource = false) { | ||
if (inline) { | ||
return `\ | ||
if (preserveSource) { | ||
return `\ | ||
/* eslint-disable */\n\ | ||
@@ -39,2 +43,10 @@ import {createInlineWorkerFactory} from 'rollup-plugin-web-worker-loader-helper';\n\ | ||
/* eslint-enable */\n`; | ||
} | ||
return `\ | ||
/* eslint-disable */\n\ | ||
import {createBase64WorkerFactory} from 'rollup-plugin-web-worker-loader-helper';\n\ | ||
const WorkerFactory = createBase64WorkerFactory('${Buffer.from(source).toString('base64')}', ${sourcemap ? `'${sourcemap.toUrl()}'` : 'null'});\n\ | ||
export default WorkerFactory;\n\ | ||
/* eslint-enable */\n`; | ||
} | ||
@@ -41,0 +53,0 @@ |
@@ -42,1 +42,22 @@ const kIsNodeJS = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'; | ||
} | ||
export function createBase64WorkerFactory(base64, sourcemap = null) { | ||
const source = kIsNodeJS ? Buffer.from(base64, 'base64').toString('ascii') : atob(base64); | ||
const start = source.indexOf('\n', 10) + 1; | ||
const body = source.substring(start) + (sourcemap ? `//# sourceMappingURL=${sourcemap}` : ''); | ||
if (kIsNodeJS) { | ||
/* node.js */ | ||
const Worker = kRequire('worker_threads').Worker; // eslint-disable-line | ||
return function WorkerFactory(options) { | ||
return new Worker(body, Object.assign({}, options, { eval: true })); | ||
}; | ||
} | ||
/* browser */ | ||
const blob = new Blob([body], { type: 'application/javascript' }); | ||
const url = URL.createObjectURL(blob); | ||
return function WorkerFactory(options) { | ||
return new Worker(url, options); | ||
}; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37170
322
72