webpack 5.90.2 → 5.91.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.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (182) hide show
  1. package/README.md +5 -5
  2. package/bin/webpack.js +5 -1
  3. package/lib/APIPlugin.js +8 -4
  4. package/lib/AutomaticPrefetchPlugin.js +1 -1
  5. package/lib/BannerPlugin.js +3 -1
  6. package/lib/Cache.js +7 -1
  7. package/lib/CacheFacade.js +3 -3
  8. package/lib/ChunkGraph.js +32 -18
  9. package/lib/ChunkGroup.js +14 -14
  10. package/lib/CleanPlugin.js +7 -5
  11. package/lib/Compilation.js +263 -94
  12. package/lib/Compiler.js +199 -83
  13. package/lib/ConditionalInitFragment.js +4 -5
  14. package/lib/ContextModule.js +4 -1
  15. package/lib/ContextModuleFactory.js +4 -2
  16. package/lib/ContextReplacementPlugin.js +3 -2
  17. package/lib/CssModule.js +0 -3
  18. package/lib/DefinePlugin.js +4 -2
  19. package/lib/DelegatedModule.js +2 -1
  20. package/lib/Dependency.js +4 -2
  21. package/lib/DependencyTemplate.js +7 -2
  22. package/lib/DllModule.js +3 -1
  23. package/lib/DllReferencePlugin.js +6 -2
  24. package/lib/EntryOptionPlugin.js +4 -1
  25. package/lib/EntryPlugin.js +6 -1
  26. package/lib/Entrypoint.js +1 -1
  27. package/lib/ExportsInfo.js +1 -4
  28. package/lib/ExternalModule.js +120 -25
  29. package/lib/ExternalModuleFactoryPlugin.js +37 -2
  30. package/lib/FileSystemInfo.js +1 -1
  31. package/lib/Generator.js +2 -1
  32. package/lib/HookWebpackError.js +2 -2
  33. package/lib/InitFragment.js +5 -3
  34. package/lib/LibManifestPlugin.js +15 -7
  35. package/lib/Module.js +54 -5
  36. package/lib/ModuleFilenameHelpers.js +1 -1
  37. package/lib/ModuleGraph.js +56 -27
  38. package/lib/ModuleGraphConnection.js +2 -1
  39. package/lib/MultiCompiler.js +61 -17
  40. package/lib/NodeStuffPlugin.js +14 -3
  41. package/lib/NormalModule.js +239 -86
  42. package/lib/NormalModuleFactory.js +163 -32
  43. package/lib/NormalModuleReplacementPlugin.js +5 -1
  44. package/lib/ProvidePlugin.js +3 -1
  45. package/lib/RawModule.js +4 -2
  46. package/lib/ResolverFactory.js +5 -1
  47. package/lib/RuntimeModule.js +6 -5
  48. package/lib/RuntimePlugin.js +1 -0
  49. package/lib/RuntimeTemplate.js +102 -34
  50. package/lib/SourceMapDevToolPlugin.js +4 -1
  51. package/lib/Stats.js +12 -5
  52. package/lib/TemplatedPathPlugin.js +32 -6
  53. package/lib/Watching.js +67 -60
  54. package/lib/WebpackError.js +6 -6
  55. package/lib/WebpackOptionsApply.js +18 -5
  56. package/lib/asset/RawDataUrlModule.js +5 -2
  57. package/lib/async-modules/AwaitDependenciesInitFragment.js +2 -2
  58. package/lib/buildChunkGraph.js +220 -392
  59. package/lib/cache/IdleFileCachePlugin.js +8 -3
  60. package/lib/cache/MemoryCachePlugin.js +1 -1
  61. package/lib/cache/MemoryWithGcCachePlugin.js +6 -2
  62. package/lib/cache/PackFileCacheStrategy.js +49 -16
  63. package/lib/cache/ResolverCachePlugin.js +14 -6
  64. package/lib/cache/getLazyHashedEtag.js +1 -1
  65. package/lib/config/defaults.js +24 -1
  66. package/lib/config/normalization.js +3 -1
  67. package/lib/container/ContainerEntryDependency.js +2 -1
  68. package/lib/container/ContainerEntryModule.js +5 -2
  69. package/lib/container/ContainerPlugin.js +14 -10
  70. package/lib/container/FallbackModule.js +3 -2
  71. package/lib/container/RemoteModule.js +2 -1
  72. package/lib/container/RemoteRuntimeModule.js +12 -3
  73. package/lib/css/CssExportsGenerator.js +34 -17
  74. package/lib/css/CssGenerator.js +20 -2
  75. package/lib/css/CssLoadingRuntimeModule.js +212 -96
  76. package/lib/css/CssModulesPlugin.js +47 -13
  77. package/lib/debug/ProfilingPlugin.js +27 -2
  78. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +9 -5
  79. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +4 -1
  80. package/lib/dependencies/CommonJsDependencyHelpers.js +2 -1
  81. package/lib/dependencies/CommonJsExportRequireDependency.js +33 -18
  82. package/lib/dependencies/CommonJsExportsDependency.js +13 -5
  83. package/lib/dependencies/CommonJsExportsParserPlugin.js +20 -15
  84. package/lib/dependencies/CommonJsImportsParserPlugin.js +1 -2
  85. package/lib/dependencies/ContextDependencyHelpers.js +49 -29
  86. package/lib/dependencies/ContextElementDependency.js +8 -1
  87. package/lib/dependencies/CssLocalIdentifierDependency.js +63 -8
  88. package/lib/dependencies/CssUrlDependency.js +5 -3
  89. package/lib/dependencies/ExportsInfoDependency.js +4 -3
  90. package/lib/dependencies/ExternalModuleInitFragment.js +5 -3
  91. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +4 -4
  92. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +43 -23
  93. package/lib/dependencies/HarmonyExportHeaderDependency.js +1 -1
  94. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +73 -32
  95. package/lib/dependencies/HarmonyExportInitFragment.js +10 -2
  96. package/lib/dependencies/HarmonyImportDependency.js +28 -12
  97. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +44 -16
  98. package/lib/dependencies/HarmonyImportSideEffectDependency.js +7 -6
  99. package/lib/dependencies/HarmonyImportSpecifierDependency.js +21 -10
  100. package/lib/dependencies/ImportDependency.js +9 -2
  101. package/lib/dependencies/ImportEagerDependency.js +4 -2
  102. package/lib/dependencies/ImportMetaContextDependency.js +7 -0
  103. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +25 -14
  104. package/lib/dependencies/ImportParserPlugin.js +12 -4
  105. package/lib/dependencies/ImportWeakDependency.js +4 -2
  106. package/lib/dependencies/LoaderDependency.js +2 -1
  107. package/lib/dependencies/LoaderImportDependency.js +2 -1
  108. package/lib/dependencies/ModuleDependency.js +4 -5
  109. package/lib/dependencies/PureExpressionDependency.js +4 -1
  110. package/lib/dependencies/RequireContextPlugin.js +1 -1
  111. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +26 -14
  112. package/lib/dependencies/RequireEnsureDependency.js +1 -1
  113. package/lib/dependencies/URLDependency.js +7 -4
  114. package/lib/dependencies/WorkerDependency.js +1 -1
  115. package/lib/dependencies/WorkerPlugin.js +6 -5
  116. package/lib/dependencies/getFunctionExpression.js +3 -1
  117. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +2 -1
  118. package/lib/hmr/LazyCompilationPlugin.js +2 -1
  119. package/lib/javascript/BasicEvaluatedExpression.js +2 -2
  120. package/lib/javascript/ChunkHelpers.js +2 -2
  121. package/lib/javascript/JavascriptParser.js +169 -57
  122. package/lib/javascript/JavascriptParserHelpers.js +1 -1
  123. package/lib/javascript/StartupHelpers.js +22 -5
  124. package/lib/logging/Logger.js +27 -2
  125. package/lib/logging/createConsoleLogger.js +11 -7
  126. package/lib/node/NodeEnvironmentPlugin.js +13 -7
  127. package/lib/node/NodeWatchFileSystem.js +37 -26
  128. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +2 -1
  129. package/lib/node/RequireChunkLoadingRuntimeModule.js +2 -1
  130. package/lib/node/nodeConsole.js +24 -1
  131. package/lib/optimize/AggressiveSplittingPlugin.js +1 -0
  132. package/lib/optimize/ConcatenatedModule.js +140 -55
  133. package/lib/optimize/EnsureChunkConditionsPlugin.js +1 -1
  134. package/lib/optimize/InnerGraph.js +7 -2
  135. package/lib/optimize/InnerGraphPlugin.js +36 -13
  136. package/lib/optimize/ModuleConcatenationPlugin.js +12 -2
  137. package/lib/optimize/RemoveParentModulesPlugin.js +1 -0
  138. package/lib/optimize/RuntimeChunkPlugin.js +6 -1
  139. package/lib/optimize/SideEffectsFlagPlugin.js +46 -15
  140. package/lib/optimize/SplitChunksPlugin.js +2 -2
  141. package/lib/performance/SizeLimitsPlugin.js +11 -0
  142. package/lib/rules/ObjectMatcherRulePlugin.js +4 -0
  143. package/lib/runtime/EnsureChunkRuntimeModule.js +2 -1
  144. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +2 -1
  145. package/lib/serialization/FileMiddleware.js +1 -0
  146. package/lib/serialization/ObjectMiddleware.js +2 -0
  147. package/lib/serialization/Serializer.js +19 -0
  148. package/lib/sharing/ConsumeSharedModule.js +3 -2
  149. package/lib/sharing/ConsumeSharedPlugin.js +17 -3
  150. package/lib/sharing/ConsumeSharedRuntimeModule.js +9 -2
  151. package/lib/sharing/ProvideSharedModule.js +2 -1
  152. package/lib/sharing/ProvideSharedPlugin.js +12 -5
  153. package/lib/sharing/resolveMatchedConfigs.js +2 -2
  154. package/lib/sharing/utils.js +13 -6
  155. package/lib/util/StackedCacheMap.js +26 -0
  156. package/lib/util/WeakTupleMap.js +57 -13
  157. package/lib/util/cleverMerge.js +24 -11
  158. package/lib/util/comparators.js +34 -14
  159. package/lib/util/conventions.js +129 -0
  160. package/lib/util/fs.js +379 -65
  161. package/lib/util/hash/BatchedHash.js +3 -0
  162. package/lib/util/hash/xxhash64.js +2 -2
  163. package/lib/util/memoize.js +2 -0
  164. package/lib/util/runtime.js +1 -1
  165. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +4 -2
  166. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +15 -6
  167. package/lib/wasm-sync/WebAssemblyGenerator.js +27 -6
  168. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +2 -1
  169. package/lib/wasm-sync/WebAssemblyParser.js +7 -4
  170. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -1
  171. package/lib/webpack.js +11 -5
  172. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +2 -1
  173. package/package.json +25 -26
  174. package/schemas/WebpackOptions.check.js +1 -1
  175. package/schemas/WebpackOptions.json +69 -8
  176. package/schemas/plugins/BannerPlugin.check.js +1 -1
  177. package/schemas/plugins/BannerPlugin.json +5 -1
  178. package/schemas/plugins/css/CssAutoGeneratorOptions.check.js +1 -1
  179. package/schemas/plugins/css/CssGeneratorOptions.check.js +1 -1
  180. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +1 -1
  181. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  182. package/types.d.ts +1772 -673
