|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var Template = require("../Template"); |
| 5 | +"use strict"; |
6 | 6 |
|
7 | | -function WebWorkerMainTemplatePlugin() {} |
8 | | -module.exports = WebWorkerMainTemplatePlugin; |
| 7 | +var Template = require("../Template"); |
9 | 8 |
|
10 | | -WebWorkerMainTemplatePlugin.prototype.constructor = WebWorkerMainTemplatePlugin; |
11 | | -WebWorkerMainTemplatePlugin.prototype.apply = function(mainTemplate) { |
12 | | - mainTemplate.plugin("local-vars", function(source, chunk) { |
13 | | - if(chunk.chunks.length > 0) { |
14 | | - return this.asString([ |
15 | | - source, |
16 | | - "", |
17 | | - "// object to store loaded chunks", |
18 | | - "// \"1\" means \"already loaded\"", |
19 | | - "var installedChunks = {", |
20 | | - this.indent( |
21 | | - chunk.ids.map(function(id) { |
22 | | - return id + ": 1"; |
23 | | - }).join(",\n") |
24 | | - ), |
25 | | - "};" |
26 | | - ]); |
27 | | - } |
28 | | - return source; |
29 | | - }); |
30 | | - mainTemplate.plugin("require-ensure", function(_, chunk, hash) { |
31 | | - var chunkFilename = this.outputOptions.chunkFilename; |
32 | | - return this.asString([ |
33 | | - "// \"1\" is the signal for \"already loaded\"", |
34 | | - "if(!installedChunks[chunkId]) {", |
35 | | - this.indent([ |
36 | | - "importScripts(" + |
37 | | - this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), { |
38 | | - hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
39 | | - hashWithLength: function(length) { |
40 | | - return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
41 | | - }.bind(this), |
42 | | - chunk: { |
43 | | - id: "\" + chunkId + \"" |
44 | | - } |
45 | | - }) + ");" |
46 | | - ]), |
47 | | - "}", |
48 | | - "return Promise.resolve();" |
49 | | - ]); |
50 | | - }); |
51 | | - mainTemplate.plugin("bootstrap", function(source, chunk, hash) { |
52 | | - if(chunk.chunks.length > 0) { |
53 | | - var chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || "")); |
| 9 | +class WebWorkerMainTemplatePlugin { |
| 10 | + apply(mainTemplate) { |
| 11 | + mainTemplate.plugin("local-vars", function(source, chunk) { |
| 12 | + if(chunk.chunks.length > 0) { |
| 13 | + return this.asString([ |
| 14 | + source, |
| 15 | + "", |
| 16 | + "// object to store loaded chunks", |
| 17 | + "// \"1\" means \"already loaded\"", |
| 18 | + "var installedChunks = {", |
| 19 | + this.indent( |
| 20 | + chunk.ids.map(function(id) { |
| 21 | + return id + ": 1"; |
| 22 | + }).join(",\n") |
| 23 | + ), |
| 24 | + "};" |
| 25 | + ]); |
| 26 | + } |
| 27 | + return source; |
| 28 | + }); |
| 29 | + mainTemplate.plugin("require-ensure", function(_, chunk, hash) { |
| 30 | + var chunkFilename = this.outputOptions.chunkFilename; |
54 | 31 | return this.asString([ |
55 | | - source, |
56 | | - "this[" + JSON.stringify(chunkCallbackName) + "] = function webpackChunkCallback(chunkIds, moreModules) {", |
| 32 | + "// \"1\" is the signal for \"already loaded\"", |
| 33 | + "if(!installedChunks[chunkId]) {", |
57 | 34 | this.indent([ |
58 | | - "for(var moduleId in moreModules) {", |
59 | | - this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")), |
60 | | - "}", |
61 | | - "while(chunkIds.length)", |
62 | | - this.indent("installedChunks[chunkIds.pop()] = 1;") |
| 35 | + "importScripts(" + |
| 36 | + this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), { |
| 37 | + hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
| 38 | + hashWithLength: function(length) { |
| 39 | + return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
| 40 | + }.bind(this), |
| 41 | + chunk: { |
| 42 | + id: "\" + chunkId + \"" |
| 43 | + } |
| 44 | + }) + ");" |
63 | 45 | ]), |
64 | | - "};" |
| 46 | + "}", |
| 47 | + "return Promise.resolve();" |
65 | 48 | ]); |
66 | | - } |
67 | | - return source; |
68 | | - }); |
69 | | - mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) { |
70 | | - var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename; |
71 | | - var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename; |
72 | | - var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || "")); |
73 | | - var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), { |
74 | | - hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
75 | | - hashWithLength: function(length) { |
76 | | - return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
77 | | - }.bind(this), |
78 | | - chunk: { |
79 | | - id: "\" + chunkId + \"" |
80 | | - } |
81 | 49 | }); |
82 | | - var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), { |
83 | | - hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
84 | | - hashWithLength: function(length) { |
85 | | - return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
86 | | - }.bind(this) |
| 50 | + mainTemplate.plugin("bootstrap", function(source, chunk, hash) { |
| 51 | + if(chunk.chunks.length > 0) { |
| 52 | + var chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || "")); |
| 53 | + return this.asString([ |
| 54 | + source, |
| 55 | + "this[" + JSON.stringify(chunkCallbackName) + "] = function webpackChunkCallback(chunkIds, moreModules) {", |
| 56 | + this.indent([ |
| 57 | + "for(var moduleId in moreModules) {", |
| 58 | + this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")), |
| 59 | + "}", |
| 60 | + "while(chunkIds.length)", |
| 61 | + this.indent("installedChunks[chunkIds.pop()] = 1;") |
| 62 | + ]), |
| 63 | + "};" |
| 64 | + ]); |
| 65 | + } |
| 66 | + return source; |
87 | 67 | }); |
| 68 | + mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) { |
| 69 | + var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename; |
| 70 | + var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename; |
| 71 | + var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || "")); |
| 72 | + var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), { |
| 73 | + hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
| 74 | + hashWithLength: function(length) { |
| 75 | + return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
| 76 | + }.bind(this), |
| 77 | + chunk: { |
| 78 | + id: "\" + chunkId + \"" |
| 79 | + } |
| 80 | + }); |
| 81 | + var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), { |
| 82 | + hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
| 83 | + hashWithLength: function(length) { |
| 84 | + return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
| 85 | + }.bind(this) |
| 86 | + }); |
88 | 87 |
|
89 | | - return source + "\n" + |
90 | | - "var parentHotUpdateCallback = this[" + JSON.stringify(hotUpdateFunction) + "];\n" + |
91 | | - "this[" + JSON.stringify(hotUpdateFunction) + "] = " + Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js")) |
92 | | - .replace(/\/\/\$semicolon/g, ";") |
93 | | - .replace(/\$require\$/g, this.requireFn) |
94 | | - .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename) |
95 | | - .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename) |
96 | | - .replace(/\$hash\$/g, JSON.stringify(hash)); |
97 | | - }); |
98 | | - mainTemplate.plugin("hash", function(hash) { |
99 | | - hash.update("webworker"); |
100 | | - hash.update("3"); |
101 | | - hash.update(this.outputOptions.publicPath + ""); |
102 | | - hash.update(this.outputOptions.filename + ""); |
103 | | - hash.update(this.outputOptions.chunkFilename + ""); |
104 | | - hash.update(this.outputOptions.chunkCallbackName + ""); |
105 | | - hash.update(this.outputOptions.library + ""); |
106 | | - }); |
107 | | -}; |
| 88 | + return source + "\n" + |
| 89 | + "var parentHotUpdateCallback = this[" + JSON.stringify(hotUpdateFunction) + "];\n" + |
| 90 | + "this[" + JSON.stringify(hotUpdateFunction) + "] = " + Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js")) |
| 91 | + .replace(/\/\/\$semicolon/g, ";") |
| 92 | + .replace(/\$require\$/g, this.requireFn) |
| 93 | + .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename) |
| 94 | + .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename) |
| 95 | + .replace(/\$hash\$/g, JSON.stringify(hash)); |
| 96 | + }); |
| 97 | + mainTemplate.plugin("hash", function(hash) { |
| 98 | + hash.update("webworker"); |
| 99 | + hash.update("3"); |
| 100 | + hash.update(`${this.outputOptions.publicPath}`); |
| 101 | + hash.update(`${this.outputOptions.filename}`); |
| 102 | + hash.update(`${this.outputOptions.chunkFilename}`); |
| 103 | + hash.update(`${this.outputOptions.chunkCallbackName}`); |
| 104 | + hash.update(`${this.outputOptions.library}`); |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
| 108 | +module.exports = WebWorkerMainTemplatePlugin; |
0 commit comments