webpack 5.98.0 → 5.99.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (251) hide show
  1. package/README.md +7 -3
  2. package/lib/AsyncDependenciesBlock.js +3 -1
  3. package/lib/BannerPlugin.js +1 -1
  4. package/lib/Cache.js +9 -7
  5. package/lib/CacheFacade.js +5 -5
  6. package/lib/Chunk.js +2 -2
  7. package/lib/ChunkGraph.js +21 -16
  8. package/lib/ChunkTemplate.js +6 -6
  9. package/lib/CleanPlugin.js +10 -10
  10. package/lib/CodeGenerationResults.js +4 -3
  11. package/lib/CompatibilityPlugin.js +4 -1
  12. package/lib/Compilation.js +326 -152
  13. package/lib/Compiler.js +13 -18
  14. package/lib/ConditionalInitFragment.js +1 -1
  15. package/lib/ConstPlugin.js +5 -3
  16. package/lib/ContextModule.js +4 -2
  17. package/lib/ContextModuleFactory.js +3 -3
  18. package/lib/ContextReplacementPlugin.js +43 -16
  19. package/lib/DefinePlugin.js +25 -24
  20. package/lib/DelegatedModule.js +4 -2
  21. package/lib/DelegatedModuleFactoryPlugin.js +2 -1
  22. package/lib/Dependency.js +19 -13
  23. package/lib/DependencyTemplates.js +4 -3
  24. package/lib/DllModule.js +4 -2
  25. package/lib/DllModuleFactory.js +2 -2
  26. package/lib/DllReferencePlugin.js +2 -1
  27. package/lib/DynamicEntryPlugin.js +1 -1
  28. package/lib/EnvironmentPlugin.js +4 -2
  29. package/lib/ExportsInfo.js +72 -40
  30. package/lib/ExternalModule.js +14 -5
  31. package/lib/ExternalModuleFactoryPlugin.js +24 -12
  32. package/lib/FileSystemInfo.js +129 -94
  33. package/lib/FlagDependencyExportsPlugin.js +6 -4
  34. package/lib/FlagDependencyUsagePlugin.js +1 -1
  35. package/lib/Generator.js +29 -1
  36. package/lib/HookWebpackError.js +2 -2
  37. package/lib/HotModuleReplacementPlugin.js +3 -9
  38. package/lib/IgnoreErrorModuleFactory.js +2 -2
  39. package/lib/IgnorePlugin.js +0 -5
  40. package/lib/InitFragment.js +1 -1
  41. package/lib/LoaderOptionsPlugin.js +8 -5
  42. package/lib/MainTemplate.js +7 -7
  43. package/lib/Module.js +40 -17
  44. package/lib/ModuleBuildError.js +3 -1
  45. package/lib/ModuleDependencyError.js +4 -3
  46. package/lib/ModuleDependencyWarning.js +4 -3
  47. package/lib/ModuleFactory.js +9 -3
  48. package/lib/ModuleFilenameHelpers.js +13 -13
  49. package/lib/ModuleGraph.js +20 -14
  50. package/lib/ModuleGraphConnection.js +7 -13
  51. package/lib/ModuleNotFoundError.js +1 -1
  52. package/lib/ModuleParseError.js +2 -1
  53. package/lib/ModuleSourceTypesConstants.js +11 -0
  54. package/lib/ModuleTemplate.js +5 -5
  55. package/lib/ModuleTypeConstants.js +15 -0
  56. package/lib/MultiCompiler.js +4 -4
  57. package/lib/MultiStats.js +1 -1
  58. package/lib/NormalModule.js +101 -54
  59. package/lib/NormalModuleFactory.js +38 -33
  60. package/lib/NormalModuleReplacementPlugin.js +3 -2
  61. package/lib/NullFactory.js +2 -2
  62. package/lib/Parser.js +4 -3
  63. package/lib/ProgressPlugin.js +1 -2
  64. package/lib/RawModule.js +4 -2
  65. package/lib/RecordIdsPlugin.js +6 -2
  66. package/lib/RequestShortener.js +3 -1
  67. package/lib/ResolverFactory.js +12 -9
  68. package/lib/RuntimeModule.js +4 -2
  69. package/lib/RuntimeTemplate.js +2 -1
  70. package/lib/SelfModuleFactory.js +2 -2
  71. package/lib/SourceMapDevToolPlugin.js +0 -8
  72. package/lib/Template.js +6 -5
  73. package/lib/TemplatedPathPlugin.js +15 -12
  74. package/lib/WebpackOptionsApply.js +1 -1
  75. package/lib/asset/AssetGenerator.js +237 -170
  76. package/lib/asset/AssetModulesPlugin.js +50 -8
  77. package/lib/asset/AssetSourceGenerator.js +18 -0
  78. package/lib/asset/RawDataUrlModule.js +4 -2
  79. package/lib/buildChunkGraph.js +14 -4
  80. package/lib/cache/MemoryWithGcCachePlugin.js +6 -2
  81. package/lib/cache/PackFileCacheStrategy.js +137 -121
  82. package/lib/cache/ResolverCachePlugin.js +15 -7
  83. package/lib/cache/getLazyHashedEtag.js +4 -3
  84. package/lib/cli.js +23 -15
  85. package/lib/config/defaults.js +93 -26
  86. package/lib/config/normalization.js +14 -13
  87. package/lib/config/target.js +8 -8
  88. package/lib/container/ContainerEntryModule.js +4 -2
  89. package/lib/container/ContainerEntryModuleFactory.js +2 -2
  90. package/lib/container/FallbackModule.js +4 -2
  91. package/lib/container/FallbackModuleFactory.js +2 -2
  92. package/lib/container/RemoteModule.js +4 -2
  93. package/lib/container/options.js +5 -5
  94. package/lib/css/CssGenerator.js +71 -9
  95. package/lib/css/CssModulesPlugin.js +30 -5
  96. package/lib/css/CssParser.js +37 -17
  97. package/lib/css/walkCssTokens.js +17 -17
  98. package/lib/debug/ProfilingPlugin.js +98 -38
  99. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +10 -4
  100. package/lib/dependencies/AMDPlugin.js +5 -2
  101. package/lib/dependencies/AMDRequireArrayDependency.js +4 -3
  102. package/lib/dependencies/AMDRequireContextDependency.js +2 -1
  103. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +15 -7
  104. package/lib/dependencies/AMDRuntimeModules.js +3 -1
  105. package/lib/dependencies/CommonJsExportRequireDependency.js +4 -5
  106. package/lib/dependencies/CommonJsExportsParserPlugin.js +3 -3
  107. package/lib/dependencies/CommonJsImportsParserPlugin.js +8 -7
  108. package/lib/dependencies/CommonJsRequireContextDependency.js +2 -1
  109. package/lib/dependencies/ContextDependencyHelpers.js +13 -6
  110. package/lib/dependencies/CssIcssExportDependency.js +15 -12
  111. package/lib/dependencies/CssIcssImportDependency.js +4 -1
  112. package/lib/dependencies/CssLocalIdentifierDependency.js +17 -14
  113. package/lib/dependencies/ExportsInfoDependency.js +6 -1
  114. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +5 -5
  115. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +22 -15
  116. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +23 -8
  117. package/lib/dependencies/HarmonyExportSpecifierDependency.js +2 -2
  118. package/lib/dependencies/HarmonyImportDependency.js +8 -6
  119. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +19 -14
  120. package/lib/dependencies/HarmonyImportSpecifierDependency.js +5 -5
  121. package/lib/dependencies/ImportContextDependency.js +2 -1
  122. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +8 -4
  123. package/lib/dependencies/JsonExportsDependency.js +24 -8
  124. package/lib/dependencies/LoaderPlugin.js +4 -14
  125. package/lib/dependencies/RequireContextDependency.js +2 -1
  126. package/lib/dependencies/RequireContextDependencyParserPlugin.js +6 -3
  127. package/lib/dependencies/RequireEnsureDependenciesBlock.js +3 -2
  128. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +11 -5
  129. package/lib/dependencies/RequireResolveContextDependency.js +1 -1
  130. package/lib/dependencies/WebAssemblyExportImportedDependency.js +1 -1
  131. package/lib/dependencies/WorkerDependency.js +6 -3
  132. package/lib/dependencies/WorkerPlugin.js +100 -41
  133. package/lib/esm/ModuleChunkFormatPlugin.js +5 -0
  134. package/lib/hmr/HotModuleReplacement.runtime.js +1 -0
  135. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +1 -0
  136. package/lib/hmr/LazyCompilationPlugin.js +32 -24
  137. package/lib/hmr/lazyCompilationBackend.js +1 -1
  138. package/lib/ids/DeterministicModuleIdsPlugin.js +1 -1
  139. package/lib/ids/HashedModuleIdsPlugin.js +2 -1
  140. package/lib/ids/IdHelpers.js +15 -14
  141. package/lib/ids/SyncModuleIdsPlugin.js +9 -5
  142. package/lib/index.js +5 -5
  143. package/lib/javascript/BasicEvaluatedExpression.js +6 -6
  144. package/lib/javascript/JavascriptGenerator.js +11 -1
  145. package/lib/javascript/JavascriptModulesPlugin.js +51 -31
  146. package/lib/javascript/JavascriptParser.js +272 -188
  147. package/lib/javascript/JavascriptParserHelpers.js +10 -9
  148. package/lib/javascript/StartupHelpers.js +4 -1
  149. package/lib/json/JsonData.js +4 -4
  150. package/lib/json/JsonGenerator.js +54 -22
  151. package/lib/json/JsonModulesPlugin.js +16 -2
  152. package/lib/json/JsonParser.js +8 -4
  153. package/lib/library/AbstractLibraryPlugin.js +7 -3
  154. package/lib/library/AssignLibraryPlugin.js +29 -1
  155. package/lib/library/EnableLibraryPlugin.js +7 -10
  156. package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
  157. package/lib/library/ModuleLibraryPlugin.js +121 -15
  158. package/lib/logging/Logger.js +2 -2
  159. package/lib/logging/createConsoleLogger.js +4 -4
  160. package/lib/node/NodeEnvironmentPlugin.js +6 -2
  161. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +1 -2
  162. package/lib/node/ReadFileCompileWasmPlugin.js +1 -2
  163. package/lib/node/nodeConsole.js +3 -1
  164. package/lib/optimize/AggressiveSplittingPlugin.js +1 -1
  165. package/lib/optimize/ConcatenatedModule.js +19 -12
  166. package/lib/optimize/InnerGraph.js +3 -2
  167. package/lib/optimize/InnerGraphPlugin.js +13 -7
  168. package/lib/optimize/LimitChunkCountPlugin.js +20 -0
  169. package/lib/optimize/ModuleConcatenationPlugin.js +10 -7
  170. package/lib/optimize/RealContentHashPlugin.js +8 -4
  171. package/lib/optimize/SideEffectsFlagPlugin.js +4 -2
  172. package/lib/optimize/SplitChunksPlugin.js +87 -65
  173. package/lib/rules/BasicEffectRulePlugin.js +9 -1
  174. package/lib/rules/BasicMatcherRulePlugin.js +15 -4
  175. package/lib/rules/ObjectMatcherRulePlugin.js +12 -3
  176. package/lib/rules/RuleSetCompiler.js +25 -14
  177. package/lib/rules/UseEffectRulePlugin.js +47 -17
  178. package/lib/runtime/GetChunkFilenameRuntimeModule.js +6 -6
  179. package/lib/schemes/HttpUriPlugin.js +38 -17
  180. package/lib/serialization/BinaryMiddleware.js +52 -19
  181. package/lib/serialization/FileMiddleware.js +78 -48
  182. package/lib/serialization/ObjectMiddleware.js +78 -29
  183. package/lib/serialization/PlainObjectSerializer.js +1 -1
  184. package/lib/serialization/Serializer.js +15 -10
  185. package/lib/serialization/SerializerMiddleware.js +80 -41
  186. package/lib/serialization/SingleItemMiddleware.js +10 -7
  187. package/lib/serialization/types.js +1 -1
  188. package/lib/sharing/ConsumeSharedModule.js +4 -2
  189. package/lib/sharing/ProvideSharedModule.js +4 -2
  190. package/lib/sharing/ProvideSharedModuleFactory.js +5 -3
  191. package/lib/sharing/utils.js +2 -2
  192. package/lib/stats/DefaultStatsFactoryPlugin.js +80 -78
  193. package/lib/stats/DefaultStatsPresetPlugin.js +43 -23
  194. package/lib/stats/DefaultStatsPrinterPlugin.js +85 -43
  195. package/lib/stats/StatsFactory.js +11 -11
  196. package/lib/stats/StatsPrinter.js +7 -7
  197. package/lib/util/ArrayHelpers.js +2 -4
  198. package/lib/util/ArrayQueue.js +1 -1
  199. package/lib/util/AsyncQueue.js +4 -4
  200. package/lib/util/IterableHelpers.js +1 -1
  201. package/lib/util/LazyBucketSortedSet.js +41 -23
  202. package/lib/util/LazySet.js +3 -2
  203. package/lib/util/MapHelpers.js +1 -1
  204. package/lib/util/ParallelismFactorCalculator.js +1 -1
  205. package/lib/util/Semaphore.js +3 -3
  206. package/lib/util/SetHelpers.js +1 -1
  207. package/lib/util/SortableSet.js +9 -7
  208. package/lib/util/TupleQueue.js +9 -8
  209. package/lib/util/TupleSet.js +2 -2
  210. package/lib/util/WeakTupleMap.js +12 -11
  211. package/lib/util/binarySearchBounds.js +2 -1
  212. package/lib/util/cleverMerge.js +84 -54
  213. package/lib/util/comparators.js +22 -21
  214. package/lib/util/compileBooleanMatcher.js +3 -3
  215. package/lib/util/concatenate.js +6 -4
  216. package/lib/util/create-schema-validation.js +4 -4
  217. package/lib/util/createHash.js +2 -2
  218. package/lib/util/deprecation.js +35 -33
  219. package/lib/util/deterministicGrouping.js +6 -6
  220. package/lib/util/findGraphRoots.js +1 -1
  221. package/lib/util/fs.js +39 -39
  222. package/lib/util/hash/wasm-hash.js +2 -2
  223. package/lib/util/identifier.js +15 -18
  224. package/lib/util/makeSerializable.js +1 -1
  225. package/lib/util/memoize.js +4 -1
  226. package/lib/util/objectToMap.js +3 -2
  227. package/lib/util/processAsyncTree.js +2 -2
  228. package/lib/util/propertyName.js +0 -1
  229. package/lib/util/registerExternalSerializer.js +15 -18
  230. package/lib/util/removeBOM.js +25 -0
  231. package/lib/util/runtime.js +34 -27
  232. package/lib/util/serialization.js +5 -16
  233. package/lib/util/smartGrouping.js +3 -3
  234. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +3 -3
  235. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +11 -0
  236. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +10 -0
  237. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +4 -0
  238. package/lib/wasm-sync/WebAssemblyGenerator.js +28 -12
  239. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +10 -0
  240. package/lib/wasm-sync/WebAssemblyParser.js +9 -4
  241. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -0
  242. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +30 -25
  243. package/package.json +19 -17
  244. package/schemas/WebpackOptions.check.js +1 -1
  245. package/schemas/WebpackOptions.json +48 -13
  246. package/schemas/plugins/JsonModulesPluginGenerator.check.d.ts +7 -0
  247. package/schemas/plugins/JsonModulesPluginGenerator.check.js +6 -0
  248. package/schemas/plugins/JsonModulesPluginGenerator.json +11 -0
  249. package/schemas/plugins/SourceMapDevToolPlugin.json +2 -2
  250. package/types.d.ts +918 -615
  251. package/lib/library/ModernModuleLibraryPlugin.js +0 -144
