webpack 5.59.0 → 5.76.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 (236) hide show
  1. package/README.md +22 -24
  2. package/bin/webpack.js +0 -0
  3. package/hot/dev-server.js +17 -4
  4. package/hot/lazy-compilation-node.js +3 -1
  5. package/hot/poll.js +1 -1
  6. package/hot/signal.js +1 -1
  7. package/lib/APIPlugin.js +33 -0
  8. package/lib/BannerPlugin.js +13 -5
  9. package/lib/Cache.js +1 -1
  10. package/lib/CacheFacade.js +4 -11
  11. package/lib/Chunk.js +6 -3
  12. package/lib/ChunkGraph.js +94 -8
  13. package/lib/ChunkGroup.js +1 -1
  14. package/lib/CleanPlugin.js +81 -20
  15. package/lib/Compilation.js +188 -93
  16. package/lib/Compiler.js +87 -18
  17. package/lib/ConstPlugin.js +2 -2
  18. package/lib/ContextModule.js +142 -51
  19. package/lib/ContextModuleFactory.js +65 -25
  20. package/lib/DelegatedModule.js +1 -1
  21. package/lib/DelegatedModuleFactoryPlugin.js +1 -1
  22. package/lib/Dependency.js +17 -0
  23. package/lib/DependencyTemplate.js +9 -0
  24. package/lib/DependencyTemplates.js +1 -1
  25. package/lib/DllModule.js +1 -1
  26. package/lib/DllReferencePlugin.js +1 -1
  27. package/lib/EntryOptionPlugin.js +2 -0
  28. package/lib/ErrorHelpers.js +2 -2
  29. package/lib/EvalDevToolModulePlugin.js +16 -1
  30. package/lib/EvalSourceMapDevToolPlugin.js +25 -4
  31. package/lib/ExportsInfo.js +5 -5
  32. package/lib/ExternalModule.js +94 -54
  33. package/lib/ExternalModuleFactoryPlugin.js +5 -5
  34. package/lib/FileSystemInfo.js +124 -58
  35. package/lib/Generator.js +3 -0
  36. package/lib/HookWebpackError.js +1 -1
  37. package/lib/HotModuleReplacementPlugin.js +3 -1
  38. package/lib/LoaderOptionsPlugin.js +1 -1
  39. package/lib/Module.js +28 -4
  40. package/lib/ModuleFilenameHelpers.js +8 -4
  41. package/lib/ModuleHashingError.js +29 -0
  42. package/lib/MultiCompiler.js +1 -1
  43. package/lib/MultiWatching.js +1 -1
  44. package/lib/NodeStuffPlugin.js +13 -3
  45. package/lib/NormalModule.js +51 -33
  46. package/lib/NormalModuleFactory.js +42 -37
  47. package/lib/ProgressPlugin.js +4 -5
  48. package/lib/RawModule.js +1 -1
  49. package/lib/RuntimeGlobals.js +29 -1
  50. package/lib/RuntimeModule.js +1 -1
  51. package/lib/RuntimePlugin.js +84 -1
  52. package/lib/RuntimeTemplate.js +114 -2
  53. package/lib/Template.js +3 -2
  54. package/lib/TemplatedPathPlugin.js +48 -23
  55. package/lib/WatchIgnorePlugin.js +19 -7
  56. package/lib/Watching.js +33 -19
  57. package/lib/WebpackOptionsApply.js +79 -11
  58. package/lib/asset/AssetGenerator.js +228 -71
  59. package/lib/asset/AssetModulesPlugin.js +3 -0
  60. package/lib/asset/AssetParser.js +1 -0
  61. package/lib/asset/AssetSourceGenerator.js +31 -6
  62. package/lib/asset/AssetSourceParser.js +1 -0
  63. package/lib/asset/RawDataUrlModule.js +148 -0
  64. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
  65. package/lib/buildChunkGraph.js +38 -7
  66. package/lib/cache/PackFileCacheStrategy.js +15 -8
  67. package/lib/cache/ResolverCachePlugin.js +90 -29
  68. package/lib/cli.js +44 -3
  69. package/lib/config/browserslistTargetHandler.js +41 -6
  70. package/lib/config/defaults.js +123 -19
  71. package/lib/config/normalization.js +10 -2
  72. package/lib/config/target.js +10 -0
  73. package/lib/container/ContainerEntryModule.js +8 -5
  74. package/lib/container/FallbackModule.js +4 -4
  75. package/lib/container/ModuleFederationPlugin.js +2 -0
  76. package/lib/container/RemoteModule.js +4 -2
  77. package/lib/container/RemoteRuntimeModule.js +8 -7
  78. package/lib/css/CssExportsGenerator.js +139 -0
  79. package/lib/css/CssGenerator.js +109 -0
  80. package/lib/css/CssLoadingRuntimeModule.js +442 -0
  81. package/lib/css/CssModulesPlugin.js +462 -0
  82. package/lib/css/CssParser.js +618 -0
  83. package/lib/css/walkCssTokens.js +659 -0
  84. package/lib/debug/ProfilingPlugin.js +24 -21
  85. package/lib/dependencies/AMDRequireDependency.js +6 -6
  86. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
  87. package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
  88. package/lib/dependencies/CommonJsImportsParserPlugin.js +344 -61
  89. package/lib/dependencies/CommonJsRequireContextDependency.js +6 -2
  90. package/lib/dependencies/CommonJsRequireDependency.js +2 -1
  91. package/lib/dependencies/ContextDependency.js +16 -2
  92. package/lib/dependencies/ContextDependencyHelpers.js +21 -8
  93. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
  94. package/lib/dependencies/ContextElementDependency.js +25 -3
  95. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  96. package/lib/dependencies/CssExportDependency.js +85 -0
  97. package/lib/dependencies/CssImportDependency.js +75 -0
  98. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  99. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  100. package/lib/dependencies/CssUrlDependency.js +132 -0
  101. package/lib/dependencies/ExportsInfoDependency.js +6 -0
  102. package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
  103. package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
  104. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +127 -0
  105. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
  106. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
  107. package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
  108. package/lib/dependencies/HarmonyImportDependency.js +23 -0
  109. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
  110. package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
  111. package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
  112. package/lib/dependencies/ImportContextDependency.js +0 -2
  113. package/lib/dependencies/ImportMetaContextDependency.js +35 -0
  114. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
  115. package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
  116. package/lib/dependencies/ImportMetaPlugin.js +22 -3
  117. package/lib/dependencies/ImportParserPlugin.js +35 -29
  118. package/lib/dependencies/JsonExportsDependency.js +17 -21
  119. package/lib/dependencies/LoaderDependency.js +13 -0
  120. package/lib/dependencies/LoaderImportDependency.js +13 -0
  121. package/lib/dependencies/LoaderPlugin.js +4 -2
  122. package/lib/dependencies/ModuleDependency.js +11 -1
  123. package/lib/dependencies/ProvidedDependency.js +31 -8
  124. package/lib/dependencies/RequireContextDependency.js +0 -16
  125. package/lib/dependencies/RequireEnsureDependency.js +2 -2
  126. package/lib/dependencies/RequireResolveContextDependency.js +2 -2
  127. package/lib/dependencies/RequireResolveDependency.js +2 -1
  128. package/lib/dependencies/URLDependency.js +3 -8
  129. package/lib/dependencies/URLPlugin.js +22 -1
  130. package/lib/dependencies/WorkerPlugin.js +2 -0
  131. package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
  132. package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
  133. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
  134. package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
  135. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
  136. package/lib/hmr/LazyCompilationPlugin.js +54 -26
  137. package/lib/hmr/lazyCompilationBackend.js +51 -12
  138. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  139. package/lib/ids/HashedModuleIdsPlugin.js +11 -14
  140. package/lib/ids/IdHelpers.js +26 -12
  141. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  142. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  143. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  144. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  145. package/lib/index.js +20 -0
  146. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
  147. package/lib/javascript/BasicEvaluatedExpression.js +5 -2
  148. package/lib/javascript/ChunkHelpers.js +33 -0
  149. package/lib/javascript/JavascriptGenerator.js +1 -0
  150. package/lib/javascript/JavascriptModulesPlugin.js +27 -2
  151. package/lib/javascript/JavascriptParser.js +143 -73
  152. package/lib/javascript/StartupHelpers.js +7 -30
  153. package/lib/json/JsonData.js +8 -0
  154. package/lib/json/JsonParser.js +4 -6
  155. package/lib/library/AssignLibraryPlugin.js +39 -15
  156. package/lib/library/EnableLibraryPlugin.js +11 -0
  157. package/lib/library/UmdLibraryPlugin.js +5 -3
  158. package/lib/node/NodeTargetPlugin.js +3 -0
  159. package/lib/node/NodeWatchFileSystem.js +85 -31
  160. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
  161. package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
  162. package/lib/optimize/ConcatenatedModule.js +62 -26
  163. package/lib/optimize/ModuleConcatenationPlugin.js +26 -4
  164. package/lib/optimize/RealContentHashPlugin.js +45 -15
  165. package/lib/optimize/SplitChunksPlugin.js +8 -1
  166. package/lib/runtime/AsyncModuleRuntimeModule.js +50 -66
  167. package/lib/runtime/BaseUriRuntimeModule.js +31 -0
  168. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  169. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  170. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  171. package/lib/runtime/LoadScriptRuntimeModule.js +11 -9
  172. package/lib/runtime/NonceRuntimeModule.js +24 -0
  173. package/lib/schemes/HttpUriPlugin.js +77 -14
  174. package/lib/serialization/FileMiddleware.js +44 -9
  175. package/lib/sharing/ConsumeSharedModule.js +8 -2
  176. package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
  177. package/lib/sharing/ProvideSharedModule.js +4 -2
  178. package/lib/sharing/ProvideSharedPlugin.js +1 -2
  179. package/lib/sharing/ShareRuntimeModule.js +1 -1
  180. package/lib/sharing/utils.js +1 -1
  181. package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
  182. package/lib/stats/DefaultStatsPrinterPlugin.js +90 -25
  183. package/lib/util/ArrayHelpers.js +30 -0
  184. package/lib/util/AsyncQueue.js +1 -1
  185. package/lib/util/compileBooleanMatcher.js +1 -1
  186. package/lib/util/create-schema-validation.js +9 -2
  187. package/lib/util/createHash.js +12 -0
  188. package/lib/util/deprecation.js +10 -2
  189. package/lib/util/deterministicGrouping.js +1 -1
  190. package/lib/util/extractUrlAndGlobal.js +3 -0
  191. package/lib/util/fs.js +11 -0
  192. package/lib/util/hash/BatchedHash.js +7 -4
  193. package/lib/util/hash/md4.js +20 -0
  194. package/lib/util/hash/wasm-hash.js +163 -0
  195. package/lib/util/hash/xxhash64.js +5 -139
  196. package/lib/util/identifier.js +65 -44
  197. package/lib/util/internalSerializables.js +15 -0
  198. package/lib/util/nonNumericOnlyHash.js +22 -0
  199. package/lib/util/semver.js +17 -10
  200. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
  201. package/lib/wasm-sync/WebAssemblyParser.js +1 -1
  202. package/lib/web/JsonpChunkLoadingRuntimeModule.js +31 -18
  203. package/lib/webpack.js +10 -3
  204. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  205. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
  206. package/module.d.ts +215 -0
  207. package/package.json +28 -32
  208. package/schemas/WebpackOptions.check.js +1 -1
  209. package/schemas/WebpackOptions.json +321 -30
  210. package/schemas/plugins/BannerPlugin.check.js +1 -1
  211. package/schemas/plugins/BannerPlugin.json +4 -0
  212. package/schemas/plugins/DllReferencePlugin.check.js +1 -1
  213. package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
  214. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  215. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  216. package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
  217. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  218. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  219. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  220. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  221. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  222. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  223. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  224. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  225. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  226. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  227. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  228. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  229. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  230. package/schemas/plugins/css/CssParserOptions.json +3 -0
  231. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
  232. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
  233. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
  234. package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
  235. package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
  236. package/types.d.ts +869 -296
