webpack 5.111.0 → 5.111.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/lib/APIPlugin.js +3 -4
  2. package/lib/CompatibilityPlugin.js +3 -4
  3. package/lib/Compilation.js +16 -35
  4. package/lib/Compiler.js +4 -5
  5. package/lib/ConstPlugin.js +16 -132
  6. package/lib/DependencyTemplate.js +2 -1
  7. package/lib/DotenvPlugin.js +24 -28
  8. package/lib/ErrorHelpers.js +113 -0
  9. package/lib/ExportsInfo.js +20 -16
  10. package/lib/ExternalModule.js +6 -8
  11. package/lib/FlagDependencyExportsPlugin.js +8 -13
  12. package/lib/FlagDependencyUsagePlugin.js +9 -13
  13. package/lib/Generator.js +24 -2
  14. package/lib/HotModuleReplacementPlugin.js +2 -0
  15. package/lib/IgnorePlugin.js +1 -1
  16. package/lib/LoaderOptionsPlugin.js +1 -1
  17. package/lib/Module.js +3 -3
  18. package/lib/ModuleGraph.js +6 -9
  19. package/lib/MultiCompiler.js +6 -16
  20. package/lib/NormalModule.js +96 -44
  21. package/lib/RuntimeTemplate.js +33 -43
  22. package/lib/SourceMapDevToolPlugin.js +53 -103
  23. package/lib/TemplatedPathPlugin.js +16 -59
  24. package/lib/asset/AssetBytesGenerator.js +7 -1
  25. package/lib/asset/AssetGenerator.js +25 -16
  26. package/lib/asset/AssetSourceGenerator.js +7 -1
  27. package/lib/buildChunkGraph.js +11 -15
  28. package/lib/cache/MemoryCachePlugin.js +4 -4
  29. package/lib/cache/MemoryWithGcCachePlugin.js +7 -8
  30. package/lib/cache/PackFileCacheStrategy.js +3 -18
  31. package/lib/cache/getLazyHashedEtag.js +3 -5
  32. package/lib/config/browserslistTargetHandler.js +2 -8
  33. package/lib/config/defaults.js +52 -78
  34. package/lib/config/getClaimedAssetTypes.js +10 -11
  35. package/lib/config/target.js +3 -4
  36. package/lib/{ContextExclusionPlugin.js → context/ContextExclusionPlugin.js} +1 -1
  37. package/lib/{ContextModule.js → context/ContextModule.js} +37 -34
  38. package/lib/{ContextModuleFactory.js → context/ContextModuleFactory.js} +19 -21
  39. package/lib/{ContextReplacementPlugin.js → context/ContextReplacementPlugin.js} +4 -4
  40. package/lib/css/CssGenerator.js +19 -11
  41. package/lib/css/CssParser.js +18 -20
  42. package/lib/css/cssMinify.js +32 -29
  43. package/lib/css/data.js +797 -288
  44. package/lib/css/syntax.js +2250 -934
  45. package/lib/debug/ProfilingPlugin.js +3 -4
  46. package/lib/dependencies/CommonJsExportRequireDependency.js +10 -11
  47. package/lib/dependencies/CommonJsImportsParserPlugin.js +4 -5
  48. package/lib/dependencies/CommonJsSelfReferenceDependency.js +2 -4
  49. package/lib/dependencies/ContextDependency.js +1 -1
  50. package/lib/dependencies/ContextDependencyHelpers.js +3 -9
  51. package/lib/dependencies/ContextElementDependency.js +1 -1
  52. package/lib/dependencies/CssIcssExportDependency.js +6 -8
  53. package/lib/dependencies/CssIcssImportDependency.js +3 -6
  54. package/lib/dependencies/CssUrlDependency.js +3 -5
  55. package/lib/dependencies/HarmonyCompatibilityDependency.js +11 -20
  56. package/lib/dependencies/HarmonyDetectionParserPlugin.js +3 -4
  57. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +2 -4
  58. package/lib/dependencies/HarmonyExportExpressionDependency.js +3 -4
  59. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +3 -4
  60. package/lib/dependencies/HarmonyExportInitFragment.js +26 -19
  61. package/lib/dependencies/HarmonyExportSpecifierDependency.js +7 -28
  62. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +103 -16
  63. package/lib/dependencies/HarmonyImportSideEffectDependency.js +3 -4
  64. package/lib/dependencies/HarmonyImportSpecifierDependency.js +16 -2
  65. package/lib/dependencies/HtmlEntryDependency.js +74 -102
  66. package/lib/dependencies/HtmlInlineScriptDependency.js +3 -4
  67. package/lib/dependencies/HtmlInlineStyleDependency.js +3 -4
  68. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +1 -1
  69. package/lib/dependencies/ImportMetaGlobHelpers.js +1 -1
  70. package/lib/dependencies/ImportMetaPlugin.js +8 -10
  71. package/lib/dependencies/ImportParserPlugin.js +1 -1
  72. package/lib/dependencies/LoaderPlugin.js +26 -1
  73. package/lib/dependencies/RequireContextDependencyParserPlugin.js +1 -1
  74. package/lib/dependencies/URLContextDependency.js +1 -1
  75. package/lib/dependencies/URLDependency.js +6 -5
  76. package/lib/dependencies/WorkerAndWorkletPlugin.js +15 -21
  77. package/lib/dependencies/WorkerDependency.js +6 -5
  78. package/lib/dependencies/WorkletDependency.js +6 -9
  79. package/lib/errors/HookWebpackError.js +3 -2
  80. package/lib/hmr/LazyCompilationPlugin.js +3 -5
  81. package/lib/html/HtmlGenerator.js +61 -82
  82. package/lib/html/HtmlModulesPlugin.js +88 -139
  83. package/lib/html/HtmlParser.js +97 -141
  84. package/lib/html/data.js +21 -0
  85. package/lib/html/htmlMinify.js +10 -11
  86. package/lib/html/syntax.js +883 -993
  87. package/lib/index.js +46 -3
  88. package/lib/javascript/ChunkFormatHelpers.js +4 -3
  89. package/lib/javascript/JavascriptGenerator.js +8 -1
  90. package/lib/javascript/JavascriptModulesPlugin.js +21 -48
  91. package/lib/javascript/JavascriptParser.js +721 -178
  92. package/lib/javascript/parser.js +6 -0
  93. package/lib/javascript/syntax.js +420 -92
  94. package/lib/json/JsonGenerator.js +7 -1
  95. package/lib/library/ModuleLibraryPlugin.js +64 -136
  96. package/lib/loaders/LoaderRunner.js +137 -9
  97. package/lib/node/NodeWatchFileSystem.js +3 -9
  98. package/lib/optimize/AggressiveMergingPlugin.js +3 -5
  99. package/lib/optimize/ConcatenatedModule.js +81 -100
  100. package/lib/optimize/FlagIncludedChunksPlugin.js +3 -11
  101. package/lib/optimize/InnerGraphPlugin.js +7 -13
  102. package/lib/optimize/LimitChunkCountPlugin.js +5 -8
  103. package/lib/optimize/MangleExportsPlugin.js +3 -4
  104. package/lib/optimize/ModuleConcatenationPlugin.js +3 -4
  105. package/lib/optimize/SideEffectsFlagPlugin.js +7 -19
  106. package/lib/optimize/SplitChunksPlugin.js +2 -4
  107. package/lib/performance/BroadContextsPlugin.js +1 -1
  108. package/lib/performance/CacheEffectivenessPlugin.js +3 -4
  109. package/lib/performance/SizeLimitsPlugin.js +2 -4
  110. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +6 -8
  111. package/lib/prefetch/ResourceHintPlugin.js +17 -21
  112. package/lib/prefetch/StartupAssetHintRuntimeModule.js +7 -10
  113. package/lib/runtime/AsyncModuleRuntimeModule.js +3 -5
  114. package/lib/runtime/GetChunkFilenameRuntimeModule.js +3 -4
  115. package/lib/runtime/GlobalRuntimeModule.js +2 -4
  116. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +55 -85
  117. package/lib/schemes/DataUriPlugin.js +3 -6
  118. package/lib/schemes/VirtualUrlPlugin.js +1 -1
  119. package/lib/serialization/BinaryMiddleware.js +9 -9
  120. package/lib/serialization/FileMiddleware.js +16 -16
  121. package/lib/serialization/ObjectMiddleware.js +30 -33
  122. package/lib/stats/DefaultStatsFactoryPlugin.js +3 -4
  123. package/lib/typescript/TypeScriptPlugin.js +2 -3
  124. package/lib/url/URLParserPlugin.js +6 -8
  125. package/lib/util/LocConverter.js +4 -7
  126. package/lib/util/SourceProcessor.js +15 -22
  127. package/lib/util/chainedImports.js +3 -4
  128. package/lib/util/concatenate.js +4 -4
  129. package/lib/util/createHooksRegistry.js +1 -2
  130. package/lib/util/dataURL.js +2 -3
  131. package/lib/util/deterministicGrouping.js +8 -13
  132. package/lib/util/findGraphRoots.js +6 -13
  133. package/lib/util/globUtils.js +1 -1
  134. package/lib/util/hash/wasm-hash.js +3 -4
  135. package/lib/util/internalSerializables.js +1 -1
  136. package/lib/util/semver.js +5 -3
  137. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +6 -1
  138. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +5 -1
  139. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +3 -4
  140. package/lib/wasm-sync/WebAssemblyGenerator.js +6 -1
  141. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +5 -1
  142. package/lib/wasm-sync/WebAssemblyUtils.js +2 -4
  143. package/package.json +30 -23
  144. package/types.d.ts +299 -280