@@ -19,9 +19,9 @@
19
19
  * @template T
20
20
  * @template N
21
21
  * @param {ContainerOptionsFormat<T>} options options passed by the user
22
- * @param {function(string | string[], string) : N} normalizeSimple normalize a simple item
23
- * @param {function(T, string) : N} normalizeOptions normalize a complex item
24
- * @param {function(string, N): void} fn processing function
22
+ * @param {(item: string | string[], itemOrKey: string) => N} normalizeSimple normalize a simple item
23
+ * @param {(value: T, key: string) => N} normalizeOptions normalize a complex item
24
+ * @param {(item: string, normalized: N) => void} fn processing function
25
25
  * @returns {void}
26
26
  */
27
27
  const process = (options, normalizeSimple, normalizeOptions, fn) => {
@@ -66,8 +66,8 @@ const process = (options, normalizeSimple, normalizeOptions, fn) => {
66
66
  * @template T
67
67
  * @template R
68
68
  * @param {ContainerOptionsFormat<T>} options options passed by the user
69
- * @param {function(string | string[], string) : R} normalizeSimple normalize a simple item
70
- * @param {function(T, string) : R} normalizeOptions normalize a complex item
69
+ * @param {(item: string | string[], itemOrKey: string) => R} normalizeSimple normalize a simple item
70
+ * @param {(value: T, key: string) => R} normalizeOptions normalize a complex item
71
71
  * @returns {[string, R][]} parsed options
72
72
  */
73
73
  const parseOptions = (options, normalizeSimple, normalizeOptions) => {
@@ -11,7 +11,10 @@ const Generator = require("../Generator");
11
11
  const InitFragment = require("../InitFragment");
12
12
  const {
13
13
  JS_AND_CSS_EXPORT_TYPES,
14
- JS_AND_CSS_TYPES
14
+ JS_AND_CSS_TYPES,
15
+ CSS_TYPES,
16
+ JS_TYPE,
17
+ CSS_TYPE
15
18
  } = require("../ModuleSourceTypesConstants");
16
19
  const RuntimeGlobals = require("../RuntimeGlobals");
17
20
  const Template = require("../Template");
@@ -26,21 +29,26 @@ const Template = require("../Template");
26
29
  /** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
27
30
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
28
31
  /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */
32
+ /** @typedef {import("../Module").BuildInfo} BuildInfo */
33
+ /** @typedef {import("../Module").BuildMeta} BuildMeta */
29
34
  /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
30
35
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
36
+ /** @typedef {import("../ModuleGraph")} ModuleGraph */
31
37
  /** @typedef {import("../NormalModule")} NormalModule */
32
38
  /** @typedef {import("../util/Hash")} Hash */
33
39
 
34
40
  class CssGenerator extends Generator {
35
41
  /**
36
42
  * @param {CssAutoGeneratorOptions | CssGlobalGeneratorOptions | CssModuleGeneratorOptions} options options
43
+ * @param {ModuleGraph} moduleGraph the module graph
37
44
  */
38
- constructor(options) {
45
+ constructor(options, moduleGraph) {
39
46
  super();
40
47
  this.convention = options.exportsConvention;
41
48
  this.localIdentName = options.localIdentName;
42
49
  this.exportsOnly = options.exportsOnly;
43
50
  this.esModule = options.esModule;
51
+ this._moduleGraph = moduleGraph;
44
52
  }
45
53
 
46
54
  /**
@@ -71,7 +79,7 @@ class CssGenerator extends Generator {
71
79
  const initFragments = [];
72
80
  /** @type {CssData} */
73
81
  const cssData = {
74
- esModule: this.esModule,
82
+ esModule: /** @type {boolean} */ (this.esModule),
75
83
  exports: new Map()
76
84
  };
77
85
 
@@ -131,7 +139,8 @@ class CssGenerator extends Generator {
131
139
 
132
140
  switch (generateContext.type) {
133
141
  case "javascript": {
134
- module.buildInfo.cssData = cssData;
142
+ /** @type {BuildInfo} */
143
+ (module.buildInfo).cssData = cssData;
135
144
 
136
145
  generateContext.runtimeRequirements.add(RuntimeGlobals.module);
137
146
 
@@ -167,6 +176,13 @@ class CssGenerator extends Generator {
167
176
  return source;
168
177
  }
169
178
 
179
+ if (
180
+ cssData.exports.size === 0 &&
181
+ !(/** @type {BuildMeta} */ (module.buildMeta).isCSSModule)
182
+ ) {
183
+ return new RawSource("");
184
+ }
185
+
170
186
  const needNsObj =
171
187
  this.esModule &&
172
188
  generateContext.moduleGraph
@@ -203,6 +219,29 @@ class CssGenerator extends Generator {
203
219
 
204
220
  return InitFragment.addToSource(source, initFragments, generateContext);
205
221
  }
222
+ default:
223
+ return null;
224
+ }
225
+ }
226
+
227
+ /**
228
+ * @param {Error} error the error
229
+ * @param {NormalModule} module module for which the code should be generated
230
+ * @param {GenerateContext} generateContext context for generate
231
+ * @returns {Source | null} generated code
232
+ */
233
+ generateError(error, module, generateContext) {
234
+ switch (generateContext.type) {
235
+ case "javascript": {
236
+ return new RawSource(
237
+ `throw new Error(${JSON.stringify(error.message)});`
238
+ );
239
+ }
240
+ case "css": {
241
+ return new RawSource(`/**\n ${error.message} \n**/`);
242
+ }
243
+ default:
244
+ return null;
206
245
  }
207
246
  }
208
247
 
@@ -212,7 +251,22 @@ class CssGenerator extends Generator {
212
251
  */
213
252
  getTypes(module) {
214
253
  // TODO, find a better way to prevent the original module from being removed after concatenation, maybe it is a bug
215
- return this.exportsOnly ? JS_AND_CSS_EXPORT_TYPES : JS_AND_CSS_TYPES;
254
+ if (this.exportsOnly) {
255
+ return JS_AND_CSS_EXPORT_TYPES;
256
+ }
257
+ const sourceTypes = new Set();
258
+ const connections = this._moduleGraph.getIncomingConnections(module);
259
+ for (const connection of connections) {
260
+ if (!connection.originModule) {
261
+ continue;
262
+ }
263
+ if (connection.originModule.type.split("/")[0] !== CSS_TYPE)
264
+ sourceTypes.add(JS_TYPE);
265
+ }
266
+ if (sourceTypes.has(JS_TYPE)) {
267
+ return JS_AND_CSS_TYPES;
268
+ }
269
+ return CSS_TYPES;
216
270
  }
217
271
 
218
272
  /**
@@ -223,11 +277,17 @@ class CssGenerator extends Generator {
223
277
  getSize(module, type) {
224
278
  switch (type) {
225
279
  case "javascript": {
226
- if (!module.buildInfo.cssData) {
280
+ const cssData = /** @type {BuildInfo} */ (module.buildInfo).cssData;
281
+ if (!cssData) {
227
282
  return 42;
228
283
  }
229
-
230
- const exports = module.buildInfo.cssData.exports;
284
+ if (cssData.exports.size === 0) {
285
+ if (/** @type {BuildMeta} */ (module.buildMeta).isCSSModule) {
286
+ return 42;
287
+ }
288
+ return 0;
289
+ }
290
+ const exports = cssData.exports;
231
291
  const stringifiedExports = JSON.stringify(
232
292
  Array.from(exports).reduce((obj, [key, value]) => {
233
293
  obj[key] = value;
@@ -246,6 +306,8 @@ class CssGenerator extends Generator {
246
306
 
247
307
  return originalSource.size();
248
308
  }
309
+ default:
310
+ return 0;
249
311
  }
250
312
  }
251
313
 
@@ -254,7 +316,7 @@ class CssGenerator extends Generator {
254
316
  * @param {UpdateHashContext} updateHashContext context for updating hash
255
317
  */
256
318
  updateHash(hash, { module }) {
257
- hash.update(this.esModule.toString());
319
+ hash.update(/** @type {boolean} */ (this.esModule).toString());
258
320
  }
259
321
  }
260
322
 
@@ -23,6 +23,7 @@ const {
23
23
  CSS_MODULE_TYPE_MODULE,
24
24
  CSS_MODULE_TYPE_AUTO
25
25
  } = require("../ModuleTypeConstants");
26
+ const NormalModule = require("../NormalModule");
26
27
  const RuntimeGlobals = require("../RuntimeGlobals");
27
28
  const SelfModuleFactory = require("../SelfModuleFactory");
28
29
  const Template = require("../Template");
@@ -42,6 +43,7 @@ const createHash = require("../util/createHash");
42
43
  const { getUndoPath } = require("../util/identifier");
43
44
  const memoize = require("../util/memoize");
44
45
  const nonNumericOnlyHash = require("../util/nonNumericOnlyHash");
46
+ const removeBOM = require("../util/removeBOM");
45
47
  const CssGenerator = require("./CssGenerator");
46
48
  const CssParser = require("./CssParser");
47
49
 
@@ -54,6 +56,7 @@ const CssParser = require("./CssParser");
54
56
  /** @typedef {import("../Compiler")} Compiler */
55
57
  /** @typedef {import("../CssModule").Inheritance} Inheritance */
56
58
  /** @typedef {import("../Module")} Module */
59
+ /** @typedef {import("../Module").BuildInfo} BuildInfo */
57
60
  /** @typedef {import("../Template").RuntimeTemplate} RuntimeTemplate */
58
61
  /** @typedef {import("../TemplatedPathPlugin").TemplatePath} TemplatePath */
59
62
  /** @typedef {import("../util/Hash")} Hash */
@@ -92,7 +95,7 @@ const getCssLoadingRuntimeModule = memoize(() =>
92
95
 
93
96
  /**
94
97
  * @param {string} name name
95
- * @returns {{oneOf: [{$ref: string}], definitions: *}} schema
98
+ * @returns {{ oneOf: [{ $ref: string }], definitions: import("../../schemas/WebpackOptions.json")["definitions"] }} schema
96
99
  */
97
100
  const getSchema = name => {
98
101
  const { definitions } = require("../../schemas/WebpackOptions.json");
@@ -298,7 +301,10 @@ class CssModulesPlugin {
298
301
  .tap(PLUGIN_NAME, generatorOptions => {
299
302
  validateGeneratorOptions[type](generatorOptions);
300
303
 
301
- return new CssGenerator(generatorOptions);
304
+ return new CssGenerator(
305
+ generatorOptions,
306
+ compilation.moduleGraph
307
+ );
302
308
  });
303
309
  normalModuleFactory.hooks.createModuleClass
304
310
  .for(type)
@@ -360,13 +366,28 @@ class CssModulesPlugin {
360
366
 
361
367
  return new CssModule(createData);
362
368
  });
369
+
370
+ NormalModule.getCompilationHooks(compilation).processResult.tap(
371
+ PLUGIN_NAME,
372
+ (result, module) => {
373
+ if (module.type === type) {
374
+ const [source, ...rest] = result;
375
+
376
+ return [removeBOM(source), ...rest];
377
+ }
378
+
379
+ return result;
380
+ }
381
+ );
363
382
  }
364
383
 
365
384
  JavascriptModulesPlugin.getCompilationHooks(
366
385
  compilation
367
386
  ).renderModuleContent.tap(PLUGIN_NAME, (source, module) => {
368
387
  if (module instanceof CssModule && module.hot) {
369
- const exports = module.buildInfo.cssData.exports;
388
+ const exports =
389
+ /** @type {BuildInfo} */
390
+ (module.buildInfo).cssData.exports;
370
391
  const stringifiedExports = JSON.stringify(
371
392
  JSON.stringify(
372
393
  Array.from(exports).reduce((obj, [key, value]) => {
@@ -390,6 +411,8 @@ class CssModulesPlugin {
390
411
 
391
412
  return new ConcatSource(source, "\n", new RawSource(hmrCode));
392
413
  }
414
+
415
+ return source;
393
416
  });
394
417
  const orderedCssModulesPerChunk = new WeakMap();
395
418
  compilation.hooks.afterCodeGeneration.tap(PLUGIN_NAME, () => {
@@ -475,7 +498,9 @@ class CssModulesPlugin {
475
498
  chunk,
476
499
  chunkGraph,
477
500
  codeGenerationResults,
478
- uniqueName: compilation.outputOptions.uniqueName,
501
+ uniqueName:
502
+ /** @type {string} */
503
+ (compilation.outputOptions.uniqueName),
479
504
  undoPath,
480
505
  modules,
481
506
  runtimeTemplate
@@ -718,7 +743,7 @@ class CssModulesPlugin {
718
743
  }
719
744
 
720
745
  /**
721
- * @param {CssModule} module css module
746
+ * @param {CssModule} module css module
722
747
  * @param {ChunkRenderContext} renderContext options object
723
748
  * @param {CompilationHooks} hooks hooks
724
749
  * @returns {Source} css module source
@@ -283,13 +283,17 @@ const eatUntilSemi = walkCssTokens.eatUntil(";");
283
283
  const eatUntilLeftCurly = walkCssTokens.eatUntil("{");
284
284
  const eatSemi = walkCssTokens.eatUntil(";");
285
285
 
286
+ /**
287
+ * @typedef {object} CssParserOptions
288
+ * @property {boolean=} importOption need handle `@import`
289
+ * @property {boolean=} url need handle URLs
290
+ * @property {("pure" | "global" | "local" | "auto")=} defaultMode default mode
291
+ * @property {boolean=} namedExports is named exports
292
+ */
293
+
286
294
  class CssParser extends Parser {
287
295
  /**
288
- * @param {object} options options
289
- * @param {boolean=} options.importOption need handle `@import`
290
- * @param {boolean=} options.url need handle URLs
291
- * @param {("pure" | "global" | "local" | "auto")=} options.defaultMode default mode
292
- * @param {boolean=} options.namedExports is named exports
296
+ * @param {CssParserOptions} [options] options
293
297
  */
294
298
  constructor({
295
299
  defaultMode = "pure",
@@ -357,6 +361,9 @@ class CssParser extends Parser {
357
361
 
358
362
  const isModules = mode === "global" || mode === "local";
359
363
 
364
+ /** @type {BuildMeta} */
365
+ (module.buildMeta).isCSSModule = isModules;
366
+
360
367
  const locConverter = new LocConverter(source);
361
368
 
362
369
  /** @type {number} */
@@ -379,7 +386,8 @@ class CssParser extends Parser {
379
386
  let lastIdentifier;
380
387
  /** @type {Set<string>} */
381
388
  const declaredCssVariables = new Set();
382
- /** @type {Map<string, { path?: string, value: string }>} */
389
+ /** @typedef {{ path?: string, value: string }} IcssDefinition */
390
+ /** @type {Map<string, IcssDefinition>} */
383
391
  const icssDefinitions = new Map();
384
392
 
385
393
  /**
@@ -447,6 +455,7 @@ class CssParser extends Parser {
447
455
  */
448
456
  const parseImportOrExport = (type, input, pos) => {
449
457
  pos = walkCssTokens.eatWhitespaceAndComments(input, pos);
458
+ /** @type {string | undefined} */
450
459
  let importPath;
451
460
  if (type === 0) {
452
461
  let cc = input.charCodeAt(pos);
@@ -517,7 +526,9 @@ class CssParser extends Parser {
517
526
  /** @type {undefined | 0 | 1 | 2} */
518
527
  let scope;
519
528
 
520
- /** @type {[number, number] | undefined} */
529
+ /** @typedef {[number, number]} Name */
530
+
531
+ /** @type {Name | undefined} */
521
532
  let name;
522
533
  /** @type {number | undefined} */
523
534
  let value;
@@ -537,10 +548,11 @@ class CssParser extends Parser {
537
548
  balanced--;
538
549
 
539
550
  if (scope === 2) {
551
+ const [nameStart, nameEnd] = /** @type {Name} */ (name);
540
552
  createDep(
541
- input.slice(name[0], name[1]),
553
+ input.slice(nameStart, nameEnd),
542
554
  input.slice(value, end - 1).trim(),
543
- name[1],
555
+ nameEnd,
544
556
  end - 1
545
557
  );
546
558
  scope = 0;
@@ -571,10 +583,11 @@ class CssParser extends Parser {
571
583
  },
572
584
  semicolon: (input, _start, end) => {
573
585
  if (scope === 2) {
586
+ const [nameStart, nameEnd] = /** @type {Name} */ (name);
574
587
  createDep(
575
- input.slice(name[0], name[1]),
588
+ input.slice(nameStart, nameEnd),
576
589
  input.slice(value, end - 1),
577
- name[1],
590
+ nameEnd,
578
591
  end - 1
579
592
  );
580
593
  scope = 0;
@@ -986,7 +999,9 @@ class CssParser extends Parser {
986
999
  }
987
1000
 
988
1001
  if (icssDefinitions.has(value)) {
989
- const def = icssDefinitions.get(value);
1002
+ const def =
1003
+ /** @type {IcssDefinition} */
1004
+ (icssDefinitions.get(value));
990
1005
 
991
1006
  value = def.value;
992
1007
  }
@@ -1068,13 +1083,18 @@ class CssParser extends Parser {
1068
1083
  },
1069
1084
  identifier: (input, start, end) => {
1070
1085
  if (isModules) {
1071
- if (icssDefinitions.has(input.slice(start, end))) {
1072
- const name = input.slice(start, end);
1073
- let { path, value } = icssDefinitions.get(name);
1086
+ const name = input.slice(start, end);
1087
+
1088
+ if (icssDefinitions.has(name)) {
1089
+ let { path, value } =
1090
+ /** @type {IcssDefinition} */
1091
+ (icssDefinitions.get(name));
1074
1092
 
1075
1093
  if (path) {
1076
1094
  if (icssDefinitions.has(path)) {
1077
- const definition = icssDefinitions.get(path);
1095
+ const definition =
1096
+ /** @type {IcssDefinition} */
1097
+ (icssDefinitions.get(path));
1078
1098
 
1079
1099
  path = definition.value.slice(1, -1);
1080
1100
  }
@@ -1555,7 +1575,7 @@ class CssParser extends Parser {
1555
1575
 
1556
1576
  /**
1557
1577
  * @param {Range} range range of the comment
1558
- * @returns {{ options: Record<string, any> | null, errors: (Error & { comment: Comment })[] | null }} result
1578
+ * @returns {{ options: Record<string, EXPECTED_ANY> | null, errors: (Error & { comment: Comment })[] | null }} result
1559
1579
  */
1560
1580
  parseCommentOptions(range) {
1561
1581
  const comments = this.getComments(range);
@@ -7,25 +7,25 @@
7
7
 
8
8
  /**
9
9
  * @typedef {object} CssTokenCallbacks
10
- * @property {(function(string, number, number, number, number): number)=} url
11
- * @property {(function(string, number, number): number)=} comment
12
- * @property {(function(string, number, number): number)=} string
13
- * @property {(function(string, number, number): number)=} leftParenthesis
14
- * @property {(function(string, number, number): number)=} rightParenthesis
15
- * @property {(function(string, number, number): number)=} function
16
- * @property {(function(string, number, number): number)=} colon
17
- * @property {(function(string, number, number): number)=} atKeyword
18
- * @property {(function(string, number, number): number)=} delim
19
- * @property {(function(string, number, number): number)=} identifier
20
- * @property {(function(string, number, number, boolean): number)=} hash
21
- * @property {(function(string, number, number): number)=} leftCurlyBracket
22
- * @property {(function(string, number, number): number)=} rightCurlyBracket
23
- * @property {(function(string, number, number): number)=} semicolon
24
- * @property {(function(string, number, number): number)=} comma
25
- * @property {(function(): boolean)=} needTerminate
10
+ * @property {((input: string, start: number, end: number, innerStart: number, innerEnd: number) => number)=} url
11
+ * @property {((input: string, start: number, end: number) => number)=} comment
12
+ * @property {((input: string, start: number, end: number) => number)=} string
13
+ * @property {((input: string, start: number, end: number) => number)=} leftParenthesis
14
+ * @property {((input: string, start: number, end: number) => number)=} rightParenthesis
15
+ * @property {((input: string, start: number, end: number) => number)=} function
16
+ * @property {((input: string, start: number, end: number) => number)=} colon
17
+ * @property {((input: string, start: number, end: number) => number)=} atKeyword
18
+ * @property {((input: string, start: number, end: number) => number)=} delim
19
+ * @property {((input: string, start: number, end: number) => number)=} identifier
20
+ * @property {((input: string, start: number, end: number, isId: boolean) => number)=} hash
21
+ * @property {((input: string, start: number, end: number) => number)=} leftCurlyBracket
22
+ * @property {((input: string, start: number, end: number) => number)=} rightCurlyBracket
23
+ * @property {((input: string, start: number, end: number) => number)=} semicolon
24
+ * @property {((input: string, start: number, end: number) => number)=} comma
25
+ * @property {(() => boolean)=} needTerminate
26
26
  */
27
27
 
28
- /** @typedef {function(string, number, CssTokenCallbacks): number} CharHandler */
28
+ /** @typedef {(input: string, pos: number, callbacks: CssTokenCallbacks) => number} CharHandler */
29
29
 
30
30
  // spec: https://drafts.csswg.org/css-syntax/
31
31