@@ -6,22 +6,27 @@
6
6
  "use strict";
7
7
 
8
8
  const RuntimeGlobals = require("./RuntimeGlobals");
9
+ const { getChunkFilenameTemplate } = require("./css/CssModulesPlugin");
9
10
  const RuntimeRequirementsDependency = require("./dependencies/RuntimeRequirementsDependency");
10
11
  const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
11
12
  const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
12
13
  const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
14
+ const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
13
15
  const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
14
16
  const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
15
17
  const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
18
+ const CreateScriptRuntimeModule = require("./runtime/CreateScriptRuntimeModule");
16
19
  const CreateScriptUrlRuntimeModule = require("./runtime/CreateScriptUrlRuntimeModule");
17
20
  const DefinePropertyGettersRuntimeModule = require("./runtime/DefinePropertyGettersRuntimeModule");
18
21
  const EnsureChunkRuntimeModule = require("./runtime/EnsureChunkRuntimeModule");
19
22
  const GetChunkFilenameRuntimeModule = require("./runtime/GetChunkFilenameRuntimeModule");
20
23
  const GetMainFilenameRuntimeModule = require("./runtime/GetMainFilenameRuntimeModule");
24
+ const GetTrustedTypesPolicyRuntimeModule = require("./runtime/GetTrustedTypesPolicyRuntimeModule");
21
25
  const GlobalRuntimeModule = require("./runtime/GlobalRuntimeModule");
