webpack 5.97.1 → 5.99.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 (262) hide show
  1. package/README.md +345 -392
  2. package/lib/AsyncDependenciesBlock.js +3 -1
  3. package/lib/BannerPlugin.js +1 -1
  4. package/lib/Cache.js +9 -7
  5. package/lib/CacheFacade.js +5 -5
  6. package/lib/Chunk.js +2 -2
  7. package/lib/ChunkGraph.js +21 -16
  8. package/lib/ChunkTemplate.js +6 -6
  9. package/lib/CleanPlugin.js +10 -10
  10. package/lib/CodeGenerationResults.js +4 -3
  11. package/lib/CompatibilityPlugin.js +4 -1
  12. package/lib/Compilation.js +343 -167
  13. package/lib/Compiler.js +13 -18
  14. package/lib/ConditionalInitFragment.js +1 -1
  15. package/lib/ConstPlugin.js +5 -3
  16. package/lib/ContextModule.js +4 -2
  17. package/lib/ContextModuleFactory.js +3 -3
  18. package/lib/ContextReplacementPlugin.js +43 -16
  19. package/lib/DefinePlugin.js +25 -24
  20. package/lib/DelegatedModule.js +4 -2
  21. package/lib/DelegatedModuleFactoryPlugin.js +2 -1
  22. package/lib/Dependency.js +19 -13
  23. package/lib/DependencyTemplates.js +4 -3
  24. package/lib/DllModule.js +4 -2
  25. package/lib/DllModuleFactory.js +2 -2
  26. package/lib/DllReferencePlugin.js +2 -1
  27. package/lib/DynamicEntryPlugin.js +1 -1
  28. package/lib/EnvironmentPlugin.js +4 -2
  29. package/lib/ExportsInfo.js +72 -40
  30. package/lib/ExternalModule.js +14 -5
  31. package/lib/ExternalModuleFactoryPlugin.js +24 -12
  32. package/lib/FileSystemInfo.js +129 -94
  33. package/lib/FlagDependencyExportsPlugin.js +6 -4
  34. package/lib/FlagDependencyUsagePlugin.js +1 -1
  35. package/lib/Generator.js +29 -1
  36. package/lib/HookWebpackError.js +2 -2
  37. package/lib/HotModuleReplacementPlugin.js +3 -9
  38. package/lib/IgnoreErrorModuleFactory.js +2 -2
  39. package/lib/IgnorePlugin.js +0 -5
  40. package/lib/InitFragment.js +1 -1
  41. package/lib/LoaderOptionsPlugin.js +8 -5
  42. package/lib/MainTemplate.js +7 -7
  43. package/lib/Module.js +44 -21
  44. package/lib/ModuleBuildError.js +3 -1
  45. package/lib/ModuleDependencyError.js +4 -3
  46. package/lib/ModuleDependencyWarning.js +4 -3
  47. package/lib/ModuleFactory.js +9 -3
  48. package/lib/ModuleFilenameHelpers.js +21 -39
  49. package/lib/ModuleGraph.js +20 -14
  50. package/lib/ModuleGraphConnection.js +7 -13
  51. package/lib/ModuleNotFoundError.js +1 -1
  52. package/lib/ModuleParseError.js +2 -1
  53. package/lib/ModuleSourceTypesConstants.js +11 -0
  54. package/lib/ModuleTemplate.js +5 -5
  55. package/lib/ModuleTypeConstants.js +15 -0
  56. package/lib/MultiCompiler.js +4 -4
  57. package/lib/MultiStats.js +1 -1
  58. package/lib/NormalModule.js +101 -54
  59. package/lib/NormalModuleFactory.js +38 -33
  60. package/lib/NormalModuleReplacementPlugin.js +3 -2
  61. package/lib/NullFactory.js +2 -2
  62. package/lib/Parser.js +4 -3
  63. package/lib/ProgressPlugin.js +1 -2
  64. package/lib/RawModule.js +4 -2
  65. package/lib/RecordIdsPlugin.js +6 -2
  66. package/lib/RequestShortener.js +3 -1
  67. package/lib/ResolverFactory.js +12 -9
  68. package/lib/RuntimeModule.js +4 -2
  69. package/lib/RuntimeTemplate.js +2 -1
  70. package/lib/SelfModuleFactory.js +2 -2
  71. package/lib/SourceMapDevToolPlugin.js +0 -8
  72. package/lib/Template.js +6 -5
  73. package/lib/TemplatedPathPlugin.js +15 -12
  74. package/lib/WebpackOptionsApply.js +1 -1
  75. package/lib/asset/AssetGenerator.js +237 -170
  76. package/lib/asset/AssetModulesPlugin.js +52 -10
  77. package/lib/asset/AssetSourceGenerator.js +18 -0
  78. package/lib/asset/RawDataUrlModule.js +4 -2
  79. package/lib/buildChunkGraph.js +14 -4
  80. package/lib/cache/MemoryWithGcCachePlugin.js +6 -2
  81. package/lib/cache/PackFileCacheStrategy.js +137 -121
  82. package/lib/cache/ResolverCachePlugin.js +15 -7
  83. package/lib/cache/getLazyHashedEtag.js +4 -3
  84. package/lib/cli.js +23 -15
  85. package/lib/config/defaults.js +104 -28
  86. package/lib/config/normalization.js +14 -13
  87. package/lib/config/target.js +8 -8
  88. package/lib/container/ContainerEntryModule.js +4 -2
  89. package/lib/container/ContainerEntryModuleFactory.js +2 -2
  90. package/lib/container/FallbackModule.js +4 -2
  91. package/lib/container/FallbackModuleFactory.js +2 -2
  92. package/lib/container/RemoteModule.js +4 -2
  93. package/lib/container/options.js +5 -5
  94. package/lib/css/CssGenerator.js +71 -9
  95. package/lib/css/CssLoadingRuntimeModule.js +5 -2
  96. package/lib/css/CssModulesPlugin.js +41 -9
  97. package/lib/css/CssParser.js +37 -17
  98. package/lib/css/walkCssTokens.js +17 -17
  99. package/lib/debug/ProfilingPlugin.js +98 -38
  100. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +10 -4
  101. package/lib/dependencies/AMDPlugin.js +5 -2
  102. package/lib/dependencies/AMDRequireArrayDependency.js +4 -3
  103. package/lib/dependencies/AMDRequireContextDependency.js +2 -1
  104. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +15 -7
  105. package/lib/dependencies/AMDRuntimeModules.js +3 -1
  106. package/lib/dependencies/CommonJsExportRequireDependency.js +4 -5
  107. package/lib/dependencies/CommonJsExportsParserPlugin.js +3 -3
  108. package/lib/dependencies/CommonJsImportsParserPlugin.js +38 -7
  109. package/lib/dependencies/CommonJsRequireContextDependency.js +2 -1
  110. package/lib/dependencies/ContextDependencyHelpers.js +13 -6
  111. package/lib/dependencies/CssIcssExportDependency.js +15 -12
  112. package/lib/dependencies/CssIcssImportDependency.js +4 -1
  113. package/lib/dependencies/CssLocalIdentifierDependency.js +18 -15
  114. package/lib/dependencies/ExportsInfoDependency.js +6 -1
  115. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +5 -5
  116. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +22 -15
  117. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +23 -8
  118. package/lib/dependencies/HarmonyExportSpecifierDependency.js +2 -2
  119. package/lib/dependencies/HarmonyImportDependency.js +8 -6
  120. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +19 -14
  121. package/lib/dependencies/HarmonyImportSpecifierDependency.js +5 -5
  122. package/lib/dependencies/ImportContextDependency.js +2 -1
  123. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +8 -4
  124. package/lib/dependencies/JsonExportsDependency.js +49 -27
  125. package/lib/dependencies/LoaderPlugin.js +4 -14
  126. package/lib/dependencies/RequireContextDependency.js +2 -1
  127. package/lib/dependencies/RequireContextDependencyParserPlugin.js +6 -3
  128. package/lib/dependencies/RequireEnsureDependenciesBlock.js +3 -2
  129. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +11 -5
  130. package/lib/dependencies/RequireResolveContextDependency.js +1 -1
  131. package/lib/dependencies/WebAssemblyExportImportedDependency.js +1 -1
  132. package/lib/dependencies/WorkerDependency.js +6 -3
  133. package/lib/dependencies/WorkerPlugin.js +100 -41
  134. package/lib/esm/ModuleChunkFormatPlugin.js +5 -0
  135. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +3 -2
  136. package/lib/hmr/HotModuleReplacement.runtime.js +1 -0
  137. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +1 -0
  138. package/lib/hmr/LazyCompilationPlugin.js +32 -24
  139. package/lib/hmr/lazyCompilationBackend.js +1 -1
  140. package/lib/ids/DeterministicModuleIdsPlugin.js +1 -1
  141. package/lib/ids/HashedModuleIdsPlugin.js +2 -1
  142. package/lib/ids/IdHelpers.js +15 -14
  143. package/lib/ids/SyncModuleIdsPlugin.js +9 -5
  144. package/lib/index.js +5 -5
  145. package/lib/javascript/BasicEvaluatedExpression.js +6 -6
  146. package/lib/javascript/JavascriptGenerator.js +11 -1
  147. package/lib/javascript/JavascriptModulesPlugin.js +54 -34
  148. package/lib/javascript/JavascriptParser.js +272 -188
  149. package/lib/javascript/JavascriptParserHelpers.js +10 -9
  150. package/lib/javascript/StartupHelpers.js +4 -1
  151. package/lib/json/JsonData.js +4 -4
  152. package/lib/json/JsonGenerator.js +54 -22
  153. package/lib/json/JsonModulesPlugin.js +16 -3
  154. package/lib/json/JsonParser.js +10 -4
  155. package/lib/library/AbstractLibraryPlugin.js +7 -3
  156. package/lib/library/AssignLibraryPlugin.js +29 -1
  157. package/lib/library/EnableLibraryPlugin.js +7 -10
  158. package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
  159. package/lib/library/ModuleLibraryPlugin.js +121 -15
  160. package/lib/logging/Logger.js +2 -2
  161. package/lib/logging/createConsoleLogger.js +4 -4
  162. package/lib/node/NodeEnvironmentPlugin.js +6 -2
  163. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +1 -2
  164. package/lib/node/ReadFileCompileWasmPlugin.js +1 -2
  165. package/lib/node/nodeConsole.js +3 -1
  166. package/lib/optimize/AggressiveSplittingPlugin.js +1 -1
  167. package/lib/optimize/ConcatenatedModule.js +19 -12
  168. package/lib/optimize/InnerGraph.js +3 -2
  169. package/lib/optimize/InnerGraphPlugin.js +13 -7
  170. package/lib/optimize/LimitChunkCountPlugin.js +20 -0
  171. package/lib/optimize/ModuleConcatenationPlugin.js +10 -7
  172. package/lib/optimize/RealContentHashPlugin.js +8 -4
  173. package/lib/optimize/SideEffectsFlagPlugin.js +4 -2
  174. package/lib/optimize/SplitChunksPlugin.js +90 -65
  175. package/lib/rules/BasicEffectRulePlugin.js +9 -1
  176. package/lib/rules/BasicMatcherRulePlugin.js +15 -4
  177. package/lib/rules/ObjectMatcherRulePlugin.js +12 -3
  178. package/lib/rules/RuleSetCompiler.js +25 -14
  179. package/lib/rules/UseEffectRulePlugin.js +47 -17
  180. package/lib/runtime/AutoPublicPathRuntimeModule.js +1 -1
  181. package/lib/runtime/GetChunkFilenameRuntimeModule.js +6 -6
  182. package/lib/schemes/HttpUriPlugin.js +38 -17
  183. package/lib/serialization/BinaryMiddleware.js +52 -19
  184. package/lib/serialization/FileMiddleware.js +78 -48
  185. package/lib/serialization/ObjectMiddleware.js +78 -29
  186. package/lib/serialization/PlainObjectSerializer.js +1 -1
  187. package/lib/serialization/Serializer.js +15 -10
  188. package/lib/serialization/SerializerMiddleware.js +80 -41
  189. package/lib/serialization/SingleItemMiddleware.js +10 -7
  190. package/lib/serialization/types.js +1 -1
  191. package/lib/sharing/ConsumeSharedModule.js +4 -2
  192. package/lib/sharing/ProvideSharedModule.js +4 -2
  193. package/lib/sharing/ProvideSharedModuleFactory.js +5 -3
  194. package/lib/sharing/utils.js +2 -2
  195. package/lib/stats/DefaultStatsFactoryPlugin.js +81 -79
  196. package/lib/stats/DefaultStatsPresetPlugin.js +43 -23
  197. package/lib/stats/DefaultStatsPrinterPlugin.js +85 -43
  198. package/lib/stats/StatsFactory.js +11 -11
  199. package/lib/stats/StatsPrinter.js +7 -7
  200. package/lib/util/ArrayHelpers.js +2 -4
  201. package/lib/util/ArrayQueue.js +1 -1
  202. package/lib/util/AsyncQueue.js +4 -4
  203. package/lib/util/IterableHelpers.js +1 -1
  204. package/lib/util/LazyBucketSortedSet.js +41 -23
  205. package/lib/util/LazySet.js +3 -2
  206. package/lib/util/MapHelpers.js +1 -1
  207. package/lib/util/ParallelismFactorCalculator.js +1 -1
  208. package/lib/util/Semaphore.js +3 -3
  209. package/lib/util/SetHelpers.js +1 -1
  210. package/lib/util/SortableSet.js +9 -7
  211. package/lib/util/TupleQueue.js +9 -8
  212. package/lib/util/TupleSet.js +2 -2
  213. package/lib/util/WeakTupleMap.js +12 -11
  214. package/lib/util/binarySearchBounds.js +2 -1
  215. package/lib/util/cleverMerge.js +84 -54
  216. package/lib/util/comparators.js +22 -21
  217. package/lib/util/compileBooleanMatcher.js +3 -3
  218. package/lib/util/concatenate.js +6 -4
  219. package/lib/util/create-schema-validation.js +4 -4
  220. package/lib/util/createHash.js +2 -2
  221. package/lib/util/deprecation.js +35 -33
  222. package/lib/util/deterministicGrouping.js +6 -6
  223. package/lib/util/findGraphRoots.js +1 -1
  224. package/lib/util/fs.js +39 -39
  225. package/lib/util/hash/wasm-hash.js +2 -2
  226. package/lib/util/identifier.js +15 -18
  227. package/lib/util/makeSerializable.js +1 -1
  228. package/lib/util/memoize.js +4 -1
  229. package/lib/util/objectToMap.js +3 -2
  230. package/lib/util/processAsyncTree.js +2 -2
  231. package/lib/util/propertyName.js +0 -1
  232. package/lib/util/registerExternalSerializer.js +15 -18
  233. package/lib/util/removeBOM.js +25 -0
  234. package/lib/util/runtime.js +34 -27
  235. package/lib/util/semver.js +18 -8
  236. package/lib/util/serialization.js +5 -16
  237. package/lib/util/smartGrouping.js +3 -3
  238. package/lib/validateSchema.js +4 -7
  239. package/lib/wasm/EnableWasmLoadingPlugin.js +2 -2
  240. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +3 -3
  241. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +11 -0
  242. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +10 -0
  243. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +2 -2
  244. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +4 -0
  245. package/lib/wasm-sync/WebAssemblyGenerator.js +28 -12
  246. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +10 -0
  247. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +2 -2
  248. package/lib/wasm-sync/WebAssemblyParser.js +9 -4
  249. package/lib/web/JsonpChunkLoadingRuntimeModule.js +4 -2
  250. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -0
  251. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +30 -25
  252. package/package.json +27 -26
  253. package/schemas/WebpackOptions.check.js +1 -1
  254. package/schemas/WebpackOptions.json +48 -13
  255. package/schemas/plugins/JsonModulesPluginGenerator.check.d.ts +7 -0
  256. package/schemas/plugins/JsonModulesPluginGenerator.check.js +6 -0
  257. package/schemas/plugins/JsonModulesPluginGenerator.json +11 -0
  258. package/schemas/plugins/JsonModulesPluginParser.check.js +1 -1
  259. package/schemas/plugins/JsonModulesPluginParser.json +4 -0
  260. package/schemas/plugins/SourceMapDevToolPlugin.json +2 -2
  261. package/types.d.ts +888 -583
  262. package/lib/library/ModernModuleLibraryPlugin.js +0 -144
