worker-plugin
Advanced tools
Comparing version 4.0.3 to 5.0.0
@@ -6,3 +6,2 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var WebWorkerTemplatePlugin = _interopDefault(require('webpack/lib/webworker/WebWorkerTemplatePlugin')); | ||
var FetchCompileWasmTemplatePlugin = _interopDefault(require('webpack/lib/web/FetchCompileWasmTemplatePlugin')); | ||
var WORKER_PLUGIN_SYMBOL = _interopDefault(require('./symbol.js')); | ||
@@ -25,2 +24,10 @@ | ||
*/ | ||
var FetchCompileWasmPlugin; | ||
try { | ||
FetchCompileWasmPlugin = require('webpack/lib/web/FetchCompileWasmPlugin'); // Webpack 5 | ||
} catch (e) {} | ||
FetchCompileWasmPlugin = FetchCompileWasmPlugin || require('webpack/lib/web/FetchCompileWasmTemplatePlugin'); // Webpack 4 | ||
var NAME = 'WorkerPluginLoader'; | ||
@@ -41,6 +48,6 @@ var hasWarned = false; | ||
var options = loaderUtils.getOptions(this) || {}; | ||
var chunkFilename = compilerOptions.output.chunkFilename.replace(/\.([a-z]+)$/i, '.worker.$1'); | ||
var chunkFilename = compilerOptions.output.chunkFilename.replace(/\.([a-z]+)(\?.+)?$/i, '.worker.$1$2'); | ||
var workerOptions = { | ||
filename: chunkFilename.replace(/\[(?:chunkhash|contenthash)(:\d+(?::\d+)?)?\]/g, '[hash$1]'), | ||
chunkFilename: chunkFilename, | ||
filename: (options.filename || pluginOptions.filename || chunkFilename).replace(/\[(?:chunkhash|contenthash)(:\d+(?::\d+)?)?\]/g, '[hash$1]'), | ||
chunkFilename: options.chunkFilename || pluginOptions.chunkFilename || chunkFilename, | ||
globalObject: pluginOptions.globalObject || 'self' | ||
@@ -65,4 +72,4 @@ }; | ||
workerCompiler.context = this._compiler.context; | ||
new WebWorkerTemplatePlugin(workerOptions).apply(workerCompiler); | ||
new FetchCompileWasmTemplatePlugin({ | ||
new WebWorkerTemplatePlugin().apply(workerCompiler); | ||
new FetchCompileWasmPlugin({ | ||
mangleImports: compilerOptions.optimization.mangleWasmImports | ||
@@ -83,3 +90,4 @@ }).apply(workerCompiler); | ||
var entry = entries && entries[0] && entries[0].files[0]; | ||
var entry = entries && entries[0] && entries[0].files.values().next().value; // compatible with Array (v4) and Set (v5) prototypes | ||
if (!err && !entry) { err = Error(("WorkerPlugin: no entry for " + request)); } | ||
@@ -86,0 +94,0 @@ if (err) { return cb(err); } |
@@ -5,3 +5,2 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var WORKER_PLUGIN_SYMBOL = _interopDefault(require('./symbol.js')); | ||
var ParserHelpers = _interopDefault(require('webpack/lib/ParserHelpers')); | ||
@@ -23,2 +22,10 @@ /** | ||
*/ | ||
var ParserHelpers; | ||
try { | ||
ParserHelpers = require('webpack/lib/javascript/JavascriptParserHelpers'); // Webpack 5 | ||
} catch (e) {} | ||
ParserHelpers = ParserHelpers || require('webpack/lib/ParserHelpers'); // Webpack 4 | ||
var HarmonyImportSpecifierDependency; | ||
@@ -49,10 +56,2 @@ | ||
var dep = parser.evaluateExpression(expr.arguments[0]); | ||
if (!dep.isString()) { | ||
parser.state.module.warnings.push({ | ||
message: ("new " + workerTypeString + "() will only be bundled if passed a String.") | ||
}); | ||
return false; | ||
} | ||
var optsExpr = expr.arguments[1]; | ||
@@ -82,4 +81,15 @@ var hasInitOptions = false; | ||
if (!opts || opts.type !== 'module') { | ||
// If an unknown type value is passed, it's probably an error and we can warn the developer: | ||
if (opts && opts.type !== 'classic') { | ||
parser.state.module.warnings.push({ | ||
message: ("new " + workerTypeString + "() will only be bundled if passed options that include { type: 'module' }." + (opts ? ("\n Received: new " + workerTypeString + "()(" + (JSON.stringify(dep.string)) + ", " + (JSON.stringify(opts)) + ")") : '')) | ||
}); | ||
} | ||
return false; | ||
} | ||
if (!dep.isString()) { | ||
parser.state.module.warnings.push({ | ||
message: ("new " + workerTypeString + "() will only be bundled if passed options that include { type: 'module' }." + (opts ? ("\n Received: new " + workerTypeString + "()(" + (JSON.stringify(dep.string)) + ", " + (JSON.stringify(opts)) + ")") : '')) | ||
message: ("new " + workerTypeString + "(\"..\", { type: \"module\" }) will only be bundled if passed a String.") | ||
}); | ||
@@ -86,0 +96,0 @@ return false; |
{ | ||
"name": "worker-plugin", | ||
"version": "4.0.3", | ||
"version": "5.0.0", | ||
"description": "Webpack plugin to bundle Workers automagically.", | ||
@@ -5,0 +5,0 @@ "main": "dist/worker-plugin.js", |
@@ -42,2 +42,4 @@ <p align="center"> | ||
> **Note:** If you're planning on having more than one worker, you'll need to make sure [`output.filename`](https://webpack.js.org/configuration/output/#outputfilename) is set to something dynamic, e.g. `"[name].bundle.js"` otherwise the generated filenames will overwrite one another. | ||
## Usage | ||
@@ -44,0 +46,0 @@ |
@@ -19,3 +19,7 @@ /** | ||
import WORKER_PLUGIN_SYMBOL from './symbol'; | ||
import ParserHelpers from 'webpack/lib/ParserHelpers'; | ||
let ParserHelpers; | ||
try { | ||
ParserHelpers = require('webpack/lib/javascript/JavascriptParserHelpers'); // Webpack 5 | ||
} catch (e) {} | ||
ParserHelpers = ParserHelpers || require('webpack/lib/ParserHelpers'); // Webpack 4 | ||
let HarmonyImportSpecifierDependency; | ||
@@ -46,9 +50,2 @@ try { | ||
if (!dep.isString()) { | ||
parser.state.module.warnings.push({ | ||
message: `new ${workerTypeString}() will only be bundled if passed a String.` | ||
}); | ||
return false; | ||
} | ||
const optsExpr = expr.arguments[1]; | ||
@@ -75,4 +72,14 @@ let hasInitOptions = false; | ||
if (!opts || opts.type !== 'module') { | ||
// If an unknown type value is passed, it's probably an error and we can warn the developer: | ||
if (opts && opts.type !== 'classic') { | ||
parser.state.module.warnings.push({ | ||
message: `new ${workerTypeString}() will only be bundled if passed options that include { type: 'module' }.${opts ? `\n Received: new ${workerTypeString}()(${JSON.stringify(dep.string)}, ${JSON.stringify(opts)})` : ''}` | ||
}); | ||
} | ||
return false; | ||
} | ||
if (!dep.isString()) { | ||
parser.state.module.warnings.push({ | ||
message: `new ${workerTypeString}() will only be bundled if passed options that include { type: 'module' }.${opts ? `\n Received: new ${workerTypeString}()(${JSON.stringify(dep.string)}, ${JSON.stringify(opts)})` : ''}` | ||
message: `new ${workerTypeString}("..", { type: "module" }) will only be bundled if passed a String.` | ||
}); | ||
@@ -79,0 +86,0 @@ return false; |
@@ -20,5 +20,10 @@ /** | ||
import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin'; | ||
import FetchCompileWasmTemplatePlugin from 'webpack/lib/web/FetchCompileWasmTemplatePlugin'; | ||
import WORKER_PLUGIN_SYMBOL from './symbol'; | ||
let FetchCompileWasmPlugin; | ||
try { | ||
FetchCompileWasmPlugin = require('webpack/lib/web/FetchCompileWasmPlugin'); // Webpack 5 | ||
} catch (e) {} | ||
FetchCompileWasmPlugin = FetchCompileWasmPlugin || require('webpack/lib/web/FetchCompileWasmTemplatePlugin'); // Webpack 4 | ||
const NAME = 'WorkerPluginLoader'; | ||
@@ -34,3 +39,3 @@ let hasWarned = false; | ||
const plugin = compilerOptions.plugins.find(p => p[WORKER_PLUGIN_SYMBOL]) || {}; | ||
const pluginOptions = plugin && plugin.options || {}; | ||
const pluginOptions = (plugin && plugin.options) || {}; | ||
@@ -43,6 +48,6 @@ if (pluginOptions.globalObject == null && !hasWarned && compilerOptions.output && compilerOptions.output.globalObject === 'window') { | ||
const options = loaderUtils.getOptions(this) || {}; | ||
const chunkFilename = compilerOptions.output.chunkFilename.replace(/\.([a-z]+)$/i, '.worker.$1'); | ||
const chunkFilename = compilerOptions.output.chunkFilename.replace(/\.([a-z]+)(\?.+)?$/i, '.worker.$1$2'); | ||
const workerOptions = { | ||
filename: chunkFilename.replace(/\[(?:chunkhash|contenthash)(:\d+(?::\d+)?)?\]/g, '[hash$1]'), | ||
chunkFilename, | ||
filename: (options.filename || pluginOptions.filename || chunkFilename).replace(/\[(?:chunkhash|contenthash)(:\d+(?::\d+)?)?\]/g, '[hash$1]'), | ||
chunkFilename: options.chunkFilename || pluginOptions.chunkFilename || chunkFilename, | ||
globalObject: pluginOptions.globalObject || 'self' | ||
@@ -64,4 +69,4 @@ }; | ||
workerCompiler.context = this._compiler.context; | ||
(new WebWorkerTemplatePlugin(workerOptions)).apply(workerCompiler); | ||
(new FetchCompileWasmTemplatePlugin({ | ||
(new WebWorkerTemplatePlugin()).apply(workerCompiler); | ||
(new FetchCompileWasmPlugin({ | ||
mangleImports: compilerOptions.optimization.mangleWasmImports | ||
@@ -83,3 +88,3 @@ })).apply(workerCompiler); | ||
} | ||
const entry = entries && entries[0] && entries[0].files[0]; | ||
const entry = entries && entries[0] && entries[0].files.values().next().value; // compatible with Array (v4) and Set (v5) prototypes | ||
if (!err && !entry) err = Error(`WorkerPlugin: no entry for ${request}`); | ||
@@ -86,0 +91,0 @@ if (err) return cb(err); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
61912
448
204