webpack 5.102.1 → 5.104.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 (220) hide show
  1. package/README.md +121 -134
  2. package/hot/dev-server.js +18 -3
  3. package/hot/emitter-event-target.js +7 -0
  4. package/hot/lazy-compilation-node.js +45 -29
  5. package/hot/lazy-compilation-universal.js +18 -0
  6. package/hot/lazy-compilation-web.js +15 -5
  7. package/hot/load-http.js +7 -0
  8. package/hot/only-dev-server.js +19 -4
  9. package/lib/APIPlugin.js +6 -0
  10. package/lib/Chunk.js +1 -1
  11. package/lib/ChunkGraph.js +9 -7
  12. package/lib/ChunkGroup.js +8 -5
  13. package/lib/CleanPlugin.js +6 -3
  14. package/lib/CodeGenerationResults.js +2 -1
  15. package/lib/CompatibilityPlugin.js +28 -2
  16. package/lib/Compilation.js +58 -21
  17. package/lib/Compiler.js +3 -3
  18. package/lib/ConcatenationScope.js +0 -15
  19. package/lib/ContextModule.js +6 -3
  20. package/lib/ContextModuleFactory.js +6 -4
  21. package/lib/CssModule.js +6 -1
  22. package/lib/DefinePlugin.js +45 -14
  23. package/lib/DelegatedModule.js +7 -4
  24. package/lib/Dependency.js +8 -1
  25. package/lib/DependencyTemplate.js +1 -0
  26. package/lib/DllModule.js +6 -3
  27. package/lib/DotenvPlugin.js +462 -0
  28. package/lib/EnvironmentPlugin.js +19 -16
  29. package/lib/EvalSourceMapDevToolPlugin.js +16 -0
  30. package/lib/ExportsInfo.js +6 -2
  31. package/lib/ExternalModule.js +28 -35
  32. package/lib/ExternalModuleFactoryPlugin.js +11 -9
  33. package/lib/ExternalsPlugin.js +2 -1
  34. package/lib/FileSystemInfo.js +1 -1
  35. package/lib/Generator.js +10 -7
  36. package/lib/HookWebpackError.js +33 -4
  37. package/lib/HotModuleReplacementPlugin.js +22 -0
  38. package/lib/ManifestPlugin.js +235 -0
  39. package/lib/Module.js +27 -15
  40. package/lib/ModuleBuildError.js +1 -1
  41. package/lib/ModuleError.js +1 -1
  42. package/lib/ModuleFilenameHelpers.js +1 -1
  43. package/lib/ModuleGraph.js +29 -13
  44. package/lib/ModuleGraphConnection.js +2 -2
  45. package/lib/ModuleSourceTypeConstants.js +189 -0
  46. package/lib/ModuleTypeConstants.js +1 -4
  47. package/lib/ModuleWarning.js +1 -1
  48. package/lib/MultiCompiler.js +1 -1
  49. package/lib/NodeStuffPlugin.js +424 -116
  50. package/lib/NormalModule.js +23 -20
  51. package/lib/NormalModuleFactory.js +7 -10
  52. package/lib/Parser.js +1 -1
  53. package/lib/RawModule.js +7 -4
  54. package/lib/RuntimeGlobals.js +22 -4
  55. package/lib/RuntimeModule.js +1 -1
  56. package/lib/RuntimePlugin.js +27 -6
  57. package/lib/RuntimeTemplate.js +120 -57
  58. package/lib/SourceMapDevToolPlugin.js +26 -1
  59. package/lib/Template.js +17 -6
  60. package/lib/TemplatedPathPlugin.js +5 -6
  61. package/lib/WebpackError.js +0 -1
  62. package/lib/WebpackOptionsApply.js +67 -15
  63. package/lib/asset/AssetBytesGenerator.js +16 -12
  64. package/lib/asset/AssetGenerator.js +31 -26
  65. package/lib/asset/AssetSourceGenerator.js +16 -12
  66. package/lib/asset/RawDataUrlModule.js +6 -3
  67. package/lib/buildChunkGraph.js +4 -2
  68. package/lib/cache/PackFileCacheStrategy.js +6 -5
  69. package/lib/cli.js +2 -43
  70. package/lib/config/browserslistTargetHandler.js +24 -0
  71. package/lib/config/defaults.js +226 -61
  72. package/lib/config/normalization.js +4 -3
  73. package/lib/config/target.js +11 -0
  74. package/lib/container/ContainerEntryModule.js +6 -3
  75. package/lib/container/FallbackModule.js +6 -3
  76. package/lib/container/RemoteModule.js +1 -3
  77. package/lib/css/CssGenerator.js +304 -76
  78. package/lib/css/CssLoadingRuntimeModule.js +14 -4
  79. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +56 -0
  80. package/lib/css/CssModulesPlugin.js +72 -67
  81. package/lib/css/CssParser.js +1726 -732
  82. package/lib/css/walkCssTokens.js +128 -11
  83. package/lib/dependencies/CachedConstDependency.js +24 -10
  84. package/lib/dependencies/CommonJsImportsParserPlugin.js +0 -9
  85. package/lib/dependencies/CommonJsPlugin.js +12 -0
  86. package/lib/dependencies/CommonJsRequireContextDependency.js +1 -1
  87. package/lib/dependencies/ContextDependencyHelpers.js +2 -2
  88. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +3 -1
  89. package/lib/dependencies/CssIcssExportDependency.js +389 -12
  90. package/lib/dependencies/CssIcssImportDependency.js +114 -51
  91. package/lib/dependencies/CssIcssSymbolDependency.js +31 -33
  92. package/lib/dependencies/CssImportDependency.js +17 -6
  93. package/lib/dependencies/CssUrlDependency.js +3 -2
  94. package/lib/dependencies/DynamicExports.js +7 -7
  95. package/lib/dependencies/ExternalModuleDependency.js +7 -4
  96. package/lib/dependencies/ExternalModuleInitFragment.js +3 -2
  97. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +96 -0
  98. package/lib/dependencies/HarmonyAcceptDependency.js +6 -1
  99. package/lib/dependencies/HarmonyAcceptImportDependency.js +2 -1
  100. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +12 -1
  101. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +35 -23
  102. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +13 -9
  103. package/lib/dependencies/HarmonyExports.js +4 -4
  104. package/lib/dependencies/HarmonyImportDependency.js +28 -27
  105. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +28 -69
  106. package/lib/dependencies/HarmonyImportSideEffectDependency.js +4 -3
  107. package/lib/dependencies/HarmonyImportSpecifierDependency.js +10 -8
  108. package/lib/dependencies/ImportDependency.js +8 -2
  109. package/lib/dependencies/ImportEagerDependency.js +6 -3
  110. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +1 -1
  111. package/lib/dependencies/ImportMetaPlugin.js +154 -9
  112. package/lib/dependencies/ImportParserPlugin.js +21 -23
  113. package/lib/dependencies/ImportPhase.js +121 -0
  114. package/lib/dependencies/ImportWeakDependency.js +6 -3
  115. package/lib/dependencies/LocalModulesHelpers.js +3 -3
  116. package/lib/dependencies/ModuleDependency.js +5 -1
  117. package/lib/dependencies/ModuleHotAcceptDependency.js +1 -1
  118. package/lib/dependencies/WorkerPlugin.js +2 -2
  119. package/lib/dependencies/getFunctionExpression.js +1 -1
  120. package/lib/esm/ExportWebpackRequireRuntimeModule.js +1 -8
  121. package/lib/esm/ModuleChunkFormatPlugin.js +5 -4
  122. package/lib/hmr/HotModuleReplacement.runtime.js +2 -1
  123. package/lib/hmr/LazyCompilationPlugin.js +5 -3
  124. package/lib/ids/IdHelpers.js +20 -8
  125. package/lib/index.js +6 -0
  126. package/lib/javascript/ChunkHelpers.js +16 -5
  127. package/lib/javascript/JavascriptGenerator.js +105 -104
  128. package/lib/javascript/JavascriptModulesPlugin.js +80 -37
  129. package/lib/javascript/JavascriptParser.js +161 -44
  130. package/lib/json/JsonGenerator.js +5 -4
  131. package/lib/json/JsonParser.js +9 -2
  132. package/lib/library/AbstractLibraryPlugin.js +1 -1
  133. package/lib/library/AmdLibraryPlugin.js +4 -1
  134. package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
  135. package/lib/library/ModuleLibraryPlugin.js +41 -23
  136. package/lib/library/SystemLibraryPlugin.js +8 -1
  137. package/lib/library/UmdLibraryPlugin.js +2 -2
  138. package/lib/logging/Logger.js +5 -4
  139. package/lib/logging/createConsoleLogger.js +2 -2
  140. package/lib/node/NodeTargetPlugin.js +9 -1
  141. package/lib/node/ReadFileCompileWasmPlugin.js +0 -2
  142. package/lib/optimize/ConcatenatedModule.js +208 -167
  143. package/lib/optimize/ModuleConcatenationPlugin.js +5 -4
  144. package/lib/optimize/SideEffectsFlagPlugin.js +3 -2
  145. package/lib/optimize/SplitChunksPlugin.js +60 -46
  146. package/lib/rules/RuleSetCompiler.js +1 -1
  147. package/lib/runtime/AsyncModuleRuntimeModule.js +28 -18
  148. package/lib/runtime/AutoPublicPathRuntimeModule.js +8 -3
  149. package/lib/runtime/GetChunkFilenameRuntimeModule.js +3 -2
  150. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +89 -55
  151. package/lib/schemes/HttpUriPlugin.js +78 -7
  152. package/lib/serialization/AggregateErrorSerializer.js +1 -2
  153. package/lib/serialization/ObjectMiddleware.js +0 -2
  154. package/lib/serialization/SingleItemMiddleware.js +1 -1
  155. package/lib/sharing/ConsumeSharedModule.js +1 -1
  156. package/lib/sharing/ConsumeSharedPlugin.js +5 -3
  157. package/lib/sharing/ProvideSharedModule.js +1 -1
  158. package/lib/sharing/resolveMatchedConfigs.js +15 -9
  159. package/lib/sharing/utils.js +1 -1
  160. package/lib/stats/DefaultStatsFactoryPlugin.js +8 -5
  161. package/lib/stats/DefaultStatsPresetPlugin.js +1 -1
  162. package/lib/stats/DefaultStatsPrinterPlugin.js +1 -1
  163. package/lib/util/StringXor.js +1 -1
  164. package/lib/util/URLAbsoluteSpecifier.js +2 -2
  165. package/lib/util/binarySearchBounds.js +2 -2
  166. package/lib/util/comparators.js +54 -76
  167. package/lib/util/compileBooleanMatcher.js +78 -6
  168. package/lib/util/createHash.js +20 -199
  169. package/lib/util/deprecation.js +1 -1
  170. package/lib/util/deterministicGrouping.js +6 -3
  171. package/lib/util/fs.js +75 -75
  172. package/lib/util/hash/BatchedHash.js +10 -9
  173. package/lib/util/hash/BulkUpdateHash.js +138 -0
  174. package/lib/util/hash/DebugHash.js +75 -0
  175. package/lib/util/hash/hash-digest.js +216 -0
  176. package/lib/util/identifier.js +82 -17
  177. package/lib/util/internalSerializables.js +2 -6
  178. package/lib/util/runtime.js +3 -3
  179. package/lib/util/source.js +2 -2
  180. package/lib/wasm/EnableWasmLoadingPlugin.js +10 -4
  181. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +3 -2
  182. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +11 -7
  183. package/lib/wasm-sync/WebAssemblyGenerator.js +9 -6
  184. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +11 -6
  185. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +6 -2
  186. package/lib/web/FetchCompileWasmPlugin.js +0 -2
  187. package/lib/webpack.js +85 -82
  188. package/module.d.ts +5 -0
  189. package/package.json +34 -28
  190. package/schemas/WebpackOptions.check.js +1 -1
  191. package/schemas/WebpackOptions.json +160 -101
  192. package/schemas/plugins/{css/CssAutoParserOptions.check.d.ts → ManifestPlugin.check.d.ts} +1 -1
  193. package/schemas/plugins/ManifestPlugin.check.js +6 -0
  194. package/schemas/plugins/ManifestPlugin.json +98 -0
  195. package/schemas/plugins/SourceMapDevToolPlugin.check.js +1 -1
  196. package/schemas/plugins/SourceMapDevToolPlugin.json +16 -3
  197. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  198. package/schemas/plugins/container/ContainerReferencePlugin.json +4 -1
  199. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  200. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  201. package/schemas/plugins/container/ModuleFederationPlugin.json +4 -1
  202. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  203. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  204. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  205. package/schemas/plugins/json/JsonModulesPluginParser.check.js +1 -1
  206. package/types.d.ts +771 -436
  207. package/lib/ModuleSourceTypesConstants.js +0 -123
  208. package/lib/dependencies/CssLocalIdentifierDependency.js +0 -250
  209. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +0 -112
  210. package/schemas/plugins/css/CssAutoGeneratorOptions.check.d.ts +0 -7
  211. package/schemas/plugins/css/CssAutoGeneratorOptions.check.js +0 -6
  212. package/schemas/plugins/css/CssAutoGeneratorOptions.json +0 -3
  213. package/schemas/plugins/css/CssAutoParserOptions.check.js +0 -6
  214. package/schemas/plugins/css/CssAutoParserOptions.json +0 -3
  215. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.d.ts +0 -7
  216. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +0 -6
  217. package/schemas/plugins/css/CssGlobalGeneratorOptions.json +0 -3
  218. package/schemas/plugins/css/CssGlobalParserOptions.check.d.ts +0 -7
  219. package/schemas/plugins/css/CssGlobalParserOptions.check.js +0 -6
  220. package/schemas/plugins/css/CssGlobalParserOptions.json +0 -3
