Socket
Socket
Sign inDemoInstall

workerize-loader

Package Overview
Dependencies
82
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.4 to 1.1.0

182

dist/index.js

@@ -10,100 +10,118 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

function loader() {}
var CACHE = {};
var tapName = 'workerize-loader';
function compilationHook(compiler, handler) {
if (compiler.hooks) {
return compiler.hooks.compilation.tap(tapName, handler);
}
return compiler.plugin('compilation', handler);
if (compiler.hooks) {
return compiler.hooks.compilation.tap(tapName, handler);
}
return compiler.plugin('compilation', handler);
}
function parseHook(data, handler) {
if (data.normalModuleFactory.hooks) {
return data.normalModuleFactory.hooks.parser.for('javascript/auto').tap(tapName, handler);
}
return data.normalModuleFactory.plugin('parser', handler);
if (data.normalModuleFactory.hooks) {
return data.normalModuleFactory.hooks.parser.for('javascript/auto').tap(tapName, handler);
}
return data.normalModuleFactory.plugin('parser', handler);
}
function exportDeclarationHook(parser, handler) {
if (parser.hooks) {
return parser.hooks.exportDeclaration.tap(tapName, handler);
}
return parser.plugin('export declaration', handler);
if (parser.hooks) {
return parser.hooks.exportDeclaration.tap(tapName, handler);
}
return parser.plugin('export declaration', handler);
}
loader.pitch = function (request) {
var this$1 = this;
var this$1 = this;
this.cacheable(false);
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
});
var worker = {};
worker.options = {
filename: filename,
chunkFilename: ("[id]." + filename),
namedChunkFilename: null
};
var compilerOptions = this._compiler.options || {};
if (compilerOptions.output && compilerOptions.output.globalObject === 'window') {
console.warn('Warning (workerize-loader): output.globalObject is set to "window". It should be set to "self" or "this" to support HMR in Workers.');
this.cacheable(false);
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
});
var worker = {};
worker.options = {
filename: filename,
chunkFilename: ("[id]." + filename),
namedChunkFilename: null
};
var compilerOptions = this._compiler.options || {};
if (compilerOptions.output && compilerOptions.output.globalObject === 'window') {
console.warn('Warning (workerize-loader): output.globalObject is set to "window". It should be set to "self" or "this" to support HMR in Workers.');
}
worker.compiler = this._compilation.createChildCompiler('worker', worker.options);
new WebWorkerTemplatePlugin(worker.options).apply(worker.compiler);
if (this.target !== 'webworker' && this.target !== 'web') {
new NodeTargetPlugin().apply(worker.compiler);
}
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) {
if (!compilation.cache[subCache]) { compilation.cache[subCache] = {}; }
compilation.cache = compilation.cache[subCache];
}
worker.compiler = this._compilation.createChildCompiler('worker', worker.options);
new WebWorkerTemplatePlugin(worker.options).apply(worker.compiler);
if (this.target !== 'webworker' && this.target !== 'web') {
new NodeTargetPlugin().apply(worker.compiler);
}
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) {
if (!compilation.cache[subCache])
{ compilation.cache[subCache] = {}; }
compilation.cache = compilation.cache[subCache];
parseHook(data, function (parser, options) {
exportDeclarationHook(parser, function (expr) {
var decl = expr.declaration || expr;
var ref = parser.state;
var compilation = ref.compilation;
var current = ref.current;
var entry = compilation.entries[0].resource; // only process entry exports
if (current.resource !== entry) { return; }
var exports = compilation.__workerizeExports || (compilation.__workerizeExports = {});
if (decl.id) {
exports[decl.id.name] = true;
} else if (decl.declarations) {
for (var i = 0; i < decl.declarations.length; i++) {
exports[decl.declarations[i].id.name] = true;
}
} else {
console.warn('[workerize] unknown export declaration: ', expr);
}
parseHook(data, function (parser, options) {
exportDeclarationHook(parser, function (expr) {
var decl = expr.declaration || expr;
var ref = parser.state;
var compilation = ref.compilation;
var current = ref.current;
var entry = compilation.entries[0].resource;
if (current.resource !== entry)
{ return; }
var exports = compilation.__workerizeExports || (compilation.__workerizeExports = {});
if (decl.id) {
exports[decl.id.name] = true;
} else if (decl.declarations) {
for (var i = 0;i < decl.declarations.length; i++) {
exports[decl.declarations[i].id.name] = true;
}
} else {
console.warn('[workerize] unknown export declaration: ', expr);
}
});
});
});
});
worker.compiler.runAsChild(function (err, entries, compilation) {
if (err)
{ return cb(err); }
if (entries[0]) {
worker.file = entries[0].files[0];
var contents = compilation.assets[worker.file].source();
var exports = Object.keys(CACHE[worker.file] = compilation.__workerizeExports || CACHE[worker.file] || {});
if (options.inline) {
worker.url = "URL.createObjectURL(new Blob([" + (JSON.stringify(contents)) + "]))";
} else {
worker.url = "__webpack_public_path__ + " + (JSON.stringify(worker.file));
}
if (options.fallback === false) {
delete this$1._compilation.assets[worker.file];
}
return cb(null, ("\n\t\t\t\tvar addMethods = require(" + (loaderUtils.stringifyRequest(this$1, 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(" + (worker.url) + ", { 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"));
}
return cb(null, null);
});
});
worker.compiler.runAsChild(function (err, entries, compilation) {
if (err) { return cb(err); }
if (entries[0]) {
worker.file = entries[0].files[0];
var contents = compilation.assets[worker.file].source();
var exports = Object.keys(CACHE[worker.file] = compilation.__workerizeExports || CACHE[worker.file] || {}); // console.log('Workerized exports: ', exports.join(', '));
if (options.inline) {
worker.url = "URL.createObjectURL(new Blob([" + (JSON.stringify(contents)) + "]))";
} else {
worker.url = "__webpack_public_path__ + " + (JSON.stringify(worker.file));
}
if (options.fallback === false) {
delete this$1._compilation.assets[worker.file];
}
var workerUrl = worker.url;
if (options.import) {
workerUrl = "\"data:,importScripts('\"+location.origin+" + workerUrl + "+\"')\"";
}
return cb(null, ("\n\t\t\t\tvar addMethods = require(" + (loaderUtils.stringifyRequest(this$1, 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"));
}
return cb(null, null);
});
};

@@ -110,0 +128,0 @@

@@ -0,42 +1,48 @@

/* global __webpack_exports__ */
function workerSetup() {
addEventListener('message', function (e) {
var ref = e.data;
var type = ref.type;
var method = ref.method;
var id = ref.id;
var params = ref.params;
var f, p;
if (type === 'RPC' && method) {
if (f = __webpack_exports__[method]) {
p = Promise.resolve().then(function () { return f.apply(__webpack_exports__, params); });
} else {
p = Promise.reject('No such method');
}
p.then(function (result) {
postMessage({
type: 'RPC',
id: id,
result: result
});
}).catch(function (e) {
var error = {
message: e
};
if (e.stack) {
error.message = e.message;
error.stack = e.stack;
error.name = e.name;
}
postMessage({
type: 'RPC',
id: id,
error: error
});
});
addEventListener('message', function (e) {
var ref = e.data;
var type = ref.type;
var method = ref.method;
var id = ref.id;
var params = ref.params;
var f,
p;
if (type === 'RPC' && method) {
if (f = __webpack_exports__[method]) {
p = Promise.resolve().then(function () { return f.apply(__webpack_exports__, params); });
} else {
p = Promise.reject('No such method');
}
p.then(function (result) {
postMessage({
type: 'RPC',
id: id,
result: result
});
}).catch(function (e) {
var error = {
message: e
};
if (e.stack) {
error.message = e.message;
error.stack = e.stack;
error.name = e.name;
}
});
postMessage({
type: 'RPC',
method: 'ready'
});
postMessage({
type: 'RPC',
id: id,
error: error
});
});
}
});
postMessage({
type: 'RPC',
method: 'ready'
});
}

@@ -46,3 +52,3 @@

function rpcWorkerLoader(content) {
return content + workerScript;
return content + workerScript;
}

@@ -49,0 +55,0 @@

function addMethods(worker, methods) {
var c = 0;
var callbacks = {};
worker.addEventListener('message', function (e) {
var d = e.data;
if (d.type !== 'RPC')
{ return; }
if (d.id) {
var f = callbacks[d.id];
if (f) {
delete callbacks[d.id];
if (d.error) {
f[1](Object.assign(Error(d.error.message), d.error));
} else {
f[0](d.result);
}
}
var c = 0;
var callbacks = {};
worker.addEventListener('message', function (e) {
var d = e.data;
if (d.type !== 'RPC') { return; }
if (d.id) {
var f = callbacks[d.id];
if (f) {
delete callbacks[d.id];
if (d.error) {
f[1](Object.assign(Error(d.error.message), d.error));
} else {
var evt = document.createEvent('Event');
evt.initEvent(d.method, false, false);
evt.data = d.params;
worker.dispatchEvent(evt);
f[0](d.result);
}
});
methods.forEach(function (method) {
worker[method] = (function () {
var params = [], len = arguments.length;
while ( len-- ) params[ len ] = arguments[ len ];
}
} else {
var evt = document.createEvent('Event');
evt.initEvent(d.method, false, false);
evt.data = d.params;
worker.dispatchEvent(evt);
}
});
methods.forEach(function (method) {
worker[method] = function () {
var params = [], len = arguments.length;
while ( len-- ) params[ len ] = arguments[ len ];
return new Promise(function (a, b) {
var id = ++c;
callbacks[id] = [a,b];
worker.postMessage({
type: 'RPC',
id: id,
method: method,
params: params
});
});
});
return new Promise(function (a, b) {
var id = ++c;
callbacks[id] = [a, b];
worker.postMessage({
type: 'RPC',
id: id,
method: method,
params: params
});
});
};
});
}

@@ -43,0 +45,0 @@

{
"name": "workerize-loader",
"version": "1.0.4",
"version": "1.1.0",
"description": "Automatically move a module into a Web Worker (Webpack loader)",

@@ -36,10 +36,10 @@ "main": "dist/index.js",

"devDependencies": {
"eslint": "^5.0.0",
"eslint": "^5.16.0",
"eslint-config-developit": "^1.1.1",
"karmatic": "^1.2.0",
"microbundle": "^0.6.0",
"webpack": "^4.17.1"
"karmatic": "^1.3.1",
"microbundle": "^0.11.0",
"webpack": "^4.39.2"
},
"dependencies": {
"loader-utils": "^1.1.0"
"loader-utils": "^1.2.3"
},

@@ -46,0 +46,0 @@ "peerDependencies": {

@@ -125,2 +125,7 @@ import path from 'path';

let workerUrl = worker.url;
if (options.import) {
workerUrl = `"data:,importScripts('"+location.origin+${workerUrl}+"')"`;
}
return cb(null, `

@@ -130,3 +135,3 @@ var addMethods = require(${loaderUtils.stringifyRequest(this, path.resolve(__dirname, 'rpc-wrapper.js'))})

module.exports = function() {
var w = new Worker(${worker.url}, { name: ${JSON.stringify(filename)} })
var w = new Worker(${workerUrl}, { name: ${JSON.stringify(filename)} })
addMethods(w, methods)

@@ -133,0 +138,0 @@ ${ options.ready ? 'w.ready = new Promise(function(r) { w.addEventListener("ready", function(){ r(w) }) })' : '' }

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

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc