forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotModuleReplacementPlugin.js
More file actions
512 lines (468 loc) · 16 KB
/
HotModuleReplacementPlugin.js
File metadata and controls
512 lines (468 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var Template = require("./Template");
var BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
var ModuleHotAcceptDependency = require("./dependencies/ModuleHotAcceptDependency");
var ModuleHotDeclineDependency = require("./dependencies/ModuleHotDeclineDependency");
var RawSource = require("webpack-core/lib/RawSource");
function HotModuleReplacementPlugin(outputOptions) {
this.outputOptions = outputOptions;
}
module.exports = HotModuleReplacementPlugin;
HotModuleReplacementPlugin.prototype.apply = function(compiler) {
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
compiler.plugin("compilation", function(compilation, params) {
var hotUpdateChunkTemplate = compilation.compiler.hotUpdateChunkTemplate;
if(!hotUpdateChunkTemplate && !compilation.mainTemplate.renderHotModuleReplacementInit) return;
var normalModuleFactory = params.normalModuleFactory;
var contextModuleFactory = params.contextModuleFactory;
compilation.dependencyFactories.set(ModuleHotAcceptDependency, normalModuleFactory);
compilation.dependencyTemplates.set(ModuleHotAcceptDependency, new ModuleHotAcceptDependency.Template());
compilation.dependencyFactories.set(ModuleHotDeclineDependency, normalModuleFactory);
compilation.dependencyTemplates.set(ModuleHotDeclineDependency, new ModuleHotDeclineDependency.Template());
compilation.plugin("record", function(compilation, records) {
if(records.hash === this.hash) return;
records.hash = compilation.hash;
records.moduleHashs = {};
this.modules.forEach(function(module) {
var identifier = module.identifier();
var hash = require("crypto").createHash("md5");
module.updateHash(hash);
records.moduleHashs[identifier] = hash.digest("hex");
});
records.chunkHashs = {};
this.chunks.forEach(function(chunk) {
records.chunkHashs[chunk.id] = chunk.hash;
});
records.chunkModuleIds = {};
this.chunks.forEach(function(chunk) {
records.chunkModuleIds[chunk.id] = chunk.modules.map(function(m) { return m.id; });
});
});
compilation.plugin("after-hash", function() {
var records = this.records;
if(!records) return;
var lastHash = records.hash || "x";
var preHash = records.preHash || "x";
var prepreHash = records.prepreHash || "x";
if(preHash === this.hash) {
this.modifyHash(prepreHash);
return;
}
records.prepreHash = records.hash || "x";
records.preHash = this.hash;
this.modifyHash(records.prepreHash);
});
compilation.plugin("additional-chunk-assets", function() {
var records = this.records;
if(records.hash === this.hash) return;
if(!records.moduleHashs || !records.chunkHashs || !records.chunkModuleIds) return;
var moduleHashs = {};
this.modules.forEach(function(module) {
var identifier = module.identifier();
var hash = require("crypto").createHash("md5");
module.updateHash(hash);
hash = hash.digest("hex");
module.hotUpdate = records.moduleHashs[identifier] !== hash;
});
var hotUpdateMainContent = {
h: this.hash,
c: []
};
Object.keys(records.chunkHashs).forEach(function(chunkId) {
chunkId = +chunkId;
var currentChunk = this.chunks.filter(function(chunk) {
return chunk.id === chunkId;
})[0];
if(currentChunk) {
var newModules = currentChunk.modules.filter(function(module) {
return module.hotUpdate;
});
if(newModules.length > 0) {
var source = hotUpdateChunkTemplate.render(chunkId, newModules, this.hash, this.moduleTemplate, this.dependencyTemplates);
var filename = hotUpdateChunkFilename
.replace(Template.REGEXP_HASH, records.hash)
.replace(Template.REGEXP_ID, chunkId);
this.additionalChunkAssets.push(filename);
this.assets[filename] = source;
hotUpdateMainContent.c.push(chunkId);
currentChunk.files.push(filename);
this.applyPlugins("chunk-asset", currentChunk, filename);
}
}
}, this);
var source = new RawSource(JSON.stringify(hotUpdateMainContent));
var filename = hotUpdateMainFilename.replace(Template.REGEXP_HASH, records.hash);
this.assets[filename] = source;
});
var mainTemplate = compilation.mainTemplate;
compilation.mainTemplate = Object.create(mainTemplate);
compilation.mainTemplate.updateHash = function(hash) {
hash.update("HotMainTemplateDecorator");
mainTemplate.updateHash(hash);
};
compilation.mainTemplate.renderRequireFunctionForModule = function(hash, chunk, varModuleId) {
return "hotCreateRequire(" + varModuleId + ")";
};
compilation.mainTemplate.renderInit = function(hash, chunk) {
var buf = mainTemplate.renderInit(hash, chunk);
buf = buf.concat(this.renderHotModuleReplacementInit(hash, chunk));
buf.push("\n\n");
buf.push(hotInitCode
.replace(/\$require\$/g, this.requireFn)
.replace(/\$hash\$/g, JSON.stringify(hash))
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.chunks.length > 0 ? "for(var chunkId in installedChunks)" : "var chunkId = 0;"));
return buf;
};
compilation.mainTemplate.renderCurrentHashCode = function(hash) {
return "hotCurrentHash";
};
compilation.mainTemplate.renderModule = function(hash, chunk, varModuleId) {
var buf = mainTemplate.renderModule(hash, chunk, varModuleId);
buf.push(buf.pop() + ",");
buf.push("hot: hotCreateModule(" + varModuleId + "),");
buf.push("parents: [hotCurrentParent],");
buf.push("data: hotCurrentModuleData[" + varModuleId + "],");
buf.push("children: []");
return buf;
};
});
compiler.parser.plugin("evaluate Identifier module.hot", function(expr) {
return new BasicEvaluatedExpression()
.setBoolean(!!this.state.compilation.compiler.hotUpdateChunkTemplate)
.setRange(expr.range);
});
compiler.parser.plugin("call module.hot.accept", function(expr) {
if(!this.state.compilation.compiler.hotUpdateChunkTemplate) return false;
if(expr.arguments.length > 1) {
var param = this.evaluateExpression(expr.arguments[0]);
if(param.isString()) {
var dep = new ModuleHotAcceptDependency(param.string, param.range);
dep.optional = true;
this.state.module.addDependency(dep);
}
}
});
compiler.parser.plugin("call module.hot.decline", function(expr) {
if(!this.state.compilation.compiler.hotUpdateChunkTemplate) return false;
if(expr.arguments.length > 1) {
var param = this.evaluateExpression(expr.arguments[0]);
if(param.isString()) {
var dep = new ModuleHotDeclineDependency(param.string, param.range);
dep.optional = true;
this.state.module.addDependency(dep);
}
}
});
compiler.parser.plugin("expression module.hot", function() {
return true;
});
};
var hotInitCode = Template.getFunctionContent(function() {
var hotApplyOnUpdate = true;
var hotCurrentHash = $hash$;
var hotCurrentModuleData = {};
var hotCurrentParent = 0;
function hotCreateRequire(moduleId) {
var me = installedModules[moduleId];
var fn = function(request) {
if(installedModules[request] && installedModules[request].parents.indexOf(moduleId) < 0)
installedModules[request].parents.push(moduleId);
if(me && me.children.indexOf(request) < 0)
me.children.push(request);
hotCurrentParent = moduleId;
return $require$(request);
};
fn.e = function(chunkId, callback) {
if(hotStatus === "ready")
hotSetStatus("prepare");
hotChunksLoading++;
$require$.e(chunkId, function() {
try {
callback.call(null, fn);
} finally {
finishChunkLoading();
}
function finishChunkLoading() {
hotChunksLoading--;
if(hotStatus === "prepare") {
if(!hotWaitingFilesMap[chunkId]) {
hotEnsureUpdateChunk(chunkId);
}
if(hotChunksLoading === 0 && hotWaitingFiles === 0) {
hotUpdateDownloaded();
}
}
}
});
}
for(var name in $require$)
fn[name] = $require$[name];
return fn;
}
function hotCreateModule(moduleId) {
var hot = {
// private stuff
_acceptedDependencies: {},
_declinedDependencies: {},
_selfAccepted: false,
_selfDeclined: false,
_disposeHandlers: [],
// Module API
accept: function(dep, callback) {
if(typeof dep === "undefined")
hot._selfAccepted = true;
else if(typeof dep === "number")
hot._acceptedDependencies[dep] = callback;
else for(var i = 0; i < dep.length; i++)
hot._acceptedDependencies[dep[i]] = callback;
},
decline: function(dep) {
if(typeof dep === "undefined")
hot._selfDeclined = true;
else if(typeof dep === "number")
hot._declinedDependencies[dep] = true;
else for(var i = 0; i < dep.length; i++)
hot._declinedDependencies[dep[i]] = true;
},
dispose: function(callback) {
hot._disposeHandlers.push(callback);
},
addDisposeHandler: function(callback) {
hot._disposeHandlers.push(callback);
},
removeDisposeHandler: function(callback) {
var idx = hot._disposeHandlers.indexOf(callback);
if(idx >= 0) hot._disposeHandlers.splice(idx, 1);
},
// Management API
check: hotCheck,
apply: hotApply,
setApplyOnUpdate: function(applyOnUpdate) {
hotApplyOnUpdate = applyOnUpdate;
},
status: function(l) {
if(!l) return hotStatus;
hotStatusHandlers.push(l);
},
addStatusHandler: function(l) {
hotStatusHandlers.push(l);
},
removeStatusHandler: function(l) {
var idx = hotStatusHandlers.indexOf(l);
if(idx >= 0) hotStatusHandlers.splice(idx, 1);
}
};
return hot;
}
var hotStatusHandlers = [];
var hotStatus = "idle";
function hotSetStatus(newStatus) {
var oldStatus = hotStatus;
hotStatus = newStatus;
for(var i = 0; i < hotStatusHandlers.length; i++)
hotStatusHandlers[i].call(null, newStatus);
}
// while downloading
var hotWaitingFiles = 0;
var hotChunksLoading = 0;
var hotWaitingFilesMap = {};
var hotAvailibleFilesMap = {};
var hotCallback;
// The update info
var hotUpdate, hotUpdateNewHash;
function hotCheck(callback) {
callback = callback || function(err) { if(err) throw err };
if(hotStatus !== "idle") throw new Error("check() is only allowed in idle status");
hotSetStatus("check");
hotDownloadManifest(function(err, update) {
if(err) return callback(err);
if(!update) {
hotSetStatus("idle");
callback(null, null);
return;
}
hotAvailibleFilesMap = {};
hotWaitingFilesMap = {};
for(var i = 0; i < update.c.length; i++)
hotAvailibleFilesMap[update.c[i]] = true;
hotUpdateNewHash = update.h;
hotSetStatus("prepare");
hotCallback = callback;
hotUpdate = {};
/*foreachInstalledChunks*/ {
hotEnsureUpdateChunk(chunkId);
}
if(hotChunksLoading === 0 && hotWaitingFiles === 0) {
hotUpdateDownloaded();
}
});
}
function hotAddUpdateChunk(chunkId, moreModules) {
for(var moduleId in moreModules) {
hotUpdate[moduleId] = moreModules[moduleId];
}
if(--hotWaitingFiles === 0 && hotChunksLoading === 0) {
hotUpdateDownloaded();
}
}
function hotEnsureUpdateChunk(chunkId) {
if(!hotAvailibleFilesMap[chunkId]) {
hotWaitingFilesMap[chunkId] = true;
} else {
hotWaitingFiles++;
hotDownloadUpdateChunk(chunkId);
}
}
function hotUpdateDownloaded() {
hotSetStatus("ready");
var callback = hotCallback;
hotCallback = null;
if(!callback) return;
if(hotApplyOnUpdate) {
hotApply(callback);
} else {
var outdatedModules = [];
for(var id in hotUpdate) {
outdatedModules.push(+id);
}
callback(null, outdatedModules);
}
}
function hotApply(callback) {
callback = callback || function(err) { if(err) throw err };
if(hotStatus !== "ready") throw new Error("apply() is only allowed in ready status");
// at begin all updates modules are outdated
// the "outdated" status can propagate to parents if they don't accept the children
var outdatedDependencies = {};
var outdatedModules = [];
for(var id in hotUpdate) {
outdatedModules.push(+id);
}
var queue = outdatedModules.slice();
while(queue.length > 0) {
var moduleId = queue.pop();
var module = installedModules[moduleId];
if(!module || module.hot._selfAccepted)
continue;
if(module.hot._selfDeclined) {
hotSetStatus("abort");
return callback(new Error("Aborted because of self decline: " + moduleId));
}
if(moduleId === 0) {
hotSetStatus("abort");
return callback(new Error("Aborted because of bubbling"));
}
for(var i = 0; i < module.parents.length; i++) {
var parentId = module.parents[i];
var parent = installedModules[parentId];
if(parent.hot._declinedDependencies[moduleId]) {
hotSetStatus("abort");
return callback(new Error("Aborted because of declined dependency: " + moduleId + " in " + parentId));
}
if(outdatedModules.indexOf(parentId) >= 0) continue;
if(parent.hot._acceptedDependencies[moduleId]) {
if(!outdatedDependencies[parentId]) outdatedDependencies[parentId] = [];
if(outdatedDependencies[parentId].indexOf(moduleId) >= 0) continue;
outdatedDependencies[parentId].push(moduleId);
continue;
}
delete outdatedDependencies[parentId];
outdatedModules.push(parentId);
queue.push(parentId);
}
}
// Store self accepted outdated modules to require them later by the module system
var outdatedSelfAcceptedModules = [];
for(var i = 0; i < outdatedModules.length; i++) {
var moduleId = outdatedModules[i];
if(installedModules[moduleId] && installedModules[moduleId].hot._selfAccepted)
outdatedSelfAcceptedModules.push(moduleId);
}
// Now in "dispose" phase
hotSetStatus("dispose");
var queue = outdatedModules.slice();
while(queue.length > 0) {
var moduleId = queue.pop();
var module = installedModules[moduleId];
if(!module) continue;
var data = {};
// Call dispose handlers
var disposeHandlers = module.hot._disposeHandlers;
for(var j = 0; j < disposeHandlers.length; j++) {
var cb = disposeHandlers[j]
cb(data);
}
hotCurrentModuleData[moduleId] = data;
// remove module from cache
delete installedModules[moduleId];
// remove "parents" references from all children
for(var j = 0; j < module.children.length; j++) {
var child = installedModules[module.children[j]];
if(!child) continue;
var idx = child.parents.indexOf(moduleId);
if(idx >= 0) {
child.parents.splice(idx, 1);
if(child.parents.length === 0 && child.hot && child.hot._disposeHandlers && child.hot._disposeHandlers.length > 0) {
// Child has dispose handlers and no more references, dispose it too
queue.push(child.id);
}
}
}
}
// remove outdated dependency from module children
for(var moduleId in outdatedDependencies) {
var module = installedModules[moduleId];
var moduleOutdatedDependencies = outdatedDependencies[moduleId];
for(var j = 0; j < moduleOutdatedDependencies.length; j++) {
var dependency = moduleOutdatedDependencies[j];
var idx = module.children.indexOf(dependency);
if(idx >= 0) module.children.splice(idx, 1);
}
}
// Not in "apply" phase
hotSetStatus("apply");
hotCurrentHash = hotUpdateNewHash;
// insert new code
for(var moduleId in hotUpdate) {
modules[moduleId] = hotUpdate[moduleId];
}
// call accept handlers
var error = null;
for(var moduleId in outdatedDependencies) {
var module = installedModules[moduleId];
var moduleOutdatedDependencies = outdatedDependencies[moduleId];
var callbacks = [];
for(var i = 0; i < moduleOutdatedDependencies.length; i++) {
var dependency = moduleOutdatedDependencies[i];
var cb = module.hot._acceptedDependencies[dependency];
if(callbacks.indexOf(cb) >= 0) continue;
callbacks.push(cb);
}
for(var i = 0; i < callbacks.length; i++) {
var cb = callbacks[i];
try {
cb(outdatedDependencies);
} catch(err) {
if(!error)
error = err;
}
}
}
if(error) {
hotSetStatus("fail");
return callback(error);
}
// Load self accepted modules
for(var i = 0; i < outdatedSelfAcceptedModules.length; i++) {
var moduleId = outdatedSelfAcceptedModules[i];
hotCurrentParent = moduleId;
$require$(moduleId);
}
hotSetStatus("idle");
callback(null, outdatedModules);
}
});