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
@@ -23,14 +23,16 @@ const { registerNotSerializable } = require("../util/serialization");
23
23
  /** @typedef {import("../Compilation")} Compilation */
24
24
  /** @typedef {import("../Compiler")} Compiler */
25
25
  /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
26
+ /** @typedef {import("../Module").BuildCallback} BuildCallback */
26
27
  /** @typedef {import("../Module").BuildMeta} BuildMeta */
27
28
  /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
28
29
  /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
29
30
  /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
31
+ /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
30
32
  /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
31
33
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
34
+ /** @typedef {import("../ModuleFactory").ModuleFactoryCallback} ModuleFactoryCallback */
32
35
  /** @typedef {import("../ModuleFactory").ModuleFactoryCreateData} ModuleFactoryCreateData */
33
- /** @typedef {import("../ModuleFactory").ModuleFactoryResult} ModuleFactoryResult */
34
36
  /** @typedef {import("../RequestShortener")} RequestShortener */
35
37
  /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
36
38
  /** @typedef {import("../WebpackError")} WebpackError */
@@ -42,8 +44,8 @@ const { registerNotSerializable } = require("../util/serialization");
42
44
 
43
45
  /**
44
46
  * @typedef {object} BackendApi
45
- * @property {function(function((Error | null)=) : void): void} dispose
46
- * @property {function(Module): ModuleResult} module
47
+ * @property {(callback: (err?: (Error | null)) => void) => void} dispose
48
+ * @property {(module: Module) => ModuleResult} module
47
49
  */
48
50
 
