|
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 NodeMainTemplatePlugin(asyncChunkLoading) { |
8 | | - this.asyncChunkLoading = asyncChunkLoading; |
9 | | -} |
10 | | -module.exports = NodeMainTemplatePlugin; |
11 | | -NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) { |
12 | | - var self = this; |
13 | | - mainTemplate.plugin("local-vars", function(source, chunk) { |
14 | | - if(chunk.chunks.length > 0) { |
15 | | - return this.asString([ |
16 | | - source, |
17 | | - "", |
18 | | - "// object to store loaded chunks", |
19 | | - "// \"0\" means \"already loaded\"", |
20 | | - "var installedChunks = {", |
21 | | - this.indent( |
22 | | - chunk.ids.map(function(id) { |
23 | | - return id + ": 0"; |
24 | | - }).join(",\n") |
25 | | - ), |
26 | | - "};" |
27 | | - ]); |
28 | | - } |
29 | | - return source; |
30 | | - }); |
31 | | - mainTemplate.plugin("require-extensions", function(source, chunk) { |
32 | | - if(chunk.chunks.length > 0) { |
33 | | - return this.asString([ |
34 | | - source, |
35 | | - "", |
36 | | - "// uncatched error handler for webpack runtime", |
37 | | - this.requireFn + ".oe = function(err) {", |
38 | | - this.indent([ |
39 | | - "process.nextTick(function() {", |
40 | | - this.indent("throw err; // catch this error by using System.import().catch()"), |
41 | | - "});" |
42 | | - ]), |
43 | | - "};" |
44 | | - ]); |
45 | | - } |
46 | | - return source; |
47 | | - }); |
48 | | - mainTemplate.plugin("require-ensure", function(_, chunk, hash) { |
49 | | - var chunkFilename = this.outputOptions.chunkFilename; |
50 | | - var chunkMaps = chunk.getChunkMaps(); |
51 | | - var insertMoreModules = [ |
52 | | - "var moreModules = chunk.modules, chunkIds = chunk.ids;", |
53 | | - "for(var moduleId in moreModules) {", |
54 | | - this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")), |
55 | | - "}" |
56 | | - ]; |
57 | | - if(self.asyncChunkLoading) { |
58 | | - return this.asString([ |
59 | | - "// \"0\" is the signal for \"already loaded\"", |
60 | | - "if(installedChunks[chunkId] === 0)", |
61 | | - this.indent([ |
62 | | - "return Promise.resolve();" |
63 | | - ]), |
64 | | - "// array of [resolve, reject, promise] means \"currently loading\"", |
65 | | - "if(installedChunks[chunkId])", |
66 | | - this.indent([ |
67 | | - "return installedChunks[chunkId][2];" |
68 | | - ]), |
69 | | - "// load the chunk and return promise to it", |
70 | | - "var promise = new Promise(function(resolve, reject) {", |
71 | | - this.indent([ |
72 | | - "installedChunks[chunkId] = [resolve, reject];", |
73 | | - "var filename = __dirname + " + this.applyPluginsWaterfall("asset-path", JSON.stringify("/" + chunkFilename), { |
74 | | - hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
75 | | - hashWithLength: function(length) { |
76 | | - return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
77 | | - }.bind(this), |
78 | | - chunk: { |
79 | | - id: "\" + chunkId + \"", |
80 | | - hash: "\" + " + JSON.stringify(chunkMaps.hash) + "[chunkId] + \"", |
81 | | - hashWithLength: function(length) { |
82 | | - var shortChunkHashMap = {}; |
83 | | - Object.keys(chunkMaps.hash).forEach(function(chunkId) { |
84 | | - if(typeof chunkMaps.hash[chunkId] === "string") |
85 | | - shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length); |
86 | | - }); |
87 | | - return "\" + " + JSON.stringify(shortChunkHashMap) + "[chunkId] + \""; |
88 | | - }, |
89 | | - name: "\" + (" + JSON.stringify(chunkMaps.name) + "[chunkId]||chunkId) + \"" |
90 | | - } |
91 | | - }) + ";", |
92 | | - "require('fs').readFile(filename, 'utf-8', function(err, content) {", |
| 7 | +const Template = require("../Template"); |
| 8 | + |
| 9 | +module.exports = class NodeMainTemplatePlugin { |
| 10 | + constructor(asyncChunkLoading) { |
| 11 | + this.asyncChunkLoading = asyncChunkLoading; |
| 12 | + } |
| 13 | + |
| 14 | + apply(mainTemplate) { |
| 15 | + const asyncChunkLoading = this.asyncChunkLoading; |
| 16 | + mainTemplate.plugin("local-vars", function(source, chunk) { |
| 17 | + if(chunk.chunks.length > 0) { |
| 18 | + return this.asString([ |
| 19 | + source, |
| 20 | + "", |
| 21 | + "// object to store loaded chunks", |
| 22 | + "// \"0\" means \"already loaded\"", |
| 23 | + "var installedChunks = {", |
| 24 | + this.indent(chunk.ids.map((id) => `${id}: 0`).join(",\n")), |
| 25 | + "};" |
| 26 | + ]); |
| 27 | + } |
| 28 | + return source; |
| 29 | + }); |
| 30 | + mainTemplate.plugin("require-extensions", function(source, chunk) { |
| 31 | + if(chunk.chunks.length > 0) { |
| 32 | + return this.asString([ |
| 33 | + source, |
| 34 | + "", |
| 35 | + "// uncatched error handler for webpack runtime", |
| 36 | + `${this.requireFn}.oe = function(err) {`, |
93 | 37 | this.indent([ |
94 | | - "if(err) return reject(err);", |
95 | | - "var chunk = {};", |
96 | | - "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" + |
97 | | - "(chunk, require, require('path').dirname(filename), filename);" |
98 | | - ].concat(insertMoreModules).concat([ |
99 | | - "var callbacks = [];", |
100 | | - "for(var i = 0; i < chunkIds.length; i++) {", |
| 38 | + "process.nextTick(function() {", |
| 39 | + this.indent("throw err; // catch this error by using System.import().catch()"), |
| 40 | + "});" |
| 41 | + ]), |
| 42 | + "};" |
| 43 | + ]); |
| 44 | + } |
| 45 | + return source; |
| 46 | + }); |
| 47 | + mainTemplate.plugin("require-ensure", function(_, chunk, hash) { |
| 48 | + const chunkFilename = this.outputOptions.chunkFilename; |
| 49 | + const chunkMaps = chunk.getChunkMaps(); |
| 50 | + const insertMoreModules = [ |
| 51 | + "var moreModules = chunk.modules, chunkIds = chunk.ids;", |
| 52 | + "for(var moduleId in moreModules) {", |
| 53 | + this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")), |
| 54 | + "}" |
| 55 | + ]; |
| 56 | + if(asyncChunkLoading) { |
| 57 | + return this.asString([ |
| 58 | + "// \"0\" is the signal for \"already loaded\"", |
| 59 | + "if(installedChunks[chunkId] === 0)", |
| 60 | + this.indent([ |
| 61 | + "return Promise.resolve();" |
| 62 | + ]), |
| 63 | + "// array of [resolve, reject, promise] means \"currently loading\"", |
| 64 | + "if(installedChunks[chunkId])", |
| 65 | + this.indent([ |
| 66 | + "return installedChunks[chunkId][2];" |
| 67 | + ]), |
| 68 | + "// load the chunk and return promise to it", |
| 69 | + "var promise = new Promise(function(resolve, reject) {", |
| 70 | + this.indent([ |
| 71 | + "installedChunks[chunkId] = [resolve, reject];", |
| 72 | + "var filename = __dirname + " + this.applyPluginsWaterfall("asset-path", JSON.stringify(`/${chunkFilename}`), { |
| 73 | + hash: `" + ${this.renderCurrentHashCode(hash)} + "`, |
| 74 | + hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`, |
| 75 | + chunk: { |
| 76 | + id: "\" + chunkId + \"", |
| 77 | + hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`, |
| 78 | + hashWithLength: (length) => { |
| 79 | + const shortChunkHashMap = {}; |
| 80 | + Object.keys(chunkMaps.hash).forEach((chunkId) => { |
| 81 | + if(typeof chunkMaps.hash[chunkId] === "string") |
| 82 | + shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length); |
| 83 | + }); |
| 84 | + return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`; |
| 85 | + }, |
| 86 | + name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "` |
| 87 | + } |
| 88 | + }) + ";", |
| 89 | + "require('fs').readFile(filename, 'utf-8', function(err, content) {", |
101 | 90 | this.indent([ |
102 | | - "if(installedChunks[chunkIds[i]])", |
| 91 | + "if(err) return reject(err);", |
| 92 | + "var chunk = {};", |
| 93 | + "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" + |
| 94 | + "(chunk, require, require('path').dirname(filename), filename);" |
| 95 | + ].concat(insertMoreModules).concat([ |
| 96 | + "var callbacks = [];", |
| 97 | + "for(var i = 0; i < chunkIds.length; i++) {", |
103 | 98 | this.indent([ |
104 | | - "callbacks = callbacks.concat(installedChunks[chunkIds[i]][0]);" |
| 99 | + "if(installedChunks[chunkIds[i]])", |
| 100 | + this.indent([ |
| 101 | + "callbacks = callbacks.concat(installedChunks[chunkIds[i]][0]);" |
| 102 | + ]), |
| 103 | + "installedChunks[chunkIds[i]] = 0;" |
105 | 104 | ]), |
106 | | - "installedChunks[chunkIds[i]] = 0;" |
107 | | - ]), |
108 | | - "}", |
109 | | - "for(i = 0; i < callbacks.length; i++)", |
110 | | - this.indent("callbacks[i]();") |
| 105 | + "}", |
| 106 | + "for(i = 0; i < callbacks.length; i++)", |
| 107 | + this.indent("callbacks[i]();") |
| 108 | + ])), |
| 109 | + "});" |
| 110 | + ]), |
| 111 | + "});", |
| 112 | + "return installedChunks[chunkId][2] = promise;" |
| 113 | + ]); |
| 114 | + } else { |
| 115 | + const request = this.applyPluginsWaterfall("asset-path", JSON.stringify(`./${chunkFilename}`), { |
| 116 | + hash: `" + ${this.renderCurrentHashCode(hash)} + "`, |
| 117 | + hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`, |
| 118 | + chunk: { |
| 119 | + id: "\" + chunkId + \"", |
| 120 | + hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`, |
| 121 | + hashWithLength: (length) => { |
| 122 | + const shortChunkHashMap = {}; |
| 123 | + Object.keys(chunkMaps.hash).forEach((chunkId) => { |
| 124 | + if(typeof chunkMaps.hash[chunkId] === "string") |
| 125 | + shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length); |
| 126 | + }); |
| 127 | + return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`; |
| 128 | + }, |
| 129 | + name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "` |
| 130 | + } |
| 131 | + }); |
| 132 | + return this.asString([ |
| 133 | + "// \"0\" is the signal for \"already loaded\"", |
| 134 | + "if(installedChunks[chunkId] !== 0) {", |
| 135 | + this.indent([ |
| 136 | + `var chunk = require(${request});` |
| 137 | + ].concat(insertMoreModules).concat([ |
| 138 | + "for(var i = 0; i < chunkIds.length; i++)", |
| 139 | + this.indent("installedChunks[chunkIds[i]] = 0;") |
111 | 140 | ])), |
112 | | - "});" |
113 | | - ]), |
114 | | - "});", |
115 | | - "return installedChunks[chunkId][2] = promise;" |
116 | | - ]); |
117 | | - } else { |
118 | | - var request = this.applyPluginsWaterfall("asset-path", JSON.stringify("./" + chunkFilename), { |
119 | | - hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
120 | | - hashWithLength: function(length) { |
121 | | - return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
122 | | - }.bind(this), |
| 141 | + "}", |
| 142 | + "return Promise.resolve();" |
| 143 | + ]); |
| 144 | + } |
| 145 | + }); |
| 146 | + mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) { |
| 147 | + const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename; |
| 148 | + const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename; |
| 149 | + const chunkMaps = chunk.getChunkMaps(); |
| 150 | + const currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), { |
| 151 | + hash: `" + ${this.renderCurrentHashCode(hash)} + "`, |
| 152 | + hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`, |
123 | 153 | chunk: { |
124 | 154 | id: "\" + chunkId + \"", |
125 | | - hash: "\" + " + JSON.stringify(chunkMaps.hash) + "[chunkId] + \"", |
126 | | - hashWithLength: function(length) { |
127 | | - var shortChunkHashMap = {}; |
128 | | - Object.keys(chunkMaps.hash).forEach(function(chunkId) { |
| 155 | + hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`, |
| 156 | + hashWithLength: (length) => { |
| 157 | + const shortChunkHashMap = {}; |
| 158 | + Object.keys(chunkMaps.hash).forEach((chunkId) => { |
129 | 159 | if(typeof chunkMaps.hash[chunkId] === "string") |
130 | 160 | shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length); |
131 | 161 | }); |
132 | | - return "\" + " + JSON.stringify(shortChunkHashMap) + "[chunkId] + \""; |
| 162 | + return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`; |
133 | 163 | }, |
134 | | - name: "\" + (" + JSON.stringify(chunkMaps.name) + "[chunkId]||chunkId) + \"" |
| 164 | + name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "` |
135 | 165 | } |
136 | 166 | }); |
137 | | - return this.asString([ |
138 | | - "// \"0\" is the signal for \"already loaded\"", |
139 | | - "if(installedChunks[chunkId] !== 0) {", |
140 | | - this.indent([ |
141 | | - "var chunk = require(" + request + ");" |
142 | | - ].concat(insertMoreModules).concat([ |
143 | | - "for(var i = 0; i < chunkIds.length; i++)", |
144 | | - this.indent("installedChunks[chunkIds[i]] = 0;") |
145 | | - ])), |
146 | | - "}", |
147 | | - "return Promise.resolve();" |
148 | | - ]); |
149 | | - } |
150 | | - }); |
151 | | - mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) { |
152 | | - var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename; |
153 | | - var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename; |
154 | | - var chunkMaps = chunk.getChunkMaps(); |
155 | | - var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), { |
156 | | - hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
157 | | - hashWithLength: function(length) { |
158 | | - return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
159 | | - }.bind(this), |
160 | | - chunk: { |
161 | | - id: "\" + chunkId + \"", |
162 | | - hash: "\" + " + JSON.stringify(chunkMaps.hash) + "[chunkId] + \"", |
163 | | - hashWithLength: function(length) { |
164 | | - var shortChunkHashMap = {}; |
165 | | - Object.keys(chunkMaps.hash).forEach(function(chunkId) { |
166 | | - if(typeof chunkMaps.hash[chunkId] === "string") |
167 | | - shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length); |
168 | | - }); |
169 | | - return "\" + " + JSON.stringify(shortChunkHashMap) + "[chunkId] + \""; |
170 | | - }, |
171 | | - name: "\" + (" + JSON.stringify(chunkMaps.name) + "[chunkId]||chunkId) + \"" |
172 | | - } |
| 167 | + const currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), { |
| 168 | + hash: `" + ${this.renderCurrentHashCode(hash)} + "`, |
| 169 | + hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "` |
| 170 | + }); |
| 171 | + return Template.getFunctionContent(asyncChunkLoading ? require("./NodeMainTemplateAsync.runtime.js") : require("./NodeMainTemplate.runtime.js")) |
| 172 | + .replace(/\$require\$/g, this.requireFn) |
| 173 | + .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename) |
| 174 | + .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename); |
173 | 175 | }); |
174 | | - var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), { |
175 | | - hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"", |
176 | | - hashWithLength: function(length) { |
177 | | - return "\" + " + this.renderCurrentHashCode(hash, length) + " + \""; |
178 | | - }.bind(this) |
| 176 | + mainTemplate.plugin("hash", function(hash) { |
| 177 | + hash.update("node"); |
| 178 | + hash.update("3"); |
| 179 | + hash.update(this.outputOptions.filename + ""); |
| 180 | + hash.update(this.outputOptions.chunkFilename + ""); |
179 | 181 | }); |
180 | | - return Template.getFunctionContent(self.asyncChunkLoading ? require("./NodeMainTemplateAsync.runtime.js") : require("./NodeMainTemplate.runtime.js")) |
181 | | - .replace(/\$require\$/g, this.requireFn) |
182 | | - .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename) |
183 | | - .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename); |
184 | | - }); |
185 | | - mainTemplate.plugin("hash", function(hash) { |
186 | | - hash.update("node"); |
187 | | - hash.update("3"); |
188 | | - hash.update(this.outputOptions.filename + ""); |
189 | | - hash.update(this.outputOptions.chunkFilename + ""); |
190 | | - }); |
| 182 | + } |
191 | 183 | }; |
0 commit comments