workerize-loader
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -10,4 +10,4 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
function loader() {} | ||
const CACHE = {}; | ||
const tapName = 'workerize-loader'; | ||
var CACHE = {}; | ||
var tapName = 'workerize-loader'; | ||
@@ -39,16 +39,18 @@ function compilationHook(compiler, handler) { | ||
loader.pitch = function (request) { | ||
var _this = this; | ||
this.cacheable(false); | ||
const options = loaderUtils.getOptions(this) || {}; | ||
const cb = this.async(); | ||
const filename = loaderUtils.interpolateName(this, `${options.name || '[hash]'}.worker.js`, { | ||
var options = loaderUtils.getOptions(this) || {}; | ||
var cb = this.async(); | ||
var filename = loaderUtils.interpolateName(this, (options.name || '[hash]') + ".worker.js", { | ||
context: options.context || this.rootContext || this.options.context, | ||
regExp: options.regExp | ||
}); | ||
const worker = {}; | ||
var worker = {}; | ||
worker.options = { | ||
filename, | ||
chunkFilename: `[id].${filename}`, | ||
filename: filename, | ||
chunkFilename: "[id]." + filename, | ||
namedChunkFilename: null | ||
}; | ||
const compilerOptions = this._compiler.options || {}; | ||
var compilerOptions = this._compiler.options || {}; | ||
@@ -64,12 +66,15 @@ if (compilerOptions.output && compilerOptions.output.globalObject === 'window') { | ||
new NodeTargetPlugin().apply(worker.compiler); | ||
} | ||
} // webpack >= v4 supports webassembly | ||
let wasmPluginPath = null; | ||
var wasmPluginPath = null; | ||
try { | ||
wasmPluginPath = require.resolve('webpack/lib/web/FetchCompileWasmTemplatePlugin'); | ||
} catch (_err) {} | ||
} catch (_err) {// webpack <= v3, skipping | ||
} | ||
if (wasmPluginPath) { | ||
const FetchCompileWasmTemplatePlugin = require(wasmPluginPath); | ||
// eslint-disable-next-line global-require | ||
var FetchCompileWasmTemplatePlugin = require(wasmPluginPath); | ||
@@ -81,5 +86,5 @@ new FetchCompileWasmTemplatePlugin({ | ||
new SingleEntryPlugin(this.context, `!!${path.resolve(__dirname, 'rpc-worker-loader.js')}!${request}`, 'main').apply(worker.compiler); | ||
const subCache = `subcache ${__dirname} ${request}`; | ||
compilationHook(worker.compiler, (compilation, data) => { | ||
new SingleEntryPlugin(this.context, "!!" + path.resolve(__dirname, 'rpc-worker-loader.js') + "!" + request, 'main').apply(worker.compiler); | ||
var subCache = "subcache " + __dirname + " " + request; | ||
compilationHook(worker.compiler, function (compilation, data) { | ||
if (compilation.cache) { | ||
@@ -90,13 +95,13 @@ if (!compilation.cache[subCache]) compilation.cache[subCache] = {}; | ||
parseHook(data, (parser, options) => { | ||
exportDeclarationHook(parser, expr => { | ||
let decl = expr.declaration || expr, | ||
{ | ||
compilation, | ||
current | ||
} = parser.state, | ||
entry = compilation.entries[0].resource; | ||
parseHook(data, function (parser, options) { | ||
exportDeclarationHook(parser, function (expr) { | ||
var decl = expr.declaration || expr, | ||
_parser$state = parser.state, | ||
compilation = _parser$state.compilation, | ||
current = _parser$state.current, | ||
entry = compilation.entries[0].resource; // only process entry exports | ||
if (current.resource !== entry) return; | ||
let key = current.nameForCondition(); | ||
let exports = CACHE[key] || (CACHE[key] = {}); | ||
var key = current.nameForCondition(); | ||
var exports = CACHE[key] || (CACHE[key] = {}); | ||
@@ -106,3 +111,3 @@ if (decl.id) { | ||
} else if (decl.declarations) { | ||
for (let i = 0; i < decl.declarations.length; i++) { | ||
for (var i = 0; i < decl.declarations.length; i++) { | ||
exports[decl.declarations[i].id.name] = true; | ||
@@ -116,3 +121,3 @@ } | ||
}); | ||
worker.compiler.runAsChild((err, entries, compilation) => { | ||
worker.compiler.runAsChild(function (err, entries, compilation) { | ||
if (err) return cb(err); | ||
@@ -122,32 +127,25 @@ | ||
worker.file = entries[0].files[0]; | ||
let key = entries[0].entryModule.nameForCondition(); | ||
let contents = compilation.assets[worker.file].source(); | ||
let exports = Object.keys(CACHE[key] || {}); | ||
var key = entries[0].entryModule.nameForCondition(); | ||
var contents = compilation.assets[worker.file].source(); | ||
var exports = Object.keys(CACHE[key] || {}); // console.log('Workerized exports: ', exports.join(', ')); | ||
if (options.inline) { | ||
worker.url = `URL.createObjectURL(new Blob([${JSON.stringify(contents)}]))`; | ||
worker.url = "URL.createObjectURL(new Blob([" + JSON.stringify(contents) + "]))"; | ||
} else if (options.publicPath) { | ||
worker.url = "" + JSON.stringify(options.publicPath + worker.file); | ||
} else { | ||
worker.url = `__webpack_public_path__ + ${JSON.stringify(worker.file)}`; | ||
worker.url = "__webpack_public_path__ + " + JSON.stringify(worker.file); | ||
} | ||
if (options.fallback === false) { | ||
delete this._compilation.assets[worker.file]; | ||
delete _this._compilation.assets[worker.file]; | ||
} | ||
let workerUrl = worker.url; | ||
var workerUrl = worker.url; | ||
if (options.import) { | ||
workerUrl = `"data:,importScripts('"+location.origin+${workerUrl}+"')"`; | ||
workerUrl = "\"data:,importScripts('\"+location.origin+" + workerUrl + "+\"')\""; | ||
} | ||
return cb(null, ` | ||
var addMethods = require(${loaderUtils.stringifyRequest(this, path.resolve(__dirname, 'rpc-wrapper.js'))}) | ||
var methods = ${JSON.stringify(exports)} | ||
module.exports = function() { | ||
var w = new Worker(${workerUrl}, { name: ${JSON.stringify(filename)} }) | ||
addMethods(w, methods) | ||
${options.ready ? 'w.ready = new Promise(function(r) { w.addEventListener("ready", function(){ r(w) }) })' : ''} | ||
return w | ||
} | ||
`); | ||
return cb(null, "\n\t\t\t\tvar addMethods = require(" + loaderUtils.stringifyRequest(_this, path.resolve(__dirname, 'rpc-wrapper.js')) + ")\n\t\t\t\tvar methods = " + JSON.stringify(exports) + "\n\t\t\t\tmodule.exports = function() {\n\t\t\t\t\tvar w = new Worker(" + workerUrl + ", { name: " + JSON.stringify(filename) + " })\n\t\t\t\t\taddMethods(w, methods)\n\t\t\t\t\t" + (options.ready ? 'w.ready = new Promise(function(r) { w.addEventListener("ready", function(){ r(w) }) })' : '') + "\n\t\t\t\t\treturn w\n\t\t\t\t}\n\t\t\t"); | ||
} | ||
@@ -154,0 +152,0 @@ |
@@ -0,9 +1,9 @@ | ||
/* global __webpack_exports__ */ | ||
function workerSetup() { | ||
addEventListener('message', e => { | ||
let { | ||
type, | ||
method, | ||
id, | ||
params | ||
} = e.data, | ||
addEventListener('message', function (e) { | ||
var _e$data = e.data, | ||
type = _e$data.type, | ||
method = _e$data.method, | ||
id = _e$data.id, | ||
params = _e$data.params, | ||
f, | ||
@@ -14,3 +14,5 @@ p; | ||
if (f = __webpack_exports__[method]) { | ||
p = Promise.resolve().then(() => f.apply(__webpack_exports__, params)); | ||
p = Promise.resolve().then(function () { | ||
return f.apply(__webpack_exports__, params); | ||
}); | ||
} else { | ||
@@ -20,10 +22,10 @@ p = Promise.reject('No such method'); | ||
p.then(result => { | ||
p.then(function (result) { | ||
postMessage({ | ||
type: 'RPC', | ||
id, | ||
result | ||
id: id, | ||
result: result | ||
}); | ||
}).catch(e => { | ||
let error = { | ||
}).catch(function (e) { | ||
var error = { | ||
message: e | ||
@@ -40,4 +42,4 @@ }; | ||
type: 'RPC', | ||
id, | ||
error | ||
id: id, | ||
error: error | ||
}); | ||
@@ -53,5 +55,6 @@ }); | ||
const workerScript = '\n' + Function.prototype.toString.call(workerSetup).replace(/(^.*\{|\}.*$|\n\s*)/g, ''); | ||
function rpcWorkerLoader(content) { | ||
return content + workerScript; | ||
var workerScript = '\n' + Function.prototype.toString.call(workerSetup).replace(/(^.*\{|\}.*$|\n\s*)/g, ''); | ||
function rpcWorkerLoader(content, sourceMap) { | ||
var callback = this.async(); | ||
callback(null, content + workerScript, sourceMap); | ||
} | ||
@@ -58,0 +61,0 @@ |
function addMethods(worker, methods) { | ||
let c = 0; | ||
let callbacks = {}; | ||
worker.addEventListener('message', e => { | ||
let d = e.data; | ||
var c = 0; | ||
var callbacks = {}; | ||
worker.addEventListener('message', function (e) { | ||
var d = e.data; | ||
if (d.type !== 'RPC') return; | ||
if (d.id) { | ||
let f = callbacks[d.id]; | ||
var f = callbacks[d.id]; | ||
@@ -21,3 +21,3 @@ if (f) { | ||
} else { | ||
let evt = document.createEvent('Event'); | ||
var evt = document.createEvent('Event'); | ||
evt.initEvent(d.method, false, false); | ||
@@ -28,13 +28,16 @@ evt.data = d.params; | ||
}); | ||
methods.forEach(method => { | ||
worker[method] = (...params) => new Promise((a, b) => { | ||
let id = ++c; | ||
callbacks[id] = [a, b]; | ||
worker.postMessage({ | ||
type: 'RPC', | ||
id, | ||
method, | ||
params | ||
methods.forEach(function (method) { | ||
worker[method] = function () { | ||
var _arguments = arguments; | ||
return new Promise(function (a, b) { | ||
var id = ++c; | ||
callbacks[id] = [a, b]; | ||
worker.postMessage({ | ||
type: 'RPC', | ||
id: id, | ||
method: method, | ||
params: [].slice.call(_arguments) | ||
}); | ||
}); | ||
}); | ||
}; | ||
}); | ||
@@ -41,0 +44,0 @@ } |
{ | ||
"name": "workerize-loader", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Automatically move a module into a Web Worker (Webpack loader)", | ||
@@ -8,3 +8,3 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"build": "microbundle --target node --format cjs --no-compress src/*.js", | ||
"build": "microbundle --format cjs --no-compress --inline none src/*.js", | ||
"prepublishOnly": "npm run build", | ||
@@ -15,2 +15,6 @@ "dev": "karmatic watch --no-headless", | ||
}, | ||
"browserslist": [ | ||
"IE 10", | ||
"node 8" | ||
], | ||
"eslintConfig": { | ||
@@ -38,10 +42,10 @@ "extends": "eslint-config-developit", | ||
"devDependencies": { | ||
"eslint": "^6.8.0", | ||
"eslint-config-developit": "^1.1.1", | ||
"eslint": "^7.2.0", | ||
"eslint-config-developit": "^1.2.0", | ||
"karmatic": "^1.4.0", | ||
"microbundle": "0.12.0-next.8", | ||
"webpack": "^4.39.2" | ||
"microbundle": "^0.12.1", | ||
"webpack": "^4.43.0" | ||
}, | ||
"dependencies": { | ||
"loader-utils": "^1.2.3" | ||
"loader-utils": "^2.0.0" | ||
}, | ||
@@ -48,0 +52,0 @@ "peerDependencies": { |
@@ -138,2 +138,5 @@ import path from 'path'; | ||
} | ||
else if (options.publicPath) { | ||
worker.url = `${JSON.stringify(options.publicPath + worker.file)}`; | ||
} | ||
else { | ||
@@ -140,0 +143,0 @@ worker.url = `__webpack_public_path__ + ${JSON.stringify(worker.file)}`; |
@@ -14,6 +14,6 @@ /* global __webpack_exports__ */ | ||
p.then(result => { | ||
postMessage({ type: 'RPC', id, result }); | ||
}) | ||
postMessage({ type: 'RPC', id, result }); | ||
}) | ||
.catch(e => { | ||
let error = { message: e}; | ||
let error = { message: e }; | ||
if (e.stack) { | ||
@@ -33,4 +33,5 @@ error.message = e.message; | ||
export default function rpcWorkerLoader(content) { | ||
return content + workerScript; | ||
export default function rpcWorkerLoader(content, sourceMap) { | ||
const callback = this.async(); | ||
callback(null, content + workerScript, sourceMap); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37335
414
+ Addedjson5@2.2.3(transitive)
+ Addedloader-utils@2.0.4(transitive)
- Removedjson5@1.0.2(transitive)
- Removedloader-utils@1.4.2(transitive)
- Removedminimist@1.2.8(transitive)
Updatedloader-utils@^2.0.0