webpack 4.1.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +719 -721
  2. package/bin/webpack.js +69 -10
  3. package/lib/APIPlugin.js +84 -84
  4. package/lib/AmdMainTemplatePlugin.js +75 -77
  5. package/lib/AsyncDependencyToInitialChunkError.js +21 -23
  6. package/lib/BannerPlugin.js +101 -101
  7. package/lib/Chunk.js +477 -469
  8. package/lib/ChunkTemplate.js +51 -53
  9. package/lib/Compilation.js +1858 -1851
  10. package/lib/Compiler.js +493 -478
  11. package/lib/ConcurrentCompilationError.js +19 -0
  12. package/lib/ContextModule.js +696 -685
  13. package/lib/ContextModuleFactory.js +245 -243
  14. package/lib/DefinePlugin.js +197 -197
  15. package/lib/DelegatedModule.js +101 -101
  16. package/lib/DependenciesBlockVariable.js +51 -52
  17. package/lib/Dependency.js +53 -52
  18. package/lib/DllModule.js +54 -54
  19. package/lib/DllModuleFactory.js +29 -29
  20. package/lib/EnvironmentPlugin.js +65 -67
  21. package/lib/EvalDevToolModuleTemplatePlugin.js +60 -60
  22. package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +105 -105
  23. package/lib/ExportPropertyMainTemplatePlugin.js +40 -40
  24. package/lib/ExternalModule.js +159 -159
  25. package/lib/FunctionModuleTemplatePlugin.js +98 -98
  26. package/lib/HotModuleReplacement.runtime.js +631 -631
  27. package/lib/HotModuleReplacementPlugin.js +407 -406
  28. package/lib/HotUpdateChunkTemplate.js +78 -80
  29. package/lib/JavascriptGenerator.js +228 -229
  30. package/lib/JavascriptModulesPlugin.js +184 -158
  31. package/lib/JsonGenerator.js +42 -42
  32. package/lib/MainTemplate.js +406 -402
  33. package/lib/Module.js +343 -340
  34. package/lib/ModuleBuildError.js +42 -42
  35. package/lib/ModuleError.js +28 -28
  36. package/lib/ModuleFilenameHelpers.js +166 -166
  37. package/lib/ModuleTemplate.js +77 -79
  38. package/lib/ModuleWarning.js +30 -30
  39. package/lib/MultiCompiler.js +271 -259
  40. package/lib/MultiModule.js +78 -75
  41. package/lib/MultiModuleFactory.js +23 -23
  42. package/lib/MultiWatching.js +38 -37
  43. package/lib/NoModeWarning.js +23 -21
  44. package/lib/NormalModule.js +478 -470
  45. package/lib/NormalModuleFactory.js +483 -481
  46. package/lib/OptionsDefaulter.js +80 -86
  47. package/lib/Parser.js +2074 -2071
  48. package/lib/ProgressPlugin.js +231 -231
  49. package/lib/RawModule.js +54 -55
  50. package/lib/RecordIdsPlugin.js +160 -160
  51. package/lib/RemovedPluginError.js +13 -13
  52. package/lib/ResolverFactory.js +64 -67
  53. package/lib/RuntimeTemplate.js +267 -297
  54. package/lib/SetVarMainTemplatePlugin.js +57 -57
  55. package/lib/SourceMapDevToolPlugin.js +302 -308
  56. package/lib/Stats.js +1234 -1212
  57. package/lib/Template.js +205 -205
  58. package/lib/TemplatedPathPlugin.js +170 -143
  59. package/lib/UmdMainTemplatePlugin.js +264 -269
  60. package/lib/Watching.js +193 -193
  61. package/lib/WebAssemblyParser.js +50 -54
  62. package/lib/WebpackOptionsApply.js +401 -401
  63. package/lib/WebpackOptionsDefaulter.js +337 -317
  64. package/lib/WebpackOptionsValidationError.js +316 -319
  65. package/lib/debug/ProfilingPlugin.js +409 -405
  66. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +328 -311
  67. package/lib/dependencies/AMDRequireContextDependency.js +20 -20
  68. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +270 -241
  69. package/lib/dependencies/HarmonyAcceptImportDependency.js +23 -23
  70. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +620 -606
  71. package/lib/dependencies/HarmonyExportSpecifierDependency.js +53 -53
  72. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +214 -214
  73. package/lib/dependencies/HarmonyImportSpecifierDependency.js +154 -156
  74. package/lib/dependencies/ImportDependenciesBlock.js +17 -17
  75. package/lib/dependencies/ImportDependency.js +34 -34
  76. package/lib/dependencies/ImportEagerDependency.js +32 -32
  77. package/lib/dependencies/ImportParserPlugin.js +175 -179
  78. package/lib/dependencies/ImportWeakDependency.js +34 -34
  79. package/lib/dependencies/JsonExportsDependency.js +25 -25
  80. package/lib/dependencies/ModuleDependency.js +20 -20
  81. package/lib/dependencies/NullDependency.js +20 -20
  82. package/lib/dependencies/RequireContextDependency.js +22 -22
  83. package/lib/dependencies/RequireIncludeDependency.js +40 -40
  84. package/lib/dependencies/WebpackMissingModule.js +20 -22
  85. package/lib/node/NodeChunkTemplatePlugin.js +31 -31
  86. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -36
  87. package/lib/node/NodeMainTemplatePlugin.js +320 -273
  88. package/lib/node/ReadFileCompileWasmMainTemplatePlugin.js +113 -115
  89. package/lib/optimize/AggressiveSplittingPlugin.js +281 -281
  90. package/lib/optimize/ConcatenatedModule.js +1364 -1366
  91. package/lib/optimize/RemoveParentModulesPlugin.js +114 -114
  92. package/lib/optimize/SplitChunksPlugin.js +519 -491
  93. package/lib/performance/SizeLimitsPlugin.js +105 -105
  94. package/lib/util/TrackingSet.js +35 -35
  95. package/lib/util/objectToMap.js +10 -10
  96. package/lib/wasm/WasmModuleTemplatePlugin.js +106 -106
  97. package/lib/web/JsonpChunkTemplatePlugin.js +47 -47
  98. package/lib/web/JsonpExportMainTemplatePlugin.js +47 -47
  99. package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -39
  100. package/lib/web/JsonpMainTemplatePlugin.js +425 -403
  101. package/lib/webpack.js +182 -179
  102. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -35
  103. package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -40
  104. package/lib/webworker/WebWorkerMainTemplatePlugin.js +177 -154
  105. package/package.json +9 -8
  106. package/schemas/WebpackOptions.json +1973 -1951
  107. package/schemas/ajv.absolutePath.js +55 -29
  108. package/schemas/plugins/BannerPlugin.json +85 -85
  109. package/schemas/plugins/DllPlugin.json +28 -28
  110. package/schemas/plugins/DllReferencePlugin.json +99 -99
  111. package/schemas/plugins/HashedModuleIdsPlugin.json +24 -24
  112. package/schemas/plugins/LoaderOptionsPlugin.json +26 -26
  113. package/schemas/plugins/SourceMapDevToolPlugin.json +187 -187
  114. package/schemas/plugins/WatchIgnorePlugin.json +16 -16
  115. package/schemas/plugins/debug/ProfilingPlugin.json +12 -12
  116. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +22 -22
  117. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +15 -15
  118. package/schemas/plugins/optimize/MinChunkSizePlugin.json +13 -13