@@ -259,7 +259,13 @@ class JsonGenerator extends Generator {
259
259
  * @returns {Source | null} generated code
260
260
  */
261
261
  generateError(error, module, generateContext) {
262
- return new RawSource(Generator.throwBuildErrorCode(error));
262
+ return new RawSource(
263
+ Generator.throwBuildErrorCode(
264
+ error,
265
+ undefined,
266
+ generateContext.runtimeTemplate.requestShortener
267
+ )
268
+ );
263
269
  }
264
270
 
265
271
  /**
@@ -24,7 +24,7 @@ const {
24
24
  SAFE_IDENTIFIER,
25
25
  propertyAccess
26
26
  } = require("../util/property");
27
- const { getEntryRuntime, getRuntimeKey } = require("../util/runtime");
27
+ const { getEntryRuntime } = require("../util/runtime");
28
28
  const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
29
29
 
30
30
  /** @import { Source } from "webpack-sources" */
@@ -39,7 +39,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
39
39
  /** @import Compilation from "../Compilation" */
40
40
  /** @import Compiler from "../Compiler" */
41
41
  /** @import ModuleGraph from "../ModuleGraph" */
42
- /** @import Module, { BuildMeta } from "../Module" */
42
+ /** @import Module from "../Module" */
43
43
  /**
44
44
  * @import {
45
45
  * StartupRenderContext,
@@ -171,73 +171,33 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
171
171
  super.apply(compiler);
172
172
 
173
173
  compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
174
- const { onDemandExportsGeneration } =
175
- ConcatenatedModule.getCompilationHooks(compilation);
176
174
  const javascriptHooks =
177
175
  JavascriptModulesPlugin.getCompilationHooks(compilation);
178
- onDemandExportsGeneration.tap(
179
- PLUGIN_NAME,
180
- (module, runtimes, source, finalName) => {
181
- /** @type {BuildMeta} */
182
- const buildMeta = module.buildMeta || (module.buildMeta = {});
183
-
184
- /** @type {BuildMeta["exportsSourceByRuntime"]} */
185
- const exportsSourceByRuntime =
186
- buildMeta.exportsSourceByRuntime ||
187
- (buildMeta.exportsSourceByRuntime = new Map());
188
-
189
- /** @type {BuildMeta["exportsFinalNameByRuntime"]} */
190
- const exportsFinalNameByRuntime =
191
- buildMeta.exportsFinalNameByRuntime ||
192
- (buildMeta.exportsFinalNameByRuntime = new Map());
193
-
194
- for (const runtime of runtimes) {
195
- const key = getRuntimeKey(runtime);
196
- exportsSourceByRuntime.set(key, source);
197
- exportsFinalNameByRuntime.set(key, finalName);
198
- }
199
176
 
200
- return true;
201
- }
202
- );
203
- // The same for a plain entry, whose definitions come from a dependency template.
204
- // Placed after the module body, which its value bindings read.
205
- javascriptHooks.onDemandExportsGeneration.tap(
206
- PLUGIN_NAME,
207
- (module, runtime, source, placeAtEnd) => {
208
- const entries = libraryEntryModules.get(compilation);
209
- if (!entries || !entries.has(module)) return;
210
- if (!definesOnlyNativeExports(module, compilation.moduleGraph)) {
211
- return;
212
- }
213
- /** @type {BuildMeta} */
214
- const buildMeta = module.buildMeta || (module.buildMeta = {});
215
- const key = getRuntimeKey(runtime);
216
- if (placeAtEnd) {
217
- const bindings =
218
- buildMeta.exportsBindingSourceByRuntime ||
219
- (buildMeta.exportsBindingSourceByRuntime = new Map());
220
- bindings.set(key, source);
221
- } else {
222
- const sources =
223
- buildMeta.exportsSourceByRuntime ||
224
- (buildMeta.exportsSourceByRuntime = new Map());
225
- // The `__esModule` marker and the definitions both land here, so
226
- // keep whichever arrived first rather than replacing it.
227
- const before = sources.get(key);
228
- sources.set(key, before ? `${before}${source}` : source);
177
+ compilation.hooks.beforeCodeGeneration.tap(PLUGIN_NAME, () => {
178
+ const entries = libraryEntryModules.get(compilation);
179
+ if (entries === undefined) return;
180
+ const { moduleGraph } = compilation;
181
+ for (const { dependencies } of compilation.entries.values()) {
182
+ for (const dep of dependencies) {
183
+ const module = dep && moduleGraph.getModule(dep);
184
+ if (!module) continue;
185
+ const root =
186
+ module instanceof ConcatenatedModule ? module.rootModule : module;
187
+ if (!entries.has(root)) continue;
188
+ if (
189
+ root === module &&
190
+ !definesOnlyNativeExports(module, moduleGraph)
191
+ ) {
192
+ continue;
193
+ }
194
+ moduleGraph.getMeta(module).onDemandExports = true;
229
195
  }
230
- return true;
231
196
  }
232
- );
197
+ });
233
198
 
234
- // `ModuleLibraryPlugin` stashes the on-demand exports source via
235
- // `onDemandExportsGeneration` and only re-emits it when the
236
- // module is wrapped in an IIFE/factory. When the entry is
237
- // inlined directly, the stashed source — and the
238
- // `definePropertyGetters` / `requireScope` runtime helpers it
239
- // pulled in — never make it into the output. Drop those helpers
240
- // from the chunk's set so the bundle stays clean.
199
+ // The stashed exports source is re-emitted only where the entry is wrapped;
200
+ // inlined directly, the helpers it pulled in have no reader and can go.
241
201
  compilation.hooks.additionalChunkRuntimeRequirements.tap(
242
202
  PLUGIN_NAME,
243
203
  (chunk, set, { chunkGraph, codeGenerationResults }) => {
@@ -255,22 +215,16 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
255
215
  if (javascriptHooks.inlineInRuntimeBailout.isUsed()) return;
256
216
 
257
217
  const [module] = chunkGraph.getChunkEntryModulesIterable(chunk);
258
- const runtimeKey = getRuntimeKey(chunk.runtime);
259
- const buildMeta = module.buildMeta;
260
- const takenOver =
261
- (buildMeta &&
262
- buildMeta.exportsSourceByRuntime &&
263
- buildMeta.exportsSourceByRuntime.has(runtimeKey)) ||
264
- (buildMeta &&
265
- buildMeta.exportsBindingSourceByRuntime &&
266
- buildMeta.exportsBindingSourceByRuntime.has(runtimeKey));
267
- if (!takenOver) return;
268
218
  const codeGenResult = codeGenerationResults.get(
269
219
  module,
270
220
  chunk.runtime
271
221
  );
272
- const jsSource =
273
- codeGenResult && codeGenResult.sources.get("javascript");
222
+ const { data } = codeGenResult;
223
+ const takenOver =
224
+ data !== undefined &&
225
+ (data.has("exportsSource") || data.has("exportsBindingSource"));
226
+ if (!takenOver) return;
227
+ const jsSource = codeGenResult.sources.get("javascript");
274
228
  if (
275
229
  jsSource &&
276
230
  String(jsSource.source()).includes(`${RuntimeGlobals.require}.`)
@@ -608,19 +562,13 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
608
562
  ]
609
563
  : moduleGraph.getExportsInfo(module).orderedExports;
610
564
 
611
- const exportsFinalNameByRuntime =
612
- (module.buildMeta &&
613
- module.buildMeta.exportsFinalNameByRuntime &&
614
- module.buildMeta.exportsFinalNameByRuntime.get(
615
- getRuntimeKey(chunk.runtime)
616
- )) ||
617
- {};
565
+ const { data } = codeGenerationResults.get(module, chunk.runtime);
618
566
 
619
567
  const isInlinedEntryWithoutIIFE = inlined && !inlinedInIIFE;
620
568
  // Direct export bindings from on-demand concatenation
621
- const definitions = isInlinedEntryWithoutIIFE
622
- ? exportsFinalNameByRuntime
623
- : {};
569
+ /** @type {Record<string, string>} */
570
+ const definitions =
571
+ (isInlinedEntryWithoutIIFE && data && data.get("exportsFinalName")) || {};
624
572
 
625
573
  /** @type {string[]} */
626
574
  const shortHandedExports = [];
@@ -654,15 +602,12 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
654
602
  .treatAsCommonJs;
655
603
  const skipRenderDefaultExport = Boolean(treatAsCommonJs);
656
604
 
657
- // A single (non-concatenated) inlined entry is rendered at the top
658
- // level, so its own declarations are top-level bindings we can export
659
- // directly — keeping ESM live bindings instead of snapshotting
660
- // `__webpack_exports__.x` into a `const`. Concatenated entries already
661
- // get direct bindings via `definitions` above.
605
+ // A single inlined entry renders at the top level, so its declarations are
606
+ // bindings that can be exported directly, keeping ESM live bindings rather
607
+ // than snapshotting them into a `const`.
662
608
  /** @type {Map<string, string> | undefined} */
663
609
  let directBindings;
664
610
  if (isInlinedEntryWithoutIIFE && !treatAsCommonJs) {
665
- const { data } = codeGenerationResults.get(module, chunk.runtime);
666
611
  const topLevelDeclarations =
667
612
  (data && data.get("topLevelDeclarations")) ||
668
613
  (module.buildInfo && module.buildInfo.topLevelDeclarations);
@@ -681,15 +626,8 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
681
626
  }
682
627
  }
683
628
 
684
- // Taken over by `onDemandExportsGeneration` above, and re-emitted by
685
- // `renderModuleContent` only where the module is wrapped — which is not here.
686
- const bindingsTakenOver = Boolean(
687
- module.buildMeta &&
688
- module.buildMeta.exportsBindingSourceByRuntime &&
689
- module.buildMeta.exportsBindingSourceByRuntime.has(
690
- getRuntimeKey(chunk.runtime)
691
- )
692
- );
629
+ const bindingsTakenOver =
630
+ data !== undefined && data.has("exportsBindingSource");
693
631
 
694
632
  const moduleExportsInfo = moduleGraph.getExportsInfo(module);
695
633
 
@@ -711,7 +649,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
711
649
  }
712
650
 
713
651
  // Try to find all known exports of the entry module
714
- outer: for (const exportInfo of exportInfos) {
652
+ for (const exportInfo of exportInfos) {
715
653
  if (!exportInfo.provided) continue;
716
654
 
717
655
  const originalName = exportInfo.name;
@@ -719,40 +657,37 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
719
657
  if (skipRenderDefaultExport && originalName === "default") continue;
720
658
 
721
659
  // Try to find all exports from the reexported modules
722
- const target = exportInfo.findTarget(moduleGraph, (_m) => true);
723
- // A namespace object target (`export * as ns from "./module"`) has no
724
- // export name, so there is nothing that could be missing
725
- if (target && target.export) {
726
- const targetExport = target.export[0];
727
- const reexportsInfo = moduleGraph.getExportsInfo(target.module);
728
- for (const reexportInfo of reexportsInfo.orderedExports) {
729
- if (
730
- reexportInfo.provided === false &&
731
- reexportInfo.name !== "default" &&
732
- reexportInfo.name === targetExport
733
- ) {
734
- continue outer;
735
- }
736
- }
660
+ const target = exportInfo.getTarget(moduleGraph);
661
+
662
+ // A re-export is provided even when its target lacks the export, and
663
+ // the path can end in a property access, so every name is looked up.
664
+ if (
665
+ target &&
666
+ target.export &&
667
+ target.export[target.export.length - 1] !== "default" &&
668
+ moduleGraph
669
+ .getExportsInfo(target.module)
670
+ .isExportProvided(target.export) === false
671
+ ) {
672
+ continue;
737
673
  }
738
674
 
739
- const externalTarget = exportInfo.getTarget(moduleGraph);
740
675
  if (
741
- externalTarget &&
742
- externalTarget.module instanceof ExternalModule &&
743
- externalTarget.export &&
744
- externalTarget.export.length === 1 &&
745
- codeGenerationResults.has(externalTarget.module, chunk.runtime)
676
+ target &&
677
+ target.module instanceof ExternalModule &&
678
+ target.export &&
679
+ target.export.length === 1 &&
680
+ codeGenerationResults.has(target.module, chunk.runtime)
746
681
  ) {
747
682
  const reexport = codeGenerationResults.getData(
748
- externalTarget.module,
683
+ target.module,
749
684
  chunk.runtime,
750
685
  "externalReexport"
751
686
  );
752
687
  if (reexport) {
753
688
  if (!bindingsTakenOver) needExportsDeclaration = true;
754
689
  result.add(
755
- `export { ${toExportName(externalTarget.export[0])} as ${toExportName(originalName)} } ${reexport};\n`
690
+ `export { ${toExportName(target.export[0])} as ${toExportName(originalName)} } ${reexport};\n`
756
691
  );
757
692
  alreadyRenderedExports.add(originalName);
758
693
  continue;
@@ -801,7 +736,6 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
801
736
  finalName.includes("("))
802
737
  ) {
803
738
  if (exportInfo.isReexport()) {
804
- const { data } = codeGenerationResults.get(module, chunk.runtime);
805
739
  const topLevelDeclarations =
806
740
  (data && data.get("topLevelDeclarations")) ||
807
741
  (module.buildInfo && module.buildInfo.topLevelDeclarations);
@@ -880,19 +814,13 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
880
814
  renderModuleContent(
881
815
  source,
882
816
  module,
883
- { factory, inlinedInIIFE, chunk },
817
+ { factory, inlinedInIIFE, chunk, codeGenerationResults },
884
818
  libraryContext
885
819
  ) {
886
- const runtimeKey = getRuntimeKey(chunk.runtime);
887
- const buildMeta = module.buildMeta;
888
- const exportsSource =
889
- buildMeta &&
890
- buildMeta.exportsSourceByRuntime &&
891
- buildMeta.exportsSourceByRuntime.get(runtimeKey);
892
- const bindingSource =
893
- buildMeta &&
894
- buildMeta.exportsBindingSourceByRuntime &&
895
- buildMeta.exportsBindingSourceByRuntime.get(runtimeKey);
820
+ const { data } = codeGenerationResults.get(module, chunk.runtime);
821
+ if (data === undefined) return source;
822
+ const exportsSource = data.get("exportsSource");
823
+ const bindingSource = data.get("exportsBindingSource");
896
824
 
897
825
  // Re-add the exports source in a factory or IIFE-wrapped startup module; value
898
826
  // bindings read the declarations above them, so those go after the body.
@@ -10,6 +10,9 @@ const { parseResource } = require("../util/identifier");
10
10
  const { adjustSourceMapForRemovedBOM } = require("../util/removeBOM");
11
11
  const loadLoader = require("./loadLoader");
12
12
 
13
+ /** @import { RawSourceMap } from "webpack-sources" */
14
+ /** @import { Environment } from "../../declarations/WebpackOptions" */
15
+
13
16
  // Set on a loader context to have each loader's own run measured. Absent
14
17
  // unless something asked for the measurement, and read once per loader.
15
18
  const LOADER_TIMING = Symbol("loader timing");
@@ -44,14 +47,139 @@ const LOADER_TIMING = Symbol("loader timing");
44
47
 
45
48
  /** @typedef {(...args: EXPECTED_ANY[]) => void} LoaderCallback */
46
49
 
47
- /** @typedef {import("../../declarations/LoaderContext").LoaderRunnerLoaderContext<EXPECTED_ANY>} LoaderRunnerLoaderContext */
50
+ /**
51
+ * @typedef {object} AdditionalDataKnown
52
+ * @property {EXPECTED_OBJECT} webpackAST the AST webpack should parse instead of the source
53
+ */
54
+
55
+ /** @typedef {AdditionalDataKnown & Record<string, EXPECTED_ANY>} AdditionalData */
56
+
57
+ /**
58
+ * @callback WebpackLoaderContextCallback
59
+ * @param {undefined | null | Error} err error object
60
+ * @param {(string | Buffer)=} content the loader result
61
+ * @param {(null | string | RawSourceMap)=} sourceMap a source map for the result
62
+ * @param {AdditionalData=} additionalData data passed to the next loader
63
+ * @returns {void}
64
+ */
65
+
66
+ /**
67
+ * @typedef {object} LoaderEntry
68
+ * @property {string} request
69
+ * @property {string} path
70
+ * @property {string} query
71
+ * @property {string} fragment
72
+ * @property {EXPECTED_OBJECT | string | undefined} options
73
+ * @property {string} ident
74
+ * @property {EXPECTED_FUNCTION | undefined} normal
75
+ * @property {EXPECTED_FUNCTION | undefined} pitch
76
+ * @property {boolean | undefined} raw
77
+ * @property {EXPECTED_OBJECT | undefined} data
78
+ * @property {boolean} pitchExecuted
79
+ * @property {boolean} normalExecuted
80
+ * @property {("commonjs" | "module")=} type
81
+ */
82
+
83
+ /**
84
+ * The members the runner never lets a loader assign. They carry no
85
+ * documentation, so a type literal loses nothing and keeps `readonly`
86
+ * @template OptionsType
87
+ * @typedef {{ readonly currentRequest: string, readonly data: EXPECTED_ANY, readonly previousRequest: string, readonly query: string | OptionsType, readonly remainingRequest: string, readonly request: string }} LoaderRunnerReadonlyContext
88
+ */
89
+
90
+ /**
91
+ * @template OptionsType
92
+ * @typedef {object} LoaderRunnerMutableContext
93
+ * @property {(context: string) => void} addContextDependency Add a directory as dependency of the loader result.
94
+ * @property {(file: string) => void} addDependency Adds a file as dependency of the loader result in order to make them watchable.
95
+ * For example, html-loader uses this technique as it finds src and src-set attributes.
96
+ * Then, it sets the url's for those attributes as dependencies of the html file that is parsed.
97
+ * @property {(context: string) => void} addMissingDependency
98
+ * @property {() => WebpackLoaderContextCallback} async Make this loader async.
99
+ * @property {(flag?: boolean) => void} cacheable Make this loader result cacheable. By default it's cacheable.
100
+ * A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed.
101
+ * This means the loader shouldn't have other dependencies than specified with this.addDependency.
102
+ * Most loaders are deterministic and cacheable.
103
+ * @property {WebpackLoaderContextCallback} callback
104
+ * @property {() => void} clearDependencies Remove all dependencies of the loader result. Even initial dependencies and these of other loaders.
105
+ * @property {string} context The directory of the module. Can be used as context for resolving other stuff.
106
+ * eg '/workspaces/ts-loader/examples/vanilla/src'
107
+ * @property {(file: string) => void} dependency alias of addDependency
108
+ * Adds a file as dependency of the loader result in order to make them watchable.
109
+ * For example, html-loader uses this technique as it finds src and src-set attributes.
110
+ * Then, it sets the url's for those attributes as dependencies of the html file that is parsed.
111
+ * @property {() => string[]} getContextDependencies
112
+ * @property {() => string[]} getDependencies
113
+ * @property {() => string[]} getMissingDependencies
114
+ * @property {number} loaderIndex The index in the loaders array of the current loader.
115
+ * In the example: in loader1: 0, in loader2: 1
116
+ * @property {LoaderEntry[]} loaders An array of all the loaders. It is writeable in the pitch phase.
117
+ * loaders = [{request: string, path: string, query: string, module: function}]
118
+ * @property {string} resourcePath The resource path.
119
+ * In the example: "/abc/resource.js"
120
+ * @property {string} resourceQuery The resource query string.
121
+ * Example: "?query"
122
+ * @property {string} resourceFragment The resource fragment.
123
+ * Example: "#frag"
124
+ * @property {string} resource The resource inclusive query and fragment.
125
+ * Example: "/abc/resource.js?query#frag"
126
+ * @property {string} target Target of compilation.
127
+ * Example: "web"
128
+ * @property {Environment} environment Tell what kind of ES-features may be used in the generated runtime-code.
129
+ * Example: { arrowFunction: true }
130
+ */
131
+
132
+ /**
133
+ * The properties are added by https://github.com/webpack/loader-runner
134
+ * @template OptionsType
135
+ * @typedef {LoaderRunnerMutableContext<OptionsType> & LoaderRunnerReadonlyContext<OptionsType>} LoaderRunnerLoaderContext
136
+ */
137
+
138
+ /**
139
+ * @template [OptionsType={}]
140
+ * @template [ContextAdditions={}]
141
+ * @typedef {(this: import("../NormalModule").LoaderContext<OptionsType> & ContextAdditions, remainingRequest: string, previousRequest: string, data: EXPECTED_OBJECT) => string | Buffer | Promise<string | Buffer> | void} PitchLoaderDefinitionFunction
142
+ */
143
+
144
+ /**
145
+ * @template [OptionsType={}]
146
+ * @template [ContextAdditions={}]
147
+ * @typedef {(this: import("../NormalModule").LoaderContext<OptionsType> & ContextAdditions, content: string, sourceMap?: string | RawSourceMap, additionalData?: AdditionalData) => string | Buffer | Promise<string | Buffer> | void} LoaderDefinitionFunction
148
+ */
149
+
150
+ /**
151
+ * @template [OptionsType={}]
152
+ * @template [ContextAdditions={}]
153
+ * @typedef {(this: import("../NormalModule").LoaderContext<OptionsType> & ContextAdditions, content: Buffer, sourceMap?: string | RawSourceMap, additionalData?: AdditionalData) => string | Buffer | Promise<string | Buffer> | void} RawLoaderDefinitionFunction
154
+ */
155
+
156
+ /**
157
+ * @template [OptionsType={}]
158
+ * @template [ContextAdditions={}]
159
+ * @typedef {LoaderDefinitionFunction<OptionsType, ContextAdditions> & { raw?: false, pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions> }} LoaderDefinition
160
+ */
161
+
162
+ /**
163
+ * @template [OptionsType={}]
164
+ * @template [ContextAdditions={}]
165
+ * @typedef {RawLoaderDefinitionFunction<OptionsType, ContextAdditions> & { raw: true, pitch?: PitchLoaderDefinitionFunction<OptionsType, ContextAdditions> }} RawLoaderDefinition
166
+ */
167
+
168
+ /**
169
+ * @template [OptionsType={}]
170
+ * @template [ContextAdditions={}]
171
+ * @typedef {object} LoaderModule
172
+ * @property {(RawLoaderDefinitionFunction<OptionsType, ContextAdditions> | LoaderDefinitionFunction<OptionsType, ContextAdditions>)=} default
173
+ * @property {false=} raw
174
+ * @property {PitchLoaderDefinitionFunction<OptionsType, ContextAdditions>=} pitch
175
+ */
48
176
 
49
177
  /**
50
178
  * The loader context as the runner sees and mutates it: the canonical
51
179
  * `LoaderRunnerLoaderContext` shape (not re-declared here), with only the fields
52
180
  * the runner assigns internally before a loader runs widened to their mutable
53
181
  * form (nullable `context`/`callback`/`async`, `LoaderObject` loaders).
54
- * @typedef {Omit<LoaderRunnerLoaderContext, "context" | "callback" | "async" | "loaders"> & { context: string | null, callback: LoaderCallback | null, async: (() => LoaderCallback | undefined) | null, loaders: LoaderObject[] }} LoaderContext
182
+ * @typedef {Omit<LoaderRunnerLoaderContext<EXPECTED_ANY>, "context" | "callback" | "async" | "loaders"> & { context: string | null, callback: LoaderCallback | null, async: (() => LoaderCallback | undefined) | null, loaders: LoaderObject[] }} LoaderContext
55
183
  */
56
184
 
57
185
  const HASH_ESCAPE_REGEXP = /#/g;
@@ -575,7 +703,7 @@ function createLoaderContext(base) {
575
703
  * Marks the request as not cacheable with the given reasons instead of
576
704
  * attributing the currently running loader (used by the host when the cause
577
705
  * lives outside the loader, e.g. in a child compilation of `importModule`).
578
- * @param {LoaderRunnerLoaderContext} loaderContext loader context
706
+ * @param {LoaderRunnerLoaderContext<EXPECTED_ANY>} loaderContext loader context
579
707
  * @param {string[]} reasons reasons why the request is not cacheable
580
708
  * @returns {void}
581
709
  */
@@ -682,12 +810,12 @@ const ACCESSORS = {
682
810
  * @returns {void}
683
811
  */
684
812
  module.exports.runLoaders = function runLoaders(options, callback) {
685
- // reuse a context already prepared by createLoaderContext (e.g. from
686
- // NormalModule), else augment the caller-provided context (or a fresh object)
687
- // in place. State is intentionally preserved across the handoff, so host hooks
688
- // (e.g. beforeLoaders) can pre-add dependencies or mark the request
689
- // non-cacheable before the run; a caller re-running on the same context should
690
- // clearDependencies() first to avoid accumulating stale dependencies.
813
+ // WHY: reuse a context `createLoaderContext` already prepared, from
814
+ // NormalModule say, else augment the caller-provided context, or a fresh
815
+ // object, in place. State is intentionally preserved across the handoff, so a
816
+ // host hook such as `beforeLoaders` can pre-add dependencies or mark the
817
+ // request non-cacheable before the run; a caller re-running on the same context
818
+ // should `clearDependencies()` first, to avoid accumulating stale ones.
691
819
  const provided = /** @type {EXPECTED_ANY} */ (options.context);
692
820
  const loaderContext =
693
821
  provided && provided[LOADER_STATE]
@@ -147,15 +147,9 @@ class NodeWatchFileSystem {
147
147
  const directoriesSet =
148
148
  directories instanceof Set ? directories : new Set(directories);
149
149
 
150
- // Watchpack reports a watched directory (a context dependency) in
151
- // `changes` whenever its contents change, alongside the individual
152
- // file events. The default `fs.purge(dir)` matches cache keys by
153
- // prefix, so it would wipe the stat cache of every file inside the
154
- // directory even though only file-level events actually invalidate
155
- // file stats. For directories we explicitly watch, purge only the
156
- // directory's own entry (`{ exact: true }`, enhanced-resolve >=
157
- // 5.22.0); file-level events in the same aggregated batch still
158
- // handle file stats and the parent readdir invalidation.
150
+ // Watchpack reports a watched directory whenever its contents change, and
151
+ // `fs.purge(dir)` matches by prefix, wiping every file's stat cache with it.
152
+ // Only its own entry is purged; the batch's file events carry the rest.
159
153
  /**
160
154
  * @param {Changes | null | undefined} changes changes set
161
155
  * @param {Removals | null | undefined} removals removals set
@@ -53,11 +53,9 @@ class AggressiveMergingPlugin {
53
53
  },
54
54
  (chunks) => {
55
55
  const chunkGraph = compilation.chunkGraph;
56
- // Only the single best pair is merged per pass (the hook re-runs
57
- // after each merge), so track the max directly instead of building
58
- // and sorting the full O(chunks²) list of pairs every pass. A strict
59
- // `>` keeps the first-encountered best, matching the previous stable
60
- // descending sort's `[0]`.
56
+ // One pair is merged per pass and the hook re-runs, so the maximum is
57
+ // tracked directly rather than sorting every pair each time. A strict
58
+ // `>` keeps the first best found, as a stable descending sort would.
61
59
  /** @type {Chunk | undefined} */
62
60
  let bestA;
63
61
  /** @type {Chunk | undefined} */