@@ -39,16 +39,18 @@ class EnvironmentPlugin {
39
39
  * @returns {void}
40
40
  */
41
41
  apply(compiler) {
42
- /** @type {Record<string, CodeValue>} */
43
- const definitions = {};
44
- for (const key of this.keys) {
45
- const value =
46
- process.env[key] !== undefined
47
- ? process.env[key]
48
- : this.defaultValues[key];
42
+ const definePlugin = new DefinePlugin({});
49
43
 
50
- if (value === undefined) {
51
- compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
44
+ compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
45
+ /** @type {Record<string, CodeValue>} */
46
+ const definitions = {};
47
+ for (const key of this.keys) {
48
+ const value =
49
+ process.env[key] !== undefined
50
+ ? process.env[key]
51
+ : this.defaultValues[key];
52
+
53
+ if (value === undefined) {
52
54
  const error = new WebpackError(
53
55
  `${PLUGIN_NAME} - ${key} environment variable is undefined.\n\n` +
54
56
  "You can pass an object with default values to suppress this warning.\n" +
@@ -57,14 +59,15 @@ class EnvironmentPlugin {
57
59
 
58
60
  error.name = "EnvVariableNotDefinedError";
59
61
  compilation.errors.push(error);
60
- });
62
+ }
63
+ const defValue =
64
+ value === undefined ? "undefined" : JSON.stringify(value);
65
+ definitions[`process.env.${key}`] = defValue;
66
+ definitions[`import.meta.env.${key}`] = defValue;
61
67
  }
62
-
63
- definitions[`process.env.${key}`] =
64
- value === undefined ? "undefined" : JSON.stringify(value);
65
- }
66
-
67
- new DefinePlugin(definitions).apply(compiler);
68
+ definePlugin.definitions = definitions;
69
+ });
70
+ definePlugin.apply(compiler);
68
71
  }
