webpack 5.90.2 → 5.91.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


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

Files changed (182) hide show
  1. package/README.md +5 -5
  2. package/bin/webpack.js +5 -1
  3. package/lib/APIPlugin.js +8 -4
  4. package/lib/AutomaticPrefetchPlugin.js +1 -1
  5. package/lib/BannerPlugin.js +3 -1
  6. package/lib/Cache.js +7 -1
  7. package/lib/CacheFacade.js +3 -3
  8. package/lib/ChunkGraph.js +32 -18
  9. package/lib/ChunkGroup.js +14 -14
  10. package/lib/CleanPlugin.js +7 -5
  11. package/lib/Compilation.js +263 -94
  12. package/lib/Compiler.js +199 -83
  13. package/lib/ConditionalInitFragment.js +4 -5
  14. package/lib/ContextModule.js +4 -1
  15. package/lib/ContextModuleFactory.js +4 -2
  16. package/lib/ContextReplacementPlugin.js +3 -2
  17. package/lib/CssModule.js +0 -3
  18. package/lib/DefinePlugin.js +4 -2
  19. package/lib/DelegatedModule.js +2 -1
  20. package/lib/Dependency.js +4 -2
  21. package/lib/DependencyTemplate.js +7 -2
  22. package/lib/DllModule.js +3 -1
  23. package/lib/DllReferencePlugin.js +6 -2
  24. package/lib/EntryOptionPlugin.js +4 -1
  25. package/lib/EntryPlugin.js +6 -1
  26. package/lib/Entrypoint.js +1 -1
  27. package/lib/ExportsInfo.js +1 -4
  28. package/lib/ExternalModule.js +120 -25
  29. package/lib/ExternalModuleFactoryPlugin.js +37 -2
  30. package/lib/FileSystemInfo.js +1 -1
  31. package/lib/Generator.js +2 -1
  32. package/lib/HookWebpackError.js +2 -2
  33. package/lib/InitFragment.js +5 -3
  34. package/lib/LibManifestPlugin.js +15 -7
  35. package/lib/Module.js +54 -5
  36. package/lib/ModuleFilenameHelpers.js +1 -1
  37. package/lib/ModuleGraph.js +56 -27
  38. package/lib/ModuleGraphConnection.js +2 -1
  39. package/lib/MultiCompiler.js +61 -17
  40. package/lib/NodeStuffPlugin.js +14 -3
  41. package/lib/NormalModule.js +239 -86
  42. package/lib/NormalModuleFactory.js +163 -32
  43. package/lib/NormalModuleReplacementPlugin.js +5 -1
  44. package/lib/ProvidePlugin.js +3 -1
  45. package/lib/RawModule.js +4 -2
  46. package/lib/ResolverFactory.js +5 -1
  47. package/lib/RuntimeModule.js +6 -5
  48. package/lib/RuntimePlugin.js +1 -0
  49. package/lib/RuntimeTemplate.js +102 -34
  50. package/lib/SourceMapDevToolPlugin.js +4 -1
  51. package/lib/Stats.js +12 -5
  52. package/lib/TemplatedPathPlugin.js +32 -6
  53. package/lib/Watching.js +67 -60
  54. package/lib/WebpackError.js +6 -6
  55. package/lib/WebpackOptionsApply.js +18 -5
  56. package/lib/asset/RawDataUrlModule.js +5 -2
  57. package/lib/async-modules/AwaitDependenciesInitFragment.js +2 -2
  58. package/lib/buildChunkGraph.js +220 -392
  59. package/lib/cache/IdleFileCachePlugin.js +8 -3
  60. package/lib/cache/MemoryCachePlugin.js +1 -1
  61. package/lib/cache/MemoryWithGcCachePlugin.js +6 -2
  62. package/lib/cache/PackFileCacheStrategy.js +49 -16
  63. package/lib/cache/ResolverCachePlugin.js +14 -6
  64. package/lib/cache/getLazyHashedEtag.js +1 -1
  65. package/lib/config/defaults.js +24 -1
  66. package/lib/config/normalization.js +3 -1
  67. package/lib/container/ContainerEntryDependency.js +2 -1
  68. package/lib/container/ContainerEntryModule.js +5 -2
  69. package/lib/container/ContainerPlugin.js +14 -10
  70. package/lib/container/FallbackModule.js +3 -2
  71. package/lib/container/RemoteModule.js +2 -1
  72. package/lib/container/RemoteRuntimeModule.js +12 -3
  73. package/lib/css/CssExportsGenerator.js +34 -17
  74. package/lib/css/CssGenerator.js +20 -2
  75. package/lib/css/CssLoadingRuntimeModule.js +212 -96
  76. package/lib/css/CssModulesPlugin.js +47 -13
  77. package/lib/debug/ProfilingPlugin.js +27 -2
  78. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +9 -5
  79. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +4 -1
  80. package/lib/dependencies/CommonJsDependencyHelpers.js +2 -1
  81. package/lib/dependencies/CommonJsExportRequireDependency.js +33 -18
  82. package/lib/dependencies/CommonJsExportsDependency.js +13 -5
  83. package/lib/dependencies/CommonJsExportsParserPlugin.js +20 -15
  84. package/lib/dependencies/CommonJsImportsParserPlugin.js +1 -2
  85. package/lib/dependencies/ContextDependencyHelpers.js +49 -29
  86. package/lib/dependencies/ContextElementDependency.js +8 -1
  87. package/lib/dependencies/CssLocalIdentifierDependency.js +63 -8
  88. package/lib/dependencies/CssUrlDependency.js +5 -3
  89. package/lib/dependencies/ExportsInfoDependency.js +4 -3
  90. package/lib/dependencies/ExternalModuleInitFragment.js +5 -3
  91. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +4 -4
  92. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +43 -23
  93. package/lib/dependencies/HarmonyExportHeaderDependency.js +1 -1
  94. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +73 -32
  95. package/lib/dependencies/HarmonyExportInitFragment.js +10 -2
  96. package/lib/dependencies/HarmonyImportDependency.js +28 -12
  97. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +44 -16
  98. package/lib/dependencies/HarmonyImportSideEffectDependency.js +7 -6
  99. package/lib/dependencies/HarmonyImportSpecifierDependency.js +21 -10
  100. package/lib/dependencies/ImportDependency.js +9 -2
  101. package/lib/dependencies/ImportEagerDependency.js +4 -2
  102. package/lib/dependencies/ImportMetaContextDependency.js +7 -0
  103. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +25 -14
  104. package/lib/dependencies/ImportParserPlugin.js +12 -4
  105. package/lib/dependencies/ImportWeakDependency.js +4 -2
  106. package/lib/dependencies/LoaderDependency.js +2 -1
  107. package/lib/dependencies/LoaderImportDependency.js +2 -1
  108. package/lib/dependencies/ModuleDependency.js +4 -5
  109. package/lib/dependencies/PureExpressionDependency.js +4 -1
  110. package/lib/dependencies/RequireContextPlugin.js +1 -1
  111. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +26 -14
  112. package/lib/dependencies/RequireEnsureDependency.js +1 -1
  113. package/lib/dependencies/URLDependency.js +7 -4
  114. package/lib/dependencies/WorkerDependency.js +1 -1
  115. package/lib/dependencies/WorkerPlugin.js +6 -5
  116. package/lib/dependencies/getFunctionExpression.js +3 -1
  117. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +2 -1
  118. package/lib/hmr/LazyCompilationPlugin.js +2 -1
  119. package/lib/javascript/BasicEvaluatedExpression.js +2 -2
  120. package/lib/javascript/ChunkHelpers.js +2 -2
  121. package/lib/javascript/JavascriptParser.js +169 -57
  122. package/lib/javascript/JavascriptParserHelpers.js +1 -1
  123. package/lib/javascript/StartupHelpers.js +22 -5
  124. package/lib/logging/Logger.js +27 -2
  125. package/lib/logging/createConsoleLogger.js +11 -7
  126. package/lib/node/NodeEnvironmentPlugin.js +13 -7
  127. package/lib/node/NodeWatchFileSystem.js +37 -26
  128. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +2 -1
  129. package/lib/node/RequireChunkLoadingRuntimeModule.js +2 -1
  130. package/lib/node/nodeConsole.js +24 -1
  131. package/lib/optimize/AggressiveSplittingPlugin.js +1 -0
  132. package/lib/optimize/ConcatenatedModule.js +140 -55
  133. package/lib/optimize/EnsureChunkConditionsPlugin.js +1 -1
  134. package/lib/optimize/InnerGraph.js +7 -2
  135. package/lib/optimize/InnerGraphPlugin.js +36 -13
  136. package/lib/optimize/ModuleConcatenationPlugin.js +12 -2
  137. package/lib/optimize/RemoveParentModulesPlugin.js +1 -0
  138. package/lib/optimize/RuntimeChunkPlugin.js +6 -1
  139. package/lib/optimize/SideEffectsFlagPlugin.js +46 -15
  140. package/lib/optimize/SplitChunksPlugin.js +2 -2
  141. package/lib/performance/SizeLimitsPlugin.js +11 -0
  142. package/lib/rules/ObjectMatcherRulePlugin.js +4 -0
  143. package/lib/runtime/EnsureChunkRuntimeModule.js +2 -1
  144. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +2 -1
  145. package/lib/serialization/FileMiddleware.js +1 -0
  146. package/lib/serialization/ObjectMiddleware.js +2 -0
  147. package/lib/serialization/Serializer.js +19 -0
  148. package/lib/sharing/ConsumeSharedModule.js +3 -2
  149. package/lib/sharing/ConsumeSharedPlugin.js +17 -3
  150. package/lib/sharing/ConsumeSharedRuntimeModule.js +9 -2
  151. package/lib/sharing/ProvideSharedModule.js +2 -1
  152. package/lib/sharing/ProvideSharedPlugin.js +12 -5
  153. package/lib/sharing/resolveMatchedConfigs.js +2 -2
  154. package/lib/sharing/utils.js +13 -6
  155. package/lib/util/StackedCacheMap.js +26 -0
  156. package/lib/util/WeakTupleMap.js +57 -13
  157. package/lib/util/cleverMerge.js +24 -11
  158. package/lib/util/comparators.js +34 -14
  159. package/lib/util/conventions.js +129 -0
  160. package/lib/util/fs.js +379 -65
  161. package/lib/util/hash/BatchedHash.js +3 -0
  162. package/lib/util/hash/xxhash64.js +2 -2
  163. package/lib/util/memoize.js +2 -0
  164. package/lib/util/runtime.js +1 -1
  165. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +4 -2
  166. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +15 -6
  167. package/lib/wasm-sync/WebAssemblyGenerator.js +27 -6
  168. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +2 -1
  169. package/lib/wasm-sync/WebAssemblyParser.js +7 -4
  170. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -1
  171. package/lib/webpack.js +11 -5
  172. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +2 -1
  173. package/package.json +25 -26
  174. package/schemas/WebpackOptions.check.js +1 -1
  175. package/schemas/WebpackOptions.json +69 -8
  176. package/schemas/plugins/BannerPlugin.check.js +1 -1
  177. package/schemas/plugins/BannerPlugin.json +5 -1
  178. package/schemas/plugins/css/CssAutoGeneratorOptions.check.js +1 -1
  179. package/schemas/plugins/css/CssGeneratorOptions.check.js +1 -1
  180. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +1 -1
  181. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  182. package/types.d.ts +1772 -673
