webpack 4.14.0 → 4.15.1

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 (60) hide show
  1. package/bin/webpack.js +7 -2
  2. package/lib/AsyncDependenciesBlock.js +44 -0
  3. package/lib/AutomaticPrefetchPlugin.js +1 -1
  4. package/lib/Chunk.js +53 -3
  5. package/lib/ChunkGroup.js +1 -1
  6. package/lib/ChunkTemplate.js +14 -2
  7. package/lib/CommentCompilationWarning.js +2 -2
  8. package/lib/Compilation.js +435 -34
  9. package/lib/Compiler.js +57 -4
  10. package/lib/ContextModule.js +3 -15
  11. package/lib/DelegatedModule.js +9 -1
  12. package/lib/DependenciesBlock.js +36 -3
  13. package/lib/DependenciesBlockVariable.js +22 -0
  14. package/lib/Dependency.js +25 -4
  15. package/lib/DllEntryPlugin.js +4 -1
  16. package/lib/DynamicEntryPlugin.js +21 -1
  17. package/lib/EntryOptionPlugin.js +12 -0
  18. package/lib/ExtendedAPIPlugin.js +8 -4
  19. package/lib/ExternalModuleFactoryPlugin.js +1 -1
  20. package/lib/GraphHelpers.js +2 -1
  21. package/lib/HotModuleReplacement.runtime.js +8 -5
  22. package/lib/HotModuleReplacementPlugin.js +114 -116
  23. package/lib/MainTemplate.js +19 -4
  24. package/lib/Module.js +9 -3
  25. package/lib/ModuleReason.js +8 -0
  26. package/lib/MultiEntryPlugin.js +25 -3
  27. package/lib/NormalModule.js +5 -23
  28. package/lib/RuleSet.js +3 -3
  29. package/lib/SingleEntryPlugin.js +14 -0
  30. package/lib/Stats.js +2 -1
  31. package/lib/Template.js +4 -1
  32. package/lib/UmdMainTemplatePlugin.js +12 -12
  33. package/lib/WebpackError.js +4 -0
  34. package/lib/WebpackOptionsDefaulter.js +3 -0
  35. package/lib/WebpackOptionsValidationError.js +0 -1
  36. package/lib/compareLocations.js +13 -17
  37. package/lib/debug/ProfilingPlugin.js +5 -7
  38. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +4 -6
  39. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +0 -2
  40. package/lib/dependencies/LoaderDependency.js +3 -0
  41. package/lib/dependencies/LoaderPlugin.js +3 -1
  42. package/lib/dependencies/ModuleDependency.js +3 -0
  43. package/lib/dependencies/MultiEntryDependency.js +5 -0
  44. package/lib/dependencies/SingleEntryDependency.js +3 -0
  45. package/lib/node/NodeMainTemplateAsync.runtime.js +1 -1
  46. package/lib/optimize/ConcatenatedModule.js +1 -0
  47. package/lib/optimize/SplitChunksPlugin.js +142 -2
  48. package/lib/util/Semaphore.js +12 -0
  49. package/lib/util/SetHelpers.js +4 -4
  50. package/lib/util/SortableSet.js +1 -1
  51. package/lib/util/createHash.js +15 -0
  52. package/lib/util/deterministicGrouping.js +251 -0
  53. package/lib/util/identifier.js +27 -0
  54. package/lib/wasm/WasmMainTemplatePlugin.js +10 -4
  55. package/lib/wasm/WebAssemblyGenerator.js +12 -12
  56. package/lib/wasm/WebAssemblyInInitialChunkError.js +88 -0
  57. package/lib/wasm/WebAssemblyModulesPlugin.js +28 -0
  58. package/lib/webpack.web.js +2 -2
  59. package/package.json +8 -7
  60. package/schemas/WebpackOptions.json +37 -1
@@ -35,20 +35,90 @@ const createHash = require("./util/createHash");
35
35
  const Queue = require("./util/Queue");
36
36
  const SortableSet = require("./util/SortableSet");
37
37
  const GraphHelpers = require("./GraphHelpers");
38
+ const ModuleDependency = require("./dependencies/ModuleDependency");
38
39
 
39
40
  /** @typedef {import("./Module")} Module */
41
+ /** @typedef {import("./Compiler")} Compiler */
42
+ /** @typedef {import("webpack-sources").Source} Source */
43
+ /** @typedef {import("./WebpackError")} WebpackError */
44
+ /** @typedef {import("./DependenciesBlockVariable")} DependenciesBlockVariable */
45
+ /** @typedef {import("./dependencies/SingleEntryDependency")} SingleEntryDependency */
46
+ /** @typedef {import("./dependencies/MultiEntryDependency")} MultiEntryDependency */
47
+ /** @typedef {import("./dependencies/DllEntryDependency")} DllEntryDependency */
40
48
  /** @typedef {import("./dependencies/DependencyReference")} DependencyReference */
41
- /** @typedef {import("./Dependency")} Dependency */
42
49
  /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
43
50
  /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