69
72
  }
70
73
 
@@ -18,6 +18,7 @@ const { makePathsAbsolute } = require("./util/identifier");
18
18
  /** @typedef {import("webpack-sources").RawSourceMap} RawSourceMap */
19
19
  /** @typedef {import("webpack-sources").Source} Source */
20
20
  /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */
21
+ /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").Rules} Rules */
21
22
  /** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
22
23
  /** @typedef {import("./Compiler")} Compiler */
23
24
 
@@ -163,6 +164,21 @@ class EvalSourceMapDevToolPlugin {
163
164
  }
164
165
  );
165
166
  sourceMap.sources = moduleFilenames;
167
+ sourceMap.ignoreList = options.ignoreList
168
+ ? sourceMap.sources.reduce(
169
+ /** @type {(acc: number[], sourceName: string, idx: number) => number[]} */ (
170
+ (acc, sourceName, idx) => {
171
+ const rule = /** @type {Rules} */ (options.ignoreList);
172
+ if (ModuleFilenameHelpers.matchPart(sourceName, rule)) {
173
+ acc.push(idx);
174
+ }
175
+ return acc;
176
+ }
177
+ ),
178
+ []
179
+ )
180
+ : [];
181
+
166
182
  if (options.noSources) {
167
183
  sourceMap.sourcesContent = undefined;
168
184
  }