@@ -6,22 +6,23 @@
6
6
 
7
7
  const { Tracer } = require("chrome-trace-event");
8
8
  const {
9
- JAVASCRIPT_MODULE_TYPE_AUTO,
10
- JAVASCRIPT_MODULE_TYPE_DYNAMIC,
11
- JAVASCRIPT_MODULE_TYPE_ESM,
12
- WEBASSEMBLY_MODULE_TYPE_ASYNC,
13
- WEBASSEMBLY_MODULE_TYPE_SYNC,
9
+ JAVASCRIPT_MODULES,
10
+ CSS_MODULES,
11
+ WEBASSEMBLY_MODULES,
14
12
  JSON_MODULE_TYPE
15
13
  } = require("../ModuleTypeConstants");
16
14
  const createSchemaValidation = require("../util/create-schema-validation");
17
15
  const { dirname, mkdirpSync } = require("../util/fs");
18
16
 
17
+ /** @typedef {import("tapable").FullTap} FullTap */
19
18
  /** @typedef {import("../../declarations/plugins/debug/ProfilingPlugin").ProfilingPluginOptions} ProfilingPluginOptions */
20
19
  /** @typedef {import("../Compilation")} Compilation */
21
20
  /** @typedef {import("../Compiler")} Compiler */
22
21
  /** @typedef {import("../ContextModuleFactory")} ContextModuleFactory */