44
- /** @typedef {import("webpack-sources").Source} Source */
45
-
51
+ /** @typedef {import("./Dependency")} Dependency */
52
+ /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
53
+ /** @typedef {import("./Dependency").DependencyTemplate} DependencyTemplate */
54
+ /** @typedef {import("./util/createHash").Hash} Hash */
55
+
56
+ // TODO use @callback
57
+ /** @typedef {{[assetName: string]: Source}} CompilationAssets */
58
+ /** @typedef {(err: Error|null, result?: Module) => void } ModuleCallback */
59
+ /** @typedef {(err?: Error|null, result?: Module) => void } ModuleChainCallback */
60
+ /** @typedef {(module: Module) => void} OnModuleCallback */
61
+ /** @typedef {(err?: Error|null) => void} Callback */
62
+ /** @typedef {(d: Dependency) => any} DepBlockVarDependenciesCallback */
63
+ /** @typedef {new (...args: any[]) => Dependency} DepConstructor */
64
+ /** @typedef {{apply: () => void}} Plugin */
65
+
66
+ /**
67
+ * @typedef {Object} ModuleFactoryCreateDataContextInfo
68
+ * @property {string} issuer
69
+ * @property {string} compiler
70
+ */
71
+
72
+ /**
73
+ * @typedef {Object} ModuleFactoryCreateData
74
+ * @property {ModuleFactoryCreateDataContextInfo} contextInfo
75
+ * @property {any=} resolveOptions
76
+ * @property {string} context
77
+ * @property {Dependency[]} dependencies
78
+ */
79
+
80
+ /**
81
+ * @typedef {Object} ModuleFactory
82
+ * @property {(data: ModuleFactoryCreateData, callback: ModuleCallback) => any} create
83
+ */
84
+
85
+ /**
86
+ * @typedef {Object} SortedDependency
87
+ * @property {ModuleFactory} factory
88
+ * @property {Dependency[]} dependencies
89
+ */
90
+
91
+ /**
92
+ * @typedef {Object} AvailableModulesChunkGroupMapping
93
+ * @property {ChunkGroup} chunkGroup
94
+ * @property {Set<Module>} availableModules
95
+ */
96
+
97
+ /**
98
+ * @typedef {Object} DependenciesBlockLike
99
+ * @property {Dependency[]} dependencies
100
+ * @property {AsyncDependenciesBlock[]} blocks
101
+ * @property {DependenciesBlockVariable[]} variables
102
+ */
103
+
104
+ /**
105
+ * @param {Chunk} a first chunk to sort by id
106
+ * @param {Chunk} b second chunk to sort by id
107
+ * @returns {-1|0|1} sort value
108
+ */
46
109
  const byId = (a, b) => {
47
- if (a.id < b.id) return -1;
48
- if (a.id > b.id) return 1;
110
+ if (a.id !== null && b.id !== null) {
111
+ if (a.id < b.id) return -1;
112
+ if (a.id > b.id) return 1;
113
+ }
49
114
  return 0;
50
115
  };
51
116
 