@@ -5,6 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const { ImportPhaseUtils } = require("./dependencies/ImportPhase");
8
9
  const { equals } = require("./util/ArrayHelpers");
9
10
  const SortableSet = require("./util/SortableSet");
10
11
  const makeSerializable = require("./util/makeSerializable");
@@ -1345,8 +1346,11 @@ class ExportInfo {
1345
1346
  export: rawTarget.export,
1346
1347
  deferred: Boolean(
1347
1348
  rawTarget.connection.dependency &&
1348
- /** @type {HarmonyImportDependency} */
1349
- (rawTarget.connection.dependency).defer
1349
+ ImportPhaseUtils.isDefer(
1350
+ /** @type {HarmonyImportDependency} */ (
1351
+ rawTarget.connection.dependency
1352
+ ).phase
1353
+ )
1350
1354
  )
1351
1355
  };
1352
1356
  for (;;) {
@@ -14,8 +14,9 @@ const Module = require("./Module");
14
14
  const {
15
15
  CSS_IMPORT_TYPES,
16
16
  CSS_URL_TYPES,
17
- JS_TYPES
18
- } = require("./ModuleSourceTypesConstants");
17
+ JAVASCRIPT_TYPE,
18
+ JAVASCRIPT_TYPES
19
+ } = require("./ModuleSourceTypeConstants");
19
20
  const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
20
21
  const RuntimeGlobals = require("./RuntimeGlobals");
21
22
  const Template = require("./Template");
@@ -28,6 +29,7 @@ const propertyAccess = require("./util/propertyAccess");
28
29
  const { register } = require("./util/serialization");
29
30
 
30
31
  /** @typedef {import("webpack-sources").Source} Source */
32
+ /** @typedef {import("../declarations/WebpackOptions").ExternalsType} ExternalsType */
31
33
  /** @typedef {import("../declarations/WebpackOptions").HashFunction} HashFunction */
32
34
  /** @typedef {import("./config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
33
35
  /** @typedef {import("./Chunk")} Chunk */
@@ -85,7 +87,7 @@ const RUNTIME_REQUIREMENTS_FOR_SCRIPT = new Set([RuntimeGlobals.loadScript]);
85
87
  const RUNTIME_REQUIREMENTS_FOR_MODULE = new Set([
86
88
  RuntimeGlobals.definePropertyGetters
87
89
  ]);
88
- const EMPTY_RUNTIME_REQUIREMENTS = new Set([]);
90
+ const EMPTY_RUNTIME_REQUIREMENTS = new Set();
89
91
 
90
92
  /**
91
93
  * @param {string | string[]} variableName the variable name or path
@@ -442,10 +444,6 @@ const getSourceForModuleExternal = (
442
444
  dependencyMeta,
443
445
  concatenationScope
444
446
  ) => {
445
- if (!Array.isArray(moduleAndSpecifiers)) {
446
- moduleAndSpecifiers = [moduleAndSpecifiers];
447
- }
448
-
449
447
  /** @type {Imported} */
450
448
  let imported = true;