@@ -1,406 +1,407 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
- const Template = require("./Template");
7
- const ModuleHotAcceptDependency = require("./dependencies/ModuleHotAcceptDependency");
8
- const ModuleHotDeclineDependency = require("./dependencies/ModuleHotDeclineDependency");
9
- const RawSource = require("webpack-sources").RawSource;
10
- const ConstDependency = require("./dependencies/ConstDependency");
11
- const NullFactory = require("./NullFactory");
12
- const ParserHelpers = require("./ParserHelpers");
13
- const createHash = require("./util/createHash");
14
- const SyncBailHook = require("tapable").SyncBailHook;
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
- compiler.hooks.compilation.tap(
39
- "HotModuleReplacementPlugin",
40
- (compilation, { normalModuleFactory }) => {
41
- const hotUpdateChunkTemplate = compilation.hotUpdateChunkTemplate;
42
- if (!hotUpdateChunkTemplate) return;
43
-
44
- compilation.dependencyFactories.set(ConstDependency, new NullFactory());
45
- compilation.dependencyTemplates.set(
46
- ConstDependency,
47
- new ConstDependency.Template()
48
- );
49
-
50
- compilation.dependencyFactories.set(
51
- ModuleHotAcceptDependency,
52
- normalModuleFactory
53
- );
54
- compilation.dependencyTemplates.set(
55
- ModuleHotAcceptDependency,
56
- new ModuleHotAcceptDependency.Template()
57
- );
58
-
59
- compilation.dependencyFactories.set(
60
- ModuleHotDeclineDependency,
61
- normalModuleFactory
62
- );
63
- compilation.dependencyTemplates.set(
64
- ModuleHotDeclineDependency,
65
- new ModuleHotDeclineDependency.Template()
66
- );
67
-
68
- compilation.hooks.record.tap(
69
- "HotModuleReplacementPlugin",
70
- (compilation, records) => {
71
- if (records.hash === compilation.hash) return;
72
- records.hash = compilation.hash;
73
- records.moduleHashs = {};
74
- for (const module of compilation.modules) {
75
- const identifier = module.identifier();
76
- const hash = createHash(compilation.outputOptions.hashFunction);
77
- module.updateHash(hash);
78
- records.moduleHashs[identifier] = hash.digest("hex");
79
- }
80
- records.chunkHashs = {};
81
- for (const chunk of compilation.chunks) {
82
- records.chunkHashs[chunk.id] = chunk.hash;
83
- }
84
- records.chunkModuleIds = {};
85
- for (const chunk of compilation.chunks) {
86
- records.chunkModuleIds[chunk.id] = Array.from(
87
- chunk.modulesIterable,
88
- m => m.id
89
- );
90
- }
91
- }
92
- );
93
- let initialPass = false;
94
- let recompilation = false;
95
- compilation.hooks.afterHash.tap("HotModuleReplacementPlugin", () => {
96
- let records = compilation.records;
97
- if (!records) {
98
- initialPass = true;
99
- return;
100
- }
101
- if (!records.hash) initialPass = true;
102
- const preHash = records.preHash || "x";
103
- const prepreHash = records.prepreHash || "x";
104
- if (preHash === compilation.hash) {
105
- recompilation = true;
106
- compilation.modifyHash(prepreHash);
107
- return;
108
- }
109
- records.prepreHash = records.hash || "x";
110
- records.preHash = compilation.hash;
111
- compilation.modifyHash(records.prepreHash);
112
- });
113
- compilation.hooks.shouldGenerateChunkAssets.tap(
114
- "HotModuleReplacementPlugin",
115
- () => {
116
- if (multiStep && !recompilation && !initialPass) return false;
117
- }
118
- );
119
- compilation.hooks.needAdditionalPass.tap(
120
- "HotModuleReplacementPlugin",
121
- () => {
122
- if (multiStep && !recompilation && !initialPass) return true;
123
- }
124
- );
125
- compilation.hooks.additionalChunkAssets.tap(
126
- "HotModuleReplacementPlugin",
127
- () => {
128
- const records = compilation.records;
129
- if (records.hash === compilation.hash) return;
130
- if (
131
- !records.moduleHashs ||
132
- !records.chunkHashs ||
133
- !records.chunkModuleIds
134
- )
135
- return;
136
- for (const module of compilation.modules) {
137
- const identifier = module.identifier();
138
- let hash = createHash(compilation.outputOptions.hashFunction);
139
- module.updateHash(hash);
140
- hash = hash.digest("hex");
141
- module.hotUpdate = records.moduleHashs[identifier] !== hash;
142
- }
143
- const hotUpdateMainContent = {
144
- h: compilation.hash,
145
- c: {}
146
- };
147
- for (let chunkId of Object.keys(records.chunkHashs)) {
148
- chunkId = isNaN(+chunkId) ? chunkId : +chunkId;
149
- const currentChunk = compilation.chunks.find(
150
- chunk => chunk.id === chunkId
151
- );
152
- if (currentChunk) {
153
- const newModules = currentChunk
154
- .getModules()
155
- .filter(module => module.hotUpdate);
156
- const allModules = new Set();
157
- for (const module of currentChunk.modulesIterable) {
158
- allModules.add(module.id);
159
- }
160
- const removedModules = records.chunkModuleIds[chunkId].filter(
161
- id => !allModules.has(id)
162
- );
163
- if (newModules.length > 0 || removedModules.length > 0) {
164
- const source = hotUpdateChunkTemplate.render(
165
- chunkId,
166
- newModules,
167
- removedModules,
168
- compilation.hash,
169
- compilation.moduleTemplates.javascript,
170
- compilation.dependencyTemplates
171
- );
172
- const filename = compilation.getPath(hotUpdateChunkFilename, {
173
- hash: records.hash,
174
- chunk: currentChunk
175
- });
176
- compilation.additionalChunkAssets.push(filename);
177
- compilation.assets[filename] = source;
178
- hotUpdateMainContent.c[chunkId] = true;
179
- currentChunk.files.push(filename);
180
- compilation.hooks.chunkAsset.call(
181
- "HotModuleReplacementPlugin",
182
- currentChunk,
183
- filename
184
- );
185
- }
186
- } else {
187
- hotUpdateMainContent.c[chunkId] = false;
188
- }
189
- }
190
- const source = new RawSource(JSON.stringify(hotUpdateMainContent));
191
- const filename = compilation.getPath(hotUpdateMainFilename, {
192
- hash: records.hash
193
- });
194
- compilation.assets[filename] = source;
195
- }
196
- );
197
-
198
- const mainTemplate = compilation.mainTemplate;
199
-
200
- mainTemplate.hooks.hash.tap("HotModuleReplacementPlugin", hash => {
201
- hash.update("HotMainTemplateDecorator");
202
- });
203
-
204
- mainTemplate.hooks.moduleRequire.tap(
205
- "HotModuleReplacementPlugin",
206
- (_, chunk, hash, varModuleId) => {
207
- return `hotCreateRequire(${varModuleId})`;
208
- }
209
- );
210
-
211
- mainTemplate.hooks.requireExtensions.tap(
212
- "HotModuleReplacementPlugin",
213
- source => {
214
- const buf = [source];
215
- buf.push("");
216
- buf.push("// __webpack_hash__");
217
- buf.push(
218
- mainTemplate.requireFn +
219
- ".h = function() { return hotCurrentHash; };"
220
- );
221
- return Template.asString(buf);
222
- }
223
- );
224
-
225
- const needChunkLoadingCode = chunk => {
226
- for (const chunkGroup of chunk.groupsIterable) {
227
- if (chunkGroup.chunks.length > 1) return true;
228
- if (chunkGroup.getNumberOfChildren() > 0) return true;
229
- }
230
- return false;
231
- };
232
-
233
- mainTemplate.hooks.bootstrap.tap(
234
- "HotModuleReplacementPlugin",
235
- (source, chunk, hash) => {
236
- source = mainTemplate.hooks.hotBootstrap.call(source, chunk, hash);
237
- return Template.asString([
238
- source,
239
- "",
240
- hotInitCode
241
- .replace(/\$require\$/g, mainTemplate.requireFn)
242
- .replace(/\$hash\$/g, JSON.stringify(hash))
243
- .replace(/\$requestTimeout\$/g, requestTimeout)
244
- .replace(
245
- /\/\*foreachInstalledChunks\*\//g,
246
- needChunkLoadingCode(chunk)
247
- ? "for(var chunkId in installedChunks)"
248
- : `var chunkId = ${JSON.stringify(chunk.id)};`
249
- )
250
- ]);
251
- }
252
- );
253
-
254
- mainTemplate.hooks.globalHash.tap(
255
- "HotModuleReplacementPlugin",
256
- () => true
257
- );
258
-
259
- mainTemplate.hooks.currentHash.tap(
260
- "HotModuleReplacementPlugin",
261
- (_, length) => {
262
- if (isFinite(length)) return `hotCurrentHash.substr(0, ${length})`;
263
- else return "hotCurrentHash";
264
- }
265
- );
266
-
267
- mainTemplate.hooks.moduleObj.tap(
268
- "HotModuleReplacementPlugin",
269
- (source, chunk, hash, varModuleId) => {
270
- return Template.asString([
271
- `${source},`,
272
- `hot: hotCreateModule(${varModuleId}),`,
273
- "parents: (hotCurrentParentsTemp = hotCurrentParents, hotCurrentParents = [], hotCurrentParentsTemp),",
274
- "children: []"
275
- ]);
276
- }
277
- );
278
-
279
- const handler = (parser, parserOptions) => {
280
- parser.hooks.expression
281
- .for("__webpack_hash__")
282
- .tap(
283
- "HotModuleReplacementPlugin",
284
- ParserHelpers.toConstantDependencyWithWebpackRequire(
285
- parser,
286
- "__webpack_require__.h()"
287
- )
288
- );
289
- parser.hooks.evaluateTypeof
290
- .for("__webpack_hash__")
291
- .tap(
292
- "HotModuleReplacementPlugin",
293
- ParserHelpers.evaluateToString("string")
294
- );
295
- parser.hooks.evaluateIdentifier.for("module.hot").tap(
296
- {
297
- name: "HotModuleReplacementPlugin",
298
- before: "NodeStuffPlugin"
299
- },
300
- expr => {
301
- return ParserHelpers.evaluateToIdentifier(
302
- "module.hot",
303
- !!parser.state.compilation.hotUpdateChunkTemplate
304
- )(expr);
305
- }
306
- );
307
- // TODO webpack 5: refactor this, no custom hooks
308
- if (!parser.hooks.hotAcceptCallback)
309
- parser.hooks.hotAcceptCallback = new SyncBailHook([
310
- "expression",
311
- "requests"
312
- ]);
313
- if (!parser.hooks.hotAcceptWithoutCallback)
314
- parser.hooks.hotAcceptWithoutCallback = new SyncBailHook([
315
- "expression",
316
- "requests"
317
- ]);
318
- parser.hooks.call
319
- .for("module.hot.accept")
320
- .tap("HotModuleReplacementPlugin", expr => {
321
- if (!parser.state.compilation.hotUpdateChunkTemplate)
322
- return false;
323
- if (expr.arguments.length >= 1) {
324
- const arg = parser.evaluateExpression(expr.arguments[0]);
325
- let params = [];
326
- let requests = [];
327
- if (arg.isString()) {
328
- params = [arg];
329
- } else if (arg.isArray()) {
330
- params = arg.items.filter(param => param.isString());
331
- }
332
- if (params.length > 0) {
333
- params.forEach((param, idx) => {
334
- const request = param.string;
335
- const dep = new ModuleHotAcceptDependency(
336
- request,
337
- param.range
338
- );
339
- dep.optional = true;
340
- dep.loc = Object.create(expr.loc);
341
- dep.loc.index = idx;
342
- parser.state.module.addDependency(dep);
343
- requests.push(request);
344
- });
345
- if (expr.arguments.length > 1)
346
- parser.hooks.hotAcceptCallback.call(
347
- expr.arguments[1],
348
- requests
349
- );
350
- else
351
- parser.hooks.hotAcceptWithoutCallback.call(expr, requests);
352
- }
353
- }
354
- });
355
- parser.hooks.call
356
- .for("module.hot.decline")
357
- .tap("HotModuleReplacementPlugin", expr => {
358
- if (!parser.state.compilation.hotUpdateChunkTemplate)
359
- return false;
360
- if (expr.arguments.length === 1) {
361
- const arg = parser.evaluateExpression(expr.arguments[0]);
362
- let params = [];
363
- if (arg.isString()) {
364
- params = [arg];
365
- } else if (arg.isArray()) {
366
- params = arg.items.filter(param => param.isString());
367
- }
368
- params.forEach((param, idx) => {
369
- const dep = new ModuleHotDeclineDependency(
370
- param.string,
371
- param.range
372
- );
373
- dep.optional = true;
374
- dep.loc = Object.create(expr.loc);
375
- dep.loc.index = idx;
376
- parser.state.module.addDependency(dep);
377
- });
378
- }
379
- });
380
- parser.hooks.expression
381
- .for("module.hot")
382
- .tap("HotModuleReplacementPlugin", ParserHelpers.skipTraversal);
383
- };
384
-
385
- // TODO add HMR support for javascript/esm
386
- normalModuleFactory.hooks.parser
387
- .for("javascript/auto")
388
- .tap("HotModuleReplacementPlugin", handler);
389
- normalModuleFactory.hooks.parser
390
- .for("javascript/dynamic")
391
- .tap("HotModuleReplacementPlugin", handler);
392
-
393
- compilation.hooks.normalModuleLoader.tap(
394
- "HotModuleReplacementPlugin",
395
- context => {
396
- context.hot = true;
397
- }
398
- );
399
- }
400
- );
401
- }
402
- };
403
-
404
- const hotInitCode = Template.getFunctionContent(
405
- require("./HotModuleReplacement.runtime.js")
406
- );
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
+ const createHash = require("./util/createHash");
16
+
17
+ module.exports = class HotModuleReplacementPlugin {
18
+ constructor(options) {
19
+ this.options = options || {};
20
+ this.multiStep = this.options.multiStep;
21
+ this.fullBuildTimeout = this.options.fullBuildTimeout || 200;
22
+ this.requestTimeout = this.options.requestTimeout || 10000;
23
+ }
24
+
25
+ apply(compiler) {
26
+ const multiStep = this.multiStep;
27
+ const fullBuildTimeout = this.fullBuildTimeout;
28
+ const requestTimeout = this.requestTimeout;
29
+ const hotUpdateChunkFilename =
30
+ compiler.options.output.hotUpdateChunkFilename;
31
+ const hotUpdateMainFilename = compiler.options.output.hotUpdateMainFilename;
32
+ compiler.hooks.additionalPass.tapAsync(
33
+ "HotModuleReplacementPlugin",
34
+ callback => {
35
+ if (multiStep) return setTimeout(callback, fullBuildTimeout);
36
+ return callback();
37
+ }
38
+ );
39
+ compiler.hooks.compilation.tap(
40
+ "HotModuleReplacementPlugin",
41
+ (compilation, { normalModuleFactory }) => {
42
+ const hotUpdateChunkTemplate = compilation.hotUpdateChunkTemplate;
43
+ if (!hotUpdateChunkTemplate) return;
44
+
45
+ compilation.dependencyFactories.set(ConstDependency, new NullFactory());
46
+ compilation.dependencyTemplates.set(
47
+ ConstDependency,
48
+ new ConstDependency.Template()
49
+ );
50
+
51
+ compilation.dependencyFactories.set(
52
+ ModuleHotAcceptDependency,
53
+ normalModuleFactory
54
+ );
55
+ compilation.dependencyTemplates.set(
56
+ ModuleHotAcceptDependency,
57
+ new ModuleHotAcceptDependency.Template()
58
+ );
59
+
60
+ compilation.dependencyFactories.set(
61
+ ModuleHotDeclineDependency,
62
+ normalModuleFactory
63
+ );
64
+ compilation.dependencyTemplates.set(
65
+ ModuleHotDeclineDependency,
66
+ new ModuleHotDeclineDependency.Template()
67
+ );
68
+
69
+ compilation.hooks.record.tap(
70
+ "HotModuleReplacementPlugin",
71
+ (compilation, records) => {
72
+ if (records.hash === compilation.hash) return;
73
+ records.hash = compilation.hash;
74
+ records.moduleHashs = {};
75
+ for (const module of compilation.modules) {
76
+ const identifier = module.identifier();
77
+ const hash = createHash(compilation.outputOptions.hashFunction);
78
+ module.updateHash(hash);
79
+ records.moduleHashs[identifier] = hash.digest("hex");
80
+ }
81
+ records.chunkHashs = {};
82
+ for (const chunk of compilation.chunks) {
83
+ records.chunkHashs[chunk.id] = chunk.hash;
84
+ }
85
+ records.chunkModuleIds = {};
86
+ for (const chunk of compilation.chunks) {
87
+ records.chunkModuleIds[chunk.id] = Array.from(
88
+ chunk.modulesIterable,
89
+ m => m.id
90
+ );
91
+ }
92
+ }
93
+ );
94
+ let initialPass = false;
95
+ let recompilation = false;
96
+ compilation.hooks.afterHash.tap("HotModuleReplacementPlugin", () => {
97
+ let records = compilation.records;
98
+ if (!records) {
99
+ initialPass = true;
100
+ return;
101
+ }
102
+ if (!records.hash) initialPass = true;
103
+ const preHash = records.preHash || "x";
104
+ const prepreHash = records.prepreHash || "x";
105
+ if (preHash === compilation.hash) {
106
+ recompilation = true;
107
+ compilation.modifyHash(prepreHash);
108
+ return;
109
+ }
110
+ records.prepreHash = records.hash || "x";
111
+ records.preHash = compilation.hash;
112
+ compilation.modifyHash(records.prepreHash);
113
+ });
114
+ compilation.hooks.shouldGenerateChunkAssets.tap(
115
+ "HotModuleReplacementPlugin",
116
+ () => {
117
+ if (multiStep && !recompilation && !initialPass) return false;
118
+ }
119
+ );
120
+ compilation.hooks.needAdditionalPass.tap(
121
+ "HotModuleReplacementPlugin",
122
+ () => {
123
+ if (multiStep && !recompilation && !initialPass) return true;
124
+ }
125
+ );
126
+ compilation.hooks.additionalChunkAssets.tap(
127
+ "HotModuleReplacementPlugin",
128
+ () => {
129
+ const records = compilation.records;
130
+ if (records.hash === compilation.hash) return;
131
+ if (
132
+ !records.moduleHashs ||
133
+ !records.chunkHashs ||
134
+ !records.chunkModuleIds
135
+ )
136
+ return;
137
+ for (const module of compilation.modules) {
138
+ const identifier = module.identifier();
139
+ let hash = createHash(compilation.outputOptions.hashFunction);
140
+ module.updateHash(hash);
141
+ hash = hash.digest("hex");
142
+ module.hotUpdate = records.moduleHashs[identifier] !== hash;
143
+ }
144
+ const hotUpdateMainContent = {
145
+ h: compilation.hash,
146
+ c: {}
147
+ };
148
+ for (let chunkId of Object.keys(records.chunkHashs)) {
149
+ chunkId = isNaN(+chunkId) ? chunkId : +chunkId;
150
+ const currentChunk = compilation.chunks.find(
151
+ chunk => chunk.id === chunkId
152
+ );
153
+ if (currentChunk) {
154
+ const newModules = currentChunk
155
+ .getModules()
156
+ .filter(module => module.hotUpdate);
157
+ const allModules = new Set();
158
+ for (const module of currentChunk.modulesIterable) {
159
+ allModules.add(module.id);
160
+ }
161
+ const removedModules = records.chunkModuleIds[chunkId].filter(
162
+ id => !allModules.has(id)
163
+ );
164
+ if (newModules.length > 0 || removedModules.length > 0) {
165
+ const source = hotUpdateChunkTemplate.render(
166
+ chunkId,
167
+ newModules,
168
+ removedModules,
169
+ compilation.hash,
170
+ compilation.moduleTemplates.javascript,
171
+ compilation.dependencyTemplates
172
+ );
173
+ const filename = compilation.getPath(hotUpdateChunkFilename, {
174
+ hash: records.hash,
175
+ chunk: currentChunk
176
+ });
177
+ compilation.additionalChunkAssets.push(filename);
178
+ compilation.assets[filename] = source;
179
+ hotUpdateMainContent.c[chunkId] = true;
180
+ currentChunk.files.push(filename);
181
+ compilation.hooks.chunkAsset.call(
182
+ "HotModuleReplacementPlugin",
183
+ currentChunk,
184
+ filename
185
+ );
186
+ }
187
+ } else {
188
+ hotUpdateMainContent.c[chunkId] = false;
189
+ }
190
+ }
191
+ const source = new RawSource(JSON.stringify(hotUpdateMainContent));
192
+ const filename = compilation.getPath(hotUpdateMainFilename, {
193
+ hash: records.hash
194
+ });
195
+ compilation.assets[filename] = source;
196
+ }
197
+ );
198
+
199
+ const mainTemplate = compilation.mainTemplate;
200
+
201
+ mainTemplate.hooks.hash.tap("HotModuleReplacementPlugin", hash => {
202
+ hash.update("HotMainTemplateDecorator");
203
+ });
204
+
205
+ mainTemplate.hooks.moduleRequire.tap(
206
+ "HotModuleReplacementPlugin",
207
+ (_, chunk, hash, varModuleId) => {
208
+ return `hotCreateRequire(${varModuleId})`;
209
+ }
210
+ );
211
+
212
+ mainTemplate.hooks.requireExtensions.tap(
213
+ "HotModuleReplacementPlugin",
214
+ source => {
215
+ const buf = [source];
216
+ buf.push("");
217
+ buf.push("// __webpack_hash__");
218
+ buf.push(
219
+ mainTemplate.requireFn +
220
+ ".h = function() { return hotCurrentHash; };"
221
+ );
222
+ return Template.asString(buf);
223
+ }
224
+ );
225
+
226
+ const needChunkLoadingCode = chunk => {
227
+ for (const chunkGroup of chunk.groupsIterable) {
228
+ if (chunkGroup.chunks.length > 1) return true;
229
+ if (chunkGroup.getNumberOfChildren() > 0) return true;
230
+ }
231
+ return false;
232
+ };
233
+
234
+ mainTemplate.hooks.bootstrap.tap(
235
+ "HotModuleReplacementPlugin",
236
+ (source, chunk, hash) => {
237
+ source = mainTemplate.hooks.hotBootstrap.call(source, chunk, hash);
238
+ return Template.asString([
239
+ source,
240
+ "",
241
+ hotInitCode
242
+ .replace(/\$require\$/g, mainTemplate.requireFn)
243
+ .replace(/\$hash\$/g, JSON.stringify(hash))
244
+ .replace(/\$requestTimeout\$/g, requestTimeout)
245
+ .replace(
246
+ /\/\*foreachInstalledChunks\*\//g,
247
+ needChunkLoadingCode(chunk)
248
+ ? "for(var chunkId in installedChunks)"
249
+ : `var chunkId = ${JSON.stringify(chunk.id)};`
250
+ )
251
+ ]);
252
+ }
253
+ );
254
+
255
+ mainTemplate.hooks.globalHash.tap(
256
+ "HotModuleReplacementPlugin",
257
+ () => true
258
+ );
259
+
260
+ mainTemplate.hooks.currentHash.tap(
261
+ "HotModuleReplacementPlugin",
262
+ (_, length) => {
263
+ if (isFinite(length)) return `hotCurrentHash.substr(0, ${length})`;
264
+ else return "hotCurrentHash";
265
+ }
266
+ );
267
+
268
+ mainTemplate.hooks.moduleObj.tap(
269
+ "HotModuleReplacementPlugin",
270
+ (source, chunk, hash, varModuleId) => {
271
+ return Template.asString([
272
+ `${source},`,
273
+ `hot: hotCreateModule(${varModuleId}),`,
274
+ "parents: (hotCurrentParentsTemp = hotCurrentParents, hotCurrentParents = [], hotCurrentParentsTemp),",
275
+ "children: []"
276
+ ]);
277
+ }
278
+ );
279
+
280
+ const handler = (parser, parserOptions) => {
281
+ parser.hooks.expression
282
+ .for("__webpack_hash__")
283
+ .tap(
284
+ "HotModuleReplacementPlugin",
285
+ ParserHelpers.toConstantDependencyWithWebpackRequire(
286
+ parser,
287
+ "__webpack_require__.h()"
288
+ )
289
+ );
290
+ parser.hooks.evaluateTypeof
291
+ .for("__webpack_hash__")
292
+ .tap(
293
+ "HotModuleReplacementPlugin",
294
+ ParserHelpers.evaluateToString("string")
295
+ );
296
+ parser.hooks.evaluateIdentifier.for("module.hot").tap(
297
+ {
298
+ name: "HotModuleReplacementPlugin",
299
+ before: "NodeStuffPlugin"
300
+ },
301
+ expr => {
302
+ return ParserHelpers.evaluateToIdentifier(
303
+ "module.hot",
304
+ !!parser.state.compilation.hotUpdateChunkTemplate
305
+ )(expr);
306
+ }
307
+ );
308
+ // TODO webpack 5: refactor this, no custom hooks
309
+ if (!parser.hooks.hotAcceptCallback)
310
+ parser.hooks.hotAcceptCallback = new SyncBailHook([
311
+ "expression",
312
+ "requests"
313
+ ]);
314
+ if (!parser.hooks.hotAcceptWithoutCallback)
315
+ parser.hooks.hotAcceptWithoutCallback = new SyncBailHook([
316
+ "expression",
317
+ "requests"
318
+ ]);
319
+ parser.hooks.call
320
+ .for("module.hot.accept")
321
+ .tap("HotModuleReplacementPlugin", expr => {
322
+ if (!parser.state.compilation.hotUpdateChunkTemplate)
323
+ return false;
324
+ if (expr.arguments.length >= 1) {
325
+ const arg = parser.evaluateExpression(expr.arguments[0]);
326
+ let params = [];
327
+ let requests = [];
328
+ if (arg.isString()) {
329
+ params = [arg];
330
+ } else if (arg.isArray()) {
331
+ params = arg.items.filter(param => param.isString());
332
+ }
333
+ if (params.length > 0) {
334
+ params.forEach((param, idx) => {
335
+ const request = param.string;
336
+ const dep = new ModuleHotAcceptDependency(
337
+ request,
338
+ param.range
339
+ );
340
+ dep.optional = true;
341
+ dep.loc = Object.create(expr.loc);
342
+ dep.loc.index = idx;
343
+ parser.state.module.addDependency(dep);
344
+ requests.push(request);
345
+ });
346
+ if (expr.arguments.length > 1)
347
+ parser.hooks.hotAcceptCallback.call(
348
+ expr.arguments[1],
349
+ requests
350
+ );
351
+ else
352
+ parser.hooks.hotAcceptWithoutCallback.call(expr, requests);
353
+ }
354
+ }
355
+ });
356
+ parser.hooks.call
357
+ .for("module.hot.decline")
358
+ .tap("HotModuleReplacementPlugin", expr => {
359
+ if (!parser.state.compilation.hotUpdateChunkTemplate)
360
+ return false;
361
+ if (expr.arguments.length === 1) {
362
+ const arg = parser.evaluateExpression(expr.arguments[0]);
363
+ let params = [];
364
+ if (arg.isString()) {
365
+ params = [arg];
366
+ } else if (arg.isArray()) {
367
+ params = arg.items.filter(param => param.isString());
368
+ }
369
+ params.forEach((param, idx) => {
370
+ const dep = new ModuleHotDeclineDependency(
371
+ param.string,
372
+ param.range
373
+ );
374
+ dep.optional = true;
375
+ dep.loc = Object.create(expr.loc);
376
+ dep.loc.index = idx;
377
+ parser.state.module.addDependency(dep);
378
+ });
379
+ }
380
+ });
381
+ parser.hooks.expression
382
+ .for("module.hot")
383
+ .tap("HotModuleReplacementPlugin", ParserHelpers.skipTraversal);
384
+ };
385
+
386
+ // TODO add HMR support for javascript/esm
387
+ normalModuleFactory.hooks.parser
388
+ .for("javascript/auto")
389
+ .tap("HotModuleReplacementPlugin", handler);
390
+ normalModuleFactory.hooks.parser
391
+ .for("javascript/dynamic")
392
+ .tap("HotModuleReplacementPlugin", handler);
393
+
394
+ compilation.hooks.normalModuleLoader.tap(
395
+ "HotModuleReplacementPlugin",
396
+ context => {
397
+ context.hot = true;
398
+ }
399
+ );
400
+ }
401
+ );
402
+ }
403
+ };
404
+
405
+ const hotInitCode = Template.getFunctionContent(
406
+ require("./HotModuleReplacement.runtime.js")
407
+ );