22
26
  const HasOwnPropertyRuntimeModule = require("./runtime/HasOwnPropertyRuntimeModule");
23
27
  const LoadScriptRuntimeModule = require("./runtime/LoadScriptRuntimeModule");
24
28
  const MakeNamespaceObjectRuntimeModule = require("./runtime/MakeNamespaceObjectRuntimeModule");
29
+ const NonceRuntimeModule = require("./runtime/NonceRuntimeModule");
25
30
  const OnChunksLoadedRuntimeModule = require("./runtime/OnChunksLoadedRuntimeModule");
26
31
  const PublicPathRuntimeModule = require("./runtime/PublicPathRuntimeModule");
27
32
  const RelativeUrlRuntimeModule = require("./runtime/RelativeUrlRuntimeModule");
@@ -39,7 +44,9 @@ const GLOBALS_ON_REQUIRE = [
39
44
  RuntimeGlobals.runtimeId,
40
45
  RuntimeGlobals.compatGetDefaultExport,
41
46
  RuntimeGlobals.createFakeNamespaceObject,
47
+ RuntimeGlobals.createScript,
42
48
  RuntimeGlobals.createScriptUrl,
49
+ RuntimeGlobals.getTrustedTypesPolicy,
43
50
  RuntimeGlobals.definePropertyGetters,
44
51
  RuntimeGlobals.ensureChunk,
45
52
  RuntimeGlobals.entryModuleId,
@@ -91,6 +98,15 @@ class RuntimePlugin {
91
98
  */
92
99
  apply(compiler) {
93
100
  compiler.hooks.compilation.tap("RuntimePlugin", compilation => {
101
+ const globalChunkLoading = compilation.outputOptions.chunkLoading;
102
+ const isChunkLoadingDisabledForChunk = chunk => {
103
+ const options = chunk.getEntryOptions();
104
+ const chunkLoading =
105
+ options && options.chunkLoading !== undefined
106
+ ? options.chunkLoading
107
+ : globalChunkLoading;
108
+ return chunkLoading === false;
109
+ };
94
110
  compilation.dependencyTemplates.set(
95
111
  RuntimeRequirementsDependency,
96
112
  new RuntimeRequirementsDependency.Template()
@@ -218,7 +234,15 @@ class RuntimePlugin {
218
234
  compilation.hooks.runtimeRequirementInTree
219
235
  .for(RuntimeGlobals.systemContext)
220
236
  .tap("RuntimePlugin", chunk => {
221
- if (compilation.outputOptions.library.type === "system") {
237
+ const { outputOptions } = compilation;
238
+ const { library: globalLibrary } = outputOptions;
239
+ const entryOptions = chunk.getEntryOptions();
240
+ const libraryType =
241
+ entryOptions && entryOptions.library !== undefined
242
+ ? entryOptions.library.type
243
+ : globalLibrary.type;
244
+
245
+ if (libraryType === "system") {
222
246
  compilation.addRuntimeModule(
223
247
  chunk,
224
248
  new SystemContextRuntimeModule()
@@ -253,6 +277,30 @@ class RuntimePlugin {
253
277
  );
254
278
  return true;
255
279
  });
280
+ compilation.hooks.runtimeRequirementInTree
281
+ .for(RuntimeGlobals.getChunkCssFilename)
282
+ .tap("RuntimePlugin", (chunk, set) => {
283
+ if (
284
+ typeof compilation.outputOptions.cssChunkFilename === "string" &&
285
+ /\[(full)?hash(:\d+)?\]/.test(
286
+ compilation.outputOptions.cssChunkFilename
287
+ )
288
+ ) {
289
+ set.add(RuntimeGlobals.getFullHash);
290
+ }
291
+ compilation.addRuntimeModule(
292
+ chunk,
293
+ new GetChunkFilenameRuntimeModule(
294
+ "css",
295
+ "css",
296
+ RuntimeGlobals.getChunkCssFilename,
297
+ chunk =>
298
+ getChunkFilenameTemplate(chunk, compilation.outputOptions),
299
+ set.has(RuntimeGlobals.hmrDownloadUpdateHandlers)
300
+ )
301
+ );
302
+ return true;
303
+ });
256
304
  compilation.hooks.runtimeRequirementInTree
257
305
  .for(RuntimeGlobals.getChunkUpdateScriptFilename)
258
306
  .tap("RuntimePlugin", (chunk, set) => {
@@ -331,15 +379,36 @@ class RuntimePlugin {
331
379
  );
332
380
  return true;
333
381
  });
382
+ compilation.hooks.runtimeRequirementInTree
383
+ .for(RuntimeGlobals.createScript)
384
+ .tap("RuntimePlugin", (chunk, set) => {
385
+ if (compilation.outputOptions.trustedTypes) {
386
+ set.add(RuntimeGlobals.getTrustedTypesPolicy);
387
+ }
388
+ compilation.addRuntimeModule(chunk, new CreateScriptRuntimeModule());
389
+ return true;
390
+ });
334
391
  compilation.hooks.runtimeRequirementInTree
335
392
  .for(RuntimeGlobals.createScriptUrl)
336
393
  .tap("RuntimePlugin", (chunk, set) => {
394
+ if (compilation.outputOptions.trustedTypes) {
395
+ set.add(RuntimeGlobals.getTrustedTypesPolicy);
396
+ }
337
397
  compilation.addRuntimeModule(
338
398
  chunk,
339
399
  new CreateScriptUrlRuntimeModule()
340
400
  );
341
401
  return true;
342
402
  });
403
+ compilation.hooks.runtimeRequirementInTree
404
+ .for(RuntimeGlobals.getTrustedTypesPolicy)
405
+ .tap("RuntimePlugin", (chunk, set) => {
406
+ compilation.addRuntimeModule(
407
+ chunk,
408
+ new GetTrustedTypesPolicyRuntimeModule(set)
409
+ );
410
+ return true;
411
+ });
343
412
  compilation.hooks.runtimeRequirementInTree
344
413
  .for(RuntimeGlobals.relativeUrl)
345
414
  .tap("RuntimePlugin", (chunk, set) => {
@@ -355,6 +424,20 @@ class RuntimePlugin {
355
424
  );
356
425
  return true;
357
426
  });
427
+ compilation.hooks.runtimeRequirementInTree
428
+ .for(RuntimeGlobals.baseURI)
429
+ .tap("RuntimePlugin", chunk => {
430
+ if (isChunkLoadingDisabledForChunk(chunk)) {
431
+ compilation.addRuntimeModule(chunk, new BaseUriRuntimeModule());
432
+ return true;
433
+ }
434
+ });
435
+ compilation.hooks.runtimeRequirementInTree
436
+ .for(RuntimeGlobals.scriptNonce)
437
+ .tap("RuntimePlugin", chunk => {
438
+ compilation.addRuntimeModule(chunk, new NonceRuntimeModule());
439
+ return true;
440
+ });
358
441
  // TODO webpack 6: remove CompatRuntimeModule
359
442
  compilation.hooks.additionalTreeRuntimeRequirements.tap(
360
443
  "RuntimePlugin",
@@ -16,6 +16,7 @@ const { forEachRuntime, subtractRuntime } = require("./util/runtime");
16
16
  /** @typedef {import("../declarations/WebpackOptions").OutputNormalized} OutputOptions */
17
17
  /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
18
18
  /** @typedef {import("./ChunkGraph")} ChunkGraph */
19
+ /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
19
20
  /** @typedef {import("./Compilation")} Compilation */
20
21
  /** @typedef {import("./Dependency")} Dependency */
21
22
  /** @typedef {import("./Module")} Module */
@@ -50,6 +51,27 @@ Module has these incoming connections: ${Array.from(
50
51
  ).join("")}`;
51
52
  };
52
53
 
54
+ /**
55
+ * @param {string|undefined} definition global object definition
56
+ * @returns {string} save to use global object
57
+ */
58
+ function getGlobalObject(definition) {
59
+ if (!definition) return definition;
60
+ const trimmed = definition.trim();
61
+
62
+ if (
63
+ // identifier, we do not need real identifier regarding ECMAScript/Unicode
64
+ trimmed.match(/^[_\p{L}][_0-9\p{L}]*$/iu) ||
65
+ // iife
66
+ // call expression
67
+ // expression in parentheses
68
+ trimmed.match(/^([_\p{L}][_0-9\p{L}]*)?\(.*\)$/iu)
69
+ )
70
+ return trimmed;
71
+
72
+ return `Object(${trimmed})`;
73
+ }
74
+
53
75
  class RuntimeTemplate {
54
76
  /**
55
77
  * @param {Compilation} compilation the compilation
@@ -60,6 +82,8 @@ class RuntimeTemplate {
60
82
  this.compilation = compilation;
61
83
  this.outputOptions = outputOptions || {};
62
84
  this.requestShortener = requestShortener;
85
+ this.globalObject = getGlobalObject(outputOptions.globalObject);
86
+ this.contentHashReplacement = "X".repeat(outputOptions.hashDigestLength);
63
87
  }
64
88
 
65
89
  isIIFE() {
@@ -78,6 +102,10 @@ class RuntimeTemplate {
78
102
  return this.outputOptions.environment.arrowFunction;
79
103
  }
80
104
 
105
+ supportsOptionalChaining() {
106
+ return this.outputOptions.environment.optionalChaining;
107
+ }
108
+
81
109
  supportsForOf() {
82
110
  return this.outputOptions.environment.forOf;
83
111
  }
@@ -99,8 +127,7 @@ class RuntimeTemplate {
99
127
  }
100
128
 
101
129
  supportTemplateLiteral() {
102
- // TODO
103
- return false;
130
+ return this.outputOptions.environment.templateLiteral;
104
131
  }
105
132
 
106
133
  returningFunction(returnValue, args = "") {
@@ -115,6 +142,71 @@ class RuntimeTemplate {
115
142
  : `function(${args}) {\n${Template.indent(body)}\n}`;
116
143
  }
117
144
 
145
+ /**
146
+ * @param {Array<string|{expr: string}>} args args
147
+ * @returns {string} result expression
148
+ */
149
+ concatenation(...args) {
150
+ const len = args.length;
151
+
152
+ if (len === 2) return this._es5Concatenation(args);
153
+ if (len === 0) return '""';
154
+ if (len === 1) {
155
+ return typeof args[0] === "string"
156
+ ? JSON.stringify(args[0])
157
+ : `"" + ${args[0].expr}`;
158
+ }
159
+ if (!this.supportTemplateLiteral()) return this._es5Concatenation(args);
160
+
161
+ // cost comparison between template literal and concatenation:
162
+ // both need equal surroundings: `xxx` vs "xxx"
163
+ // template literal has constant cost of 3 chars for each expression
164
+ // es5 concatenation has cost of 3 + n chars for n expressions in row
165
+ // when a es5 concatenation ends with an expression it reduces cost by 3
166
+ // when a es5 concatenation starts with an single expression it reduces cost by 3
167
+ // e. g. `${a}${b}${c}` (3*3 = 9) is longer than ""+a+b+c ((3+3)-3 = 3)
168
+ // e. g. `x${a}x${b}x${c}x` (3*3 = 9) is shorter than "x"+a+"x"+b+"x"+c+"x" (4+4+4 = 12)
169
+
170
+ let templateCost = 0;
171
+ let concatenationCost = 0;
172
+
173
+ let lastWasExpr = false;
174
+ for (const arg of args) {
175
+ const isExpr = typeof arg !== "string";
176
+ if (isExpr) {
177
+ templateCost += 3;
178
+ concatenationCost += lastWasExpr ? 1 : 4;
179
+ }
180
+ lastWasExpr = isExpr;
181
+ }
182
+ if (lastWasExpr) concatenationCost -= 3;
183
+ if (typeof args[0] !== "string" && typeof args[1] === "string")
184
+ concatenationCost -= 3;
185
+
186
+ if (concatenationCost <= templateCost) return this._es5Concatenation(args);
187
+
188
+ return `\`${args
189
+ .map(arg => (typeof arg === "string" ? arg : `\${${arg.expr}}`))
190
+ .join("")}\``;
191
+ }
192
+
193
+ /**
194
+ * @param {Array<string|{expr: string}>} args args (len >= 2)
195
+ * @returns {string} result expression
196
+ * @private
197
+ */
198
+ _es5Concatenation(args) {
199
+ const str = args
200
+ .map(arg => (typeof arg === "string" ? JSON.stringify(arg) : arg.expr))
201
+ .join(" + ");
202
+
203
+ // when the first two args are expression, we need to prepend "" + to force string
204
+ // concatenation instead of number addition.
205
+ return typeof args[0] !== "string" && typeof args[1] !== "string"
206
+ ? `"" + ${str}`
207
+ : str;
208
+ }
209
+
118
210
  expressionFunction(expression, args = "") {
119
211
  return this.supportsArrowFunction()
120
212
  ? `(${args}) => (${expression})`
@@ -924,6 +1016,26 @@ class RuntimeTemplate {
924
1016
  runtimeRequirements.add(RuntimeGlobals.exports);
925
1017
  return `${RuntimeGlobals.makeNamespaceObject}(${exportsArgument});\n`;
926
1018
  }
1019
+
1020
+ /**
1021
+ * @param {Object} options options object
1022
+ * @param {Module} options.module the module
1023
+ * @param {string} options.publicPath the public path
1024
+ * @param {RuntimeSpec=} options.runtime runtime
1025
+ * @param {CodeGenerationResults} options.codeGenerationResults the code generation results
1026
+ * @returns {string} the url of the asset
1027
+ */
1028
+ assetUrl({ publicPath, runtime, module, codeGenerationResults }) {
1029
+ if (!module) {
1030
+ return "data:,";
1031
+ }
1032
+ const codeGen = codeGenerationResults.get(module, runtime);
1033
+ const { data } = codeGen;
1034
+ const url = data.get("url");
1035
+ if (url) return url.toString();
1036
+ const filename = data.get("filename");
1037
+ return publicPath + filename;
1038
+ }
927
1039
  }
928
1040
 
929
1041
  module.exports = RuntimeTemplate;
package/lib/Template.js CHANGED
@@ -217,7 +217,7 @@ class Template {
217
217
  if (Array.isArray(s)) {
218
218
  return s.map(Template.indent).join("\n");
219
219
  } else {
220
- const str = s.trimRight();
220
+ const str = s.trimEnd();
221
221
  if (!str) return "";
222
222
  const ind = str[0] === "\n" ? "" : "\t";
223
223
  return ind + str.replace(/\n([^\n])/g, "\n\t$1");
@@ -370,7 +370,8 @@ class Template {
370
370
  dependencyTemplates: renderContext.dependencyTemplates,
371
371
  moduleGraph: renderContext.moduleGraph,
372
372
  runtimeTemplate: renderContext.runtimeTemplate,
373
- runtime: renderContext.chunk.runtime
373
+ runtime: renderContext.chunk.runtime,
374
+ codeGenerationResults
374
375
  });
375
376
  if (!codeGenResult) continue;
376
377
  runtimeSource = codeGenResult.sources.get("runtime");
@@ -5,6 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const mime = require("mime-types");
8
9
  const { basename, extname } = require("path");
9
10
  const util = require("util");
10
11
  const Chunk = require("./Chunk");
@@ -117,29 +118,53 @@ const replacePathVariables = (path, data, assetInfo) => {
117
118
  // [name] - file
118
119
  // [ext] - .js
119
120
  if (typeof data.filename === "string") {
120
- const { path: file, query, fragment } = parseResource(data.filename);
121
-
122
- const ext = extname(file);
123
- const base = basename(file);
124
- const name = base.slice(0, base.length - ext.length);
125
- const path = file.slice(0, file.length - base.length);
126
-
127
- replacements.set("file", replacer(file));
128
- replacements.set("query", replacer(query, true));
129
- replacements.set("fragment", replacer(fragment, true));
130
- replacements.set("path", replacer(path, true));
131
- replacements.set("base", replacer(base));
132
- replacements.set("name", replacer(name));
133
- replacements.set("ext", replacer(ext, true));
134
- // Legacy
135
- replacements.set(
136
- "filebase",
137
- deprecated(
138
- replacer(base),
139
- "[filebase] is now [base]",
140
- "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_FILENAME"
141
- )
142
- );
121
+ // check that filename is data uri
122
+ let match = data.filename.match(/^data:([^;,]+)/);
123
+ if (match) {
124
+ const ext = mime.extension(match[1]);
125
+ const emptyReplacer = replacer("", true);
126
+
127
+ replacements.set("file", emptyReplacer);
128
+ replacements.set("query", emptyReplacer);
129
+ replacements.set("fragment", emptyReplacer);
130
+ replacements.set("path", emptyReplacer);
131
+ replacements.set("base", emptyReplacer);
132
+ replacements.set("name", emptyReplacer);
133
+ replacements.set("ext", replacer(ext ? `.${ext}` : "", true));
134
+ // Legacy
135
+ replacements.set(
136
+ "filebase",
137
+ deprecated(
138
+ emptyReplacer,
139
+ "[filebase] is now [base]",
140
+ "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_FILENAME"
141
+ )
142
+ );
143
+ } else {
144
+ const { path: file, query, fragment } = parseResource(data.filename);
145
+
146
+ const ext = extname(file);
147
+ const base = basename(file);
148
+ const name = base.slice(0, base.length - ext.length);
149
+ const path = file.slice(0, file.length - base.length);
150
+
151
+ replacements.set("file", replacer(file));
152
+ replacements.set("query", replacer(query, true));
153
+ replacements.set("fragment", replacer(fragment, true));
154
+ replacements.set("path", replacer(path, true));
155
+ replacements.set("base", replacer(base));
156
+ replacements.set("name", replacer(name));
157
+ replacements.set("ext", replacer(ext, true));
158
+ // Legacy
159
+ replacements.set(
160
+ "filebase",
161
+ deprecated(
162
+ replacer(base),
163
+ "[filebase] is now [base]",
164
+ "DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_FILENAME"
165
+ )
166
+ );
167
+ }
143
168
  }
144
169
 
145
170
  // Compilation context
@@ -5,6 +5,7 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ const { groupBy } = require("./util/ArrayHelpers");
8
9
  const createSchemaValidation = require("./util/create-schema-validation");
9
10
 
10
11
  /** @typedef {import("../declarations/plugins/WatchIgnorePlugin").WatchIgnorePluginOptions} WatchIgnorePluginOptions */
@@ -40,14 +41,12 @@ class IgnoringWatchFileSystem {
40
41
  p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0
41
42
  );
42
43
 
43
- const notIgnored = path => !ignored(path);
44
-
45
- const ignoredFiles = files.filter(ignored);
46
- const ignoredDirs = dirs.filter(ignored);
44
+ const [ignoredFiles, notIgnoredFiles] = groupBy(files, ignored);
45
+ const [ignoredDirs, notIgnoredDirs] = groupBy(dirs, ignored);
47
46
 
48
47
  const watcher = this.wfs.watch(
49
- files.filter(notIgnored),
50
- dirs.filter(notIgnored),
48
+ notIgnoredFiles,
49
+ notIgnoredDirs,
51
50
  missing,
52
51
  startTime,
53
52
  options,
@@ -88,7 +87,20 @@ class IgnoringWatchFileSystem {
88
87
  fileTimestamps.set(path, IGNORE_TIME_ENTRY);
89
88
  }
90
89
  return fileTimestamps;
91
- }
90
+ },
91
+ getInfo:
92
+ watcher.getInfo &&
93
+ (() => {
94
+ const info = watcher.getInfo();
95
+ const { fileTimeInfoEntries, contextTimeInfoEntries } = info;
96
+ for (const path of ignoredFiles) {
97
+ fileTimeInfoEntries.set(path, IGNORE_TIME_ENTRY);
98
+ }
99
+ for (const path of ignoredDirs) {
100
+ contextTimeInfoEntries.set(path, IGNORE_TIME_ENTRY);
101
+ }
102
+ return info;
103
+ })
92
104
  };
93
105
  }
94
106
  }
package/lib/Watching.js CHANGED
@@ -15,7 +15,7 @@ const Stats = require("./Stats");
15
15
  /**
16
16
  * @template T
17
17
  * @callback Callback
18
- * @param {Error=} err
18
+ * @param {(Error | null)=} err
19
19
  * @param {T=} result
20
20
  */
21
21
 
@@ -49,7 +49,7 @@ class Watching {
49
49
  this.watchOptions = {};
50
50
  }
51
51
  if (typeof this.watchOptions.aggregateTimeout !== "number") {
52
- this.watchOptions.aggregateTimeout = 200;
52
+ this.watchOptions.aggregateTimeout = 20;
53
53
  }
54
54
  this.compiler = compiler;
55
55
  this.running = false;
@@ -109,30 +109,44 @@ class Watching {
109
109
  this.lastWatcherStartTime = Date.now();
110
110
  }
111
111
  this.compiler.fsStartTime = Date.now();
112
- this._mergeWithCollected(
113
- changedFiles ||
114
- (this.pausedWatcher &&
112
+ if (
113
+ changedFiles &&
114
+ removedFiles &&
115
+ fileTimeInfoEntries &&
116
+ contextTimeInfoEntries
117
+ ) {
118
+ this._mergeWithCollected(changedFiles, removedFiles);
119
+ this.compiler.fileTimestamps = fileTimeInfoEntries;
120
+ this.compiler.contextTimestamps = contextTimeInfoEntries;
121
+ } else if (this.pausedWatcher) {
122
+ if (this.pausedWatcher.getInfo) {
123
+ const {
124
+ changes,
125
+ removals,
126
+ fileTimeInfoEntries,
127
+ contextTimeInfoEntries
128
+ } = this.pausedWatcher.getInfo();
129
+ this._mergeWithCollected(changes, removals);
130
+ this.compiler.fileTimestamps = fileTimeInfoEntries;
131
+ this.compiler.contextTimestamps = contextTimeInfoEntries;
132
+ } else {
133
+ this._mergeWithCollected(
115
134
  this.pausedWatcher.getAggregatedChanges &&
116
- this.pausedWatcher.getAggregatedChanges()),
117
- (this.compiler.removedFiles =
118
- removedFiles ||
119
- (this.pausedWatcher &&
135
+ this.pausedWatcher.getAggregatedChanges(),
120
136
  this.pausedWatcher.getAggregatedRemovals &&
121
- this.pausedWatcher.getAggregatedRemovals()))
122
- );
123
-
137
+ this.pausedWatcher.getAggregatedRemovals()
138
+ );
139
+ this.compiler.fileTimestamps =
140
+ this.pausedWatcher.getFileTimeInfoEntries();
141
+ this.compiler.contextTimestamps =
142
+ this.pausedWatcher.getContextTimeInfoEntries();
143
+ }
144
+ }
124
145
  this.compiler.modifiedFiles = this._collectedChangedFiles;
125
146
  this._collectedChangedFiles = undefined;
126
147
  this.compiler.removedFiles = this._collectedRemovedFiles;
127
148
  this._collectedRemovedFiles = undefined;
128
149
 
129
- this.compiler.fileTimestamps =
130
- fileTimeInfoEntries ||
131
- (this.pausedWatcher && this.pausedWatcher.getFileTimeInfoEntries());
132
- this.compiler.contextTimestamps =
133
- contextTimeInfoEntries ||
134
- (this.pausedWatcher && this.pausedWatcher.getContextTimeInfoEntries());
135
-
136
150
  const run = () => {
137
151
  if (this.compiler.idle) {
138
152
  return this.compiler.cache.endIdle(err => {
@@ -35,6 +35,7 @@ const ResolverCachePlugin = require("./cache/ResolverCachePlugin");
35
35
 
36
36
  const CommonJsPlugin = require("./dependencies/CommonJsPlugin");
37
37
  const HarmonyModulesPlugin = require("./dependencies/HarmonyModulesPlugin");
38
+ const ImportMetaContextPlugin = require("./dependencies/ImportMetaContextPlugin");
38
39
  const ImportMetaPlugin = require("./dependencies/ImportMetaPlugin");
39
40
  const ImportPlugin = require("./dependencies/ImportPlugin");
40
41
  const LoaderPlugin = require("./dependencies/LoaderPlugin");
@@ -118,11 +119,69 @@ class WebpackOptionsApply extends OptionsApply {
118
119
  if (options.externalsPresets.webAsync) {
119
120
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
120
121
  const ExternalsPlugin = require("./ExternalsPlugin");
121
- new ExternalsPlugin("import", /^(https?:\/\/|std:)/).apply(compiler);
122
+ new ExternalsPlugin(
123
+ "import",
124
+ options.experiments.css
125
+ ? ({ request, dependencyType }, callback) => {
126
+ if (dependencyType === "url") {
127
+ if (/^(\/\/|https?:\/\/)/.test(request))
128
+ return callback(null, `asset ${request}`);
129
+ } else if (dependencyType === "css-import") {
130
+ if (/^(\/\/|https?:\/\/)/.test(request))
131
+ return callback(null, `css-import ${request}`);
132
+ } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
133
+ if (/^\.css(\?|$)/.test(request))
134
+ return callback(null, `css-import ${request}`);
135
+ return callback(null, `import ${request}`);
136
+ }
137
+ callback();
138
+ }
139
+ : /^(\/\/|https?:\/\/|std:)/
140
+ ).apply(compiler);
122
141
  } else if (options.externalsPresets.web) {
123
142
  //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
124
143
  const ExternalsPlugin = require("./ExternalsPlugin");
125
- new ExternalsPlugin("module", /^(https?:\/\/|std:)/).apply(compiler);
144
+ new ExternalsPlugin(
145
+ "module",
146
+ options.experiments.css
147
+ ? ({ request, dependencyType }, callback) => {
148
+ if (dependencyType === "url") {
149
+ if (/^(\/\/|https?:\/\/)/.test(request))
150
+ return callback(null, `asset ${request}`);
151
+ } else if (dependencyType === "css-import") {
152
+ if (/^(\/\/|https?:\/\/)/.test(request))
153
+ return callback(null, `css-import ${request}`);
154
+ } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
155
+ if (/^\.css(\?|$)/.test(request))
156
+ return callback(null, `css-import ${request}`);
157
+ return callback(null, `module ${request}`);
158
+ }
159
+ callback();
160
+ }
161
+ : /^(\/\/|https?:\/\/|std:)/
162
+ ).apply(compiler);
163
+ } else if (options.externalsPresets.node) {
164
+ if (options.experiments.css) {
165
+ //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
166
+ const ExternalsPlugin = require("./ExternalsPlugin");
167
+ new ExternalsPlugin(
168
+ "module",
169
+ ({ request, dependencyType }, callback) => {
170
+ if (dependencyType === "url") {
171
+ if (/^(\/\/|https?:\/\/)/.test(request))
172
+ return callback(null, `asset ${request}`);
173
+ } else if (dependencyType === "css-import") {
174
+ if (/^(\/\/|https?:\/\/)/.test(request))
175
+ return callback(null, `css-import ${request}`);
176
+ } else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
177
+ if (/^\.css(\?|$)/.test(request))
178
+ return callback(null, `css-import ${request}`);
179
+ return callback(null, `module ${request}`);
180
+ }
181
+ callback();
182
+ }
183
+ ).apply(compiler);
184
+ }
126
185
  }
127
186
 
128
187
  new ChunkPrefetchPreloadPlugin().apply(compiler);
@@ -253,6 +312,11 @@ class WebpackOptionsApply extends OptionsApply {
253
312
  }).apply(compiler);
254
313
  }
255
314
 
315
+ if (options.experiments.css) {
316
+ const CssModulesPlugin = require("./css/CssModulesPlugin");
317
+ new CssModulesPlugin(options.experiments.css).apply(compiler);
318
+ }
319
+
256
320
  if (options.experiments.lazyCompilation) {
257
321
  const LazyCompilationPlugin = require("./hmr/LazyCompilationPlugin");
258
322
  const lazyOptions =
@@ -261,15 +325,18 @@ class WebpackOptionsApply extends OptionsApply {
261
325
  : null;
262
326
  new LazyCompilationPlugin({
263
327
  backend:
264
- (lazyOptions && lazyOptions.backend) ||
265
- require("./hmr/lazyCompilationBackend"),
266
- client:
267
- (lazyOptions && lazyOptions.client) ||
268
- require.resolve(
269
- `../hot/lazy-compilation-${
270
- options.externalsPresets.node ? "node" : "web"
271
- }.js`
272
- ),
328
+ typeof lazyOptions.backend === "function"
329
+ ? lazyOptions.backend
330
+ : require("./hmr/lazyCompilationBackend")({
331
+ ...lazyOptions.backend,
332
+ client:
333
+ (lazyOptions.backend && lazyOptions.backend.client) ||
334
+ require.resolve(
335
+ `../hot/lazy-compilation-${
336
+ options.externalsPresets.node ? "node" : "web"
337
+ }.js`
338
+ )
339
+ }),
273
340
  entries: !lazyOptions || lazyOptions.entries !== false,
274
341
  imports: !lazyOptions || lazyOptions.imports !== false,
275
342
  test: (lazyOptions && lazyOptions.test) || undefined
@@ -317,6 +384,7 @@ class WebpackOptionsApply extends OptionsApply {
317
384
  new RequireEnsurePlugin().apply(compiler);
318
385
  new RequireContextPlugin().apply(compiler);
319
386
  new ImportPlugin().apply(compiler);
387
+ new ImportMetaContextPlugin().apply(compiler);
320
388
  new SystemPlugin().apply(compiler);
321
389
  new ImportMetaPlugin().apply(compiler);
322
390
  new URLPlugin().apply(compiler);