451
449
  if (concatenationScope) {
@@ -465,6 +463,15 @@ const getSourceForModuleExternal = (
465
463
  }
466
464
  }
467
465
 
466
+ if (!Array.isArray(moduleAndSpecifiers)) {
467
+ moduleAndSpecifiers = [moduleAndSpecifiers];
468
+ }
469
+
470
+ // Return to `namespace` when the external request includes a specific export
471
+ if (moduleAndSpecifiers.length > 1) {
472
+ imported = true;
473
+ }
474
+
468
475
  const initFragment = new ModuleExternalInitFragment(
469
476
  moduleAndSpecifiers[0],
470
477
  imported,
@@ -473,22 +480,6 @@ const getSourceForModuleExternal = (
473
480
  runtimeTemplate.outputOptions.hashFunction
474
481
  );
475
482
  const normalizedImported = initFragment.getImported();
476
- const specifiers =
477
- normalizedImported === true
478
- ? undefined
479
- : /** @type {[string, string][]} */ (
480
- normalizedImported.map(([name, rawFinalName]) => {
481
- let finalName = rawFinalName;
482
- let counter = 0;
483
-
484
- if (concatenationScope) {
485
- while (!concatenationScope.registerUsedName(finalName)) {
486
- finalName = `${finalName}_${counter++}`;
487
- }
488
- }
489
- return [name, finalName];
490
- })
491
- );
492
483
 
493
484
  const baseAccess = `${initFragment.getNamespaceIdentifier()}${propertyAccess(
494
485
  moduleAndSpecifiers,
@@ -518,7 +509,7 @@ const getSourceForModuleExternal = (
518
509
  "x"
519
510
  )}`
520
511
  : undefined,
521
- specifiers,
512
+ specifiers: normalizedImported === true ? undefined : normalizedImported,
522
513
  runtimeRequirements: moduleRemapping
523
514
  ? RUNTIME_REQUIREMENTS_FOR_MODULE
524
515
  : undefined,
@@ -632,7 +623,7 @@ const getSourceForDefaultCase = (optional, request, runtimeTemplate) => {
632
623
  class ExternalModule extends Module {
633
624
  /**
634
625
  * @param {ExternalModuleRequest} request request
635
- * @param {string} type type
626
+ * @param {ExternalsType} type type
636
627
  * @param {string} userRequest user request
637
628
  * @param {DependencyMeta=} dependencyMeta dependency meta
638
629
  */
@@ -642,7 +633,7 @@ class ExternalModule extends Module {
642
633
  // Info from Factory
643
634
  /** @type {ExternalModuleRequest} */
644
635
  this.request = request;
645
- /** @type {string} */
636
+ /** @type {ExternalsType} */
646
637
  this.externalType = type;
647
638
  /** @type {string} */
648
639
  this.userRequest = userRequest;
@@ -665,7 +656,7 @@ class ExternalModule extends Module {
665
656
  return CSS_IMPORT_TYPES;
666
657
  }
667
658
 
668
- return JS_TYPES;
659
+ return JAVASCRIPT_TYPES;
669
660
  }
670
661
 
671
662
  /**
@@ -821,6 +812,10 @@ class ExternalModule extends Module {
821
812
  return undefined;
822
813
  }
823
814
 
815
+ /**
816
+ * @private
817
+ * @returns {{ request: string | string[], externalType: ExternalsType }} the request and external type
818
+ */
824
819
  _getRequestAndExternalType() {
825
820
  let { request, externalType } = this;
826
821
  if (typeof request === "object" && !Array.isArray(request)) {
@@ -833,8 +828,8 @@ class ExternalModule extends Module {
833
828
  /**
834
829
  * Resolve the detailed external type from the raw external type.
835
830
  * e.g. resolve "module" or "import" from "module-import" type
836
- * @param {string} externalType raw external type
837
- * @returns {string} resolved external type
831
+ * @param {ExternalsType} externalType raw external type
832
+ * @returns {ExternalsType} resolved external type
838
833
  */
839
834
  _resolveExternalType(externalType) {
840
835
  if (externalType === "module-import") {
@@ -866,7 +861,7 @@ class ExternalModule extends Module {
866
861
  /**
867
862
  * @private
868
863
  * @param {string | string[]} request request
869
- * @param {string} externalType the external type
864
+ * @param {ExternalsType} externalType the external type
870
865
  * @param {RuntimeTemplate} runtimeTemplate the runtime template
871
866
  * @param {ModuleGraph} moduleGraph the module graph
872
867
  * @param {ChunkGraph} chunkGraph the chunk graph
@@ -959,8 +954,6 @@ class ExternalModule extends Module {
959
954
  }
960
955
  case "var":
961
956
  case "promise":
962
- case "const":
963
- case "let":
964
957
  case "assign":
965
958
  default:
966
959
  return getSourceForDefaultCase(
@@ -987,7 +980,7 @@ class ExternalModule extends Module {
987
980
  case "asset": {
988
981
  const sources = new Map();
989
982
  sources.set(
990
- "javascript",
983
+ JAVASCRIPT_TYPE,
991
984
  new RawSource(`module.exports = ${JSON.stringify(request)};`)
992
985
  );
993
986
  const data = new Map();
@@ -1072,11 +1065,11 @@ class ExternalModule extends Module {
1072
1065
  const sources = new Map();
1073
1066
  if (this.useSourceMap || this.useSimpleSourceMap) {
1074
1067
  sources.set(
1075
- "javascript",
1068
+ JAVASCRIPT_TYPE,
1076
1069
  new OriginalSource(sourceString, this.identifier())
1077
1070
  );
1078
1071
  } else {
1079
- sources.set("javascript", new RawSource(sourceString));
1072
+ sources.set(JAVASCRIPT_TYPE, new RawSource(sourceString));
1080
1073
  }
1081
1074
 
1082
1075
  let runtimeRequirements = sourceData.runtimeRequirements;
@@ -16,6 +16,7 @@ const { cachedSetProperty, resolveByProperty } = require("./util/cleverMerge");
16
16
 
17
17
  /** @typedef {import("enhanced-resolve").ResolveContext} ResolveContext */
18
18
  /** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
19
+ /** @typedef {import("../declarations/WebpackOptions").ExternalsType} ExternalsType */
19
20
  /** @typedef {import("../declarations/WebpackOptions").ExternalItemValue} ExternalItemValue */
20
21
  /** @typedef {import("../declarations/WebpackOptions").ExternalItemObjectKnown} ExternalItemObjectKnown */
21
22
  /** @typedef {import("../declarations/WebpackOptions").ExternalItemObjectUnknown} ExternalItemObjectUnknown */
@@ -25,7 +26,7 @@ const { cachedSetProperty, resolveByProperty } = require("./util/cleverMerge");
25
26
  /** @typedef {import("./ModuleFactory").ModuleFactoryCreateDataContextInfo} ModuleFactoryCreateDataContextInfo */
26
27
  /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
27
28
 
28
- /** @typedef {((context: string, request: string, callback: (err?: Error | null, result?: string | false, resolveRequest?: import('enhanced-resolve').ResolveRequest) => void) => void)} ExternalItemFunctionDataGetResolveCallbackResult */
29
+ /** @typedef {((context: string, request: string, callback: (err?: Error | null, result?: string | false, resolveRequest?: import("enhanced-resolve").ResolveRequest) => void) => void)} ExternalItemFunctionDataGetResolveCallbackResult */
29
30
  /** @typedef {((context: string, request: string) => Promise<string>)} ExternalItemFunctionDataGetResolveResult */
30
31
  /** @typedef {(options?: ResolveOptions) => ExternalItemFunctionDataGetResolveCallbackResult | ExternalItemFunctionDataGetResolveResult} ExternalItemFunctionDataGetResolve */
31
32
 
@@ -50,7 +51,7 @@ const callDeprecatedExternals = util.deprecate(
50
51
  * @param {EXPECTED_FUNCTION} externalsFunction externals function
51
52
  * @param {string} context context
52
53
  * @param {string} request request
53
- * @param {(err: Error | null | undefined, value: ExternalValue | undefined, ty: ExternalType | undefined) => void} cb cb
54
+ * @param {(err: Error | null | undefined, value: ExternalValue | undefined, ty: ExternalsType | undefined) => void} cb cb
54
55
  */
55
56
  (externalsFunction, context, request, cb) => {
56
57
  // eslint-disable-next-line no-useless-call
@@ -90,13 +91,12 @@ const resolveLayer = (obj, layer) => {
90
91
  };
91
92
 
92
93
  /** @typedef {string | string[] | boolean | Record<string, string | string[]>} ExternalValue */
93
- /** @typedef {string | undefined} ExternalType */
94
94
 
95
95
  const PLUGIN_NAME = "ExternalModuleFactoryPlugin";
96
96
 
97
97
  class ExternalModuleFactoryPlugin {
98
98
  /**
99
- * @param {string} type default external type
99
+ * @param {ExternalsType} type default external type
100
100
  * @param {Externals} externals externals config
101
101
  */
102
102
  constructor(type, externals) {
@@ -122,7 +122,7 @@ class ExternalModuleFactoryPlugin {
122
122
 
123
123
  /**
124
124
  * @param {ExternalValue} value the external config
125
- * @param {ExternalType | undefined} type type of external
125
+ * @param {ExternalsType | undefined} type type of external
126
126
  * @param {HandleExternalCallback} callback callback
127
127
  * @returns {void}
128
128
  */
@@ -131,7 +131,7 @@ class ExternalModuleFactoryPlugin {
131
131
  // Not externals, fallback to original factory
132
132
  return callback();
133
133
  }
134
- /** @type {string | string[] | Record<string, string|string[]>} */
134
+ /** @type {ExternalValue} */
135
135
  let externalConfig = value === true ? dependency.request : value;
136
136
  // When no explicit type is specified, extract it from the externalConfig
137
137
  if (type === undefined) {
@@ -140,7 +140,9 @@ class ExternalModuleFactoryPlugin {
140
140
  UNSPECIFIED_EXTERNAL_TYPE_REGEXP.test(externalConfig)
141
141
  ) {
142
142
  const idx = externalConfig.indexOf(" ");
143
- type = externalConfig.slice(0, idx);
143
+ type =
144
+ /** @type {ExternalsType} */
145
+ (externalConfig.slice(0, idx));
144
146
  externalConfig = externalConfig.slice(idx + 1);
145
147
  } else if (
146
148
  Array.isArray(externalConfig) &&
@@ -149,7 +151,7 @@ class ExternalModuleFactoryPlugin {
149
151
  ) {
150
152
  const firstItem = externalConfig[0];
151
153
  const idx = firstItem.indexOf(" ");
152
- type = firstItem.slice(0, idx);
154
+ type = /** @type {ExternalsType} */ (firstItem.slice(0, idx));
153
155
  externalConfig = [
154
156
  firstItem.slice(idx + 1),
155
157
  ...externalConfig.slice(1)
@@ -255,7 +257,7 @@ class ExternalModuleFactoryPlugin {
255
257
  /**
256
258
  * @param {Error | null | undefined} err err
257
259
  * @param {ExternalValue=} value value
258
- * @param {ExternalType=} type type
260
+ * @param {ExternalsType=} type type
259
261
  * @returns {void}
260
262
  */
261
263
  const cb = (err, value, type) => {
@@ -9,6 +9,7 @@ const { ModuleExternalInitFragment } = require("./ExternalModule");
9
9
  const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
10
10
  const ConcatenatedModule = require("./optimize/ConcatenatedModule");
11
11
 
12
+ /** @typedef {import("../declarations/WebpackOptions").ExternalsType} ExternalsType */
12
13
  /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
13
14
  /** @typedef {import("./Compiler")} Compiler */
14
15
  /** @typedef {import("./ExternalModule").Imported} Imported */
@@ -17,7 +18,7 @@ const PLUGIN_NAME = "ExternalsPlugin";
17
18
 
18
19
  class ExternalsPlugin {
19
20
  /**
20
- * @param {string} type default external type
21
+ * @param {ExternalsType} type default external type
21
22
  * @param {Externals} externals externals config
22
23
  */
23
24
  constructor(type, externals) {
@@ -939,7 +939,7 @@ const mergeSets = (a, b) => {
939
939
  * Finding file or directory to manage
940
940
  * @param {string} managedPath path that is managing by {@link FileSystemInfo}
941
941
  * @param {string} path path to file or directory
942
- * @returns {string|null} managed item
942
+ * @returns {string | null} managed item
943
943
  * @example
944
944
  * getManagedItem(
945
945
  * '/Users/user/my-project/node_modules/',
package/lib/Generator.js CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const { JAVASCRIPT_TYPE } = require("./ModuleSourceTypeConstants");
9
+
8
10
  /** @typedef {import("webpack-sources").Source} Source */
9
11
  /** @typedef {import("./ChunkGraph")} ChunkGraph */
10
12
  /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
@@ -13,6 +15,7 @@
13
15
  /** @typedef {import("./Module").CodeGenerationResultData} CodeGenerationResultData */
14
16
  /** @typedef {import("./Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
15
17
  /** @typedef {import("./Module").RuntimeRequirements} RuntimeRequirements */
18
+ /** @typedef {import("./Module").SourceType} SourceType */
16
19
  /** @typedef {import("./Module").SourceTypes} SourceTypes */
17
20
  /** @typedef {import("./ModuleGraph")} ModuleGraph */
18
21
  /** @typedef {import("./NormalModule")} NormalModule */
@@ -30,7 +33,7 @@
30
33
  * @property {RuntimeSpec} runtime the runtime
31
34
  * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
32
35
  * @property {CodeGenerationResults=} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
33
- * @property {string} type which kind of code should be generated
36
+ * @property {SourceType} type which kind of code should be generated
34
37
  * @property {() => CodeGenerationResultData=} getData get access to the code generation data
35
38
  */
36
39
 
@@ -52,7 +55,7 @@
52
55
 
53
56
  class Generator {
54
57
  /**
55
- * @param {Record<string, Generator>} map map of types
58
+ * @param {{ [key in SourceType]?: Generator }} map map of types
56
59
  * @returns {ByTypeGenerator} generator by type
57
60
  */
58
61
  static byType(map) {
@@ -75,7 +78,7 @@ class Generator {
75
78
  /**
76
79
  * @abstract
77
80
  * @param {NormalModule} module the module
78
- * @param {string=} type source type
81
+ * @param {SourceType=} type source type
79
82
  * @returns {number} estimate size of the module
80
83
  */
81
84
  getSize(module, type) {
@@ -138,12 +141,12 @@ function generateError(error, module, generateContext) {
138
141
 
139
142
  class ByTypeGenerator extends Generator {
140
143
  /**
141
- * @param {Record<string, Generator>} map map of types
144
+ * @param {{ [key in SourceType]?: Generator }} map map of types
142
145
  */
143
146
  constructor(map) {
144
147
  super();
145
148
  this.map = map;
146
- this._types = new Set(Object.keys(map));
149
+ this._types = /** @type {SourceTypes} */ (new Set(Object.keys(map)));
147
150
  /** @type {GenerateErrorFn | undefined} */
148
151
  this.generateError = generateError.bind(this);
149
152
  }
@@ -158,10 +161,10 @@ class ByTypeGenerator extends Generator {
158
161
 
159
162
  /**
160
163
  * @param {NormalModule} module the module
161
- * @param {string=} type source type
164
+ * @param {SourceType=} type source type
162
165
  * @returns {number} estimate size of the module
163
166
  */
164
- getSize(module, type = "javascript") {
167
+ getSize(module, type = JAVASCRIPT_TYPE) {
165
168
  const t = type;
166
169
  const generator = this.map[t];
167
170
  return generator ? generator.getSize(module, t) : 0;
@@ -6,6 +6,10 @@
6
6
  "use strict";
7
7
 
8
8
  const WebpackError = require("./WebpackError");
9
+ const makeSerializable = require("./util/makeSerializable");
10
+
11
+ /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
12
+ /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
9
13
 
10
14
  /**
11
15
  * @template T
@@ -22,18 +26,43 @@ class HookWebpackError extends WebpackError {
22
26
  * @param {string} hook name of hook
23
27
  */
24
28
  constructor(error, hook) {
25
- super(error.message);
29
+ super(error ? error.message : undefined, error ? { cause: error } : {});
26
30
 
27
- this.name = "HookWebpackError";
28
31
  this.hook = hook;
29
32
  this.error = error;
33
+ this.name = "HookWebpackError";
30
34
  this.hideStack = true;
31
- this.details = `caused by plugins in ${hook}\n${error.stack}`;
35
+ this.stack += `\n-- inner error --\n${error ? error.stack : ""}`;
36
+ this.details = `caused by plugins in ${hook}\n${error ? error.stack : ""}`;
37
+ }
38
+
39
+ /**
40
+ * @param {ObjectSerializerContext} context context
41
+ */
42
+ serialize(context) {
43
+ const { write } = context;
44
+
45
+ write(this.error);
46
+ write(this.hook);
32
47
 
33
- this.stack += `\n-- inner error --\n${error.stack}`;
48
+ super.serialize(context);
49
+ }
50
+
51
+ /**
52
+ * @param {ObjectDeserializerContext} context context
53
+ */
54
+ deserialize(context) {
55
+ const { read } = context;
56
+
57
+ this.error = read();
58
+ this.hook = read();
59
+
60
+ super.deserialize(context);
34
61
  }
35
62
  }
36
63
 
64
+ makeSerializable(HookWebpackError, "webpack/lib/HookWebpackError");
65
+
37
66
  module.exports = HookWebpackError;
38
67
 
39
68
  /**
@@ -19,6 +19,7 @@ const {
19
19
  const NormalModule = require("./NormalModule");
20
20
  const RuntimeGlobals = require("./RuntimeGlobals");
21
21
  const WebpackError = require("./WebpackError");
22
+ const { chunkHasCss } = require("./css/CssModulesPlugin");
22
23
  const ConstDependency = require("./dependencies/ConstDependency");
23
24
  const ImportMetaHotAcceptDependency = require("./dependencies/ImportMetaHotAcceptDependency");
24
25
  const ImportMetaHotDeclineDependency = require("./dependencies/ImportMetaHotDeclineDependency");
@@ -817,6 +818,7 @@ To fix this, make sure to include [runtime] in the output.hotUpdateMainFilename
817
818
  filename,
818
819
  { removedChunkIds, removedModules, updatedChunkIds, assetInfo }
819
820
  ] of hotUpdateMainContentByFilename) {
821
+ /** @type {{c: ChunkId[], r: ChunkId[], m: ModuleId[], css?: {r: ChunkId[]}}} */
820
822
  const hotUpdateMainJson = {
821
823
  c: [...updatedChunkIds],
822
824
  r: [...removedChunkIds],
@@ -833,6 +835,26 @@ To fix this, make sure to include [runtime] in the output.hotUpdateMainFilename
833
835
  ]
834
836
  };
835
837
 
838
+ // Build CSS removed chunks list (chunks in updatedChunkIds that no longer have CSS)
839
+ /** @type {ChunkId[]} */
840
+ const cssRemovedChunkIds = [];
841
+ if (compilation.options.experiments.css) {
842
+ for (const chunkId of updatedChunkIds) {
843
+ for (const /** @type {Chunk} */ chunk of compilation.chunks) {
844
+ if (chunk.id === chunkId) {
845
+ if (!chunkHasCss(chunk, chunkGraph)) {
846
+ cssRemovedChunkIds.push(chunkId);
847
+ }
848
+ break;
849
+ }
850
+ }
851
+ }
852
+ }
853
+
854
+ if (cssRemovedChunkIds.length > 0) {
855
+ hotUpdateMainJson.css = { r: cssRemovedChunkIds };
856
+ }
857
+
836
858
  const source = new RawSource(
837
859
  (filename.endsWith(".json") ? "" : "export default ") +
838
860
  JSON.stringify(hotUpdateMainJson)