threads-plugin
Advanced tools
Comparing version 1.3.0-beta to 1.3.0-beta.2
@@ -28,2 +28,3 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
this.options = options || {}; | ||
this.instantiationsFound = false; | ||
this[WORKER_PLUGIN_SYMBOL] = true; | ||
@@ -33,4 +34,6 @@ }; | ||
WorkerPlugin.prototype.apply = function apply (compiler) { | ||
var this$1 = this; | ||
compiler.hooks.normalModuleFactory.tap(NAME, function (factory) { | ||
var workerId = 0; | ||
this$1.workerId = 0; | ||
@@ -41,46 +44,8 @@ for (var i = 0, list = JS_TYPES; i < list.length; i += 1) { | ||
factory.hooks.parser.for(("javascript/" + type)).tap(NAME, function (parser) { | ||
parser.hooks.new.for('Worker').tap(NAME, function (expr) { | ||
return this$1.processInstantiation(parser, expr); | ||
}); | ||
parser.hooks.new.for('imported var').tap(NAME, function (expr) { | ||
if (expr.callee.name !== 'Worker') { return false; } | ||
var dep = parser.evaluateExpression(expr.arguments[0]); | ||
if (!dep.isString()) { | ||
parser.state.module.warnings.push({ | ||
message: 'new Worker() will only be bundled if passed a String.' | ||
}); | ||
return false; | ||
} | ||
if (/^(https?:)?\/\//i.test(dep.string)) { | ||
// Ignore absolute URL workers | ||
return false; | ||
} | ||
var optsExpr = expr.arguments[1]; | ||
var typeModuleExpr = Types.objectProperty(Types.identifier("type"), Types.stringLiteral("module")); | ||
typeModuleExpr.range = [0, 0]; | ||
var opts = {}; | ||
if (optsExpr) { | ||
opts = {}; | ||
for (var i = optsExpr.properties.length; i--;) { | ||
var prop = optsExpr.properties[i]; | ||
if (prop.type === 'Property' && !prop.computed && !prop.shorthand && !prop.method) { | ||
opts[prop.key.name] = parser.evaluateExpression(prop.value).string; | ||
if (prop.key.name === 'type') { | ||
typeModuleExpr = prop; | ||
} | ||
} | ||
} | ||
} | ||
var loaderOptions = { | ||
name: opts.name || workerId + '' | ||
}; | ||
var req = "require(" + (JSON.stringify(workerLoader + '?' + JSON.stringify(loaderOptions) + '!' + dep.string)) + ")"; | ||
var id = "__webpack__worker__" + (workerId++); | ||
ParserHelpers.toConstantDependency(parser, id)(expr.arguments[0]); | ||
ParserHelpers.addParsedVariableToModule(parser, id, req); | ||
return this$1.processInstantiation(parser, expr); | ||
}); | ||
@@ -91,3 +56,3 @@ }); | ||
compiler.hooks.afterCompile.tap(NAME, function (compilation) { | ||
{ | ||
if (!this$1.instantiationsFound) { | ||
compilation.warnings.push({ | ||
@@ -100,3 +65,60 @@ message: 'No instantiations of threads.js workers found.\n' + 'Please check that:\n' + ' 1. You have configured Babel / TypeScript to not transpile ES modules\n' + ' 2. You import `Worker` from `threads` where you use it\n\n' + 'For more details see: https://github.com/andywer/threads-plugin\n' | ||
WorkerPlugin.prototype.processInstantiation = function processInstantiation (parser, expr) { | ||
var dep = parser.evaluateExpression(expr.arguments[0]); | ||
if (!dep.isString()) { | ||
parser.state.module.warnings.push({ | ||
message: 'new Worker() will only be bundled if passed a String.' | ||
}); | ||
return false; | ||
} | ||
if (/^(https?:)?\/\//i.test(dep.string)) { | ||
// Ignore absolute URL workers | ||
return false; | ||
} | ||
var optsExpr = expr.arguments[1]; | ||
var typeModuleExpr = Types.objectProperty(Types.identifier("type"), Types.stringLiteral("module")); | ||
typeModuleExpr.range = [0, 0]; | ||
var opts = {}; | ||
if (optsExpr) { | ||
opts = {}; | ||
for (var i = optsExpr.properties.length; i--;) { | ||
var prop = optsExpr.properties[i]; | ||
if (prop.type === 'Property' && !prop.computed && !prop.shorthand && !prop.method) { | ||
opts[prop.key.name] = parser.evaluateExpression(prop.value).string; | ||
if (prop.key.name === 'type') { | ||
typeModuleExpr = prop; | ||
} | ||
} | ||
} | ||
} | ||
var loaderOptions = { | ||
name: opts.name || this.workerId + '' | ||
}; | ||
var req = "require(" + (JSON.stringify(workerLoader + '?' + JSON.stringify(loaderOptions) + '!' + dep.string)) + ")"; | ||
var id = "__webpack__worker__" + (this.workerId++); | ||
ParserHelpers.toConstantDependency(parser, id)(expr.arguments[0]); | ||
if (typeModuleExpr && this.options.workerType) { | ||
ParserHelpers.toConstantDependency(parser, JSON.stringify(this.options.workerType))(typeModuleExpr.value); | ||
} else if (typeModuleExpr && this.options.preserveTypeModule !== true) { | ||
// Options object can contain comma at the end e.g. `{ type: 'module', }`. | ||
// Previously, `type` property was replaced with an empty string | ||
// that left this comma. | ||
// Currently the `type` property value is replaced with `undefined`. | ||
ParserHelpers.toConstantDependency(parser, 'type:undefined')(typeModuleExpr); | ||
} | ||
this.instantiationsFound = true; | ||
return ParserHelpers.addParsedVariableToModule(parser, id, req); | ||
}; | ||
module.exports = WorkerPlugin; | ||
//# sourceMappingURL=threads-plugin.js.map |
{ | ||
"name": "threads-plugin", | ||
"version": "1.3.0-beta", | ||
"version": "1.3.0-beta.2", | ||
"description": "Webpack plugin to bundle threads.js workers automagically.", | ||
@@ -5,0 +5,0 @@ "main": "dist/threads-plugin.js", |
108
src/index.js
@@ -29,2 +29,3 @@ /** | ||
this.options = options || {}; | ||
this.instantiationsFound = false | ||
this[WORKER_PLUGIN_SYMBOL] = true; | ||
@@ -34,52 +35,14 @@ } | ||
apply (compiler) { | ||
let workerId = 0; | ||
compiler.hooks.normalModuleFactory.tap(NAME, factory => { | ||
this.workerId = 0; | ||
compiler.hooks.normalModuleFactory.tap(NAME, factory => { | ||
let workerId = 0; | ||
for (const type of JS_TYPES) { | ||
factory.hooks.parser.for(`javascript/${type}`).tap(NAME, parser => { | ||
parser.hooks.new.for('Worker').tap(NAME, expr => { | ||
return this.processInstantiation(parser, expr); | ||
}); | ||
parser.hooks.new.for('imported var').tap(NAME, expr => { | ||
if (expr.callee.name !== 'Worker') return false | ||
const dep = parser.evaluateExpression(expr.arguments[0]); | ||
if (!dep.isString()) { | ||
parser.state.module.warnings.push({ | ||
message: 'new Worker() will only be bundled if passed a String.' | ||
}); | ||
return false; | ||
} | ||
if (/^(https?:)?\/\//i.test(dep.string)) { | ||
// Ignore absolute URL workers | ||
return false; | ||
} | ||
const optsExpr = expr.arguments[1]; | ||
let typeModuleExpr = Types.objectProperty( | ||
Types.identifier("type"), | ||
Types.stringLiteral("module") | ||
) | ||
typeModuleExpr.range=[0, 0] | ||
let opts = {}; | ||
if (optsExpr) { | ||
opts = {}; | ||
for (let i = optsExpr.properties.length; i--;) { | ||
const prop = optsExpr.properties[i]; | ||
if (prop.type === 'Property' && !prop.computed && !prop.shorthand && !prop.method) { | ||
opts[prop.key.name] = parser.evaluateExpression(prop.value).string; | ||
if (prop.key.name === 'type') { | ||
typeModuleExpr = prop; | ||
} | ||
} | ||
} | ||
} | ||
const loaderOptions = { name: opts.name || workerId + '' }; | ||
const req = `require(${JSON.stringify(workerLoader + '?' + JSON.stringify(loaderOptions) + '!' + dep.string)})`; | ||
const id = `__webpack__worker__${workerId++}`; | ||
ParserHelpers.toConstantDependency(parser, id)(expr.arguments[0]); | ||
ParserHelpers.addParsedVariableToModule(parser, id, req); | ||
return this.processInstantiation(parser, expr); | ||
}); | ||
@@ -91,3 +54,3 @@ }); | ||
compiler.hooks.afterCompile.tap(NAME, compilation => { | ||
if (workerId === 0) { | ||
if (!this.instantiationsFound) { | ||
compilation.warnings.push({ | ||
@@ -104,2 +67,57 @@ message: | ||
} | ||
processInstantiation (parser, expr) { | ||
const dep = parser.evaluateExpression(expr.arguments[0]); | ||
if (!dep.isString()) { | ||
parser.state.module.warnings.push({ | ||
message: 'new Worker() will only be bundled if passed a String.' | ||
}); | ||
return false; | ||
} | ||
if (/^(https?:)?\/\//i.test(dep.string)) { | ||
// Ignore absolute URL workers | ||
return false; | ||
} | ||
const optsExpr = expr.arguments[1]; | ||
let typeModuleExpr = Types.objectProperty( | ||
Types.identifier("type"), | ||
Types.stringLiteral("module") | ||
) | ||
typeModuleExpr.range = [0, 0] | ||
let opts = {}; | ||
if (optsExpr) { | ||
opts = {}; | ||
for (let i = optsExpr.properties.length; i--;) { | ||
const prop = optsExpr.properties[i]; | ||
if (prop.type === 'Property' && !prop.computed && !prop.shorthand && !prop.method) { | ||
opts[prop.key.name] = parser.evaluateExpression(prop.value).string; | ||
if (prop.key.name === 'type') { | ||
typeModuleExpr = prop; | ||
} | ||
} | ||
} | ||
} | ||
const loaderOptions = { name: opts.name || this.workerId + '' }; | ||
const req = `require(${JSON.stringify(workerLoader + '?' + JSON.stringify(loaderOptions) + '!' + dep.string)})`; | ||
const id = `__webpack__worker__${this.workerId++}`; | ||
ParserHelpers.toConstantDependency(parser, id)(expr.arguments[0]); | ||
if (typeModuleExpr && this.options.workerType) { | ||
ParserHelpers.toConstantDependency(parser, JSON.stringify(this.options.workerType))(typeModuleExpr.value); | ||
} else if (typeModuleExpr && this.options.preserveTypeModule !== true) { | ||
// Options object can contain comma at the end e.g. `{ type: 'module', }`. | ||
// Previously, `type` property was replaced with an empty string | ||
// that left this comma. | ||
// Currently the `type` property value is replaced with `undefined`. | ||
ParserHelpers.toConstantDependency(parser, 'type:undefined')(typeModuleExpr); | ||
} | ||
this.instantiationsFound = true; | ||
return ParserHelpers.addParsedVariableToModule(parser, id, req); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
344788
4111