@@ -49,7 +49,6 @@ const makeSerializable = require("./util/makeSerializable");
49
49
  const memoize = require("./util/memoize");
50
50
 
51
51
  /** @typedef {import("webpack-sources").Source} Source */
52
- /** @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext} NormalModuleLoaderContext */
53
52
  /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */
54
53
  /** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
55
54
  /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
@@ -58,17 +57,23 @@ const memoize = require("./util/memoize");
58
57
  /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
59
58
  /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
60
59
  /** @typedef {import("./Generator")} Generator */
60
+ /** @typedef {import("./Module").BuildInfo} BuildInfo */
61
+ /** @typedef {import("./Module").BuildMeta} BuildMeta */
61
62
  /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
62
63
  /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
63
64
  /** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
65
+ /** @typedef {import("./Module").KnownBuildInfo} KnownBuildInfo */
64
66
  /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
65
67
  /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
68
+ /** @typedef {import("./Module").SourceTypes} SourceTypes */
69
+ /** @typedef {import("./Module").UnsafeCacheData} UnsafeCacheData */
66
70
  /** @typedef {import("./ModuleGraph")} ModuleGraph */
67
71
  /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
68
72
  /** @typedef {import("./ModuleTypeConstants").JavaScriptModuleTypes} JavaScriptModuleTypes */
