1
|
/*
|
2
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
Author Tobias Koppers @sokra
|
4
|
*/
|
5
|
"use strict";
|
6
|
|
7
|
const { SyncBailHook } = require("tapable");
|
8
|
const { RawSource } = require("webpack-sources");
|
9
|
const Template = require("./Template");
|
10
|
const ModuleHotAcceptDependency = require("./dependencies/ModuleHotAcceptDependency");
|
11
|
const ModuleHotDeclineDependency = require("./dependencies/ModuleHotDeclineDependency");
|
12
|
const ConstDependency = require("./dependencies/ConstDependency");
|
13
|
const NullFactory = require("./NullFactory");
|
14
|
const ParserHelpers = require("./ParserHelpers");
|
15
|
|
16
|
module.exports = class HotModuleReplacementPlugin {
|
17
|
constructor(options) {
|
18
|
this.options = options || {};
|
19
|
this.multiStep = this.options.multiStep;
|
20
|
this.fullBuildTimeout = this.options.fullBuildTimeout || 200;
|
21
|
this.requestTimeout = this.options.requestTimeout || 10000;
|
22
|
}
|
23
|
|
24
|
apply(compiler) {
|
25
|
const multiStep = this.multiStep;
|
26
|
const fullBuildTimeout = this.fullBuildTimeout;
|
27
|
const requestTimeout = this.requestTimeout;
|
28
|
const hotUpdateChunkFilename =
|
29
|
compiler.options.output.hotUpdateChunkFilename;
|
30
|
const hotUpdateMainFilename = compiler.options.output.hotUpdateMainFilename;
|
31
|
compiler.hooks.additionalPass.tapAsync(
|
32
|
"HotModuleReplacementPlugin",
|
33
|
callback => {
|
34
|
if (multiStep) return setTimeout(callback, fullBuildTimeout);
|
35
|
return callback();
|
36
|
}
|
37
|
);
|
38
|
|
39
|
const addParserPlugins = (parser, parserOptions) => {
|
40
|
parser.hooks.expression
|
41
|
.for("__webpack_hash__")
|
42
|
.tap(
|
43
|
"HotModuleReplacementPlugin",
|
44
|
ParserHelpers.toConstantDependencyWithWebpackRequire(
|
45
|
parser,
|
46
|
"__webpack_require__.h()"
|
47
|
)
|
48
|
);
|
49
|
parser.hooks.evaluateTypeof
|
50
|
.for("__webpack_hash__")
|
51
|
.tap(
|
52
|
"HotModuleReplacementPlugin",
|
53
|
ParserHelpers.evaluateToString("string")
|
54
|
);
|
55
|
parser.hooks.evaluateIdentifier.for("module.hot").tap(
|
56
|
{
|
57
|
name: "HotModuleReplacementPlugin",
|
58
|
before: "NodeStuffPlugin"
|
59
|
},
|
60
|
expr => {
|
61
|
return ParserHelpers.evaluateToIdentifier(
|
62
|
"module.hot",
|
63
|
!!parser.state.compilation.hotUpdateChunkTemplate
|
64
|
)(expr);
|
65
|
}
|
66
|
);
|
67
|
// TODO webpack 5: refactor this, no custom hooks
|
68
|
if (!parser.hooks.hotAcceptCallback) {
|
69
|
parser.hooks.hotAcceptCallback = new SyncBailHook([
|
70
|
"expression",
|
71
|
"requests"
|
72
|
]);
|
73
|
}
|
74
|
if (!parser.hooks.hotAcceptWithoutCallback) {
|
75
|
parser.hooks.hotAcceptWithoutCallback = new SyncBailHook([
|
76
|
"expression",
|
77
|
"requests"
|
78
|
]);
|
79
|
}
|
80
|
parser.hooks.call
|
81
|
.for("module.hot.accept")
|
82
|
.tap("HotModuleReplacementPlugin", expr => {
|
83
|
if (!parser.state.compilation.hotUpdateChunkTemplate) {
|
84
|
return false;
|
85
|
}
|
86
|
if (expr.arguments.length >= 1) {
|
87
|
const arg = parser.evaluateExpression(expr.arguments[0]);
|
88
|
let params = [];
|
89
|
let requests = [];
|
90
|
if (arg.isString()) {
|
91
|
params = [arg];
|
92
|
} else if (arg.isArray()) {
|
93
|
params = arg.items.filter(param => param.isString());
|
94
|
}
|
95
|
if (params.length > 0) {
|
96
|
params.forEach((param, idx) => {
|
97
|
const request = param.string;
|
98
|
const dep = new ModuleHotAcceptDependency(request, param.range);
|
99
|
dep.optional = true;
|
100
|
dep.loc = Object.create(expr.loc);
|
101
|
dep.loc.index = idx;
|
102
|
parser.state.module.addDependency(dep);
|
103
|
requests.push(request);
|
104
|
});
|
105
|
if (expr.arguments.length > 1) {
|
106
|
parser.hooks.hotAcceptCallback.call(
|
107
|
expr.arguments[1],
|
108
|
requests
|
109
|
);
|
110
|
parser.walkExpression(expr.arguments[1]); // other args are ignored
|
111
|
return true;
|
112
|
} else {
|
113
|
parser.hooks.hotAcceptWithoutCallback.call(expr, requests);
|
114
|
return true;
|
115
|
}
|
116
|
}
|
117
|
}
|
118
|
});
|
119
|
parser.hooks.call
|
120
|
.for("module.hot.decline")
|
121
|
.tap("HotModuleReplacementPlugin", expr => {
|
122
|
if (!parser.state.compilation.hotUpdateChunkTemplate) {
|
123
|
return false;
|
124
|
}
|
125
|
if (expr.arguments.length === 1) {
|
126
|
const arg = parser.evaluateExpression(expr.arguments[0]);
|
127
|
let params = [];
|
128
|
if (arg.isString()) {
|
129
|
params = [arg];
|
130
|
} else if (arg.isArray()) {
|
131
|
params = arg.items.filter(param => param.isString());
|
132
|
}
|
133
|
params.forEach((param, idx) => {
|
134
|
const dep = new ModuleHotDeclineDependency(
|
135
|
param.string,
|
136
|
param.range
|
137
|
);
|
138
|
dep.optional = true;
|
139
|
dep.loc = Object.create(expr.loc);
|
140
|
dep.loc.index = idx;
|
141
|
parser.state.module.addDependency(dep);
|
142
|
});
|
143
|
}
|
144
|
});
|
145
|
parser.hooks.expression
|
146
|
.for("module.hot")
|
147
|
.tap("HotModuleReplacementPlugin", ParserHelpers.skipTraversal);
|
148
|
};
|
149
|
|
150
|
compiler.hooks.compilation.tap(
|
151
|
"HotModuleReplacementPlugin",
|
152
|
(compilation, { normalModuleFactory }) => {
|
153
|
// This applies the HMR plugin only to the targeted compiler
|
154
|
// It should not affect child compilations
|
155
|
if (compilation.compiler !== compiler) return;
|
156
|
|
157
|
const hotUpdateChunkTemplate = compilation.hotUpdateChunkTemplate;
|
158
|
if (!hotUpdateChunkTemplate) return;
|
159
|
|
160
|
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
|
161
|
compilation.dependencyTemplates.set(
|
162
|
ConstDependency,
|
163
|
new ConstDependency.Template()
|
164
|
);
|
165
|
|
166
|
compilation.dependencyFactories.set(
|
167
|
ModuleHotAcceptDependency,
|
168
|
normalModuleFactory
|
169
|
);
|
170
|
compilation.dependencyTemplates.set(
|
171
|
ModuleHotAcceptDependency,
|
172
|
new ModuleHotAcceptDependency.Template()
|
173
|
);
|
174
|
|
175
|
compilation.dependencyFactories.set(
|
176
|
ModuleHotDeclineDependency,
|
177
|
normalModuleFactory
|
178
|
);
|
179
|
compilation.dependencyTemplates.set(
|
180
|
ModuleHotDeclineDependency,
|
181
|
new ModuleHotDeclineDependency.Template()
|
182
|
);
|
183
|
|
184
|
compilation.hooks.record.tap(
|
185
|
"HotModuleReplacementPlugin",
|
186
|
(compilation, records) => {
|
187
|
if (records.hash === compilation.hash) return;
|
188
|
records.hash = compilation.hash;
|
189
|
records.moduleHashs = {};
|
190
|
for (const module of compilation.modules) {
|
191
|
const identifier = module.identifier();
|
192
|
records.moduleHashs[identifier] = module.hash;
|
193
|
}
|
194
|
records.chunkHashs = {};
|
195
|
for (const chunk of compilation.chunks) {
|
196
|
records.chunkHashs[chunk.id] = chunk.hash;
|
197
|
}
|
198
|
records.chunkModuleIds = {};
|
199
|
for (const chunk of compilation.chunks) {
|
200
|
records.chunkModuleIds[chunk.id] = Array.from(
|
201
|
chunk.modulesIterable,
|
202
|
m => m.id
|
203
|
);
|
204
|
}
|
205
|
}
|
206
|
);
|
207
|
let initialPass = false;
|
208
|
let recompilation = false;
|
209
|
compilation.hooks.afterHash.tap("HotModuleReplacementPlugin", () => {
|
210
|
let records = compilation.records;
|
211
|
if (!records) {
|
212
|
initialPass = true;
|
213
|
return;
|
214
|
}
|
215
|
if (!records.hash) initialPass = true;
|
216
|
const preHash = records.preHash || "x";
|
217
|
const prepreHash = records.prepreHash || "x";
|
218
|
if (preHash === compilation.hash) {
|
219
|
recompilation = true;
|
220
|
compilation.modifyHash(prepreHash);
|
221
|
return;
|
222
|
}
|
223
|
records.prepreHash = records.hash || "x";
|
224
|
records.preHash = compilation.hash;
|
225
|
compilation.modifyHash(records.prepreHash);
|
226
|
});
|
227
|
compilation.hooks.shouldGenerateChunkAssets.tap(
|
228
|
"HotModuleReplacementPlugin",
|
229
|
() => {
|
230
|
if (multiStep && !recompilation && !initialPass) return false;
|
231
|
}
|
232
|
);
|
233
|
compilation.hooks.needAdditionalPass.tap(
|
234
|
"HotModuleReplacementPlugin",
|
235
|
() => {
|
236
|
if (multiStep && !recompilation && !initialPass) return true;
|
237
|
}
|
238
|
);
|
239
|
compilation.hooks.additionalChunkAssets.tap(
|
240
|
"HotModuleReplacementPlugin",
|
241
|
() => {
|
242
|
const records = compilation.records;
|
243
|
if (records.hash === compilation.hash) return;
|
244
|
if (
|
245
|
!records.moduleHashs ||
|
246
|
!records.chunkHashs ||
|
247
|
!records.chunkModuleIds
|
248
|
)
|
249
|
return;
|
250
|
for (const module of compilation.modules) {
|
251
|
const identifier = module.identifier();
|
252
|
let hash = module.hash;
|
253
|
module.hotUpdate = records.moduleHashs[identifier] !== hash;
|
254
|
}
|
255
|
const hotUpdateMainContent = {
|
256
|
h: compilation.hash,
|
257
|
c: {}
|
258
|
};
|
259
|
for (const key of Object.keys(records.chunkHashs)) {
|
260
|
const chunkId = isNaN(+key) ? key : +key;
|
261
|
const currentChunk = compilation.chunks.find(
|
262
|
chunk => `${chunk.id}` === key
|
263
|
);
|
264
|
if (currentChunk) {
|
265
|
const newModules = currentChunk
|
266
|
.getModules()
|
267
|
.filter(module => module.hotUpdate);
|
268
|
const allModules = new Set();
|
269
|
for (const module of currentChunk.modulesIterable) {
|
270
|
allModules.add(module.id);
|
271
|
}
|
272
|
const removedModules = records.chunkModuleIds[chunkId].filter(
|
273
|
id => !allModules.has(id)
|
274
|
);
|
275
|
if (newModules.length > 0 || removedModules.length > 0) {
|
276
|
const source = hotUpdateChunkTemplate.render(
|
277
|
chunkId,
|
278
|
newModules,
|
279
|
removedModules,
|
280
|
compilation.hash,
|
281
|
compilation.moduleTemplates.javascript,
|
282
|
compilation.dependencyTemplates
|
283
|
);
|
284
|
const {
|
285
|
path: filename,
|
286
|
info: assetInfo
|
287
|
} = compilation.getPathWithInfo(hotUpdateChunkFilename, {
|
288
|
hash: records.hash,
|
289
|
chunk: currentChunk
|
290
|
});
|
291
|
compilation.additionalChunkAssets.push(filename);
|
292
|
compilation.emitAsset(
|
293
|
filename,
|
294
|
source,
|
295
|
Object.assign({ hotModuleReplacement: true }, assetInfo)
|
296
|
);
|
297
|
hotUpdateMainContent.c[chunkId] = true;
|
298
|
currentChunk.files.push(filename);
|
299
|
compilation.hooks.chunkAsset.call(currentChunk, filename);
|
300
|
}
|
301
|
} else {
|
302
|
hotUpdateMainContent.c[chunkId] = false;
|
303
|
}
|
304
|
}
|
305
|
const source = new RawSource(JSON.stringify(hotUpdateMainContent));
|
306
|
const {
|
307
|
path: filename,
|
308
|
info: assetInfo
|
309
|
} = compilation.getPathWithInfo(hotUpdateMainFilename, {
|
310
|
hash: records.hash
|
311
|
});
|
312
|
compilation.emitAsset(
|
313
|
filename,
|
314
|
source,
|
315
|
Object.assign({ hotModuleReplacement: true }, assetInfo)
|
316
|
);
|
317
|
}
|
318
|
);
|
319
|
|
320
|
const mainTemplate = compilation.mainTemplate;
|
321
|
|
322
|
mainTemplate.hooks.hash.tap("HotModuleReplacementPlugin", hash => {
|
323
|
hash.update("HotMainTemplateDecorator");
|
324
|
});
|
325
|
|
326
|
mainTemplate.hooks.moduleRequire.tap(
|
327
|
"HotModuleReplacementPlugin",
|
328
|
(_, chunk, hash, varModuleId) => {
|
329
|
return `hotCreateRequire(${varModuleId})`;
|
330
|
}
|
331
|
);
|
332
|
|
333
|
mainTemplate.hooks.requireExtensions.tap(
|
334
|
"HotModuleReplacementPlugin",
|
335
|
source => {
|
336
|
const buf = [source];
|
337
|
buf.push("");
|
338
|
buf.push("// __webpack_hash__");
|
339
|
buf.push(
|
340
|
mainTemplate.requireFn +
|
341
|
".h = function() { return hotCurrentHash; };"
|
342
|
);
|
343
|
return Template.asString(buf);
|
344
|
}
|
345
|
);
|
346
|
|
347
|
const needChunkLoadingCode = chunk => {
|
348
|
for (const chunkGroup of chunk.groupsIterable) {
|
349
|
if (chunkGroup.chunks.length > 1) return true;
|
350
|
if (chunkGroup.getNumberOfChildren() > 0) return true;
|
351
|
}
|
352
|
return false;
|
353
|
};
|
354
|
|
355
|
mainTemplate.hooks.bootstrap.tap(
|
356
|
"HotModuleReplacementPlugin",
|
357
|
(source, chunk, hash) => {
|
358
|
source = mainTemplate.hooks.hotBootstrap.call(source, chunk, hash);
|
359
|
return Template.asString([
|
360
|
source,
|
361
|
"",
|
362
|
hotInitCode
|
363
|
.replace(/\$require\$/g, mainTemplate.requireFn)
|
364
|
.replace(/\$hash\$/g, JSON.stringify(hash))
|
365
|
.replace(/\$requestTimeout\$/g, requestTimeout)
|
366
|
.replace(
|
367
|
/\/\*foreachInstalledChunks\*\//g,
|
368
|
needChunkLoadingCode(chunk)
|
369
|
? "for(var chunkId in installedChunks)"
|
370
|
: `var chunkId = ${JSON.stringify(chunk.id)};`
|
371
|
)
|
372
|
]);
|
373
|
}
|
374
|
);
|
375
|
|
376
|
mainTemplate.hooks.globalHash.tap(
|
377
|
"HotModuleReplacementPlugin",
|
378
|
() => true
|
379
|
);
|
380
|
|
381
|
mainTemplate.hooks.currentHash.tap(
|
382
|
"HotModuleReplacementPlugin",
|
383
|
(_, length) => {
|
384
|
if (isFinite(length)) {
|
385
|
return `hotCurrentHash.substr(0, ${length})`;
|
386
|
} else {
|
387
|
return "hotCurrentHash";
|
388
|
}
|
389
|
}
|
390
|
);
|
391
|
|
392
|
mainTemplate.hooks.moduleObj.tap(
|
393
|
"HotModuleReplacementPlugin",
|
394
|
(source, chunk, hash, varModuleId) => {
|
395
|
return Template.asString([
|
396
|
`${source},`,
|
397
|
`hot: hotCreateModule(${varModuleId}),`,
|
398
|
"parents: (hotCurrentParentsTemp = hotCurrentParents, hotCurrentParents = [], hotCurrentParentsTemp),",
|
399
|
"children: []"
|
400
|
]);
|
401
|
}
|
402
|
);
|
403
|
|
404
|
// TODO add HMR support for javascript/esm
|
405
|
normalModuleFactory.hooks.parser
|
406
|
.for("javascript/auto")
|
407
|
.tap("HotModuleReplacementPlugin", addParserPlugins);
|
408
|
normalModuleFactory.hooks.parser
|
409
|
.for("javascript/dynamic")
|
410
|
.tap("HotModuleReplacementPlugin", addParserPlugins);
|
411
|
|
412
|
compilation.hooks.normalModuleLoader.tap(
|
413
|
"HotModuleReplacementPlugin",
|
414
|
context => {
|
415
|
context.hot = true;
|
416
|
}
|
417
|
);
|
418
|
}
|
419
|
);
|
420
|
}
|
421
|
};
|
422
|
|
423
|
const hotInitCode = Template.getFunctionContent(
|
424
|
require("./HotModuleReplacement.runtime")
|
425
|
);
|