@@ -15,6 +15,7 @@ const ProvideSharedModuleFactory = require("./ProvideSharedModuleFactory");
15
15
  /** @typedef {import("../../declarations/plugins/sharing/ProvideSharedPlugin").ProvideSharedPluginOptions} ProvideSharedPluginOptions */
16
16
  /** @typedef {import("../Compilation")} Compilation */
17
17
  /** @typedef {import("../Compiler")} Compiler */
18
+ /** @typedef {import("../NormalModuleFactory").NormalModuleCreateData} NormalModuleCreateData */
18
19
 
19
20
  const validate = createSchemaValidation(
20
21
  require("../../schemas/plugins/sharing/ProvideSharedPlugin.check.js"),
@@ -112,6 +113,12 @@ class ProvideSharedPlugin {
112
113
  }
113
114
  }
114
115
  compilationData.set(compilation, resolvedProvideMap);
116
+ /**
117
+ * @param {string} key key
118
+ * @param {ProvideOptions} config config
119
+ * @param {NormalModuleCreateData["resource"]} resource resource
120
+ * @param {NormalModuleCreateData["resourceResolveData"]} resourceResolveData resource resolve data
121
+ */
115
122
  const provideSharedModule = (
116
123
  key,
117
124
  config,
@@ -151,7 +158,7 @@ class ProvideSharedPlugin {
151
158
  normalModuleFactory.hooks.module.tap(
152
159
  "ProvideSharedPlugin",
153
160
  (module, { resource, resourceResolveData }, resolveData) => {
154
- if (resolvedProvideMap.has(resource)) {
161
+ if (resolvedProvideMap.has(/** @type {string} */ (resource))) {
155
162
  return module;
156
163
  }
157
164
  const { request } = resolveData;
@@ -161,7 +168,7 @@ class ProvideSharedPlugin {
161
168
  provideSharedModule(
162
169
  request,
163
170
  config,
164
- resource,
171
+ /** @type {string} */ (resource),
165
172
  resourceResolveData
166
173
  );
167
174
  resolveData.cacheable = false;
@@ -171,12 +178,12 @@ class ProvideSharedPlugin {
171
178
  if (request.startsWith(prefix)) {
172
179
  const remainder = request.slice(prefix.length);
173
180
  provideSharedModule(
174
- resource,
181
+ /** @type {string} */ (resource),
175
182
  {
176
183
  ...config,
177
184
  shareKey: config.shareKey + remainder
178
185
  },
179
- resource,
186
+ /** @type {string} */ (resource),
180
187
  resourceResolveData
181
188
  );
182
189
  resolveData.cacheable = false;
@@ -209,7 +216,7 @@ class ProvideSharedPlugin {
209
216
  },
210
217
  err => {
211
218
  if (err) return reject(err);
212
- resolve();
219
+ resolve(null);
213
220
  }
214
221
  );
215
222
  })
@@ -64,10 +64,10 @@ exports.resolveMatchedConfigs = (compilation, configs) => {
64
64
  name: `shared module ${request}`
65
65
  })
66
66
  );
67
- return resolve();
67
+ return resolve(null);
68
68
  }
69
69
  resolved.set(/** @type {string} */ (result), config);
70
- resolve();
70
+ resolve(null);
71
71
  }
72
72
  );