69
73
  /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
70
74
  /** @typedef {import("./Parser")} Parser */
71
75
  /** @typedef {import("./RequestShortener")} RequestShortener */
76
+ /** @typedef {import("./ResolverFactory").ResolveContext} ResolveContext */
72
77
  /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
73
78
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
74
79
  /** @typedef {import("./logging/Logger").Logger} WebpackLogger */
@@ -78,6 +83,11 @@ const memoize = require("./util/memoize");
78
83
  /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
79
84
  /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
80
85
 
86
+ /** @typedef {{[k: string]: any}} ParserOptions */
87
+ /** @typedef {{[k: string]: any}} GeneratorOptions */
88
+
89
+ /** @typedef {UnsafeCacheData & { parser: undefined | Parser, parserOptions: undefined | ParserOptions, generator: undefined | Generator, generatorOptions: undefined | GeneratorOptions }} NormalModuleUnsafeCacheData */
90
+
81
91
  /**
82
92
  * @typedef {Object} SourceMap
83
93
  * @property {number} version
@@ -212,9 +222,9 @@ makeSerializable(
212
222
  * @property {string} context context directory for resolving
213
223
  * @property {string=} matchResource path + query of the matched resource (virtual)
214
224
  * @property {Parser} parser the parser used
215
- * @property {Record<string, any>=} parserOptions the options of the parser used
225
+ * @property {ParserOptions=} parserOptions the options of the parser used
216
226
  * @property {Generator} generator the generator used
217
- * @property {Record<string, any>=} generatorOptions the options of the generator used
227
+ * @property {GeneratorOptions=} generatorOptions the options of the generator used
218
228
  * @property {ResolveOptions=} resolveOptions options used for resolving requests from this module
219
229
  */
220
230
 
@@ -264,9 +274,12 @@ class NormalModule extends Module {
264
274
  ),
265
275
  needBuild: new AsyncSeriesBailHook(["module", "context"])
266
276
  };
267
- compilationHooksMap.set(compilation, hooks);
277
+ compilationHooksMap.set(
278
+ compilation,
279
+ /** @type {NormalModuleCompilationHooks} */ (hooks)
280
+ );
268
281
  }
269
- return hooks;
282
+ return /** @type {NormalModuleCompilationHooks} */ (hooks);
270
283
  }
271
284
 
272
285
  /**
@@ -300,11 +313,13 @@ class NormalModule extends Module {
300
313
  this.rawRequest = rawRequest;
301
314
  /** @type {boolean} */
302
315
  this.binary = /^(asset|webassembly)\b/.test(type);
303
- /** @type {Parser} */
316
+ /** @type {undefined | Parser} */
304
317
  this.parser = parser;
318
+ /** @type {undefined | ParserOptions} */
305
319
  this.parserOptions = parserOptions;
306
- /** @type {Generator} */
320
+ /** @type {undefined | Generator} */
307
321
  this.generator = generator;
322
+ /** @type {undefined | GeneratorOptions} */
308
323
  this.generatorOptions = generatorOptions;
309
324
  /** @type {string} */
310
325
  this.resource = resource;
@@ -319,13 +334,22 @@ class NormalModule extends Module {
319
334
  }
320
335
 
321
336
  // Info from Build
322
- /** @type {(WebpackError | null)=} */
337
+ /** @type {WebpackError | null} */
323
338
  this.error = null;
324
- /** @private @type {Source=} */
339
+ /**
340
+ * @private
341
+ * @type {Source | null}
342
+ */
325
343
  this._source = null;
326
- /** @private @type {Map<string, number> | undefined} **/
344
+ /**
345
+ * @private
346
+ * @type {Map<string, number> | undefined}
347
+ **/
327
348
  this._sourceSizes = undefined;
328
- /** @private @type {Set<string>} */
349
+ /**
350
+ * @private
351
+ * @type {undefined | SourceTypes}
352
+ **/
329
353
  this._sourceTypes = undefined;
330
354
 
331
355
  // Cache