23
22
  /** @typedef {import("../ModuleFactory")} ModuleFactory */
24
23
  /** @typedef {import("../NormalModuleFactory")} NormalModuleFactory */
24
+ /** @typedef {import("../Parser")} Parser */
25
+ /** @typedef {import("../ResolverFactory")} ResolverFactory */
25
26
  /** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */
26
27
 
27
28
  /** @typedef {TODO} Inspector */
@@ -42,6 +43,7 @@ try {
42
43
  // eslint-disable-next-line n/no-unsupported-features/node-builtins
43
44
  inspector = require("inspector");
44
45
  } catch (_err) {
46
+ // eslint-disable-next-line no-console
45
47
  console.log("Unable to CPU profile in < node 8.0");
46
48
  }
47
49
 
@@ -86,19 +88,27 @@ class Profiler {
86
88
 
87
89
  /**
88
90
  * @param {string} method method name
89
- * @param {object} [params] params
91
+ * @param {Record<string, EXPECTED_ANY>} [params] params
90
92
  * @returns {Promise<TODO>} Promise for the result
91
93
  */
92
94
  sendCommand(method, params) {
93
95
  if (this.hasSession()) {
94
96
  return new Promise((res, rej) => {
95
- this.session.post(method, params, (err, params) => {
96
- if (err !== null) {
97
- rej(err);
98
- } else {
99
- res(params);
97
+ this.session.post(
98
+ method,
99
+ params,
100
+ /**
101
+ * @param {Error | null} err error
102
+ * @param {object} params params
103
+ */
104
+ (err, params) => {
105
+ if (err !== null) {
106
+ rej(err);
107
+ } else {
108
+ res(params);
109
+ }
100
110
  }
101
- });
111
+ );
102
112
  });
103
113
  }
104
114
  return Promise.resolve();
@@ -140,7 +150,7 @@ class Profiler {
140
150
  * @property {Tracer} trace instance of Tracer
141
151
  * @property {number} counter Counter
142
152
  * @property {Profiler} profiler instance of Profiler
143
- * @property {Function} end the end function
153
+ * @property {(callback: (err?: null | Error) => void) => void} end the end function
144
154
  */
145
155
 
146
156
  /**
@@ -242,7 +252,11 @@ class ProfilingPlugin {
242
252
  }
243
253
 
244
254
  for (const hookName of Object.keys(compiler.resolverFactory.hooks)) {
245
- const hook = compiler.resolverFactory.hooks[hookName];
255
+ const hook =
256
+ compiler.resolverFactory.hooks[
257
+ /** @type {keyof ResolverFactory["hooks"]} */
258
+ (hookName)
259
+ ];
246
260
  if (hook) {
247
261
  hook.intercept(makeInterceptorFor("Resolver", tracer)(hookName));
248
262
  }
@@ -263,7 +277,9 @@ class ProfilingPlugin {
263
277
  "Context Module Factory"
264
278
  );
265
279
  interceptAllParserHooks(normalModuleFactory, tracer);
280
+ interceptAllGeneratorHooks(normalModuleFactory, tracer);
266
281
  interceptAllJavascriptModulesPluginHooks(compilation, tracer);
282
+ interceptAllCssModulesPluginHooks(compilation, tracer);
267
283
  }
268
284
  );
269
285
 
@@ -335,7 +351,7 @@ class ProfilingPlugin {
335
351
  }
336
352
 
337
353
  /**
338
- * @param {any} instance instance
354
+ * @param {EXPECTED_ANY & { hooks: TODO }} instance instance
339
355
  * @param {Trace} tracer tracer
340
356
  * @param {string} logLabel log label
341
357
  */
@@ -356,12 +372,10 @@ const interceptAllHooksFor = (instance, tracer, logLabel) => {
356
372
  */
357
373
  const interceptAllParserHooks = (moduleFactory, tracer) => {
358
374
  const moduleTypes = [
359
- JAVASCRIPT_MODULE_TYPE_AUTO,
360
- JAVASCRIPT_MODULE_TYPE_DYNAMIC,
361
- JAVASCRIPT_MODULE_TYPE_ESM,
375
+ ...JAVASCRIPT_MODULES,
362
376
  JSON_MODULE_TYPE,
363
- WEBASSEMBLY_MODULE_TYPE_ASYNC,
364
- WEBASSEMBLY_MODULE_TYPE_SYNC
377
+ ...WEBASSEMBLY_MODULES,
378
+ ...CSS_MODULES
365
379
  ];
366
380
 
367
381
  for (const moduleType of moduleTypes) {
@@ -373,6 +387,27 @@ const interceptAllParserHooks = (moduleFactory, tracer) => {
373
387
  }
374
388
  };
375
389
 
390
+ /**
391
+ * @param {NormalModuleFactory} moduleFactory normal module factory
392
+ * @param {Trace} tracer tracer
393
+ */
394
+ const interceptAllGeneratorHooks = (moduleFactory, tracer) => {
395
+ const moduleTypes = [
396
+ ...JAVASCRIPT_MODULES,
397
+ JSON_MODULE_TYPE,
398
+ ...WEBASSEMBLY_MODULES,
399
+ ...CSS_MODULES
400
+ ];
401
+
402
+ for (const moduleType of moduleTypes) {
403
+ moduleFactory.hooks.generator
404
+ .for(moduleType)
405
+ .tap(PLUGIN_NAME, (parser, parserOpts) => {
406
+ interceptAllHooksFor(parser, tracer, "Generator");
407
+ });
408
+ }
409
+ };
410
+
376
411
  /**
377
412
  * @param {Compilation} compilation compilation
378
413
  * @param {Trace} tracer tracer
@@ -390,36 +425,53 @@ const interceptAllJavascriptModulesPluginHooks = (compilation, tracer) => {
390
425
  );
391
426
  };
392
427
 
428
+ /**
429
+ * @param {Compilation} compilation compilation
430
+ * @param {Trace} tracer tracer
431
+ */
432
+ const interceptAllCssModulesPluginHooks = (compilation, tracer) => {
433
+ interceptAllHooksFor(
434
+ {
435
+ hooks: require("../css/CssModulesPlugin").getCompilationHooks(compilation)
436
+ },
437
+ tracer,
438
+ "CssModulesPlugin"
439
+ );
440
+ };
441
+
442
+ /** @typedef {(...args: EXPECTED_ANY[]) => EXPECTED_ANY | Promise<(...args: EXPECTED_ANY[]) => EXPECTED_ANY>} PluginFunction */
443
+
393
444
  /**
394
445
  * @param {string} instance instance
395
446
  * @param {Trace} tracer tracer
396
- * @returns {TODO} interceptor
447
+ * @returns {(hookName: string) => TODO} interceptor
397
448
  */
398
449
  const makeInterceptorFor = (instance, tracer) => hookName => ({
450
+ /**
451
+ * @param {FullTap} tapInfo tap info
452
+ * @returns {FullTap} modified full tap
453
+ */
399
454
  register: tapInfo => {
400
- const { name, type, fn } = tapInfo;
455
+ const { name, type, fn: internalFn } = tapInfo;
401
456
  const newFn =
402
457
  // Don't tap our own hooks to ensure stream can close cleanly
403
458
  name === PLUGIN_NAME
404
- ? fn
459
+ ? internalFn
405
460
  : makeNewProfiledTapFn(hookName, tracer, {
406
461
  name,
407
462
  type,
408
- fn
463
+ fn: /** @type {PluginFunction} */ (internalFn)
409
464
  });
410
465
  return { ...tapInfo, fn: newFn };
411
466
  }
412
467
  });
413
468
 
414
- // TODO improve typing
415
- /** @typedef {(...args: TODO[]) => void | Promise<TODO>} PluginFunction */
416
-
417
469
  /**
418
470
  * @param {string} hookName Name of the hook to profile.
419
471
  * @param {Trace} tracer The trace object.
420
472
  * @param {object} options Options for the profiled fn.
421
473
  * @param {string} options.name Plugin name
422
- * @param {string} options.type Plugin type (sync | async | promise)
474
+ * @param {"sync" | "async" | "promise"} options.type Plugin type (sync | async | promise)
423
475
  * @param {PluginFunction} options.fn Plugin function
424
476
  * @returns {PluginFunction} Chainable hooked function.
425
477
  */
@@ -435,7 +487,9 @@ const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
435
487
  id,
436
488
  cat: defaultCategory
437
489
  });
438
- const promise = /** @type {Promise<*>} */ (fn(...args));
490
+ const promise =
491
+ /** @type {Promise<(...args: EXPECTED_ANY[]) => EXPECTED_ANY>} */
492
+ (fn(...args));
439
493
  return promise.then(r => {
440
494
  tracer.trace.end({
441
495
  name,
@@ -454,14 +508,20 @@ const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
454
508
  cat: defaultCategory
455
509
  });
456
510
  const callback = args.pop();
457
- fn(...args, (...r) => {
458
- tracer.trace.end({
459
- name,
460
- id,
461
- cat: defaultCategory
462
- });
463
- callback(...r);
464
- });
511
+ fn(
512
+ ...args,
513
+ /**
514
+ * @param {...EXPECTED_ANY[]} r result
515
+ */
516
+ (...r) => {
517
+ tracer.trace.end({
518
+ name,
519
+ id,
520
+ cat: defaultCategory
521
+ });
522
+ callback(...r);
523
+ }
524
+ );
465
525
  };
466
526
  case "sync":
467
527
  return (...args) => {
@@ -496,7 +556,7 @@ const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
496
556
  return r;
497
557
  };
498
558
  default:
499
- break;
559
+ return fn;
500
560
  }
501
561
  };
