|
1 |
| -var WebWorkerTemplatePlugin = require("webpack/lib/webworker/WebWorkerTemplatePlugin"); |
2 |
| -var SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin"); |
3 |
| -var path = require("path"); |
| 1 | +const path = require('path'); |
| 2 | +const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin'); |
| 3 | +const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin'); |
| 4 | +const loaderUtils = require('loader-utils'); |
4 | 5 |
|
5 |
| -var loaderUtils = require("loader-utils"); |
6 |
| -module.exports = function() {}; |
7 |
| -module.exports.pitch = function(request) { |
8 |
| - if(!this.webpack) throw new Error("Only usable with webpack"); |
| 6 | +module.exports = function workerLoader() {}; |
| 7 | + |
| 8 | +module.exports.pitch = function pitch(request) { |
| 9 | + if (!this.webpack) throw new Error('Only usable with webpack'); |
9 | 10 | this.cacheable(false);
|
10 |
| - var callback = this.async(); |
11 |
| - var query = loaderUtils.parseQuery(this.query); |
12 |
| - var filename = loaderUtils.interpolateName(this, query.name || "[hash].worker.js", { |
| 11 | + const callback = this.async(); |
| 12 | + const query = loaderUtils.parseQuery(this.query); |
| 13 | + const filename = loaderUtils.interpolateName(this, query.name || '[hash].worker.js', { |
13 | 14 | context: query.context || this.options.context,
|
14 |
| - regExp: query.regExp |
| 15 | + regExp: query.regExp, |
15 | 16 | });
|
16 |
| - var outputOptions = { |
17 |
| - filename: filename, |
18 |
| - chunkFilename: "[id]." + filename, |
19 |
| - namedChunkFilename: null |
| 17 | + const outputOptions = { |
| 18 | + filename, |
| 19 | + chunkFilename: `[id].${filename}`, |
| 20 | + namedChunkFilename: null, |
20 | 21 | };
|
21 |
| - if(this.options && this.options.worker && this.options.worker.output) { |
22 |
| - for(var name in this.options.worker.output) { |
| 22 | + if (this.options && this.options.worker && this.options.worker.output) { |
| 23 | + Object.keys(this.options.worker.output).forEach((name) => { |
23 | 24 | outputOptions[name] = this.options.worker.output[name];
|
24 |
| - } |
| 25 | + }); |
25 | 26 | }
|
26 |
| - var workerCompiler = this._compilation.createChildCompiler("worker", outputOptions); |
| 27 | + const workerCompiler = this._compilation.createChildCompiler('worker', outputOptions); |
27 | 28 | workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions));
|
28 |
| - workerCompiler.apply(new SingleEntryPlugin(this.context, "!!" + request, "main")); |
29 |
| - if(this.options && this.options.worker && this.options.worker.plugins) { |
30 |
| - this.options.worker.plugins.forEach(function(plugin) { |
31 |
| - workerCompiler.apply(plugin); |
32 |
| - }); |
| 29 | + workerCompiler.apply(new SingleEntryPlugin(this.context, `!!${request}`, 'main')); |
| 30 | + if (this.options && this.options.worker && this.options.worker.plugins) { |
| 31 | + this.options.worker.plugins.forEach(plugin => workerCompiler.apply(plugin)); |
33 | 32 | }
|
34 |
| - var subCache = "subcache " + __dirname + " " + request; |
35 |
| - workerCompiler.plugin("compilation", function(compilation) { |
36 |
| - if(compilation.cache) { |
37 |
| - if(!compilation.cache[subCache]) |
| 33 | + const subCache = `subcache ${__dirname} ${request}`; |
| 34 | + workerCompiler.plugin('compilation', (compilation) => { |
| 35 | + if (compilation.cache) { |
| 36 | + if (!compilation.cache[subCache]) { |
38 | 37 | compilation.cache[subCache] = {};
|
| 38 | + } |
39 | 39 | compilation.cache = compilation.cache[subCache];
|
40 | 40 | }
|
41 | 41 | });
|
42 |
| - workerCompiler.runAsChild(function(err, entries, compilation) { |
43 |
| - if(err) return callback(err); |
| 42 | + workerCompiler.runAsChild((err, entries, compilation) => { |
| 43 | + if (err) return callback(err); |
44 | 44 | if (entries[0]) {
|
45 |
| - var workerFile = entries[0].files[0]; |
46 |
| - var constructor = "new Worker(__webpack_public_path__ + " + JSON.stringify(workerFile) + ")"; |
47 |
| - if(query.inline) { |
48 |
| - constructor = "require(" + JSON.stringify("!!" + path.join(__dirname, "createInlineWorker.js")) + ")(" + |
49 |
| - JSON.stringify(compilation.assets[workerFile].source()) + ", __webpack_public_path__ + " + JSON.stringify(workerFile) + ")"; |
| 45 | + const [workerFile] = entries[0].files; |
| 46 | + let constructor = `new Worker(__webpack_public_path__ + ${JSON.stringify(workerFile)})`; |
| 47 | + if (query.inline) { |
| 48 | + constructor = `require(${JSON.stringify(`!!${path.join(__dirname, 'createInlineWorker.js')}`)})(${ |
| 49 | + JSON.stringify(compilation.assets[workerFile].source()) |
| 50 | + }, __webpack_public_path__ + ${ |
| 51 | + JSON.stringify(workerFile) |
| 52 | + })`; |
50 | 53 | }
|
51 |
| - return callback(null, "module.exports = function() {\n\treturn " + constructor + ";\n};"); |
52 |
| - } else { |
53 |
| - return callback(null, null); |
| 54 | + return callback(null, `module.exports = function() {\n\treturn ${constructor};\n};`); |
54 | 55 | }
|
| 56 | + return callback(null, null); |
55 | 57 | });
|
56 | 58 | };
|
0 commit comments