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