@@ -419,7 +443,7 @@ class NormalModule extends Module {
419
443
  // TODO reconsider this for webpack 6
420
444
  if (this.buildInfo) {
421
445
  if (this._sourceTypes === undefined) this.getSourceTypes();
422
- for (const type of this._sourceTypes) {
446
+ for (const type of /** @type {SourceTypes} */ (this._sourceTypes)) {
423
447
  this.size(type);
424
448
  }
425
449
  }
@@ -433,15 +457,22 @@ class NormalModule extends Module {
433
457
  /**
434
458
  * Module should be unsafe cached. Get data that's needed for that.
435
459
  * This data will be passed to restoreFromUnsafeCache later.
436
- * @returns {object} cached data
460
+ * @returns {UnsafeCacheData} cached data
437
461
  */
438
462
  getUnsafeCacheData() {
439
- const data = super.getUnsafeCacheData();
463
+ const data =
464
+ /** @type {NormalModuleUnsafeCacheData} */
465
+ (super.getUnsafeCacheData());
440
466
  data.parserOptions = this.parserOptions;
441
467
  data.generatorOptions = this.generatorOptions;
442
468
  return data;
443
469
  }
444
470
 
471
+ /**
472
+ * restore unsafe cache data
473
+ * @param {NormalModuleUnsafeCacheData} unsafeCacheData data from getUnsafeCacheData
474
+ * @param {NormalModuleFactory} normalModuleFactory the normal module factory handling the unsafe caching
475
+ */
445
476
  restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory) {
446
477
  this._restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory);
447
478
  }
@@ -466,8 +497,8 @@ class NormalModule extends Module {
466
497
  /**
467
498
  * @param {string} context the compilation context
468
499
  * @param {string} name the asset name
469
- * @param {string} content the content
470
- * @param {string | TODO} sourceMap an optional source map
500
+ * @param {string | Buffer} content the content
501
+ * @param {(string | SourceMap)=} sourceMap an optional source map
471
502
  * @param {Object=} associatedObjectForCache object for caching
472
503
  * @returns {Source} the created source
473
504
  */
@@ -493,7 +524,11 @@ class NormalModule extends Module {
493
524
  return new SourceMapSource(
494
525
  content,
495
526
  name,
496
- contextifySourceMap(context, sourceMap, associatedObjectForCache)
527
+ contextifySourceMap(
528
+ context,
529
+ /** @type {SourceMap} */ (sourceMap),
530
+ associatedObjectForCache
531
+ )
497
532
  );
498
533
  }
499
534
  }
@@ -502,12 +537,14 @@ class NormalModule extends Module {
502
537
  }
503
538
 
504
539
  /**
540
+ * @private
541
+ * @template T
505
542
  * @param {ResolverWithOptions} resolver a resolver
506
543
  * @param {WebpackOptions} options webpack options
507
544
  * @param {Compilation} compilation the compilation
508
545
  * @param {InputFileSystem} fs file system from reading
509
546
  * @param {NormalModuleCompilationHooks} hooks the hooks
510
- * @returns {NormalModuleLoaderContext} loader context
547
+ * @returns {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} loader context
511
548
  */
512
549
  _createLoaderContext(resolver, options, compilation, fs, hooks) {
513
550
  const { requestShortener } = compilation.runtimeTemplate;
@@ -516,16 +553,19 @@ class NormalModule extends Module {
516
553
  if (!currentLoader) return "(not in loader scope)";
517
554
  return requestShortener.shorten(currentLoader.loader);
518
555
  };
556
+ /**
557
+ * @returns {ResolveContext} resolve context
558
+ */
519
559
  const getResolveContext = () => {
520
560
  return {
521
561
  fileDependencies: {
522
- add: d => loaderContext.addDependency(d)
562
+ add: d => /** @type {TODO} */ (loaderContext).addDependency(d)
523
563
  },
524
564
  contextDependencies: {
525
- add: d => loaderContext.addContextDependency(d)
565
+ add: d => /** @type {TODO} */ (loaderContext).addContextDependency(d)
526
566
  },
527
567
  missingDependencies: {
528
- add: d => loaderContext.addMissingDependency(d)
568
+ add: d => /** @type {TODO} */ (loaderContext).addMissingDependency(d)
529
569
  }
530
570
  };
531
571
  };
@@ -542,26 +582,41 @@ class NormalModule extends Module {
542
582
  contextify.bindContextCache(this.context, compilation.compiler.root)
543
583
  );
544
584
  const utils = {
585
+ /**
586
+ * @param {string} context context
587
+ * @param {string} request request
588
+ * @returns {string} result
589
+ */
545
590
  absolutify: (context, request) => {
546
591
  return context === this.context
547
592
  ? getAbsolutifyInContext()(request)
548
593
  : getAbsolutify()(context, request);
549
594
  },
595
+ /**
596
+ * @param {string} context context
597
+ * @param {string} request request
598
+ * @returns {string} result
599
+ */
550
600
  contextify: (context, request) => {
551
601
  return context === this.context
552
602
  ? getContextifyInContext()(request)
553
603
  : getContextify()(context, request);
554
604
  },
605
+ /**
606
+ * @param {(string | typeof import("./util/Hash"))=} type type
607
+ * @returns {Hash} hash
608
+ */
555
609
  createHash: type => {
556
610
  return createHash(type || compilation.outputOptions.hashFunction);
557
611
  }
558
612
  };
613
+ /** @type {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} */
559
614
  const loaderContext = {
560
615
  version: 2,
561
616
  getOptions: schema => {
562
617
  const loader = this.getCurrentLoader(loaderContext);
563
618
 
564
- let { options } = loader;
619
+ let { options } = /** @type {LoaderItem} */ (loader);
565
620
 
566
621
  if (typeof options === "string") {
567
622
  if (options.startsWith("{") && options.endsWith("}")) {
@@ -649,27 +704,39 @@ class NormalModule extends Module {
649
704
  };
650
705
  },
651
706
  emitFile: (name, content, sourceMap, assetInfo) => {
652
- if (!this.buildInfo.assets) {
653
- this.buildInfo.assets = Object.create(null);
654
- this.buildInfo.assetsInfo = new Map();
707
+ const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
708
+
709
+ if (!buildInfo.assets) {
710
+ buildInfo.assets = Object.create(null);
711
+ buildInfo.assetsInfo = new Map();
655
712
  }
656
- this.buildInfo.assets[name] = this.createSourceForAsset(
657
- options.context,
713
+
714
+ const assets =
715
+ /** @type {NonNullable<KnownBuildInfo["assets"]>} */
716
+ (buildInfo.assets);
717
+ const assetsInfo =
718
+ /** @type {NonNullable<KnownBuildInfo["assetsInfo"]>} */
719
+ (buildInfo.assetsInfo);
720
+
721
+ assets[name] = this.createSourceForAsset(
722
+ /** @type {string} */ (options.context),
658
723
  name,
659
724
  content,
660
725
  sourceMap,
661
726
  compilation.compiler.root
662
727
  );
663
- this.buildInfo.assetsInfo.set(name, assetInfo);
728
+ assetsInfo.set(name, assetInfo);
664
729
  },
665
730
  addBuildDependency: dep => {
666
- if (this.buildInfo.buildDependencies === undefined) {
667
- this.buildInfo.buildDependencies = new LazySet();
731
+ const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
732
+
733
+ if (buildInfo.buildDependencies === undefined) {
734
+ buildInfo.buildDependencies = new LazySet();
668
735
  }
669
- this.buildInfo.buildDependencies.add(dep);
736
+ buildInfo.buildDependencies.add(dep);
670
737
  },
671
738
  utils,
672
- rootContext: options.context,
739
+ rootContext: /** @type {string} */ (options.context),
673
740
  webpack: true,
674
741
  sourceMap: !!this.useSourceMap,
675
742
  mode: options.mode || "production",
@@ -686,6 +753,12 @@ class NormalModule extends Module {
686
753
  return loaderContext;
687
754
  }
688
755
 
756
+ // TODO remove `loaderContext` in webpack@6
757
+ /**
758
+ * @param {TODO} loaderContext loader context
759
+ * @param {number} index index
760
+ * @returns {LoaderItem | null} loader
761
+ */
689
762
  getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
690
763
  if (
691
764
  this.loaders &&
@@ -702,7 +775,7 @@ class NormalModule extends Module {
702
775
  /**
703
776
  * @param {string} context the compilation context
704
777
  * @param {string | Buffer} content the content
705
- * @param {string | TODO} sourceMap an optional source map
778
+ * @param {(string | SourceMapSource)=} sourceMap an optional source map
706
779
  * @param {Object=} associatedObjectForCache object for caching
707
780
  * @returns {Source} the created source
708
781
  */
@@ -723,7 +796,11 @@ class NormalModule extends Module {
723
796
  return new SourceMapSource(
724
797
  content,
725
798
  contextifySourceUrl(context, identifier, associatedObjectForCache),
726
- contextifySourceMap(context, sourceMap, associatedObjectForCache)
799
+ contextifySourceMap(
800
+ context,
801
+ /** @type {TODO} */ (sourceMap),
802
+ associatedObjectForCache
803
+ )
727
804
  );
728
805
  }
729
806
 
@@ -791,7 +868,7 @@ class NormalModule extends Module {
791
868
  }
792
869
 
793
870
  this._source = this.createSource(
794
- options.context,
871
+ /** @type {string} */ (options.context),
795
872
  this.binary ? asBuffer(source) : asString(source),
796
873
  sourceMap,
797
874
  compilation.compiler.root
@@ -806,10 +883,12 @@ class NormalModule extends Module {
806
883
  return callback();
807
884
  };
808
885
 
809
- this.buildInfo.fileDependencies = new LazySet();
810
- this.buildInfo.contextDependencies = new LazySet();
811
- this.buildInfo.missingDependencies = new LazySet();
812
- this.buildInfo.cacheable = true;
886
+ const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
887
+
888
+ buildInfo.fileDependencies = new LazySet();
889
+ buildInfo.contextDependencies = new LazySet();
890
+ buildInfo.missingDependencies = new LazySet();
891
+ buildInfo.cacheable = true;
813
892
 
814
893
  try {
815
894
  hooks.beforeLoaders.call(this.loaders, this, loaderContext);
@@ -819,7 +898,8 @@ class NormalModule extends Module {
819
898
  }
820
899
 
821
900
  if (this.loaders.length > 0) {
822
- this.buildInfo.buildDependencies = new LazySet();
901
+ /** @type {BuildInfo} */
902
+ (this.buildInfo).buildDependencies = new LazySet();
823
903
  }
824
904
 
825
905
  runLoaders(
@@ -846,23 +926,43 @@ class NormalModule extends Module {
846
926
  loaderContext._compilation =
847
927
  loaderContext._compiler =
848
928
  loaderContext._module =
929
+ // eslint-disable-next-line no-warning-comments
930
+ // @ts-ignore
849
931
  loaderContext.fs =
850
932
  undefined;
851
933
 
852
934
  if (!result) {
853
- this.buildInfo.cacheable = false;
935
+ /** @type {BuildInfo} */
936
+ (this.buildInfo).cacheable = false;
854
937
  return processResult(
855
938
  err || new Error("No result from loader-runner processing"),
856
939
  null
857
940
  );
858
941
  }
859
- this.buildInfo.fileDependencies.addAll(result.fileDependencies);
860
- this.buildInfo.contextDependencies.addAll(result.contextDependencies);
861
- this.buildInfo.missingDependencies.addAll(result.missingDependencies);
942
+
943
+ const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
944
+
945
+ const fileDependencies =
946
+ /** @type {NonNullable<KnownBuildInfo["fileDependencies"]>} */
947
+ (buildInfo.fileDependencies);
948
+ const contextDependencies =
949
+ /** @type {NonNullable<KnownBuildInfo["contextDependencies"]>} */
950
+ (buildInfo.contextDependencies);
951
+ const missingDependencies =
952
+ /** @type {NonNullable<KnownBuildInfo["missingDependencies"]>} */
953
+ (buildInfo.missingDependencies);
954
+
955
+ fileDependencies.addAll(result.fileDependencies);
956
+ contextDependencies.addAll(result.contextDependencies);
957
+ missingDependencies.addAll(result.missingDependencies);
862
958
  for (const loader of this.loaders) {
863
- this.buildInfo.buildDependencies.add(loader.loader);
959
+ const buildDependencies =
960
+ /** @type {NonNullable<KnownBuildInfo["buildDependencies"]>} */
961
+ (buildInfo.buildDependencies);
962
+
963
+ buildDependencies.add(loader.loader);
864
964
  }
865
- this.buildInfo.cacheable = this.buildInfo.cacheable && result.cacheable;
965
+ buildInfo.cacheable = buildInfo.cacheable && result.cacheable;
866
966
  processResult(err, result.result);
867
967
  }
868
968
  );
@@ -879,6 +979,11 @@ class NormalModule extends Module {
879
979
  this.addError(error);
880
980
  }
881
981
 
982
+ /**
983
+ * @param {TODO} rule rule
984
+ * @param {string} content content
985
+ * @returns {boolean} result
986
+ */
882
987
  applyNoParseRule(rule, content) {
883
988
  // must start with "rule" if rule is a string
884
989
  if (typeof rule === "string") {
@@ -892,9 +997,11 @@ class NormalModule extends Module {
892
997
  return rule.test(content);
893
998
  }
894
999
 
895
- // check if module should not be parsed
896
- // returns "true" if the module should !not! be parsed
897
- // returns "false" if the module !must! be parsed
1000
+ /**
1001
+ * @param {TODO} noParseRule no parse rule
1002
+ * @param {string} request request
1003
+ * @returns {boolean} check if module should not be parsed, returns "true" if the module should !not! be parsed, returns "false" if the module !must! be parsed
1004
+ */
898
1005
  shouldPreventParsing(noParseRule, request) {
899
1006
  // if no noParseRule exists, return false
900
1007
  // the module !must! be parsed.
@@ -920,6 +1027,10 @@ class NormalModule extends Module {
920
1027
  return false;
921
1028
  }
922
1029
 
1030
+ /**
1031
+ * @param {Compilation} compilation compilation
1032
+ * @private
1033
+ */
923
1034
  _initBuildHash(compilation) {
924
1035
  const hash = createHash(compilation.outputOptions.hashFunction);
925
1036
  if (this._source) {
@@ -928,7 +1039,8 @@ class NormalModule extends Module {
928
1039
  }
929
1040
  hash.update("meta");
930
1041
  hash.update(JSON.stringify(this.buildMeta));
931
- this.buildInfo.hash = /** @type {string} */ (hash.digest("hex"));
1042
+ /** @type {BuildInfo} */
1043
+ (this.buildInfo).hash = /** @type {string} */ (hash.digest("hex"));
932
1044
  }
933
1045
 
934
1046
  /**
@@ -974,10 +1086,18 @@ class NormalModule extends Module {
974
1086
  return callback();
975
1087
  }
976
1088
 
1089
+ /**
1090
+ * @param {Error} e error
1091
+ * @returns {void}
1092
+ */
977
1093
  const handleParseError = e => {
978
- const source = this._source.source();
1094
+ const source = /** @type {Source} */ (this._source).source();
979
1095
  const loaders = this.loaders.map(item =>
980
- contextify(options.context, item.loader, compilation.compiler.root)
1096
+ contextify(
1097
+ /** @type {string} */ (options.context),
1098
+ item.loader,
1099
+ compilation.compiler.root
1100
+ )
981
1101
  );
982
1102
  const error = new ModuleParseError(source, e, loaders, this.type);
983
1103
  this.markModuleAsErrored(error);
@@ -985,7 +1105,7 @@ class NormalModule extends Module {
985
1105
  return callback();
986
1106
  };
987
1107
 
988
- const handleParseResult = result => {
1108
+ const handleParseResult = () => {
989
1109
  this.dependencies.sort(
990
1110
  concatComparators(
991
1111
  compareSelect(a => a.loc, compareLocations),
@@ -993,7 +1113,9 @@ class NormalModule extends Module {
993
1113
  )
994
1114
  );
995
1115
  this._initBuildHash(compilation);
996
- this._lastSuccessfulBuildMeta = this.buildMeta;
1116
+ this._lastSuccessfulBuildMeta =
1117
+ /** @type {BuildMeta} */
1118
+ (this.buildMeta);
997
1119
  return handleBuildDone();
998
1120
  };
999
1121
 
@@ -1006,12 +1128,17 @@ class NormalModule extends Module {
1006
1128
  }
1007
1129
 
1008
1130
  const snapshotOptions = compilation.options.snapshot.module;
1009
- if (!this.buildInfo.cacheable || !snapshotOptions) {
1131
+ const { cacheable } = /** @type {BuildInfo} */ (this.buildInfo);
1132
+ if (!cacheable || !snapshotOptions) {
1010
1133
  return callback();
1011
1134
  }
1012
1135
  // add warning for all non-absolute paths in fileDependencies, etc
1013
1136
  // This makes it easier to find problems with watching and/or caching
1137
+ /** @type {undefined | Set<string>} */
1014
1138
  let nonAbsoluteDependencies = undefined;
1139
+ /**
1140
+ * @param {LazySet<string>} deps deps
1141
+ */
1015
1142
  const checkDependencies = deps => {
1016
1143
  for (const dep of deps) {
1017
1144
  if (!ABSOLUTE_PATH_REGEX.test(dep)) {
@@ -1028,7 +1155,11 @@ class NormalModule extends Module {
1028
1155
  );
1029
1156
  if (absolute !== dep && ABSOLUTE_PATH_REGEX.test(absolute)) {
1030
1157
  (depWithoutGlob !== dep
1031
- ? this.buildInfo.contextDependencies
1158
+ ? /** @type {NonNullable<KnownBuildInfo["contextDependencies"]>} */
1159
+ (
1160
+ /** @type {BuildInfo} */ (this.buildInfo)
1161
+ .contextDependencies
1162
+ )
1032
1163
  : deps
1033
1164
  ).add(absolute);
1034
1165
  }
@@ -1038,9 +1169,19 @@ class NormalModule extends Module {
1038
1169
  }
1039
1170
  }
1040
1171
  };
1041
- checkDependencies(this.buildInfo.fileDependencies);
1042
- checkDependencies(this.buildInfo.missingDependencies);
1043
- checkDependencies(this.buildInfo.contextDependencies);
1172
+ const buildInfo = /** @type {BuildInfo} */ (this.buildInfo);
1173
+ const fileDependencies =
1174
+ /** @type {NonNullable<KnownBuildInfo["fileDependencies"]>} */
1175
+ (buildInfo.fileDependencies);
1176
+ const contextDependencies =
1177
+ /** @type {NonNullable<KnownBuildInfo["contextDependencies"]>} */
1178
+ (buildInfo.contextDependencies);
1179
+ const missingDependencies =
1180
+ /** @type {NonNullable<KnownBuildInfo["missingDependencies"]>} */
1181
+ (buildInfo.missingDependencies);
1182
+ checkDependencies(fileDependencies);
1183
+ checkDependencies(missingDependencies);
1184
+ checkDependencies(contextDependencies);
1044
1185
  if (nonAbsoluteDependencies !== undefined) {
1045
1186
  const InvalidDependenciesModuleWarning =
1046
1187
  getInvalidDependenciesModuleWarning();
@@ -1051,19 +1192,19 @@ class NormalModule extends Module {
1051
1192
  // convert file/context/missingDependencies into filesystem snapshot
1052
1193
  compilation.fileSystemInfo.createSnapshot(
1053
1194
  startTime,
1054
- this.buildInfo.fileDependencies,
1055
- this.buildInfo.contextDependencies,
1056
- this.buildInfo.missingDependencies,
1195
+ fileDependencies,
1196
+ contextDependencies,
1197
+ missingDependencies,
1057
1198
  snapshotOptions,
1058
1199
  (err, snapshot) => {
1059
1200
  if (err) {
1060
1201
  this.markModuleAsErrored(err);
1061
1202
  return;
1062
1203
  }
1063
- this.buildInfo.fileDependencies = undefined;
1064
- this.buildInfo.contextDependencies = undefined;
1065
- this.buildInfo.missingDependencies = undefined;
1066
- this.buildInfo.snapshot = snapshot;
1204
+ buildInfo.fileDependencies = undefined;
1205
+ buildInfo.contextDependencies = undefined;
1206
+ buildInfo.missingDependencies = undefined;
1207
+ buildInfo.snapshot = snapshot;
1067
1208
  return callback();
1068
1209
  }
1069
1210
  );
@@ -1082,15 +1223,16 @@ class NormalModule extends Module {
1082
1223
  const noParseRule = options.module && options.module.noParse;
1083
1224
  if (this.shouldPreventParsing(noParseRule, this.request)) {
1084
1225
  // We assume that we need module and exports
1085
- this.buildInfo.parsed = false;
1226
+ /** @type {BuildInfo} */
1227
+ (this.buildInfo).parsed = false;
1086
1228
  this._initBuildHash(compilation);
1087
1229
  return handleBuildDone();
1088
1230
  }
1089
1231
 
1090
- let result;
1091
1232
  try {
1092
- const source = this._source.source();
1093
- result = this.parser.parse(this._ast || source, {
1233
+ const source = /** @type {Source} */ (this._source).source();
1234
+ /** @type {Parser} */
1235
+ (this.parser).parse(this._ast || source, {
1094
1236
  source,
1095
1237
  current: this,
1096
1238
  module: this,
@@ -1098,10 +1240,10 @@ class NormalModule extends Module {
1098
1240
  options: options
1099
1241
  });
1100
1242
  } catch (e) {
1101
- handleParseError(e);
1243
+ handleParseError(/** @type {Error} */ (e));
1102
1244
  return;
1103
1245
  }
1104
- handleParseResult(result);
1246
+ handleParseResult();
1105
1247
  });
1106
1248
  }
1107
1249
 
@@ -1110,7 +1252,9 @@ class NormalModule extends Module {
1110
1252
  * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
1111
1253
  */
1112
1254
  getConcatenationBailoutReason(context) {
1113
- return this.generator.getConcatenationBailoutReason(this, context);
1255
+ return /** @type {Generator} */ (
1256
+ this.generator
1257
+ ).getConcatenationBailoutReason(this, context);
1114
1258
  }
1115
1259
 
1116
1260
  /**
@@ -1162,11 +1306,13 @@ class NormalModule extends Module {
1162
1306
  }
1163
1307
 
1164
1308
  /**
1165
- * @returns {Set<string>} types available (do not mutate)
1309
+ * @returns {SourceTypes} types available (do not mutate)
1166
1310
  */
1167
1311
  getSourceTypes() {
1168
1312
  if (this._sourceTypes === undefined) {
1169
- this._sourceTypes = this.generator.getTypes(this);
1313
+ this._sourceTypes = /** @type {Generator} */ (this.generator).getTypes(
1314
+ this
1315
+ );
1170
1316
  }
1171
1317
  return this._sourceTypes;
1172
1318
  }
@@ -1189,7 +1335,9 @@ class NormalModule extends Module {
1189
1335
  /** @type {Set<string>} */
1190
1336
  const runtimeRequirements = new Set();
1191
1337
 
1192
- if (!this.buildInfo.parsed) {
1338
+ const { parsed } = /** @type {BuildInfo} */ (this.buildInfo);
1339
+
1340
+ if (!parsed) {
1193
1341
  runtimeRequirements.add(RuntimeGlobals.module);
1194
1342
  runtimeRequirements.add(RuntimeGlobals.exports);
1195
1343
  runtimeRequirements.add(RuntimeGlobals.thisAsExports);
@@ -1206,7 +1354,7 @@ class NormalModule extends Module {
1206
1354
  ? new RawSource(
1207
1355
  "throw new Error(" + JSON.stringify(this.error.message) + ");"
1208
1356
  )
1209
- : this.generator.generate(this, {
1357
+ : /** @type {Generator} */ (this.generator).generate(this, {
1210
1358
  dependencyTemplates,
1211
1359
  runtimeTemplate,
1212
1360
  moduleGraph,
@@ -1261,15 +1409,16 @@ class NormalModule extends Module {
1261
1409
  // always try to build in case of an error
1262
1410
  if (this.error) return callback(null, true);
1263
1411
 
1412
+ const { cacheable, snapshot, valueDependencies } =
1413
+ /** @type {BuildInfo} */ (this.buildInfo);
1414
+
1264
1415
  // always build when module is not cacheable
1265
- if (!this.buildInfo.cacheable) return callback(null, true);
1416
+ if (!cacheable) return callback(null, true);
1266
1417
 
1267
1418
  // build when there is no snapshot to check
1268
- if (!this.buildInfo.snapshot) return callback(null, true);
1419
+ if (!snapshot) return callback(null, true);
1269
1420
 
1270
1421
  // build when valueDependencies have changed
1271
- /** @type {Map<string, string | Set<string>>} */
1272
- const valueDependencies = this.buildInfo.valueDependencies;
1273
1422
  if (valueDependencies) {
1274
1423
  if (!valueCacheVersions) return callback(null, true);
1275
1424
  for (const [key, value] of valueDependencies) {
@@ -1288,7 +1437,7 @@ class NormalModule extends Module {
1288
1437
  }
1289
1438
 
1290
1439
  // check snapshot for validity
1291
- fileSystemInfo.checkSnapshotValid(this.buildInfo.snapshot, (err, valid) => {
1440
+ fileSystemInfo.checkSnapshotValid(snapshot, (err, valid) => {
1292
1441
  if (err) return callback(err);
1293
1442
  if (!valid) return callback(null, true);
1294
1443
  const hooks = NormalModule.getCompilationHooks(compilation);
@@ -1316,7 +1465,10 @@ class NormalModule extends Module {
1316
1465
  if (cachedSize !== undefined) {
1317
1466
  return cachedSize;
1318
1467
  }
1319
- const size = Math.max(1, this.generator.getSize(this, type));
1468
+ const size = Math.max(
1469
+ 1,
1470
+ /** @type {Generator} */ (this.generator).getSize(this, type)
1471
+ );
1320
1472
  if (this._sourceSizes === undefined) {
1321
1473
  this._sourceSizes = new Map();
1322
1474
  }
@@ -1336,7 +1488,8 @@ class NormalModule extends Module {
1336
1488
  missingDependencies,
1337
1489
  buildDependencies
1338
1490
  ) {
1339
- const { snapshot, buildDependencies: buildDeps } = this.buildInfo;
1491
+ const { snapshot, buildDependencies: buildDeps } =
1492
+ /** @type {BuildInfo} */ (this.buildInfo);
1340
1493
  if (snapshot) {
1341
1494
  fileDependencies.addAll(snapshot.getFileIterable());
1342
1495
  contextDependencies.addAll(snapshot.getContextIterable());
@@ -1346,7 +1499,7 @@ class NormalModule extends Module {
1346
1499
  fileDependencies: fileDeps,
1347
1500
  contextDependencies: contextDeps,
1348
1501
  missingDependencies: missingDeps
1349
- } = this.buildInfo;
1502
+ } = /** @type {BuildInfo} */ (this.buildInfo);
1350
1503
  if (fileDeps !== undefined) fileDependencies.addAll(fileDeps);
1351
1504
  if (contextDeps !== undefined) contextDependencies.addAll(contextDeps);
1352
1505
  if (missingDeps !== undefined) missingDependencies.addAll(missingDeps);
@@ -1362,7 +1515,7 @@ class NormalModule extends Module {
1362
1515
  * @returns {void}
1363
1516
  */
1364
1517
  updateHash(hash, context) {
1365
- hash.update(this.buildInfo.hash);
1518
+ hash.update(/** @type {BuildInfo} */ (this.buildInfo).hash);
1366
1519
  this.generator.updateHash(hash, {
1367
1520
  module: this,
1368
1521
  ...context