webpack 5.59.0 → 5.71.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 (209) hide show
  1. package/README.md +1 -7
  2. package/hot/lazy-compilation-node.js +3 -1
  3. package/hot/poll.js +1 -1
  4. package/hot/signal.js +1 -1
  5. package/lib/APIPlugin.js +33 -0
  6. package/lib/BannerPlugin.js +10 -4
  7. package/lib/Cache.js +1 -1
  8. package/lib/CacheFacade.js +4 -11
  9. package/lib/Chunk.js +6 -3
  10. package/lib/ChunkGraph.js +1 -2
  11. package/lib/ChunkGroup.js +1 -1
  12. package/lib/CleanPlugin.js +81 -20
  13. package/lib/Compilation.js +179 -91
  14. package/lib/Compiler.js +86 -17
  15. package/lib/ConstPlugin.js +2 -2
  16. package/lib/ContextModule.js +142 -51
  17. package/lib/ContextModuleFactory.js +65 -25
  18. package/lib/DelegatedModule.js +1 -1
  19. package/lib/DelegatedModuleFactoryPlugin.js +1 -1
  20. package/lib/Dependency.js +17 -0
  21. package/lib/DependencyTemplate.js +9 -0
  22. package/lib/DependencyTemplates.js +1 -1
  23. package/lib/DllModule.js +1 -1
  24. package/lib/EntryOptionPlugin.js +2 -0
  25. package/lib/ErrorHelpers.js +2 -2
  26. package/lib/EvalDevToolModulePlugin.js +16 -1
  27. package/lib/EvalSourceMapDevToolPlugin.js +18 -1
  28. package/lib/ExportsInfo.js +4 -4
  29. package/lib/ExternalModule.js +94 -54
  30. package/lib/ExternalModuleFactoryPlugin.js +5 -5
  31. package/lib/FileSystemInfo.js +89 -44
  32. package/lib/Generator.js +3 -0
  33. package/lib/HookWebpackError.js +1 -1
  34. package/lib/HotModuleReplacementPlugin.js +3 -1
  35. package/lib/LoaderOptionsPlugin.js +1 -1
  36. package/lib/Module.js +27 -4
  37. package/lib/ModuleFilenameHelpers.js +8 -4
  38. package/lib/ModuleHashingError.js +29 -0
  39. package/lib/MultiCompiler.js +1 -1
  40. package/lib/MultiWatching.js +1 -1
  41. package/lib/NodeStuffPlugin.js +10 -0
  42. package/lib/NormalModule.js +41 -26
  43. package/lib/NormalModuleFactory.js +42 -37
  44. package/lib/ProgressPlugin.js +4 -5
  45. package/lib/RawModule.js +1 -1
  46. package/lib/RuntimeGlobals.js +29 -1
  47. package/lib/RuntimeModule.js +1 -1
  48. package/lib/RuntimePlugin.js +77 -1
  49. package/lib/RuntimeTemplate.js +114 -2
  50. package/lib/Template.js +2 -1
  51. package/lib/TemplatedPathPlugin.js +48 -23
  52. package/lib/WatchIgnorePlugin.js +19 -7
  53. package/lib/Watching.js +33 -19
  54. package/lib/WebpackOptionsApply.js +57 -11
  55. package/lib/asset/AssetGenerator.js +193 -63
  56. package/lib/asset/AssetModulesPlugin.js +3 -0
  57. package/lib/asset/RawDataUrlModule.js +148 -0
  58. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -4
  59. package/lib/buildChunkGraph.js +36 -6
  60. package/lib/cache/PackFileCacheStrategy.js +7 -4
  61. package/lib/cache/ResolverCachePlugin.js +90 -29
  62. package/lib/cli.js +44 -3
  63. package/lib/config/browserslistTargetHandler.js +41 -6
  64. package/lib/config/defaults.js +115 -19
  65. package/lib/config/normalization.js +9 -0
  66. package/lib/config/target.js +10 -0
  67. package/lib/container/ContainerEntryModule.js +8 -5
  68. package/lib/container/FallbackModule.js +4 -4
  69. package/lib/container/RemoteModule.js +4 -2
  70. package/lib/css/CssExportsGenerator.js +139 -0
  71. package/lib/css/CssGenerator.js +109 -0
  72. package/lib/css/CssLoadingRuntimeModule.js +440 -0
  73. package/lib/css/CssModulesPlugin.js +462 -0
  74. package/lib/css/CssParser.js +618 -0
  75. package/lib/css/walkCssTokens.js +659 -0
  76. package/lib/debug/ProfilingPlugin.js +24 -21
  77. package/lib/dependencies/AMDRequireDependency.js +6 -6
  78. package/lib/dependencies/CommonJsExportsParserPlugin.js +1 -2
  79. package/lib/dependencies/CommonJsFullRequireDependency.js +5 -1
  80. package/lib/dependencies/CommonJsImportsParserPlugin.js +5 -3
  81. package/lib/dependencies/CommonJsRequireContextDependency.js +5 -1
  82. package/lib/dependencies/ContextDependency.js +1 -0
  83. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  84. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +4 -1
  85. package/lib/dependencies/ContextElementDependency.js +41 -3
  86. package/lib/dependencies/CreateScriptUrlDependency.js +12 -0
  87. package/lib/dependencies/CssExportDependency.js +85 -0
  88. package/lib/dependencies/CssImportDependency.js +75 -0
  89. package/lib/dependencies/CssLocalIdentifierDependency.js +119 -0
  90. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +101 -0
  91. package/lib/dependencies/CssUrlDependency.js +132 -0
  92. package/lib/dependencies/ExportsInfoDependency.js +6 -0
  93. package/lib/dependencies/HarmonyAcceptImportDependency.js +5 -3
  94. package/lib/dependencies/HarmonyCompatibilityDependency.js +5 -5
  95. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +95 -0
  96. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +12 -3
  97. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +25 -17
  98. package/lib/dependencies/HarmonyExportInitFragment.js +4 -1
  99. package/lib/dependencies/HarmonyImportDependency.js +23 -0
  100. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +142 -45
  101. package/lib/dependencies/HarmonyImportSpecifierDependency.js +46 -22
  102. package/lib/dependencies/HarmonyModulesPlugin.js +10 -0
  103. package/lib/dependencies/ImportContextDependency.js +0 -2
  104. package/lib/dependencies/ImportMetaContextDependency.js +35 -0
  105. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +252 -0
  106. package/lib/dependencies/ImportMetaContextPlugin.js +59 -0
  107. package/lib/dependencies/ImportMetaPlugin.js +22 -3
  108. package/lib/dependencies/LoaderPlugin.js +4 -2
  109. package/lib/dependencies/RequireContextDependency.js +0 -16
  110. package/lib/dependencies/RequireEnsureDependency.js +2 -2
  111. package/lib/dependencies/URLDependency.js +3 -8
  112. package/lib/dependencies/URLPlugin.js +1 -1
  113. package/lib/esm/ModuleChunkFormatPlugin.js +74 -49
  114. package/lib/esm/ModuleChunkLoadingPlugin.js +3 -1
  115. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +25 -9
  116. package/lib/hmr/HotModuleReplacement.runtime.js +29 -14
  117. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +4 -3
  118. package/lib/hmr/LazyCompilationPlugin.js +54 -26
  119. package/lib/hmr/lazyCompilationBackend.js +51 -12
  120. package/lib/ids/DeterministicModuleIdsPlugin.js +55 -35
  121. package/lib/ids/HashedModuleIdsPlugin.js +11 -14
  122. package/lib/ids/IdHelpers.js +25 -11
  123. package/lib/ids/NamedModuleIdsPlugin.js +6 -9
  124. package/lib/ids/NaturalModuleIdsPlugin.js +10 -13
  125. package/lib/ids/OccurrenceModuleIdsPlugin.js +13 -10
  126. package/lib/ids/SyncModuleIdsPlugin.js +140 -0
  127. package/lib/index.js +13 -0
  128. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +2 -2
  129. package/lib/javascript/BasicEvaluatedExpression.js +5 -2
  130. package/lib/javascript/ChunkHelpers.js +33 -0
  131. package/lib/javascript/JavascriptGenerator.js +1 -0
  132. package/lib/javascript/JavascriptModulesPlugin.js +27 -2
  133. package/lib/javascript/JavascriptParser.js +82 -48
  134. package/lib/javascript/StartupHelpers.js +7 -30
  135. package/lib/library/AssignLibraryPlugin.js +39 -15
  136. package/lib/library/EnableLibraryPlugin.js +11 -0
  137. package/lib/library/UmdLibraryPlugin.js +5 -3
  138. package/lib/node/NodeTargetPlugin.js +3 -0
  139. package/lib/node/NodeWatchFileSystem.js +85 -31
  140. package/lib/node/ReadFileChunkLoadingRuntimeModule.js +23 -8
  141. package/lib/node/RequireChunkLoadingRuntimeModule.js +24 -9
  142. package/lib/optimize/ConcatenatedModule.js +21 -9
  143. package/lib/optimize/ModuleConcatenationPlugin.js +5 -2
  144. package/lib/optimize/SplitChunksPlugin.js +8 -1
  145. package/lib/runtime/AsyncModuleRuntimeModule.js +27 -17
  146. package/lib/runtime/BaseUriRuntimeModule.js +31 -0
  147. package/lib/runtime/CreateScriptRuntimeModule.js +36 -0
  148. package/lib/runtime/CreateScriptUrlRuntimeModule.js +9 -34
  149. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +76 -0
  150. package/lib/schemes/HttpUriPlugin.js +77 -14
  151. package/lib/serialization/FileMiddleware.js +44 -9
  152. package/lib/sharing/ConsumeSharedModule.js +8 -2
  153. package/lib/sharing/ConsumeSharedRuntimeModule.js +26 -5
  154. package/lib/sharing/ProvideSharedModule.js +4 -2
  155. package/lib/sharing/ShareRuntimeModule.js +1 -1
  156. package/lib/sharing/utils.js +1 -1
  157. package/lib/stats/DefaultStatsFactoryPlugin.js +113 -68
  158. package/lib/stats/DefaultStatsPrinterPlugin.js +89 -24
  159. package/lib/util/ArrayHelpers.js +30 -0
  160. package/lib/util/AsyncQueue.js +1 -1
  161. package/lib/util/compileBooleanMatcher.js +1 -1
  162. package/lib/util/create-schema-validation.js +9 -2
  163. package/lib/util/createHash.js +12 -0
  164. package/lib/util/deprecation.js +10 -2
  165. package/lib/util/deterministicGrouping.js +1 -1
  166. package/lib/util/extractUrlAndGlobal.js +3 -0
  167. package/lib/util/fs.js +11 -0
  168. package/lib/util/hash/BatchedHash.js +7 -4
  169. package/lib/util/hash/md4.js +20 -0
  170. package/lib/util/hash/wasm-hash.js +163 -0
  171. package/lib/util/hash/xxhash64.js +5 -139
  172. package/lib/util/identifier.js +65 -44
  173. package/lib/util/internalSerializables.js +15 -0
  174. package/lib/util/nonNumericOnlyHash.js +22 -0
  175. package/lib/util/semver.js +17 -10
  176. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +9 -3
  177. package/lib/web/JsonpChunkLoadingRuntimeModule.js +20 -9
  178. package/lib/webpack.js +10 -3
  179. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -11
  180. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +33 -22
  181. package/module.d.ts +215 -0
  182. package/package.json +23 -28
  183. package/schemas/WebpackOptions.check.js +1 -1
  184. package/schemas/WebpackOptions.json +254 -29
  185. package/schemas/plugins/DllReferencePlugin.check.js +1 -1
  186. package/schemas/plugins/HashedModuleIdsPlugin.check.js +1 -1
  187. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  188. package/schemas/plugins/asset/AssetGeneratorOptions.check.js +1 -1
  189. package/schemas/plugins/asset/AssetParserOptions.check.js +1 -1
  190. package/schemas/plugins/asset/AssetResourceGeneratorOptions.check.js +1 -1
  191. package/schemas/plugins/container/ContainerPlugin.check.js +1 -1
  192. package/schemas/plugins/container/ContainerPlugin.json +2 -1
  193. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  194. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  195. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  196. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  197. package/schemas/plugins/container/ModuleFederationPlugin.json +3 -1
  198. package/schemas/plugins/css/CssGeneratorOptions.check.d.ts +7 -0
  199. package/schemas/plugins/css/CssGeneratorOptions.check.js +6 -0
  200. package/schemas/plugins/css/CssGeneratorOptions.json +3 -0
  201. package/schemas/plugins/css/CssParserOptions.check.d.ts +7 -0
  202. package/schemas/plugins/css/CssParserOptions.check.js +6 -0
  203. package/schemas/plugins/css/CssParserOptions.json +3 -0
  204. package/schemas/plugins/optimize/AggressiveSplittingPlugin.check.js +1 -1
  205. package/schemas/plugins/optimize/LimitChunkCountPlugin.check.js +1 -1
  206. package/schemas/plugins/optimize/MinChunkSizePlugin.check.js +1 -1
  207. package/schemas/plugins/schemes/HttpUriPlugin.check.js +1 -1
  208. package/schemas/plugins/schemes/HttpUriPlugin.json +4 -0
  209. package/types.d.ts +628 -179