49
51
  const HMR_DEPENDENCY_TYPES = new Set([
@@ -54,7 +56,7 @@ const HMR_DEPENDENCY_TYPES = new Set([
54
56
  ]);
55
57
 
56
58
  /**
57
- * @param {undefined|string|RegExp|Function} test test option
59
+ * @param {Options["test"]} test test option
58
60
  * @param {Module} module the module
59
61
  * @returns {boolean | null | string} true, if the module should be selected
60
62
  */
@@ -169,7 +171,7 @@ class LazyCompilationProxyModule extends Module {
169
171
 
170
172
  /**
171
173
  * @param {NeedBuildContext} context context info
172
- * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
174
+ * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
173
175
  * @returns {void}
174
176
  */
175
177
  needBuild(context, callback) {
@@ -181,7 +183,7 @@ class LazyCompilationProxyModule extends Module {
181
183
  * @param {Compilation} compilation the compilation
182
184
  * @param {ResolverWithOptions} resolver the resolver
183
185
  * @param {InputFileSystem} fs the file system
184
- * @param {function(WebpackError=): void} callback callback function
186
+ * @param {BuildCallback} callback callback function
185
187
  * @returns {void}
186
188
  */
187
189
  build(options, compilation, resolver, fs, callback) {
@@ -315,13 +317,13 @@ class LazyCompilationDependencyFactory extends ModuleFactory {
315
317
 
316
318
  /**
317
319
  * @param {ModuleFactoryCreateData} data data object
318
- * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback
320
+ * @param {ModuleFactoryCallback} callback callback
319
321
  * @returns {void}
320
322
  */
321
323
  create(data, callback) {
322
- const dependency = /** @type {LazyCompilationDependency} */ (
323
- data.dependencies[0]
324
- );
324
+ const dependency =
325
+ /** @type {LazyCompilationDependency} */
326
+ (data.dependencies[0]);
325
327
  callback(null, {
326
328
  module: dependency.proxyModule.originalModule
327
329
  });
@@ -331,7 +333,7 @@ class LazyCompilationDependencyFactory extends ModuleFactory {
331
333
  /**
332
334
  * @callback BackendHandler
333
335
  * @param {Compiler} compiler compiler
334
- * @param {function(Error | null, BackendApi=): void} callback callback
336
+ * @param {(err: Error | null, backendApi?: BackendApi) => void} callback callback
335
337
  * @returns {void}
336
338
  */
337
339
 
@@ -341,13 +343,19 @@ class LazyCompilationDependencyFactory extends ModuleFactory {
341
343
  * @returns {Promise<BackendApi>} backend
342
344
  */
343
345
 
346
+ /** @typedef {BackendHandler | PromiseBackendHandler} BackEnd */
347
+
348
+ /**
349
+ * @typedef {object} Options options
350
+ * @property {BackEnd} backend the backend
351
+ * @property {boolean=} entries
352
+ * @property {boolean=} imports
353
+ * @property {(RegExp | string | ((module: Module) => boolean))=} test additional filter for lazy compiled entrypoint modules
354
+ */
355
+
344
356
  class LazyCompilationPlugin {
345
357
  /**
346
- * @param {object} options options
347
- * @param {BackendHandler | PromiseBackendHandler} options.backend the backend
348
- * @param {boolean} options.entries true, when entries are lazy compiled
349
- * @param {boolean} options.imports true, when import() modules are lazy compiled
350
- * @param {RegExp | string | (function(Module): boolean) | undefined} options.test additional filter for lazy compiled entrypoint modules
358
+ * @param {Options} options options
351
359
  */
352
360
  constructor({ backend, entries, imports, test }) {
353
361
  this.backend = backend;
@@ -386,7 +394,7 @@ class LazyCompilationPlugin {
386
394
  (compilation, { normalModuleFactory }) => {
387
395
  normalModuleFactory.hooks.module.tap(
388
396
  "LazyCompilationPlugin",
389
- (originalModule, createData, resolveData) => {
397
+ (module, createData, resolveData) => {
390
398
  if (
391
399
  resolveData.dependencies.every(dep =>
392
400
  HMR_DEPENDENCY_TYPES.has(dep.type)
@@ -407,7 +415,7 @@ class LazyCompilationPlugin {
407
415
  hmrDep.request
408
416
  )
409
417
  );
410
- if (!isReferringToDynamicImport) return;
418
+ if (!isReferringToDynamicImport) return module;
411
419
  } else if (
412
420
  !resolveData.dependencies.every(
413
421
  dep =>
@@ -418,21 +426,21 @@ class LazyCompilationPlugin {
418
426
  (this.entries && dep.type === "entry")
419
427
  )
420
428
  )
421
- return;
429
+ return module;
422
430
  if (
423
431
  /webpack[/\\]hot[/\\]|webpack-dev-server[/\\]client|webpack-hot-middleware[/\\]client/.test(
424
432
  resolveData.request
425
433
  ) ||
426
- !checkTest(this.test, originalModule)
434
+ !checkTest(this.test, module)
427
435
  )
428
- return;
429
- const moduleInfo = backend.module(originalModule);
430
- if (!moduleInfo) return;
436
+ return module;
437
+ const moduleInfo = backend.module(module);
438
+ if (!moduleInfo) return module;
431
439
  const { client, data, active } = moduleInfo;
432
440
 
433
441
  return new LazyCompilationProxyModule(
434
442
  compiler.context,
435
- originalModule,
443
+ module,
436
444
  resolveData.request,
437
445
  client,
438
446
  data,
@@ -43,7 +43,7 @@ module.exports = options => (compiler, callback) => {
43
43
  (options.server)
44
44
  );
45
45
  })();
46
- /** @type {function(Server): void} */
46
+ /** @type {(server: Server) => void} */
47
47
  const listen =
48
48
  typeof options.listen === "function"
49
49
  ? options.listen
@@ -20,7 +20,7 @@ const {
20
20
  /**
21
21
  * @typedef {object} DeterministicModuleIdsPluginOptions
22
22
  * @property {string=} context context relative to which module identifiers are computed
23
- * @property {function(Module): boolean=} test selector function for modules
23
+ * @property {((module: Module) => boolean)=} test selector function for modules
24
24
  * @property {number=} maxLength maximum id length in digits (used as starting point)
25
25
  * @property {number=} salt hash salt for ids
26
26
  * @property {boolean=} fixedLength do not increase the maxLength to find an optimal id space size
@@ -5,6 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const { DEFAULTS } = require("../config/defaults");
8
9
  const {
9
10
  compareModulesByPreOrderIndexOrIdentifier
10
11
  } = require("../util/comparators");
@@ -37,7 +38,7 @@ class HashedModuleIdsPlugin {
37
38
  /** @type {HashedModuleIdsPluginOptions} */
38
39
  this.options = {
39
40
  context: undefined,
40
- hashFunction: "md4",
41
+ hashFunction: DEFAULTS.HASH_FUNCTION,
41
42
  hashDigest: "base64",
42
43
  hashDigestLength: 4,
43
44
  ...options
@@ -14,6 +14,7 @@ const numberHash = require("../util/numberHash");
14
14
  /** @typedef {import("../Compilation")} Compilation */
15
15
  /** @typedef {import("../Module")} Module */
16
16
  /** @typedef {typeof import("../util/Hash")} Hash */
17
+ /** @typedef {import("../util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
17
18
 
18
19
  /**
19
20
  * @param {string} str string to hash
@@ -75,7 +76,7 @@ const shortenLongString = (string, delimiter, hashFunction) => {
75
76
  /**
76
77
  * @param {Module} module the module
77
78
  * @param {string} context context directory
78
- * @param {object=} associatedObjectForCache an object to which the cache will be attached
79
+ * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
79
80
  * @returns {string} short module name
80
81
  */
81
82
  const getShortModuleName = (module, context, associatedObjectForCache) => {
@@ -95,7 +96,7 @@ module.exports.getShortModuleName = getShortModuleName;
95
96
  * @param {Module} module the module
96
97
  * @param {string} context context directory
97
98
  * @param {string | Hash} hashFunction hash function to use
98
- * @param {object=} associatedObjectForCache an object to which the cache will be attached
99
+ * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
99
100
  * @returns {string} long module name
100
101
  */
101
102
  const getLongModuleName = (
@@ -113,7 +114,7 @@ module.exports.getLongModuleName = getLongModuleName;
113
114
  /**
114
115
  * @param {Module} module the module
115
116
  * @param {string} context context directory
116
- * @param {object=} associatedObjectForCache an object to which the cache will be attached
117
+ * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
117
118
  * @returns {string} full module name
118
119
  */
119
120
  const getFullModuleName = (module, context, associatedObjectForCache) =>
@@ -126,7 +127,7 @@ module.exports.getFullModuleName = getFullModuleName;
126
127
  * @param {string} context context directory
127
128
  * @param {string} delimiter delimiter for names
128
129
  * @param {string | Hash} hashFunction hash function to use
129
- * @param {object=} associatedObjectForCache an object to which the cache will be attached
130
+ * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
130
131
  * @returns {string} short chunk name
131
132
  */
132
133
  const getShortChunkName = (
@@ -156,7 +157,7 @@ module.exports.getShortChunkName = getShortChunkName;
156
157
  * @param {string} context context directory
157
158
  * @param {string} delimiter delimiter for names
158
159
  * @param {string | Hash} hashFunction hash function to use
159
- * @param {object=} associatedObjectForCache an object to which the cache will be attached
160
+ * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
160
161
  * @returns {string} short chunk name
161
162
  */
162
163
  const getLongChunkName = (
@@ -189,7 +190,7 @@ module.exports.getLongChunkName = getLongChunkName;
189
190
  * @param {Chunk} chunk the chunk
190
191
  * @param {ChunkGraph} chunkGraph the chunk graph
191
192
  * @param {string} context context directory
192
- * @param {object=} associatedObjectForCache an object to which the cache will be attached
193
+ * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
193
194
  * @returns {string} full chunk name
194
195
  */
195
196
  const getFullChunkName = (
@@ -226,7 +227,7 @@ const addToMapOfItems = (map, key, value) => {
226
227
 
227
228
  /**
228
229
  * @param {Compilation} compilation the compilation
229
- * @param {function(Module): boolean=} filter filter modules
230
+ * @param {((module: Module) => boolean)=} filter filter modules
230
231
  * @returns {[Set<string>, Module[]]} used module ids as strings and modules without id matching the filter
231
232
  */
232
233
  const getUsedModuleIdsAndModules = (compilation, filter) => {
@@ -286,11 +287,11 @@ module.exports.getUsedChunkIds = getUsedChunkIds;
286
287
  /**
287
288
  * @template T
288
289
  * @param {Iterable<T>} items list of items to be named
289
- * @param {function(T): string} getShortName get a short name for an item
290
- * @param {function(T, string): string} getLongName get a long name for an item
291
- * @param {function(T, T): -1|0|1} comparator order of items
290
+ * @param {(item: T) => string} getShortName get a short name for an item
291
+ * @param {(item: T, name: string) => string} getLongName get a long name for an item
292
+ * @param {(a: T, b: T) => -1 | 0 | 1} comparator order of items
292
293
  * @param {Set<string>} usedIds already used ids, will not be assigned
293
- * @param {function(T, string): void} assignName assign a name to an item
294
+ * @param {(item: T, name: string) => void} assignName assign a name to an item
294
295
  * @returns {T[]} list of items without a name
295
296
  */
296
297
  const assignNames = (
@@ -354,9 +355,9 @@ module.exports.assignNames = assignNames;
354
355
  /**
355
356
  * @template T
356
357
  * @param {T[]} items list of items to be named
357
- * @param {function(T): string} getName get a name for an item
358
- * @param {function(T, T): -1|0|1} comparator order of items
359
- * @param {function(T, number): boolean} assignId assign an id to an item
358
+ * @param {(item: T) => string} getName get a name for an item
359
+ * @param {(a: T, n: T) => -1 | 0 | 1} comparator order of items
360
+ * @param {(item: T, id: number) => boolean} assignId assign an id to an item
360
361
  * @param {number[]} ranges usable ranges for ids
361
362
  * @param {number} expandFactor factor to create more ranges
362
363
  * @param {number} extraSpace extra space to allocate, i. e. when some ids are already used
@@ -14,13 +14,17 @@ const { getUsedModuleIdsAndModules } = require("./IdHelpers");
14
14
 
15
15
  const plugin = "SyncModuleIdsPlugin";
16
16
 
17
+ /**
18
+ * @typedef {object} SyncModuleIdsPluginOptions
19
+ * @property {string} path path to file
20
+ * @property {string=} context context for module names
21
+ * @property {((module: Module) => boolean)=} test selector for modules
22
+ * @property {"read" | "create" | "merge" | "update"=} mode operation mode (defaults to merge)
23
+ */
24
+
17
25
  class SyncModuleIdsPlugin {
18
26
  /**
19
- * @param {object} options options
20
- * @param {string} options.path path to file
21
- * @param {string=} options.context context for module names
22
- * @param {function(Module): boolean} options.test selector for modules
23
- * @param {"read" | "create" | "merge" | "update"=} options.mode operation mode (defaults to merge)
27
+ * @param {SyncModuleIdsPluginOptions} options options
24
28
  */
25
29
  constructor({ path, context, test, mode }) {
26
30
  this._path = path;
package/lib/index.js CHANGED
@@ -64,15 +64,15 @@ const memoize = require("./util/memoize");
64
64
  /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
65
65
 
66
66
  /**
67
- * @template {Function} T
68
- * @param {function(): T} factory factory function
67
+ * @template {EXPECTED_FUNCTION} T
68
+ * @param {() => T} factory factory function
69
69
  * @returns {T} function
70
70
  */
71
71
  const lazyFunction = factory => {
72
72
  const fac = memoize(factory);
73
73
  const f = /** @type {any} */ (
74
74
  /**
75
- * @param {...any} args args
75
+ * @param {...EXPECTED_ANY} args args
76
76
  * @returns {T} result
77
77
  */
78
78
  (...args) => fac()(...args)
@@ -120,13 +120,13 @@ module.exports = mergeExports(fn, {
120
120
  return require("./webpack");
121
121
  },
122
122
  /**
123
- * @returns {function(Configuration | Configuration[]): void} validate fn
123
+ * @returns {(configuration: Configuration | Configuration[]) => void} validate fn
124
124
  */
125
125
  get validate() {
126
126
  const webpackOptionsSchemaCheck = require("../schemas/WebpackOptions.check.js");
127
127
  const getRealValidate = memoize(
128
128
  /**
129
- * @returns {function(Configuration | Configuration[]): void} validate fn
129
+ * @returns {(configuration: Configuration | Configuration[]) => void} validate fn
130
130
  */
131
131
  () => {
132
132
  const validateSchema = require("./validateSchema");
@@ -7,7 +7,7 @@
7
7
 
8
8
  /** @typedef {import("estree").Node} Node */
9
9
  /** @typedef {import("./JavascriptParser").Range} Range */
10
- /** @typedef {import("./JavascriptParser").VariableInfoInterface} VariableInfoInterface */
10
+ /** @typedef {import("./JavascriptParser").VariableInfo} VariableInfo */
11
11
 
12
12
  const TypeUnknown = 0;
13
13
  const TypeUndefined = 1;
@@ -51,7 +51,7 @@ class BasicEvaluatedExpression {
51
51
  this.quasis = undefined;
52
52
  /** @type {BasicEvaluatedExpression[] | undefined} */
53
53
  this.parts = undefined;
54
- /** @type {any[] | undefined} */
54
+ /** @type {EXPECTED_ANY[] | undefined} */
55
55
  this.array = undefined;
56
56
  /** @type {BasicEvaluatedExpression[] | undefined} */
57
57
  this.items = undefined;
@@ -63,9 +63,9 @@ class BasicEvaluatedExpression {
63
63
  this.postfix = undefined;
64
64
  /** @type {BasicEvaluatedExpression[] | undefined} */
65
65
  this.wrappedInnerExpressions = undefined;
66
- /** @type {string | VariableInfoInterface | undefined} */
66
+ /** @type {string | VariableInfo | undefined} */
67
67
  this.identifier = undefined;
68
- /** @type {string | VariableInfoInterface | undefined} */
68
+ /** @type {string | VariableInfo | undefined} */
69
69
  this.rootInfo = undefined;
70
70
  /** @type {(() => string[]) | undefined} */
71
71
  this.getMembers = undefined;
@@ -386,8 +386,8 @@ class BasicEvaluatedExpression {
386
386
 
387
387
  /**
388
388
  * Set's the value of this expression to a particular identifier and its members.
389
- * @param {string | VariableInfoInterface} identifier identifier to set
390
- * @param {string | VariableInfoInterface} rootInfo root info
389
+ * @param {string | VariableInfo} identifier identifier to set
390
+ * @param {string | VariableInfo} rootInfo root info
391
391
  * @param {() => string[]} getMembers members
392
392
  * @param {() => boolean[]=} getMembersOptionals optional members
393
393
  * @param {() => Range[]=} getMemberRanges ranges of progressively increasing sub-expressions
@@ -36,7 +36,7 @@ const deprecatedGetInitFragments = util.deprecate(
36
36
  * @returns {InitFragment<GenerateContext>[]} init fragments
37
37
  */
38
38
  (template, dependency, templateContext) =>
39
- /** @type {DependencyTemplate & { getInitFragments: function(Dependency, DependencyTemplateContext): InitFragment<GenerateContext>[] }} */
39
+ /** @type {DependencyTemplate & { getInitFragments: (dependency: Dependency, dependencyTemplateContext: DependencyTemplateContext) => InitFragment<GenerateContext>[] }} */
40
40
  (template).getInitFragments(dependency, templateContext),
41
41
  "DependencyTemplate.getInitFragment is deprecated (use apply(dep, source, { initFragments }) instead)",
42
42
  "DEP_WEBPACK_JAVASCRIPT_GENERATOR_GET_INIT_FRAGMENTS"
@@ -110,6 +110,16 @@ class JavascriptGenerator extends Generator {
110
110
  return InitFragment.addToSource(source, initFragments, generateContext);
111
111
  }
112
112
 
113
+ /**
114
+ * @param {Error} error the error
115
+ * @param {NormalModule} module module for which the code should be generated
116
+ * @param {GenerateContext} generateContext context for generate
117
+ * @returns {Source | null} generated code
118
+ */
119
+ generateError(error, module, generateContext) {
120
+ return new RawSource(`throw new Error(${JSON.stringify(error.message)});`);
121
+ }
122
+
113
123
  /**
114
124
  * @param {Module} module the module to generate
115
125
  * @param {InitFragment<GenerateContext>[]} initFragments mutable list of init fragments
@@ -26,11 +26,12 @@ const {
26
26
  JAVASCRIPT_MODULE_TYPE_ESM,
27
27
  WEBPACK_MODULE_TYPE_RUNTIME
28
28
  } = require("../ModuleTypeConstants");
29
+ const NormalModule = require("../NormalModule");
29
30
  const RuntimeGlobals = require("../RuntimeGlobals");
30
31
  const Template = require("../Template");
31
32
  const { last, someInIterable } = require("../util/IterableHelpers");
32
33
  const StringXor = require("../util/StringXor");
33
- const { compareModulesByIdentifier } = require("../util/comparators");
34
+ const { compareModulesByIdOrIdentifier } = require("../util/comparators");
34
35
  const {
35
36
  getPathInAst,
36
37
  getAllReferences,
@@ -41,6 +42,7 @@ const {
41
42
  } = require("../util/concatenate");
42
43
  const createHash = require("../util/createHash");
43
44
  const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
45
+ const removeBOM = require("../util/removeBOM");
44
46
  const { intersectRuntime } = require("../util/runtime");
45
47
  const JavascriptGenerator = require("./JavascriptGenerator");
46
48
  const JavascriptParser = require("./JavascriptParser");
@@ -48,13 +50,14 @@ const JavascriptParser = require("./JavascriptParser");
48
50
  /** @typedef {import("eslint-scope").Reference} Reference */
49
51
  /** @typedef {import("eslint-scope").Scope} Scope */
50
52
  /** @typedef {import("eslint-scope").Variable} Variable */
53
+ /** @typedef {import("estree").Program} Program */
51
54
  /** @typedef {import("webpack-sources").Source} Source */
52
55
  /** @typedef {import("../../declarations/WebpackOptions").Output} OutputOptions */
53
56
  /** @typedef {import("../Chunk")} Chunk */
54
57
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
55
58
  /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */
56
59
  /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
57
- /** @typedef {import("../Compilation").ModuleObject} ModuleObject */
60
+ /** @typedef {import("../Compilation").ExecuteModuleObject} ExecuteModuleObject */
58
61
  /** @typedef {import("../Compiler")} Compiler */
59
62
  /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
60
63
  /** @typedef {import("../Entrypoint")} Entrypoint */
@@ -248,7 +251,7 @@ class JavascriptModulesPlugin {
248
251
 
249
252
  constructor(options = {}) {
250
253
  this.options = options;
251
- /** @type {WeakMap<Source, TODO>} */
254
+ /** @type {WeakMap<Source, { source: Source, needModule:boolean, needExports: boolean, needRequire: boolean, needThisAsExports: boolean, needStrict: boolean | undefined }>} */
252
255
  this._moduleFactoryCache = new WeakMap();
253
256
  }
254
257
 
@@ -262,24 +265,45 @@ class JavascriptModulesPlugin {
262
265
  PLUGIN_NAME,
263
266
  (compilation, { normalModuleFactory }) => {
264
267
  const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
265
- normalModuleFactory.hooks.createParser
266
- .for(JAVASCRIPT_MODULE_TYPE_AUTO)
267
- .tap(PLUGIN_NAME, options => new JavascriptParser("auto"));
268
- normalModuleFactory.hooks.createParser
269
- .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
270
- .tap(PLUGIN_NAME, options => new JavascriptParser("script"));
271
- normalModuleFactory.hooks.createParser
272
- .for(JAVASCRIPT_MODULE_TYPE_ESM)
273
- .tap(PLUGIN_NAME, options => new JavascriptParser("module"));
274
- normalModuleFactory.hooks.createGenerator
275
- .for(JAVASCRIPT_MODULE_TYPE_AUTO)
276
- .tap(PLUGIN_NAME, () => new JavascriptGenerator());
277
- normalModuleFactory.hooks.createGenerator
278
- .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
279
- .tap(PLUGIN_NAME, () => new JavascriptGenerator());
280
- normalModuleFactory.hooks.createGenerator
281
- .for(JAVASCRIPT_MODULE_TYPE_ESM)
282
- .tap(PLUGIN_NAME, () => new JavascriptGenerator());
268
+
269
+ for (const type of [
270
+ JAVASCRIPT_MODULE_TYPE_AUTO,
271
+ JAVASCRIPT_MODULE_TYPE_DYNAMIC,
272
+ JAVASCRIPT_MODULE_TYPE_ESM
273
+ ]) {
274
+ normalModuleFactory.hooks.createParser
275
+ .for(type)
276
+ .tap(PLUGIN_NAME, _options => {
277
+ switch (type) {
278
+ case JAVASCRIPT_MODULE_TYPE_AUTO: {
279
+ return new JavascriptParser("auto");
280
+ }
281
+ case JAVASCRIPT_MODULE_TYPE_DYNAMIC: {
282
+ return new JavascriptParser("script");
283
+ }
284
+ case JAVASCRIPT_MODULE_TYPE_ESM: {
285
+ return new JavascriptParser("module");
286
+ }
287
+ }
288
+ });
289
+ normalModuleFactory.hooks.createGenerator
290
+ .for(type)
291
+ .tap(PLUGIN_NAME, () => new JavascriptGenerator());
292
+
293
+ NormalModule.getCompilationHooks(compilation).processResult.tap(
294
+ PLUGIN_NAME,
295
+ (result, module) => {
296
+ if (module.type === type) {
297
+ const [source, ...rest] = result;
298
+
299
+ return [removeBOM(source), ...rest];
300
+ }
301
+
302
+ return result;
303
+ }
304
+ );
305
+ }
306
+
283
307
  compilation.hooks.renderManifest.tap(PLUGIN_NAME, (result, options) => {
284
308
  const {
285
309
  hash,
@@ -487,7 +511,7 @@ class JavascriptModulesPlugin {
487
511
  );
488
512
 
489
513
  const moduleObject =
490
- /** @type {ModuleObject} */
514
+ /** @type {ExecuteModuleObject} */
491
515
  (options.moduleObject);
492
516
 
493
517
  try {
@@ -678,7 +702,7 @@ class JavascriptModulesPlugin {
678
702
  const modules = chunkGraph.getOrderedChunkModulesIterableBySourceType(
679
703
  chunk,
680
704
  "javascript",
681
- compareModulesByIdentifier
705
+ compareModulesByIdOrIdentifier(chunkGraph)
682
706
  );
683
707
  const allModules = modules ? Array.from(modules) : [];
684
708
  let strictHeader;
@@ -757,7 +781,7 @@ class JavascriptModulesPlugin {
757
781
  chunkGraph.getOrderedChunkModulesIterableBySourceType(
758
782
  chunk,
759
783
  "javascript",
760
- compareModulesByIdentifier
784
+ compareModulesByIdOrIdentifier(chunkGraph)
761
785
  ) || []
762
786
  );
763
787
 
@@ -977,7 +1001,7 @@ class JavascriptModulesPlugin {
977
1001
  const lastEntryModule =
978
1002
  /** @type {Module} */
979
1003
  (last(chunkGraph.getChunkEntryModulesIterable(chunk)));
980
- /** @type {function(string[], string): Source} */
1004
+ /** @type {(content: string[], name: string) => Source} */
981
1005
  const toSource = useSourceMap
982
1006
  ? (content, name) =>
983
1007
  new OriginalSource(Template.asString(content), name)
@@ -1506,7 +1530,7 @@ class JavascriptModulesPlugin {
1506
1530
  const renamedInlinedModules = new Map();
1507
1531
  const { runtimeTemplate } = renderContext;
1508
1532
 
1509
- /** @typedef {{ source: Source, module: Module, ast: any, variables: Set<Variable>, through: Set<Reference>, usedInNonInlined: Set<Variable>, moduleScope: Scope }} Info */
1533
+ /** @typedef {{ source: Source, module: Module, ast: Program, variables: Set<Variable>, through: Set<Reference>, usedInNonInlined: Set<Variable>, moduleScope: Scope }} Info */
1510
1534
  /** @type {Map<Module, Info>} */
1511
1535
  const inlinedModulesToInfo = new Map();
1512
1536
  /** @type {Set<string>} */
@@ -1583,12 +1607,6 @@ class JavascriptModulesPlugin {
1583
1607
  }
1584
1608
 
1585
1609
  for (const variable of info.variables) {
1586
- allUsedNames.add(variable.name);
1587
- const references = getAllReferences(variable);
1588
- const allIdentifiers = new Set(
1589
- references.map(r => r.identifier).concat(variable.identifiers)
1590
- );
1591
-
1592
1610
  const usedNamesInScopeInfo = new Map();
1593
1611
  const ignoredScopes = new Set();
1594
1612
 
@@ -1601,6 +1619,9 @@ class JavascriptModulesPlugin {
1601
1619
 
1602
1620
  if (allUsedNames.has(name) || usedNames.has(name)) {
1603
1621
  const references = getAllReferences(variable);
1622
+ const allIdentifiers = new Set(
1623
+ references.map(r => r.identifier).concat(variable.identifiers)
1624
+ );
1604
1625
  for (const ref of references) {
1605
1626
  addScopeSymbols(
1606
1627
  ref.from,
@@ -1635,9 +1656,8 @@ class JavascriptModulesPlugin {
1635
1656
  }
1636
1657
  source.replace(r[0], r[1] - 1, newName);
1637
1658
  }
1638
- } else {
1639
- allUsedNames.add(name);
1640
1659
  }
1660
+ allUsedNames.add(name);
1641
1661
  }
1642
1662
 
1643
1663
  renamedInlinedModules.set(m, source);