73
73
  });
@@ -8,6 +8,8 @@
8
8
  const { join, dirname, readJson } = require("../util/fs");
9
9
 
10
10
  /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
11
+ /** @typedef {import("../util/fs").JsonObject} JsonObject */
12
+ /** @typedef {import("../util/fs").JsonPrimitive} JsonPrimitive */
11
13
 
12
14
  // Extreme shorthand only for github. eg: foo/bar
13
15
  const RE_URL_GITHUB_EXTREME_SHORT = /^[^/@:.\s][^/@:\s]*\/[^@:\s]*[^/@:\s]#\S+/;
@@ -373,9 +375,9 @@ exports.getDescriptionFile = getDescriptionFile;
373
375
 
374
376
  /**
375
377
  *
376
- * @param {object} data description file data i.e.: package.json
378
+ * @param {JsonObject} data description file data i.e.: package.json
377
379
  * @param {string} packageName name of the dependency
378
- * @returns {string} normalized version
380
+ * @returns {string | undefined} normalized version
379
381
  */
380
382
  const getRequiredVersionFromDescriptionFile = (data, packageName) => {
381
383
  const dependencyTypes = [
@@ -386,12 +388,17 @@ const getRequiredVersionFromDescriptionFile = (data, packageName) => {
386
388
  ];
387
389
 
388
390
  for (const dependencyType of dependencyTypes) {
391
+ const dependency = /** @type {JsonObject} */ (data[dependencyType]);
389
392
  if (
390
- data[dependencyType] &&
391
- typeof data[dependencyType] === "object" &&
392
- packageName in data[dependencyType]
393
+ dependency &&
394
+ typeof dependency === "object" &&
395
+ packageName in dependency
393
396
  ) {
394
- return normalizeVersion(data[dependencyType][packageName]);
397
+ return normalizeVersion(
398
+ /** @type {Exclude<JsonPrimitive, null | boolean| number>} */ (
399
+ dependency[packageName]
400
+ )
401
+ );
395
402
  }
396
403
  }
397
404
  };
@@ -8,6 +8,29 @@
8
8
  /**
9
9
  * @template K
10
10
  * @template V
11
+ *
12
+ * The StackedCacheMap is a data structure designed as an alternative to a Map
13
+ * in situations where you need to handle multiple item additions and
14
+ * frequently access the largest map.
15
+ *
16
+ * It is particularly optimized for efficiently adding multiple items
17
+ * at once, which can be achieved using the `addAll` method.
18
+ *
19
+ * It has a fallback Map that is used when the map to be added is mutable.
20
+ *
21
+ * Note: `delete` and `has` are not supported for performance reasons.
22
+ *
23
+ * @example
24
+ * ```js
25
+ * const map = new StackedCacheMap();
26
+ * map.addAll(new Map([["a", 1], ["b", 2]]), true);
27
+ * map.addAll(new Map([["c", 3], ["d", 4]]), true);
28
+ * map.get("a"); // 1
29
+ * map.get("d"); // 4
30
+ * for (const [key, value] of map) {
31
+ * console.log(key, value);
32
+ * }
33
+ * ```
11
34
  */
12
35
  class StackedCacheMap {
13
36
  constructor() {
@@ -18,6 +41,9 @@ class StackedCacheMap {
18
41
  }
19
42
 
20
43
  /**
44
+ * If `immutable` is true, the map can be referenced by the StackedCacheMap
45
+ * and should not be changed afterwards. If the map is mutable, all items
46
+ * are copied into a fallback Map.
21
47
  * @param {ReadonlyMap<K, V>} map map to add
22
48
  * @param {boolean=} immutable if 'map' is immutable and StackedCacheMap can keep referencing it
23
49
  */
@@ -5,6 +5,21 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ /**
9
+ * @template {any[]} T
10
+ * @template V
11
+ * @typedef {Map<object, WeakTupleMap<T, V>>} M
12
+ */
13
+ /**
14
+ * @template {any[]} T
15
+ * @template V
16
+ * @typedef {WeakMap<object, WeakTupleMap<T, V>>} W
17
+ */
18
+
19
+ /**
20
+ * @param {any} thing thing
21
+ * @returns {boolean} true if is weak
22
+ */
8
23
  const isWeakKey = thing => typeof thing === "object" && thing !== null;
9
24
 
10
25
  /**
@@ -15,11 +30,20 @@ class WeakTupleMap {
15
30
  constructor() {
16
31
  /** @private */
17
32
  this.f = 0;
18
- /** @private @type {any} */
33
+ /**
34
+ * @private
35
+ * @type {any}
36
+ **/
19
37
  this.v = undefined;
20
- /** @private @type {Map<object, WeakTupleMap<T, V>> | undefined} */
38
+ /**
39
+ * @private
40
+ * @type {M<T, V> | undefined}
41
+ **/
21
42
  this.m = undefined;
22
- /** @private @type {WeakMap<object, WeakTupleMap<T, V>> | undefined} */
43
+ /**
44
+ * @private
45
+ * @type {W<T, V> | undefined}
46
+ **/
23
47
  this.w = undefined;
24
48
  }
25
49
 
@@ -41,7 +65,7 @@ class WeakTupleMap {
41
65
  * @returns {boolean} true, if the tuple is in the Set
42
66
  */
43
67
  has(...args) {
44
- /** @type {WeakTupleMap<T, V>} */
68
+ /** @type {WeakTupleMap<T, V> | undefined} */
45
69
  let node = this;
46
70
  for (let i = 0; i < args.length; i++) {
47
71
  node = node._peek(args[i]);
@@ -52,10 +76,10 @@ class WeakTupleMap {
52
76
 
53
77
  /**
54
78
  * @param {T} args tuple
55
- * @returns {V} the value
79
+ * @returns {V | undefined} the value
56
80
  */
57
81
  get(...args) {
58
- /** @type {WeakTupleMap<T, V>} */
82
+ /** @type {WeakTupleMap<T, V> | undefined} */
59
83
  let node = this;
60
84
  for (let i = 0; i < args.length; i++) {
61
85
  node = node._peek(args[i]);
@@ -86,7 +110,7 @@ class WeakTupleMap {
86
110
  * @returns {void}
87
111
  */
88
112
  delete(...args) {
89
- /** @type {WeakTupleMap<T, V>} */
113
+ /** @type {WeakTupleMap<T, V> | undefined} */
90
114
  let node = this;
91
115
  for (let i = 0; i < args.length; i++) {
92
116
  node = node._peek(args[i]);
@@ -113,6 +137,10 @@ class WeakTupleMap {
113
137
  return (this.f & 1) === 1;
114
138
  }
115
139
 
140
+ /**
141
+ * @param {any} v value
142
+ * @private
143
+ */
116
144
  _setValue(v) {
117
145
  this.f |= 1;
118
146
  this.v = v;
@@ -123,16 +151,26 @@ class WeakTupleMap {
123
151
  this.v = undefined;
124
152
  }
125
153
 
154
+ /**
155
+ * @param {any} thing thing
156
+ * @returns {WeakTupleMap<T, V> | undefined} thing
157
+ * @private
158
+ */
126
159
  _peek(thing) {
127
160
  if (isWeakKey(thing)) {
128
161
  if ((this.f & 4) !== 4) return undefined;
129
- return this.w.get(thing);
162
+ return /** @type {W<T, V>} */ (this.w).get(thing);
130
163
  } else {
131
164
  if ((this.f & 2) !== 2) return undefined;
132
- return this.m.get(thing);
165
+ return /** @type {M<T, V>} */ (this.m).get(thing);
133
166
  }
134
167
  }
135
168
 
169
+ /**
170
+ * @private
171
+ * @param {any} thing thing
172
+ * @returns {WeakTupleMap<T, V>} value
173
+ */
136
174
  _get(thing) {
137
175
  if (isWeakKey(thing)) {
138
176
  if ((this.f & 4) !== 4) {
@@ -142,12 +180,15 @@ class WeakTupleMap {
142
180
  (this.w = newMap).set(thing, newNode);
143
181
  return newNode;
144
182
  }
145
- const entry = this.w.get(thing);
183
+ const entry =
184
+ /** @type {W<T, V>} */
185
+ (this.w).get(thing);
146
186
  if (entry !== undefined) {
147
187
  return entry;
148
188
  }
149
189
  const newNode = new WeakTupleMap();
150
- this.w.set(thing, newNode);
190
+ /** @type {W<T, V>} */
191
+ (this.w).set(thing, newNode);
151
192
  return newNode;
152
193
  } else {
153
194
  if ((this.f & 2) !== 2) {
@@ -157,12 +198,15 @@ class WeakTupleMap {
157
198
  (this.m = newMap).set(thing, newNode);
158
199
  return newNode;
159
200
  }
160
- const entry = this.m.get(thing);
201
+ const entry =
202
+ /** @type {M<T, V>} */
203
+ (this.m).get(thing);
161
204
  if (entry !== undefined) {
162
205
  return entry;
163
206
  }
164
207
  const newNode = new WeakTupleMap();
165
- this.m.set(thing, newNode);
208
+ /** @type {M<T, V>} */
209
+ (this.m).set(thing, newNode);
166
210
  return newNode;
167
211
  }
168
212
  }
@@ -38,11 +38,11 @@ const cachedCleverMerge = (first, second) => {
38
38
  innerCache = new WeakMap();
39
39
  mergeCache.set(first, innerCache);
40
40
  }
41
- const prevMerge = innerCache.get(second);
41
+ const prevMerge = /** @type {T & O} */ (innerCache.get(second));
42
42
  if (prevMerge !== undefined) return prevMerge;
43
43
  const newMerge = _cleverMerge(first, second, true);
44
44
  innerCache.set(second, newMerge);
45
- return newMerge;
45
+ return /** @type {T & O} */ (newMerge);
46
46
  };
47
47
 
48
48
  /**
@@ -69,7 +69,7 @@ const cachedSetProperty = (obj, property, value) => {
69
69
 
70
70
  let result = mapByValue.get(value);
71
71
 
72
- if (result) return result;
72
+ if (result) return /** @type {T} */ (result);
73
73
 
74
74
  result = {
75
75
  ...obj,
@@ -77,7 +77,7 @@ const cachedSetProperty = (obj, property, value) => {
77
77
  };
78
78
  mapByValue.set(value, result);
79
79
 
80
- return result;
80
+ return /** @type {T} */ (result);
81
81
  };
82
82
 
83
83
  /**
@@ -257,7 +257,7 @@ const cleverMerge = (first, second) => {
257
257
  if (typeof second !== "object" || second === null) return second;
258
258
  if (typeof first !== "object" || first === null) return first;
259
259
 
260
- return _cleverMerge(first, second, false);
260
+ return /** @type {T & O} */ (_cleverMerge(first, second, false));
261
261
  };
262
262
 
263
263
  /**
@@ -498,27 +498,40 @@ const mergeSingleValue = (a, b, internalCaching) => {
498
498
  };
499
499
 
500
500
  /**
501
- * @template T
501
+ * @template {object} T
502
502
  * @param {T} obj the object
503
+ * @param {(keyof T)[]=} keysToKeepOriginalValue keys to keep original value
503
504
  * @returns {T} the object without operations like "..." or DELETE
504
505
  */
505
- const removeOperations = obj => {
506
+ const removeOperations = (obj, keysToKeepOriginalValue = []) => {
506
507
  const newObj = /** @type {T} */ ({});
507
508
  for (const key of Object.keys(obj)) {
508
- const value = obj[key];
509
+ const value = obj[/** @type {keyof T} */ (key)];
509
510
  const type = getValueType(value);
511
+ if (
512
+ type === VALUE_TYPE_OBJECT &&
513
+ keysToKeepOriginalValue.includes(/** @type {keyof T} */ (key))
514
+ ) {
515
+ newObj[/** @type {keyof T} */ (key)] = value;
516
+ continue;
517
+ }
510
518
  switch (type) {
511
519
  case VALUE_TYPE_UNDEFINED:
512
520
  case VALUE_TYPE_DELETE:
513
521
  break;
514
522
  case VALUE_TYPE_OBJECT:
515
- newObj[key] = removeOperations(value);
523
+ newObj[key] = removeOperations(
524
+ /** @type {TODO} */ (value),
525
+ keysToKeepOriginalValue
526
+ );
516
527
  break;
517
528
  case VALUE_TYPE_ARRAY_EXTEND:
518
- newObj[key] = value.filter(i => i !== "...");
529
+ newObj[key] =
530
+ /** @type {any[]} */
531
+ (value).filter(i => i !== "...");
519
532
  break;
520
533
  default:
521
- newObj[key] = value;
534
+ newObj[/** @type {keyof T} */ (key)] = value;
522
535
  break;
523
536
  }
524
537
  }
@@ -8,6 +8,7 @@
8
8
  const { compareRuntime } = require("./runtime");
9
9
 
10
10
  /** @typedef {import("../Chunk")} Chunk */
11
+ /** @typedef {import("../Chunk").ChunkId} ChunkId */
11
12
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
12
13
  /** @typedef {import("../ChunkGroup")} ChunkGroup */
13
14
  /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
@@ -46,7 +47,10 @@ const createCachedParameterizedComparator = fn => {
46
47
  * @returns {-1|0|1} compare result
47
48
  */
48
49
  exports.compareChunksById = (a, b) => {
49
- return compareIds(a.id, b.id);
50
+ return compareIds(
51
+ /** @type {ChunkId} */ (a.id),
52
+ /** @type {ChunkId} */ (b.id)
53
+ );
50
54
  };
51
55
 
52
56
  /**
@@ -166,8 +170,8 @@ exports.compareStringsNumeric = compareStringsNumeric;
166
170
  */
167
171
  const compareModulesByPostOrderIndexOrIdentifier = (moduleGraph, a, b) => {
168
172
  const cmp = compareNumbers(
169
- moduleGraph.getPostOrderIndex(a),
170
- moduleGraph.getPostOrderIndex(b)
173
+ /** @type {number} */ (moduleGraph.getPostOrderIndex(a)),
174
+ /** @type {number} */ (moduleGraph.getPostOrderIndex(b))
171
175
  );
172
176
  if (cmp !== 0) return cmp;
173
177
  return compareIds(a.identifier(), b.identifier());
@@ -186,8 +190,8 @@ exports.compareModulesByPostOrderIndexOrIdentifier =
186
190
  */
187
191
  const compareModulesByPreOrderIndexOrIdentifier = (moduleGraph, a, b) => {
188
192
  const cmp = compareNumbers(
189
- moduleGraph.getPreOrderIndex(a),
190
- moduleGraph.getPreOrderIndex(b)
193
+ /** @type {number} */ (moduleGraph.getPreOrderIndex(a)),
194
+ /** @type {number} */ (moduleGraph.getPreOrderIndex(b))
191
195
  );
192
196
  if (cmp !== 0) return cmp;
193
197
  return compareIds(a.identifier(), b.identifier());
@@ -261,7 +265,9 @@ exports.compareStrings = compareStrings;
261
265
  * @returns {-1|0|1} compare result
262
266
  */
263
267
  const compareChunkGroupsByIndex = (a, b) => {
264
- return a.index < b.index ? -1 : 1;
268
+ return /** @type {number} */ (a.index) < /** @type {number} */ (b.index)
269
+ ? -1
270
+ : 1;
265
271
  };
266
272
 
267
273
  exports.compareChunkGroupsByIndex = compareChunkGroupsByIndex;
@@ -273,7 +279,7 @@ exports.compareChunkGroupsByIndex = compareChunkGroupsByIndex;
273
279
  */
274
280
  class TwoKeyWeakMap {
275
281
  constructor() {
276
- /** @private @type {WeakMap<any, WeakMap<any, T>>} */
282
+ /** @private @type {WeakMap<any, WeakMap<any, T | undefined>>} */
277
283
  this._map = new WeakMap();
278
284
  }
279
285
 
@@ -340,7 +346,10 @@ const concatComparators = (c1, c2, ...cRest) => {
340
346
  };
341
347
  exports.concatComparators = concatComparators;
342
348
 
343
- /** @template A, B @typedef {(input: A) => B} Selector */
349
+ /**
350
+ * @template A, B
351
+ * @typedef {(input: A) => B | undefined | null} Selector
352
+ */
344
353
 
345
354
  /** @type {TwoKeyWeakMap<Selector<any, any>, Comparator<any>, Comparator<any>>}} */
346
355
  const compareSelectCache = new TwoKeyWeakMap();
@@ -431,7 +440,11 @@ exports.keepOriginalOrder = iterable => {
431
440
  for (const item of iterable) {
432
441
  map.set(item, i++);
433
442
  }
434
- return (a, b) => compareNumbers(map.get(a), map.get(b));
443
+ return (a, b) =>
444
+ compareNumbers(
445
+ /** @type {number} */ (map.get(a)),
446
+ /** @type {number} */ (map.get(b))
447
+ );
435
448
  };
436
449
 
437
450
  /**
@@ -442,7 +455,10 @@ exports.compareChunksNatural = chunkGraph => {
442
455
  const cmpFn = exports.compareModulesById(chunkGraph);
443
456
  const cmpIterableFn = compareIterables(cmpFn);
444
457
  return concatComparators(
445
- compareSelect(chunk => chunk.name, compareIds),
458
+ compareSelect(
459
+ chunk => /** @type {string|number} */ (chunk.name),
460
+ compareIds
461
+ ),
446
462
  compareSelect(chunk => chunk.runtime, compareRuntime),
447
463
  compareSelect(
448
464
  /**
@@ -475,8 +491,10 @@ exports.compareLocations = (a, b) => {
475
491
  const bp = b.start;
476
492
  if (ap.line < bp.line) return -1;
477
493
  if (ap.line > bp.line) return 1;
478
- if (ap.column < bp.column) return -1;
479
- if (ap.column > bp.column) return 1;
494
+ if (/** @type {number} */ (ap.column) < /** @type {number} */ (bp.column))
495
+ return -1;
496
+ if (/** @type {number} */ (ap.column) > /** @type {number} */ (bp.column))
497
+ return 1;
480
498
  } else return -1;
481
499
  } else if ("start" in b) return 1;
482
500
  if ("name" in a) {
@@ -487,8 +505,10 @@ exports.compareLocations = (a, b) => {
487
505
  } else if ("name" in b) return 1;
488
506
  if ("index" in a) {
489
507
  if ("index" in b) {
490
- if (a.index < b.index) return -1;
491
- if (a.index > b.index) return 1;
508
+ if (/** @type {number} */ (a.index) < /** @type {number} */ (b.index))
509
+ return -1;
510
+ if (/** @type {number} */ (a.index) > /** @type {number} */ (b.index))
511
+ return 1;
492
512
  } else return -1;
493
513
  } else if ("index" in b) return 1;
494
514
  return 0;
@@ -0,0 +1,129 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Gengkun He @ahabhgk
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
9
+
10
+ /**
11
+ * @param {string} input input
12
+ * @param {CssGeneratorExportsConvention} convention convention
13
+ * @returns {Set<string>} results
14
+ */
15
+ exports.cssExportConvention = (input, convention) => {
16
+ const set = new Set();
17
+ if (typeof convention === "function") {
18
+ set.add(convention(input));
19
+ } else {
20
+ switch (convention) {
21
+ case "camel-case": {
22
+ set.add(input);
23
+ set.add(exports.camelCase(input));
24
+ break;
25
+ }
26
+ case "camel-case-only": {
27
+ set.add(exports.camelCase(input));
28
+ break;
29
+ }
30
+ case "dashes": {
31
+ set.add(input);
32
+ set.add(exports.dashesCamelCase(input));
33
+ break;
34
+ }
35
+ case "dashes-only": {
36
+ set.add(exports.dashesCamelCase(input));
37
+ break;
38
+ }
39
+ case "as-is": {
40
+ set.add(input);
41
+ break;
42
+ }
43
+ }
44
+ }
45
+ return set;
46
+ };
47
+
48
+ // Copy from css-loader
49
+ /**
50
+ * @param {string} input input
51
+ * @returns {string} result
52
+ */
53
+ exports.dashesCamelCase = input => {
54
+ return input.replace(/-+(\w)/g, (match, firstLetter) =>
55
+ firstLetter.toUpperCase()
56
+ );
57
+ };
58
+
59
+ // Copy from css-loader
60
+ /**
61
+ * @param {string} input input
62
+ * @returns {string} result
63
+ */
64
+ exports.camelCase = input => {
65
+ let result = input.trim();
66
+
67
+ if (result.length === 0) {
68
+ return "";
69
+ }
70
+
71
+ if (result.length === 1) {
72
+ return result.toLowerCase();
73
+ }
74
+
75
+ const hasUpperCase = result !== result.toLowerCase();
76
+
77
+ if (hasUpperCase) {
78
+ result = preserveCamelCase(result);
79
+ }
80
+
81
+ return result
82
+ .replace(/^[_.\- ]+/, "")
83
+ .toLowerCase()
84
+ .replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toUpperCase())
85
+ .replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toUpperCase());
86
+ };
87
+
88
+ // Copy from css-loader
89
+ /**
90
+ * @param {string} string string
91
+ * @returns {string} result
92
+ */
93
+ const preserveCamelCase = string => {
94
+ let result = string;
95
+ let isLastCharLower = false;
96
+ let isLastCharUpper = false;
97
+ let isLastLastCharUpper = false;
98
+
99
+ for (let i = 0; i < result.length; i++) {
100
+ const character = result[i];
101
+
102
+ if (isLastCharLower && /[\p{Lu}]/u.test(character)) {
103
+ result = `${result.slice(0, i)}-${result.slice(i)}`;
104
+ isLastCharLower = false;
105
+ isLastLastCharUpper = isLastCharUpper;
106
+ isLastCharUpper = true;
107
+ i += 1;
108
+ } else if (
109
+ isLastCharUpper &&
110
+ isLastLastCharUpper &&
111
+ /[\p{Ll}]/u.test(character)
112
+ ) {
113
+ result = `${result.slice(0, i - 1)}-${result.slice(i - 1)}`;
114
+ isLastLastCharUpper = isLastCharUpper;
115
+ isLastCharUpper = false;
116
+ isLastCharLower = true;
117
+ } else {
118
+ isLastCharLower =
119
+ character.toLowerCase() === character &&
120
+ character.toUpperCase() !== character;
121
+ isLastLastCharUpper = isLastCharUpper;
122
+ isLastCharUpper =
123
+ character.toUpperCase() === character &&
124
+ character.toLowerCase() !== character;
125
+ }
126
+ }
127
+
128
+ return result;
129
+ };