@@ -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
@@ -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,47 @@ 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);
126
163
  }
127
164
 
128
165
  new ChunkPrefetchPreloadPlugin().apply(compiler);
@@ -253,6 +290,11 @@ class WebpackOptionsApply extends OptionsApply {
253
290
  }).apply(compiler);
254
291
  }
255
292
 
293
+ if (options.experiments.css) {
294
+ const CssModulesPlugin = require("./css/CssModulesPlugin");
295
+ new CssModulesPlugin(options.experiments.css).apply(compiler);
296
+ }
297
+
256
298
  if (options.experiments.lazyCompilation) {
257
299
  const LazyCompilationPlugin = require("./hmr/LazyCompilationPlugin");
258
300
  const lazyOptions =
@@ -261,15 +303,18 @@ class WebpackOptionsApply extends OptionsApply {
261
303
  : null;
262
304
  new LazyCompilationPlugin({
263
305
  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
- ),
306
+ typeof lazyOptions.backend === "function"
307
+ ? lazyOptions.backend
308
+ : require("./hmr/lazyCompilationBackend")({
309
+ ...lazyOptions.backend,
310
+ client:
311
+ (lazyOptions.backend && lazyOptions.backend.client) ||
312
+ require.resolve(
313
+ `../hot/lazy-compilation-${
314
+ options.externalsPresets.node ? "node" : "web"
315
+ }.js`
316
+ )
317
+ }),
273
318
  entries: !lazyOptions || lazyOptions.entries !== false,
274
319
  imports: !lazyOptions || lazyOptions.imports !== false,
275
320
  test: (lazyOptions && lazyOptions.test) || undefined
@@ -317,6 +362,7 @@ class WebpackOptionsApply extends OptionsApply {
317
362
  new RequireEnsurePlugin().apply(compiler);
318
363
  new RequireContextPlugin().apply(compiler);
319
364
  new ImportPlugin().apply(compiler);
365
+ new ImportMetaContextPlugin().apply(compiler);
320
366
  new SystemPlugin().apply(compiler);
321
367
  new ImportMetaPlugin().apply(compiler);
322
368
  new URLPlugin().apply(compiler);