117
+ /**
118
+ * @param {Module} a first module to sort by
119
+ * @param {Module} b second module to sort by
120
+ * @returns {-1|0|1} sort value
121
+ */
52
122
  const byIdOrIdentifier = (a, b) => {
53
123
  if (a.id < b.id) return -1;
54
124
  if (a.id > b.id) return 1;
@@ -59,6 +129,11 @@ const byIdOrIdentifier = (a, b) => {
59
129
  return 0;
60
130
  };
61
131
 
132
+ /**
133
+ * @param {Module} a first module to sort by
134
+ * @param {Module} b second module to sort by
135
+ * @returns {-1|0|1} sort value
136
+ */
62
137
  const byIndexOrIdentifier = (a, b) => {
63
138
  if (a.index < b.index) return -1;
64
139
  if (a.index > b.index) return 1;
@@ -69,14 +144,24 @@ const byIndexOrIdentifier = (a, b) => {
69
144
  return 0;
70
145
  };
71
146
 
147
+ /**
148
+ * @param {Compilation} a first compilation to sort by
149
+ * @param {Compilation} b second compilation to sort by
150
+ * @returns {-1|0|1} sort value
151
+ */
72
152
  const byNameOrHash = (a, b) => {
73
153
  if (a.name < b.name) return -1;
74
154
  if (a.name > b.name) return 1;
75
155
  if (a.fullHash < b.fullHash) return -1;
76
- if (a.fullhash > b.fullHash) return 1;
156
+ if (a.fullHash > b.fullHash) return 1;
77
157
  return 0;
78
158
  };
79
159
 
160
+ /**
161
+ * @param {DependenciesBlockVariable[]} variables DepBlock Variables to iterate over
162
+ * @param {DepBlockVarDependenciesCallback} fn callback to apply on iterated elements
163
+ * @returns {void}
164
+ */
80
165
  const iterationBlockVariable = (variables, fn) => {
81
166
  for (
82
167
  let indexVariable = 0;
@@ -90,25 +175,45 @@ const iterationBlockVariable = (variables, fn) => {
90
175
  }
91
176
  };
92
177
 
178
+ /**
179
+ * @template T
180
+ * @param {T[]} arr array of elements to iterate over
181
+ * @param {function(T): void} fn callback applied to each element
182
+ * @returns {void}
183
+ */
93
184
  const iterationOfArrayCallback = (arr, fn) => {
94
185
  for (let index = 0; index < arr.length; index++) {
95
186
  fn(arr[index]);
96
187
  }
97
188
  };
98
189
 
99
- function addAllToSet(set, otherSet) {
190
+ /**
191
+ * @template T
192
+ * @param {Set<T>} set set to add items to
193
+ * @param {Set<T>} otherSet set to add items from
194
+ * @returns {void}
195
+ */
196
+ const addAllToSet = (set, otherSet) => {
100
197
  for (const item of otherSet) {
101
198
  set.add(item);
102
199
  }
103
- }
200
+ };
104
201
 
105
202
  class Compilation extends Tapable {
203
+ /**
204
+ * Creates an instance of Compilation.
205
+ * @param {Compiler} compiler the compiler which created the compilation
206
+ */
106
207
  constructor(compiler) {
107
208
  super();
108
209
  this.hooks = {
210
+ /** @type {SyncHook<Module>} */
109
211
  buildModule: new SyncHook(["module"]),
212
+ /** @type {SyncHook<Module>} */
110
213
  rebuildModule: new SyncHook(["module"]),
214
+ /** @type {SyncHook<Module, Error>} */
111
215
  failedModule: new SyncHook(["module", "error"]),
216
+ /** @type {SyncHook<Module>} */
112
217
  succeedModule: new SyncHook(["module"]),
113
218
 
114
219
  /** @type {SyncWaterfallHook<DependencyReference, Dependency, Module>} */
@@ -118,84 +223,146 @@ class Compilation extends Tapable {
118
223
  "module"
119
224
  ]),
120
225
 
226
+ /** @type {SyncHook<Module[]>} */
121
227
  finishModules: new SyncHook(["modules"]),
228
+ /** @type {SyncHook<Module>} */
122
229
  finishRebuildingModule: new SyncHook(["module"]),
123
-
230
+ /** @type {SyncHook} */
124
231
  unseal: new SyncHook([]),
232
+ /** @type {SyncHook} */
125
233
  seal: new SyncHook([]),
126
234
 
235
+ /** @type {SyncHook} */
236
+ beforeChunks: new SyncHook([]),
237
+ /** @type {SyncHook<Chunk[]>} */
238
+ afterChunks: new SyncHook(["chunks"]),
239
+
240
+ /** @type {SyncBailHook<Module[]>} */
127
241
  optimizeDependenciesBasic: new SyncBailHook(["modules"]),
242
+ /** @type {SyncBailHook<Module[]>} */
128
243
  optimizeDependencies: new SyncBailHook(["modules"]),
244
+ /** @type {SyncBailHook<Module[]>} */
129
245
  optimizeDependenciesAdvanced: new SyncBailHook(["modules"]),
246
+ /** @type {SyncBailHook<Module[]>} */
130
247
  afterOptimizeDependencies: new SyncHook(["modules"]),
131
248
 
249
+ /** @type {SyncHook} */
132
250
  optimize: new SyncHook([]),
133
-
251
+ /** @type {SyncBailHook<Module[]>} */
134
252
  optimizeModulesBasic: new SyncBailHook(["modules"]),
253
+ /** @type {SyncBailHook<Module[]>} */
135
254
  optimizeModules: new SyncBailHook(["modules"]),
255
+ /** @type {SyncBailHook<Module[]>} */
136
256
  optimizeModulesAdvanced: new SyncBailHook(["modules"]),
257
+ /** @type {SyncHook<Module[]>} */
137
258
  afterOptimizeModules: new SyncHook(["modules"]),
138
259
 
260
+ /** @type {SyncBailHook<Chunk[], ChunkGroup[]>} */
139
261
  optimizeChunksBasic: new SyncBailHook(["chunks", "chunkGroups"]),
262
+ /** @type {SyncBailHook<Chunk[], ChunkGroup[]>} */
140
263
  optimizeChunks: new SyncBailHook(["chunks", "chunkGroups"]),
264
+ /** @type {SyncBailHook<Chunk[], ChunkGroup[]>} */
141
265
  optimizeChunksAdvanced: new SyncBailHook(["chunks", "chunkGroups"]),
266
+ /** @type {SyncHook<Chunk[], ChunkGroup[]>} */
142
267
  afterOptimizeChunks: new SyncHook(["chunks", "chunkGroups"]),
143
268
 
269
+ /** @type {AsyncSeriesHook<Chunk[], Module[]>} */
144
270
  optimizeTree: new AsyncSeriesHook(["chunks", "modules"]),
271
+ /** @type {SyncHook<Chunk[], Module[]>} */
145
272
  afterOptimizeTree: new SyncHook(["chunks", "modules"]),
146
273
 
274
+ /** @type {SyncBailHook<Chunk[], Module[]>} */
147
275
  optimizeChunkModulesBasic: new SyncBailHook(["chunks", "modules"]),
276
+ /** @type {SyncBailHook<Chunk[], Module[]>} */
148
277
  optimizeChunkModules: new SyncBailHook(["chunks", "modules"]),
278
+ /** @type {SyncBailHook<Chunk[], Module[]>} */
149
279
  optimizeChunkModulesAdvanced: new SyncBailHook(["chunks", "modules"]),
280
+ /** @type {SyncHook<Chunk[], Module[]>} */
150
281
  afterOptimizeChunkModules: new SyncHook(["chunks", "modules"]),
282
+ /** @type {SyncBailHook} */
151
283
  shouldRecord: new SyncBailHook([]),
152
284
 
285
+ /** @type {SyncHook<Module[], any>} */
153
286
  reviveModules: new SyncHook(["modules", "records"]),
287
+ /** @type {SyncHook<Module[]>} */
154
288
  optimizeModuleOrder: new SyncHook(["modules"]),
289
+ /** @type {SyncHook<Module[]>} */
155
290
  advancedOptimizeModuleOrder: new SyncHook(["modules"]),
291
+ /** @type {SyncHook<Module[]>} */
156
292
  beforeModuleIds: new SyncHook(["modules"]),
293
+ /** @type {SyncHook<Module[]>} */
157
294
  moduleIds: new SyncHook(["modules"]),
295
+ /** @type {SyncHook<Module[]>} */
158
296
  optimizeModuleIds: new SyncHook(["modules"]),
297
+ /** @type {SyncHook<Module[]>} */
159
298
  afterOptimizeModuleIds: new SyncHook(["modules"]),
160
299
 
300
+ /** @type {SyncHook<Chunk[], any>} */
161
301
  reviveChunks: new SyncHook(["chunks", "records"]),
302
+ /** @type {SyncHook<Chunk[]>} */
162
303
  optimizeChunkOrder: new SyncHook(["chunks"]),
304
+ /** @type {SyncHook<Chunk[]>} */
163
305
  beforeChunkIds: new SyncHook(["chunks"]),
306
+ /** @type {SyncHook<Chunk[]>} */
164
307
  optimizeChunkIds: new SyncHook(["chunks"]),
308
+ /** @type {SyncHook<Chunk[]>} */
165
309
  afterOptimizeChunkIds: new SyncHook(["chunks"]),
166
310
 
311
+ /** @type {SyncHook<Module[], any>} */
167
312
  recordModules: new SyncHook(["modules", "records"]),
313
+ /** @type {SyncHook<Chunk[], any>} */
168
314
  recordChunks: new SyncHook(["chunks", "records"]),
169
315
 
316
+ /** @type {SyncHook} */
170
317
  beforeHash: new SyncHook([]),
318
+ /** @type {SyncHook<Chunk>} */
171
319
  contentHash: new SyncHook(["chunk"]),
320
+ /** @type {SyncHook} */
172
321
  afterHash: new SyncHook([]),
173
-
322
+ /** @type {SyncHook<any>} */
174
323
  recordHash: new SyncHook(["records"]),
175
-
324
+ /** @type {SyncHook<Compilation, any>} */
176
325
  record: new SyncHook(["compilation", "records"]),
177
326
 
327
+ /** @type {SyncHook} */
178
328
  beforeModuleAssets: new SyncHook([]),
329
+ /** @type {SyncBailHook} */
179
330
  shouldGenerateChunkAssets: new SyncBailHook([]),
331
+ /** @type {SyncHook} */
180
332
  beforeChunkAssets: new SyncHook([]),
333
+ /** @type {SyncHook<Chunk[]>} */
181
334
  additionalChunkAssets: new SyncHook(["chunks"]),
182
335
 
336
+ /** @type {AsyncSeriesHook} */
183
337
  additionalAssets: new AsyncSeriesHook([]),
338
+ /** @type {AsyncSeriesHook<Chunk[]>} */
184
339
  optimizeChunkAssets: new AsyncSeriesHook(["chunks"]),
340
+ /** @type {SyncHook<Chunk[]>} */
185
341
  afterOptimizeChunkAssets: new SyncHook(["chunks"]),
342
+ /** @type {AsyncSeriesHook<CompilationAssets>} */
186
343
  optimizeAssets: new AsyncSeriesHook(["assets"]),
344
+ /** @type {SyncHook<CompilationAssets>} */
187
345
  afterOptimizeAssets: new SyncHook(["assets"]),
188
346
 
347
+ /** @type {SyncBailHook} */
189
348
  needAdditionalSeal: new SyncBailHook([]),
349
+ /** @type {AsyncSeriesHook} */
190
350
  afterSeal: new AsyncSeriesHook([]),
191
351
 
352
+ /** @type {SyncHook<Chunk, Hash>} */
192
353
  chunkHash: new SyncHook(["chunk", "chunkHash"]),
354
+ /** @type {SyncHook<Module, string>} */
193
355
  moduleAsset: new SyncHook(["module", "filename"]),
356
+ /** @type {SyncHook<Chunk, string>} */
194
357
  chunkAsset: new SyncHook(["chunk", "filename"]),
195
358
 
359
+ /** @type {SyncWaterfallHook<string, TODO>} */
196
360
  assetPath: new SyncWaterfallHook(["filename", "data"]), // TODO MainTemplate
197
361
 
362
+ /** @type {SyncBailHook} */
198
363
  needAdditionalPass: new SyncBailHook([]),
364
+
365
+ /** @type {SyncHook<Compiler, string, number>} */
199
366
  childCompiler: new SyncHook([
200
367
  "childCompiler",
201
368
  "compilerName",
@@ -204,11 +371,16 @@ class Compilation extends Tapable {
204
371
 
205
372
  // TODO the following hooks are weirdly located here
206
373
  // TODO move them for webpack 5
374
+ /** @type {SyncHook<object, Module>} */
207
375
  normalModuleLoader: new SyncHook(["loaderContext", "module"]),
208
376
 
377
+ /** @type {SyncBailHook<Chunk[]>} */
209
378
  optimizeExtractedChunksBasic: new SyncBailHook(["chunks"]),
379
+ /** @type {SyncBailHook<Chunk[]>} */
210
380
  optimizeExtractedChunks: new SyncBailHook(["chunks"]),
381
+ /** @type {SyncBailHook<Chunk[]>} */
211
382
  optimizeExtractedChunksAdvanced: new SyncBailHook(["chunks"]),
383
+ /** @type {SyncHook<Chunk[]>} */
212
384
  afterOptimizeExtractedChunks: new SyncHook(["chunks"])
213
385
  };
214
386
  this._pluginCompat.tap("Compilation", options => {
@@ -222,7 +394,9 @@ class Compilation extends Tapable {
222
394
  break;
223
395
  }
224
396
  });
397
+ /** @type {string=} */
225
398
  this.name = undefined;
399
+ /** @type {Compiler} */
226
400
  this.compiler = compiler;
227
401
  this.resolverFactory = compiler.resolverFactory;
228
402
  this.inputFileSystem = compiler.inputFileSystem;
@@ -230,6 +404,7 @@ class Compilation extends Tapable {
230
404
 
231
405
  const options = (this.options = compiler.options);
232
406
  this.outputOptions = options && options.output;
407
+ /** @type {boolean=} */
233
408
  this.bail = options && options.bail;
234
409
  this.profile = options && options.profile;
235
410
  this.performance = options && options.performance;
@@ -251,32 +426,53 @@ class Compilation extends Tapable {
251
426
  this.semaphore = new Semaphore(options.parallelism || 100);
252
427
 
253
428
  this.entries = [];
429
+ /** @private @type {{name: string, request: string, module: Module}[]} */
254
430
  this._preparedEntrypoints = [];
255
431
  this.entrypoints = new Map();
432
+ /** @type {Chunk[]} */
256
433
  this.chunks = [];
434
+ /** @type {ChunkGroup[]} */
257
435
  this.chunkGroups = [];
436
+ /** @type {Map<string, ChunkGroup>} */
258
437
  this.namedChunkGroups = new Map();
438
+ /** @type {Map<string, Chunk>} */
259
439
  this.namedChunks = new Map();
440
+ /** @type {Module[]} */
260
441
  this.modules = [];
442
+ /** @private @type {Map<string, Module>} */
261
443
  this._modules = new Map();
262
444
  this.cache = null;
263
445
  this.records = null;
446
+ /** @type {string[]} */
264
447
  this.additionalChunkAssets = [];
448
+ /** @type {CompilationAssets} */
265
449
  this.assets = {};
450
+ /** @type {WebpackError[]} */
266
451
  this.errors = [];
452
+ /** @type {WebpackError[]} */
267
453
  this.warnings = [];
454
+ /** @type {Compilation[]} */
268
455
  this.children = [];
456
+ /** @type {Map<DepConstructor, ModuleFactory>} */
269
457
  this.dependencyFactories = new Map();
458
+ /** @type {Map<DepConstructor|string, DependencyTemplate|string>} */
270
459
  this.dependencyTemplates = new Map();
460
+ // TODO refactor this in webpack 5 to a custom DependencyTemplates class with a hash property
271
461
  this.dependencyTemplates.set("hash", "");
272
462
  this.childrenCounters = {};
463
+ /** @type {Set<number|string>} */
273
464
  this.usedChunkIds = null;
465
+ /** @type {Set<number>} */
274
466
  this.usedModuleIds = null;
467
+ /** @type {Map<string, number>=} */
275
468
  this.fileTimestamps = undefined;
469
+ /** @type {Map<string, number>=} */
276
470
  this.contextTimestamps = undefined;
471
+ /** @type {Set<string>=} */
277
472
  this.compilationDependencies = undefined;
278
-
473
+ /** @private @type {Map<Module, Callback[]>} */
279
474
  this._buildingModules = new Map();
475
+ /** @private @type {Map<Module, Callback[]>} */
280
476
  this._rebuildingModules = new Map();
281
477
  }
282
478
 
@@ -284,6 +480,20 @@ class Compilation extends Tapable {
284
480
  return new Stats(this);
285
481
  }
286
482
 
483
+ /**
484
+ * @typedef {Object} AddModuleResult
485
+ * @property {Module} module the added or existing module
486
+ * @property {boolean} issuer was this the first request for this module
487
+ * @property {boolean} build should the module be build
488
+ * @property {boolean} dependencies should dependencies be walked
489
+ */
490
+
491
+ /**
492
+ * @param {Module} module module to be added that was created
493
+ * @param {any=} cacheGroup cacheGroup it is apart of
494
+ * @returns {AddModuleResult} returns meta about whether or not the module had built
495
+ * had an issuer, or any dependnecies
496
+ */
287
497
  addModule(module, cacheGroup) {
288
498
  const identifier = module.identifier();
289
499
  const alreadyAddedModule = this._modules.get(identifier);
@@ -344,15 +554,30 @@ class Compilation extends Tapable {
344
554
  };
345
555
  }
346
556
 
557
+ /**
558
+ * Fetches a module from a compilation by its identifier
559
+ * @param {Module} module the module provided
560
+ * @returns {Module} the module requested
561
+ */
347
562
  getModule(module) {
348
563
  const identifier = module.identifier();
349
564
  return this._modules.get(identifier);
350
565
  }
351
566
 
567
+ /**
568
+ * Attempts to search for a module by its identifier
569
+ * @param {string} identifier identifier (usually path) for module
570
+ * @returns {Module|undefined} attempt to search for module and return it, else undefined
571
+ */
352
572
  findModule(identifier) {
353
573
  return this._modules.get(identifier);
354
574
  }
355
575
 
576
+ /**
577
+ * @param {Module} module module with its callback list
578
+ * @param {Callback} callback the callback function
579
+ * @returns {void}
580
+ */
356
581
  waitForBuildingFinished(module, callback) {
357
582
  let callbackList = this._buildingModules.get(module);
358
583
  if (callbackList) {
@@ -362,6 +587,16 @@ class Compilation extends Tapable {
362
587
  }
363
588
  }
364
589
 
590
+ /**
591
+ * Builds the module object
592
+ *
593
+ * @param {Module} module module to be built
594
+ * @param {boolean} optional optional flag
595
+ * @param {Module=} origin origin module this module build was requested from
596
+ * @param {Dependency[]=} dependencies optional dependencies from the module to be built
597
+ * @param {TODO} thisCallback the callback
598
+ * @returns {TODO} returns the callback function with results
599
+ */
365
600
  buildModule(module, optional, origin, dependencies, thisCallback) {
366
601
  let callbackList = this._buildingModules.get(module);
367
602
  if (callbackList) {
@@ -418,6 +653,11 @@ class Compilation extends Tapable {
418
653
  );
419
654
  }
420
655
 
656
+ /**
657
+ * @param {Module} module to be processed for deps
658
+ * @param {ModuleCallback} callback callback to be triggered
659
+ * @returns {void}
660
+ */
421
661
  processModuleDependencies(module, callback) {
422
662
  const dependencies = new Map();
423
663
 
@@ -481,6 +721,15 @@ class Compilation extends Tapable {
481
721
  );
482
722
  }
483
723
 
724
+ /**
725
+ * @param {Module} module module to add deps to
726
+ * @param {SortedDependency[]} dependencies set of sorted dependencies to iterate through
727
+ * @param {(boolean|null)=} bail whether to bail or not
728
+ * @param {TODO} cacheGroup optional cacheGroup
729
+ * @param {boolean} recursive whether it is recursive traversal
730
+ * @param {function} callback callback for when dependencies are finished being added
731
+ * @returns {void}
732
+ */
484
733
  addModuleDependencies(
485
734
  module,
486
735
  dependencies,
@@ -648,6 +897,14 @@ class Compilation extends Tapable {
648
897
  );
649
898
  }
650
899
 
900
+ /**
901
+ *
902
+ * @param {string} context context string path
903
+ * @param {Dependency} dependency dependency used to create Module chain
904
+ * @param {OnModuleCallback} onModule function invoked on modules creation
905
+ * @param {ModuleChainCallback} callback callback for when module chain is complete
906
+ * @returns {void} will throw if dependency instance is not a valid Dependency
907
+ */
651
908
  _addModuleChain(context, dependency, onModule, callback) {
652
909
  const start = this.profile && Date.now();
653
910
  const currentProfile = this.profile && {};
@@ -669,8 +926,8 @@ class Compilation extends Tapable {
669
926
  ) {
670
927
  throw new Error("Parameter 'dependency' must be a Dependency");
671
928
  }
672
-
673
- const moduleFactory = this.dependencyFactories.get(dependency.constructor);
929
+ const Dep = /** @type {DepConstructor} */ (dependency.constructor);
930
+ const moduleFactory = this.dependencyFactories.get(Dep);
674
931
  if (!moduleFactory) {
675
932
  throw new Error(
676
933
  `No dependency factory available for this dependency type: ${
@@ -756,12 +1013,25 @@ class Compilation extends Tapable {
756
1013
  });
757
1014
  }
758
1015
 
1016
+ /**
1017
+ *
1018
+ * @param {string} context context path for entry
1019
+ * @param {Dependency} entry entry dependency being created
1020
+ * @param {string} name name of entry
1021
+ * @param {ModuleCallback} callback callback function
1022
+ * @returns {void} returns
1023
+ */
759
1024
  addEntry(context, entry, name, callback) {
760
1025
  const slot = {
761
1026
  name: name,
762
- request: entry.request,
1027
+ request: null,
763
1028
  module: null
764
1029
  };
1030
+
1031
+ if (entry instanceof ModuleDependency) {
1032
+ slot.request = entry.request;
1033
+ }
1034
+
765
1035
  this._preparedEntrypoints.push(slot);
766
1036
  this._addModuleChain(
767
1037
  context,
@@ -785,6 +1055,12 @@ class Compilation extends Tapable {
785
1055
  );
786
1056
  }
787
1057
 
1058
+ /**
1059
+ * @param {string} context context path string
1060
+ * @param {Dependency} dependency dep used to create module
1061
+ * @param {ModuleCallback} callback module callback sending module up a level
1062
+ * @returns {void}
1063
+ */
788
1064
  prefetch(context, dependency, callback) {
789
1065
  this._addModuleChain(
790
1066
  context,
@@ -796,6 +1072,11 @@ class Compilation extends Tapable {
796
1072
  );
797
1073
  }
798
1074
 
1075
+ /**
1076
+ * @param {Module} module module to be rebuilt
1077
+ * @param {Callback} thisCallback callback when module finishes rebuilding
1078
+ * @returns {void}
1079
+ */
799
1080
  rebuildModule(module, thisCallback) {
800
1081
  let callbackList = this._rebuildingModules.get(module);
801
1082
  if (callbackList) {
@@ -858,6 +1139,10 @@ class Compilation extends Tapable {
858
1139
  }
859
1140
  }
860
1141
 
1142
+ /**
1143
+ * @param {Callback} callback signals when the seal method is finishes
1144
+ * @returns {void}
1145
+ */
861
1146
  seal(callback) {
862
1147
  this.hooks.seal.call();
863
1148
 
@@ -870,6 +1155,7 @@ class Compilation extends Tapable {
870
1155
  }
871
1156
  this.hooks.afterOptimizeDependencies.call(this.modules);
872
1157
 
1158
+ this.hooks.beforeChunks.call();
873
1159
  for (const preparedEntrypoint of this._preparedEntrypoints) {
874
1160
  const module = preparedEntrypoint.module;
875
1161
  const name = preparedEntrypoint.name;
@@ -891,6 +1177,8 @@ class Compilation extends Tapable {
891
1177
  }
892
1178
  this.processDependenciesBlocksForChunkGroups(this.chunkGroups.slice());
893
1179
  this.sortModules(this.modules);
1180
+ this.hooks.afterChunks.call(this.chunks);
1181
+
894
1182
  this.hooks.optimize.call();
895
1183
 
896
1184
  while (
@@ -999,10 +1287,19 @@ class Compilation extends Tapable {
999
1287
  });
1000
1288
  }
1001
1289
 
1290
+ /**
1291
+ * @param {Module[]} modules the modules array on compilation to perform the sort for
1292
+ * @returns {void}
1293
+ */
1002
1294
  sortModules(modules) {
1003
1295
  modules.sort(byIndexOrIdentifier);
1004
1296
  }
1005
1297
 
1298
+ /**
1299
+ * @param {Module} module moulde to report from
1300
+ * @param {DependenciesBlock[]} blocks blocks to report from
1301
+ * @returns {void}
1302
+ */
1006
1303
  reportDependencyErrorsAndWarnings(module, blocks) {
1007
1304
  for (let indexBlock = 0; indexBlock < blocks.length; indexBlock++) {
1008
1305
  const block = blocks[indexBlock];
@@ -1038,7 +1335,7 @@ class Compilation extends Tapable {
1038
1335
  /**
1039
1336
  * @param {TODO} groupOptions options for the chunk group
1040
1337
  * @param {Module} module the module the references the chunk group
1041
- * @param {TODO} loc the location from with the chunk group is reference (inside of module)
1338
+ * @param {DependencyLocation} loc the location from with the chunk group is referenced (inside of module)
1042
1339
  * @param {string} request the request from which the the chunk group is referenced
1043
1340
  * @returns {ChunkGroup} the new or existing chunk group
1044
1341
  */
@@ -1070,6 +1367,13 @@ class Compilation extends Tapable {
1070
1367
  return chunkGroup;
1071
1368
  }
1072
1369
 
1370
+ /**
1371
+ * This method first looks to see if a name is provided for a new chunk,
1372
+ * and first looks to see if any named chunks already exist and reuse that chunk instead.
1373
+ *
1374
+ * @param {string=} name optional chunk name to be provided
1375
+ * @returns {Chunk} create a chunk (invoked during seal event)
1376
+ */
1073
1377
  addChunk(name) {
1074
1378
  if (name) {
1075
1379
  const chunk = this.namedChunks.get(name);
@@ -1085,12 +1389,20 @@ class Compilation extends Tapable {
1085
1389
  return chunk;
1086
1390
  }
1087
1391
 
1392
+ /**
1393
+ * @param {Module} module module to assign depth
1394
+ * @returns {void}
1395
+ */
1088
1396
  assignDepth(module) {
1089
1397
  const queue = new Set([module]);
1090
1398
  let depth;
1091
1399
 
1092
1400
  module.depth = 0;
1093
1401
 
1402
+ /**
1403
+ * @param {Module} module module for processeing
1404
+ * @returns {void}
1405
+ */
1094
1406
  const enqueueJob = module => {
1095
1407
  const d = module.depth;
1096
1408
  if (typeof d === "number" && d <= depth) return;
@@ -1098,12 +1410,20 @@ class Compilation extends Tapable {
1098
1410
  module.depth = depth;
1099
1411
  };
1100
1412
 
1101
- const assignDepthToDependency = (dependency, depth) => {
1413
+ /**
1414
+ * @param {Dependency} dependency dependency to assign depth to
1415
+ * @returns {void}
1416
+ */
1417
+ const assignDepthToDependency = dependency => {
1102
1418
  if (dependency.module) {
1103
1419
  enqueueJob(dependency.module);
1104
1420
  }
1105
1421
  };
1106
1422
 
1423
+ /**
1424
+ * @param {DependenciesBlock} block block to assign depth to
1425
+ * @returns {void}
1426
+ */
1107
1427
  const assignDepthToDependencyBlock = block => {
1108
1428
  if (block.variables) {
1109
1429
  iterationBlockVariable(block.variables, assignDepthToDependency);
@@ -1155,7 +1475,7 @@ class Compilation extends Tapable {
1155
1475
  // eachother and Blocks with Chunks. It stops traversing when all modules
1156
1476
  // for a chunk are already available. So it doesn't connect unneeded chunks.
1157
1477
 
1158
- /** @type {Map<Chunk, {block: DependenciesBlock, chunkGroup: ChunkGroup}[]>} */
1478
+ /** @type {Map<ChunkGroup, {block: AsyncDependenciesBlock, chunkGroup: ChunkGroup}[]>} */
1159
1479
  const chunkDependencies = new Map();
1160
1480
  const allCreatedChunkGroups = new Set();
1161
1481
 
@@ -1163,6 +1483,10 @@ class Compilation extends Tapable {
1163
1483
  /** @type {Map<DependenciesBlock, { modules: Module[], blocks: AsyncDependenciesBlock[]}>} */
1164
1484
  const blockInfoMap = new Map();
1165
1485
 
1486
+ /**
1487
+ * @param {Dependency} d dependency to iterate over
1488
+ * @returns {void}
1489
+ */
1166
1490
  const iteratorDependency = d => {
1167
1491
  // We skip Dependencies without Reference
1168
1492
  const ref = this.getDependencyReference(currentModule, d);
@@ -1182,6 +1506,10 @@ class Compilation extends Tapable {
1182
1506
  blockInfoModules.add(refModule);
1183
1507
  };
1184
1508
 
1509
+ /**
1510
+ * @param {AsyncDependenciesBlock} b blocks to prepare
1511
+ * @returns {void}
1512
+ */
1185
1513
  const iteratorBlockPrepare = b => {
1186
1514
  blockInfoBlocks.push(b);
1187
1515
  blockQueue.push(b);
@@ -1191,11 +1519,11 @@ class Compilation extends Tapable {
1191
1519
  let currentModule;
1192
1520
  /** @type {DependenciesBlock} */
1193
1521
  let block;
1194
- /** @type {TODO} */
1522
+ /** @type {DependenciesBlock[]} */
1195
1523
  let blockQueue;
1196
- /** @type {Set<TODO>} */
1524
+ /** @type {Set<Module>} */
1197
1525
  let blockInfoModules;
1198
- /** @type {TODO[]} */
1526
+ /** @type {AsyncDependenciesBlock[]} */
1199
1527
  let blockInfoBlocks;
1200
1528
 
1201
1529
  for (const module of this.modules) {
@@ -1272,9 +1600,18 @@ class Compilation extends Tapable {
1272
1600
  /** @type {QueueItem[]} */
1273
1601
  let queueDelayed = [];
1274
1602
 
1275
- let module, chunk, chunkGroup;
1603
+ /** @type {Module} */
1604
+ let module;
1605
+ /** @type {Chunk} */
1606
+ let chunk;
1607
+ /** @type {ChunkGroup} */
1608
+ let chunkGroup;
1276
1609
 
1277
1610
  // For each async Block in graph
1611
+ /**
1612
+ * @param {AsyncDependenciesBlock} b iterating over each Async DepBlock
1613
+ * @returns {void}
1614
+ */
1278
1615
  const iteratorBlock = b => {
1279
1616
  // 1. We create a chunk for this Block
1280
1617
  // but only once (blockChunkGroups map)
@@ -1416,9 +1753,10 @@ class Compilation extends Tapable {
1416
1753
  }
1417
1754
 
1418
1755
  // PART TWO
1419
-
1756
+ /** @type {Set<Module>} */
1420
1757
  let availableModules;
1421
1758
  let newAvailableModules;
1759
+ /** @type {Queue<AvailableModulesChunkGroupMapping>} */
1422
1760
  const queue2 = new Queue(
1423
1761
  inputChunkGroups.map(chunkGroup => ({
1424
1762
  chunkGroup,
@@ -1426,7 +1764,13 @@ class Compilation extends Tapable {
1426
1764
  }))
1427
1765
  );
1428
1766
 
1429
- // Helper function to check if all modules of a chunk are available
1767
+ /**
1768
+ * Helper function to check if all modules of a chunk are available
1769
+ *
1770
+ * @param {ChunkGroup} chunkGroup the chunkGroup to scan
1771
+ * @param {Set<Module>} availableModules the comparitor set
1772
+ * @returns {boolean} return true if all modules of a chunk are available
1773
+ */
1430
1774
  const areModulesAvailable = (chunkGroup, availableModules) => {
1431
1775
  for (const chunk of chunkGroup.chunks) {
1432
1776
  for (const module of chunk.modulesIterable) {
@@ -1437,14 +1781,18 @@ class Compilation extends Tapable {
1437
1781
  };
1438
1782
 
1439
1783
  // For each edge in the basic chunk graph
1784
+ /**
1785
+ * @param {TODO} dep the dependency used for filtering
1786
+ * @returns {boolean} used to filter "edges" (aka Dependencies) that were pointing
1787
+ * to modules that are already available. Also filters circular dependencies in the chunks graph
1788
+ */
1440
1789
  const filterFn = dep => {
1441
- // Filter egdes that are not needed because all modules are already available
1442
- // This also filters circular dependencies in the chunks graph
1443
1790
  const depChunkGroup = dep.chunkGroup;
1444
1791
  if (areModulesAvailable(depChunkGroup, newAvailableModules)) return false; // break all modules are already available
1445
1792
  return true;
1446
1793
  };
1447
1794
 
1795
+ /** @type {Map<ChunkGroup, Set<Module>>} */
1448
1796
  const minAvailableModulesMap = new Map();
1449
1797
 
1450
1798
  // Iterative traversing of the basic chunk graph
@@ -1529,6 +1877,12 @@ class Compilation extends Tapable {
1529
1877
  }
1530
1878
  }
1531
1879
 
1880
+ /**
1881
+ *
1882
+ * @param {Module} module module relationship for removal
1883
+ * @param {DependenciesBlockLike} block //TODO: good description
1884
+ * @returns {void}
1885
+ */
1532
1886
  removeReasonsOfDependencyBlock(module, block) {
1533
1887
  const iteratorDependency = d => {
1534
1888
  if (!d.module) {
@@ -1556,6 +1910,11 @@ class Compilation extends Tapable {
1556
1910
  }
1557
1911
  }
1558
1912
 
1913
+ /**
1914
+ * @param {Module} module module to patch tie
1915
+ * @param {Chunk} chunk chunk to patch tie
1916
+ * @returns {void}
1917
+ */
1559
1918
  patchChunksAfterReasonRemoval(module, chunk) {
1560
1919
  if (!module.hasReasons()) {
1561
1920
  this.removeReasonsOfDependencyBlock(module, module);
@@ -1567,6 +1926,12 @@ class Compilation extends Tapable {
1567
1926
  }
1568
1927
  }
1569
1928
 
1929
+ /**
1930
+ *
1931
+ * @param {DependenciesBlock} block block tie for Chunk
1932
+ * @param {Chunk} chunk chunk to remove from dep
1933
+ * @returns {void}
1934
+ */
1570
1935
  removeChunkFromDependencies(block, chunk) {
1571
1936
  const iteratorDependency = d => {
1572
1937
  if (!d.module) {
@@ -1577,12 +1942,16 @@ class Compilation extends Tapable {
1577
1942
 
1578
1943
  const blocks = block.blocks;
1579
1944
  for (let indexBlock = 0; indexBlock < blocks.length; indexBlock++) {
1580
- const chunks = blocks[indexBlock].chunks;
1945
+ const asyncBlock = blocks[indexBlock];
1946
+ // Grab all chunks from the first Block's AsyncDepBlock
1947
+ const chunks = asyncBlock.chunkGroup.chunks;
1948
+ // For each chunk in chunkGroup
1581
1949
  for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
1582
- const blockChunk = chunks[indexChunk];
1583
- chunk.removeChunk(blockChunk);
1584
- blockChunk.removeParent(chunk);
1585
- this.removeChunkFromDependencies(chunks, blockChunk);
1950
+ const iteratedChunk = chunks[indexChunk];
1951
+ asyncBlock.chunkGroup.removeChunk(iteratedChunk);
1952
+ asyncBlock.chunkGroup.removeParent(iteratedChunk);
1953
+ // Recurse
1954
+ this.removeChunkFromDependencies(block, iteratedChunk);
1586
1955
  }
1587
1956
  }
1588
1957
 
@@ -1646,6 +2015,7 @@ class Compilation extends Tapable {
1646
2015
  }
1647
2016
 
1648
2017
  applyChunkIds() {
2018
+ /** @type {Set<number>} */
1649
2019
  const usedIds = new Set();
1650
2020
 
1651
2021
  // Get used ids from usedChunkIds property (i. e. from records)
@@ -1680,6 +2050,7 @@ class Compilation extends Tapable {
1680
2050
  nextFreeChunkId++;
1681
2051
 
1682
2052
  // Determine free chunk ids from 0 to maximum
2053
+ /** @type {number[]} */
1683
2054
  const unusedIds = [];
1684
2055
  if (nextFreeChunkId > 0) {
1685
2056
  let index = nextFreeChunkId;
@@ -1716,7 +2087,7 @@ class Compilation extends Tapable {
1716
2087
 
1717
2088
  const chunks = this.chunks;
1718
2089
  for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
1719
- chunks[indexChunk].sortItems(false);
2090
+ chunks[indexChunk].sortItems();
1720
2091
  }
1721
2092
  }
1722
2093
 
@@ -1737,9 +2108,19 @@ class Compilation extends Tapable {
1737
2108
 
1738
2109
  const chunks = this.chunks;
1739
2110
  for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
1740
- chunks[indexChunk].sortItems(true);
2111
+ chunks[indexChunk].sortItems();
1741
2112
  }
1742
2113
 
2114
+ /**
2115
+ * Used to sort errors and warnings in compilation. this.warnings, and
2116
+ * this.errors contribute to the compilation hash and therefore shoudl be
2117
+ * updated whenever other references (having a chunk id) are sorted. This preserves the hash
2118
+ * integrity
2119
+ *
2120
+ * @param {WebpackError} a first WebpackError instance (including subclasses)
2121
+ * @param {WebpackError} b second WebpackError instance (including subclasses)
2122
+ * @returns {-1|0|1} sort order index
2123
+ */
1743
2124
  const byMessage = (a, b) => {
1744
2125
  const ma = `${a.message}`;
1745
2126
  const mb = `${b.message}`;
@@ -1867,6 +2248,10 @@ class Compilation extends Tapable {
1867
2248
  this.hash = this.fullHash.substr(0, hashDigestLength);
1868
2249
  }
1869
2250
 
2251
+ /**
2252
+ * @param {string} update extra information
2253
+ * @returns {void}
2254
+ */
1870
2255
  modifyHash(update) {
1871
2256
  const outputOptions = this.outputOptions;
1872
2257
  const hashFunction = outputOptions.hashFunction;
@@ -1989,12 +2374,27 @@ class Compilation extends Tapable {
1989
2374
  }
1990
2375
  }
1991
2376
 
2377
+ /**
2378
+ * @param {string} filename used to get asset path with hash
2379
+ * @param {TODO=} data // TODO: figure out this param type
2380
+ * @returns {string} interpolated path
2381
+ */
1992
2382
  getPath(filename, data) {
1993
2383
  data = data || {};
1994
2384
  data.hash = data.hash || this.hash;
1995
2385
  return this.mainTemplate.getAssetPath(filename, data);
1996
2386
  }
1997
2387
 
2388
+ /**
2389
+ * This function allows you to run another instance of webpack inside of webpack however as
2390
+ * a child with different settings and configurations (if desired) applied. It copies all hooks, plugins
2391
+ * from parent (or top level compiler) and creates a child Compilation
2392
+ *
2393
+ * @param {string} name name of the child compiler
2394
+ * @param {TODO} outputOptions // Need to convert config schema to types for this
2395
+ * @param {Plugin[]} plugins webpack plugins that will be applied
2396
+ * @returns {Compiler} creates a child Compiler instance
2397
+ */
1998
2398
  createChildCompiler(name, outputOptions, plugins) {
1999
2399
  const idx = this.childrenCounters[name] || 0;
2000
2400
  this.childrenCounters[name] = idx + 1;
@@ -2008,6 +2408,7 @@ class Compilation extends Tapable {
2008
2408
  }
2009
2409
 
2010
2410
  checkConstraints() {
2411
+ /** @type {Set<number|string>} */
2011
2412
  const usedIds = new Set();
2012
2413
 
2013
2414
  const modules = this.modules;