502
562
 
@@ -248,7 +248,7 @@ class AMDDefineDependencyParserPlugin {
248
248
  * @returns {boolean | undefined} result
249
249
  */
250
250
  processCallDefine(parser, expr) {
251
- /** @type {TODO} */
251
+ /** @type {Expression | SpreadElement | undefined} */
252
252
  let array;
253
253
  /** @type {FunctionExpression | ArrowFunctionExpression | CallExpression | Identifier | undefined} */
254
254
  let fn;
@@ -385,7 +385,7 @@ class AMDDefineDependencyParserPlugin {
385
385
  let inTry;
386
386
  if (fn && isUnboundFunctionExpression(fn)) {
387
387
  inTry = parser.scope.inTry;
388
- parser.inScope(fnParams, () => {
388
+ parser.inScope(/** @type {Identifier[]} */ (fnParams), () => {
389
389
  for (const [name, varInfo] of fnRenames) {
390
390
  parser.setVariable(name, varInfo);
391
391
  }
@@ -425,7 +425,10 @@ class AMDDefineDependencyParserPlugin {
425
425
  parser.prevStatement = prev;
426
426
  parser.walkStatement(object.body);
427
427
  } else {
428
- parser.walkExpression(object.body);
428
+ parser.walkExpression(
429
+ /** @type {TODO} */
430
+ (object.body)
431
+ );
429
432
  }
430
433
  }
431
434
  );
@@ -433,7 +436,10 @@ class AMDDefineDependencyParserPlugin {
433
436
  parser.walkExpressions(fn.arguments);
434
437
  }
435
438
  } else if (fn || obj) {
436
- parser.walkExpression(fn || obj);
439
+ parser.walkExpression(
440
+ /** @type {FunctionExpression | ArrowFunctionExpression | CallExpression | ObjectExpression | Identifier} */
441
+ (fn || obj)
442
+ );
437
443
  }
438
444
 
439
445
  const dep = this.newDefineDependency(
@@ -32,6 +32,7 @@ const ConstDependency = require("./ConstDependency");
32
32
  const LocalModuleDependency = require("./LocalModuleDependency");
33
33
  const UnsupportedDependency = require("./UnsupportedDependency");
34
34
 
35
+ /** @typedef {import("../../declarations/WebpackOptions").Amd} Amd */
35
36
  /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
36
37
  /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
37
38
  /** @typedef {import("../Compiler")} Compiler */
@@ -41,9 +42,11 @@ const UnsupportedDependency = require("./UnsupportedDependency");
41
42
 
42
43
  const PLUGIN_NAME = "AMDPlugin";
43
44
 
45
+ /** @typedef {Record<string, TODO>} AmdOptions */
46
+
44
47
  class AMDPlugin {
45
48
  /**
46
- * @param {Record<string, any>} amdOptions the AMD options
49
+ * @param {AmdOptions} amdOptions the AMD options
47
50
  */
48
51
  constructor(amdOptions) {
49
52
  this.amdOptions = amdOptions;
@@ -140,7 +143,7 @@ class AMDPlugin {
140
143
  /**
141
144
  * @param {string} optionExpr option expression
142
145
  * @param {string} rootName root name
143
- * @param {function(): TODO} getMembers callback
146
+ * @param {() => TODO} getMembers callback
144
147
  */
145
148
  const tapOptionsHooks = (optionExpr, rootName, getMembers) => {
146
149
  parser.hooks.expression
@@ -7,6 +7,7 @@
7
7
 
8
8
  const DependencyTemplate = require("../DependencyTemplate");
9
9
  const makeSerializable = require("../util/makeSerializable");
10
+ const LocalModuleDependency = require("./LocalModuleDependency");
10
11
  const NullDependency = require("./NullDependency");
11
12
 
12
13
  /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
@@ -16,7 +17,6 @@ const NullDependency = require("./NullDependency");
16
17
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
17
18
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
18
19
  /** @typedef {import("./AMDRequireItemDependency")} AMDRequireItemDependency */
19
- /** @typedef {import("./LocalModuleDependency")} LocalModuleDependency */
20
20
 
21
21
  class AMDRequireArrayDependency extends NullDependency {
22
22
  /**
@@ -96,7 +96,7 @@ AMDRequireArrayDependency.Template = class AMDRequireArrayDependencyTemplate ext
96
96
  }
97
97
 
98
98
  /**
99
- * @param {TODO} dep the dependency for which the template should be applied
99
+ * @param {string | LocalModuleDependency | AMDRequireItemDependency} dep the dependency for which the template should be applied
100
100
  * @param {DependencyTemplateContext} templateContext the context object
101
101
  * @returns {string} content
102
102
  */
@@ -108,9 +108,10 @@ AMDRequireArrayDependency.Template = class AMDRequireArrayDependencyTemplate ext
108
108
  return dep;
109
109
  }
110
110
 
111
- if (dep.localModule) {
111
+ if (dep instanceof LocalModuleDependency) {
112
112
  return dep.localModule.variableName();
113
113
  }
114
+
114
115
  return runtimeTemplate.moduleExports({
115
116
  module: moduleGraph.getModule(dep),
116
117
  chunkGraph,
@@ -11,10 +11,11 @@ const ContextDependency = require("./ContextDependency");
11
11
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
12
12
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
13
13
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
14
+ /** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
14
15
 
15
16
  class AMDRequireContextDependency extends ContextDependency {
16
17
  /**
17
- * @param {TODO} options options
18
+ * @param {ContextDependencyOptions} options options
18
19
  * @param {Range} range range
19
20
  * @param {Range} valueRange value range
20
21
  */
@@ -104,7 +104,7 @@ class AMDRequireDependenciesBlockParserPlugin {
104
104
  } else if (param.isConstArray()) {
105
105
  /** @type {(string | LocalModuleDependency | AMDRequireItemDependency)[]} */
106
106
  const deps = [];
107
- for (const request of /** @type {any[]} */ (param.array)) {
107
+ for (const request of /** @type {EXPECTED_ANY[]} */ (param.array)) {
108
108
  let dep;
109
109
  let localModule;
110
110
  if (request === "require") {
@@ -158,20 +158,27 @@ class AMDRequireDependenciesBlockParserPlugin {
158
158
  if (param.string === "require") {
159
159
  dep = new ConstDependency(
160
160
  RuntimeGlobals.require,
161
- /** @type {TODO} */ (param.string),
161
+ /** @type {TODO} */
162
+ (param.string),
162
163
  [RuntimeGlobals.require]
163
164
  );
164
165
  } else if (param.string === "module") {
165
166
  dep = new ConstDependency(
166
- /** @type {BuildInfo} */
167
- (parser.state.module.buildInfo).moduleArgument,
167
+ /** @type {string} */
168
+ (
169
+ /** @type {BuildInfo} */
170
+ (parser.state.module.buildInfo).moduleArgument
171
+ ),
168
172
  /** @type {Range} */ (param.range),
169
173
  [RuntimeGlobals.module]
170
174
  );
171
175
  } else if (param.string === "exports") {
172
176
  dep = new ConstDependency(
173
- /** @type {BuildInfo} */
174
- (parser.state.module.buildInfo).exportsArgument,
177
+ /** @type {string} */
178
+ (
179
+ /** @type {BuildInfo} */
180
+ (parser.state.module.buildInfo).exportsArgument
181
+ ),
175
182
  /** @type {Range} */ (param.range),
176
183
  [RuntimeGlobals.exports]
177
184
  );
@@ -208,7 +215,8 @@ class AMDRequireDependenciesBlockParserPlugin {
208
215
  processContext(parser, expr, param) {
209
216
  const dep = ContextDependencyHelpers.create(
210
217
  AMDRequireContextDependency,
211
- /** @type {Range} */ (param.range),
218
+ /** @type {Range} */
219
+ (param.range),
212
220
  param,
213
221
  expr,
214
222
  this.options,
@@ -8,6 +8,8 @@ const RuntimeGlobals = require("../RuntimeGlobals");
8
8
  const RuntimeModule = require("../RuntimeModule");
9
9
  const Template = require("../Template");
10
10
 
11
+ /** @typedef {import("./AMDPlugin").AmdOptions} AmdOptions */
12
+
11
13
  class AMDDefineRuntimeModule extends RuntimeModule {
12
14
  constructor() {
13
15
  super("amd define");
@@ -27,7 +29,7 @@ class AMDDefineRuntimeModule extends RuntimeModule {
27
29
 
28
30
  class AMDOptionsRuntimeModule extends RuntimeModule {
29
31
  /**
30
- * @param {Record<string, boolean | number | string>} options the AMD options
32
+ * @param {AmdOptions} options the AMD options
31
33
  */
32
34
  constructor(options) {
33
35
  super("amd options");
@@ -71,9 +71,7 @@ class CommonJsExportRequireDependency extends ModuleDependency {
71
71
  * @returns {string[]} the imported id
72
72
  */
73
73
  getIds(moduleGraph) {
74
- return (
75
- /** @type {TODO} */ (moduleGraph.getMeta(this))[idsSymbol] || this.ids
76
- );
74
+ return moduleGraph.getMeta(this)[idsSymbol] || this.ids;
77
75
  }
78
76
 
79
77
  /**
@@ -82,7 +80,7 @@ class CommonJsExportRequireDependency extends ModuleDependency {
82
80
  * @returns {void}
83
81
  */
84
82
  setIds(moduleGraph, ids) {
85
- /** @type {TODO} */ (moduleGraph.getMeta(this))[idsSymbol] = ids;
83
+ moduleGraph.getMeta(this)[idsSymbol] = ids;
86
84
  }
87
85
 
88
86
  /**
@@ -190,7 +188,8 @@ class CommonJsExportRequireDependency extends ModuleDependency {
190
188
  if (reexportInfo) {
191
189
  return {
192
190
  exports: Array.from(
193
- /** @type {TODO} */ (reexportInfo).exports,
191
+ /** @type {Set<string>} */
192
+ (reexportInfo.exports),
194
193
  name => ({
195
194
  name,
196
195
  from,
@@ -44,16 +44,16 @@ const ModuleDecoratorDependency = require("./ModuleDecoratorDependency");
44
44
  * exports.foo = void 0;
45
45
  * exports.foo = "bar";
46
46
  * ```
47
- * @param {TODO} expr expression
47
+ * @param {Expression} expr expression
48
48
  * @returns {Expression | undefined} returns the value of property descriptor
49
49
  */
50
50
  const getValueOfPropertyDescription = expr => {
51
51
  if (expr.type !== "ObjectExpression") return;
52
52
  for (const property of expr.properties) {
53
- if (property.computed) continue;
53
+ if (property.type === "SpreadElement" || property.computed) continue;
54
54
  const key = property.key;
55
55
  if (key.type !== "Identifier" || key.name !== "value") continue;
56
- return property.value;
56
+ return /** @type {Expression} */ (property.value);
57
57
  }
58
58
  };
59
59
 
@@ -11,6 +11,7 @@ const RuntimeGlobals = require("../RuntimeGlobals");
11
11
  const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
12
12
  const WebpackError = require("../WebpackError");
13
13
  const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
14
+ const { VariableInfo } = require("../javascript/JavascriptParser");
14
15
  const {
15
16
  evaluateToIdentifier,
16
17
  evaluateToString,
@@ -201,9 +202,9 @@ class CommonJsImportsParserPlugin {
201
202
  const requireAsExpressionHandler = expr => {
202
203
  const dep = new CommonJsRequireContextDependency(
203
204
  {
204
- request: options.unknownContextRequest,
205
- recursive: options.unknownContextRecursive,
206
- regExp: options.unknownContextRegExp,
205
+ request: /** @type {string} */ (options.unknownContextRequest),
206
+ recursive: /** @type {boolean} */ (options.unknownContextRecursive),
207
+ regExp: /** @type {TODO} */ (options.unknownContextRegExp),
207
208
  mode: "sync"
208
209
  },
209
210
  /** @type {Range} */ (expr.range),
@@ -464,6 +465,36 @@ class CommonJsImportsParserPlugin {
464
465
  * @returns {boolean | void} true when handled
465
466
  */
466
467
  const processResolve = (expr, weak) => {
468
+ if (!weak && options.commonjsMagicComments) {
469
+ const { options: requireOptions, errors: commentErrors } =
470
+ parser.parseCommentOptions(/** @type {Range} */ (expr.range));
471
+
472
+ if (commentErrors) {
473
+ for (const e of commentErrors) {
474
+ const { comment } = e;
475
+ parser.state.module.addWarning(
476
+ new CommentCompilationWarning(
477
+ `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
478
+ /** @type {DependencyLocation} */ (comment.loc)
479
+ )
480
+ );
481
+ }
482
+ }
483
+ if (requireOptions && requireOptions.webpackIgnore !== undefined) {
484
+ if (typeof requireOptions.webpackIgnore !== "boolean") {
485
+ parser.state.module.addWarning(
486
+ new UnsupportedFeatureWarning(
487
+ `\`webpackIgnore\` expected a boolean, but received: ${requireOptions.webpackIgnore}.`,
488
+ /** @type {DependencyLocation} */ (expr.loc)
489
+ )
490
+ );
491
+ } else if (requireOptions.webpackIgnore) {
492
+ // Do not instrument `require()` if `webpackIgnore` is `true`
493
+ return true;
494
+ }
495
+ }
496
+ }
497
+
467
498
  if (expr.arguments.length !== 1) return;
468
499
  const param = parser.evaluateExpression(expr.arguments[0]);
469
500
  if (param.isConditional()) {
@@ -699,11 +730,11 @@ class CommonJsImportsParserPlugin {
699
730
  declarator.init.callee.type !== "Identifier"
700
731
  )
701
732
  return;
702
- const variableInfo =
703
- /** @type {TODO} */
704
- (parser.getVariableInfo(declarator.init.callee.name));
733
+ const variableInfo = parser.getVariableInfo(
734
+ declarator.init.callee.name
735
+ );
705
736
  if (
706
- variableInfo &&
737
+ variableInfo instanceof VariableInfo &&
707
738
  variableInfo.tagInfo &&
708
739
  variableInfo.tagInfo.tag === createRequireSpecifierTag
709
740
  ) {
@@ -12,10 +12,11 @@ const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTempl
12
12
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
13
13
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
14
14
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
15
+ /** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
15
16
 
16
17
  class CommonJsRequireContextDependency extends ContextDependency {
17
18
  /**
18
- * @param {TODO} options options for the context module
19
+ * @param {ContextDependencyOptions} options options for the context module
19
20
  * @param {Range} range location in source code
20
21
  * @param {Range | undefined} valueRange location of the require call
21
22
  * @param {boolean | string } inShorthand true or name
@@ -7,7 +7,7 @@
7
7
 
8
8
  const { parseResource } = require("../util/identifier");
9
9
 
10
- /** @typedef {import("estree").Node} EsTreeNode */
10
+ /** @typedef {import("estree").Expression} Expression */
11
11
  /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
12
12
  /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
13
13
  /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
@@ -48,11 +48,11 @@ const splitContextFromPrefix = prefix => {
48
48
  * @param {ContextDependencyConstructor} Dep the Dependency class
49
49
  * @param {Range} range source range
50
50
  * @param {BasicEvaluatedExpression} param context param
51
- * @param {EsTreeNode} expr expr
51
+ * @param {Expression} expr expr
52
52
  * @param {Pick<JavascriptParserOptions, `${"expr"|"wrapped"}Context${"Critical"|"Recursive"|"RegExp"}` | "exprContextRequest">} options options for context creation
53
53
  * @param {PartialContextDependencyOptions} contextOptions options for the ContextModule
54
54
  * @param {JavascriptParser} parser the parser
55
- * @param {...any} depArgs depArgs
55
+ * @param {...EXPECTED_ANY} depArgs depArgs
56
56
  * @returns {ContextDependency} the created Dependency
57
57
  */
58
58
  module.exports.create = (
@@ -164,7 +164,10 @@ module.exports.create = (
164
164
  });
165
165
  } else {
166
166
  // Expression
167
- parser.walkExpression(part.expression);
167
+ parser.walkExpression(
168
+ /** @type {Expression} */
169
+ (part.expression)
170
+ );
168
171
  }
169
172
  }
170
173
 
@@ -233,7 +236,11 @@ module.exports.create = (
233
236
 
234
237
  if (parser && param.wrappedInnerExpressions) {
235
238
  for (const part of param.wrappedInnerExpressions) {
236
- if (part.expression) parser.walkExpression(part.expression);
239
+ if (part.expression)
240
+ parser.walkExpression(
241
+ /** @type {Expression} */
242
+ (part.expression)
243
+ );
237
244
  }
238
245
  }
239
246
 
@@ -256,7 +263,7 @@ module.exports.create = (
256
263
  options.exprContextCritical &&
257
264
  "the request of a dependency is an expression";
258
265
 
259
- parser.walkExpression(param.expression);
266
+ parser.walkExpression(/** @type {Expression} */ (param.expression));
260
267
 
261
268
  return dep;
262
269
  };