webpack 5.102.1 → 5.104.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/README.md +121 -134
  2. package/hot/dev-server.js +18 -3
  3. package/hot/emitter-event-target.js +7 -0
  4. package/hot/lazy-compilation-node.js +45 -29
  5. package/hot/lazy-compilation-universal.js +18 -0
  6. package/hot/lazy-compilation-web.js +15 -5
  7. package/hot/load-http.js +7 -0
  8. package/hot/only-dev-server.js +19 -4
  9. package/lib/APIPlugin.js +6 -0
  10. package/lib/Chunk.js +1 -1
  11. package/lib/ChunkGraph.js +9 -7
  12. package/lib/ChunkGroup.js +8 -5
  13. package/lib/CleanPlugin.js +6 -3
  14. package/lib/CodeGenerationResults.js +2 -1
  15. package/lib/CompatibilityPlugin.js +28 -2
  16. package/lib/Compilation.js +58 -21
  17. package/lib/Compiler.js +3 -3
  18. package/lib/ConcatenationScope.js +0 -15
  19. package/lib/ContextModule.js +6 -3
  20. package/lib/ContextModuleFactory.js +6 -4
  21. package/lib/CssModule.js +6 -1
  22. package/lib/DefinePlugin.js +45 -14
  23. package/lib/DelegatedModule.js +7 -4
  24. package/lib/Dependency.js +8 -1
  25. package/lib/DependencyTemplate.js +1 -0
  26. package/lib/DllModule.js +6 -3
  27. package/lib/DotenvPlugin.js +462 -0
  28. package/lib/EnvironmentPlugin.js +19 -16
  29. package/lib/EvalSourceMapDevToolPlugin.js +16 -0
  30. package/lib/ExportsInfo.js +6 -2
  31. package/lib/ExternalModule.js +28 -35
  32. package/lib/ExternalModuleFactoryPlugin.js +11 -9
  33. package/lib/ExternalsPlugin.js +2 -1
  34. package/lib/FileSystemInfo.js +1 -1
  35. package/lib/Generator.js +10 -7
  36. package/lib/HookWebpackError.js +33 -4
  37. package/lib/HotModuleReplacementPlugin.js +22 -0
  38. package/lib/ManifestPlugin.js +235 -0
  39. package/lib/Module.js +27 -15
  40. package/lib/ModuleBuildError.js +1 -1
  41. package/lib/ModuleError.js +1 -1
  42. package/lib/ModuleFilenameHelpers.js +1 -1
  43. package/lib/ModuleGraph.js +29 -13
  44. package/lib/ModuleGraphConnection.js +2 -2
  45. package/lib/ModuleSourceTypeConstants.js +189 -0
  46. package/lib/ModuleTypeConstants.js +1 -4
  47. package/lib/ModuleWarning.js +1 -1
  48. package/lib/MultiCompiler.js +1 -1
  49. package/lib/NodeStuffPlugin.js +424 -116
  50. package/lib/NormalModule.js +23 -20
  51. package/lib/NormalModuleFactory.js +7 -10
  52. package/lib/Parser.js +1 -1
  53. package/lib/RawModule.js +7 -4
  54. package/lib/RuntimeGlobals.js +22 -4
  55. package/lib/RuntimeModule.js +1 -1
  56. package/lib/RuntimePlugin.js +27 -6
  57. package/lib/RuntimeTemplate.js +120 -57
  58. package/lib/SourceMapDevToolPlugin.js +26 -1
  59. package/lib/Template.js +17 -6
  60. package/lib/TemplatedPathPlugin.js +5 -6
  61. package/lib/WebpackError.js +0 -1
  62. package/lib/WebpackOptionsApply.js +67 -15
  63. package/lib/asset/AssetBytesGenerator.js +16 -12
  64. package/lib/asset/AssetGenerator.js +31 -26
  65. package/lib/asset/AssetSourceGenerator.js +16 -12
  66. package/lib/asset/RawDataUrlModule.js +6 -3
  67. package/lib/buildChunkGraph.js +4 -2
  68. package/lib/cache/PackFileCacheStrategy.js +6 -5
  69. package/lib/cli.js +2 -43
  70. package/lib/config/browserslistTargetHandler.js +24 -0
  71. package/lib/config/defaults.js +226 -61
  72. package/lib/config/normalization.js +4 -3
  73. package/lib/config/target.js +11 -0
  74. package/lib/container/ContainerEntryModule.js +6 -3
  75. package/lib/container/FallbackModule.js +6 -3
  76. package/lib/container/RemoteModule.js +1 -3
  77. package/lib/css/CssGenerator.js +304 -76
  78. package/lib/css/CssLoadingRuntimeModule.js +14 -4
  79. package/lib/css/CssMergeStyleSheetsRuntimeModule.js +56 -0
  80. package/lib/css/CssModulesPlugin.js +72 -67
  81. package/lib/css/CssParser.js +1726 -732
  82. package/lib/css/walkCssTokens.js +128 -11
  83. package/lib/dependencies/CachedConstDependency.js +24 -10
  84. package/lib/dependencies/CommonJsImportsParserPlugin.js +0 -9
  85. package/lib/dependencies/CommonJsPlugin.js +12 -0
  86. package/lib/dependencies/CommonJsRequireContextDependency.js +1 -1
  87. package/lib/dependencies/ContextDependencyHelpers.js +2 -2
  88. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +3 -1
  89. package/lib/dependencies/CssIcssExportDependency.js +389 -12
  90. package/lib/dependencies/CssIcssImportDependency.js +114 -51
  91. package/lib/dependencies/CssIcssSymbolDependency.js +31 -33
  92. package/lib/dependencies/CssImportDependency.js +17 -6
  93. package/lib/dependencies/CssUrlDependency.js +3 -2
  94. package/lib/dependencies/DynamicExports.js +7 -7
  95. package/lib/dependencies/ExternalModuleDependency.js +7 -4
  96. package/lib/dependencies/ExternalModuleInitFragment.js +3 -2
  97. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +96 -0
  98. package/lib/dependencies/HarmonyAcceptDependency.js +6 -1
  99. package/lib/dependencies/HarmonyAcceptImportDependency.js +2 -1
  100. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +12 -1
  101. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +35 -23
  102. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +13 -9
  103. package/lib/dependencies/HarmonyExports.js +4 -4
  104. package/lib/dependencies/HarmonyImportDependency.js +28 -27
  105. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +28 -69
  106. package/lib/dependencies/HarmonyImportSideEffectDependency.js +4 -3
  107. package/lib/dependencies/HarmonyImportSpecifierDependency.js +10 -8
  108. package/lib/dependencies/ImportDependency.js +8 -2
  109. package/lib/dependencies/ImportEagerDependency.js +6 -3
  110. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +1 -1
  111. package/lib/dependencies/ImportMetaPlugin.js +154 -9
  112. package/lib/dependencies/ImportParserPlugin.js +21 -23
  113. package/lib/dependencies/ImportPhase.js +121 -0
  114. package/lib/dependencies/ImportWeakDependency.js +6 -3
  115. package/lib/dependencies/LocalModulesHelpers.js +3 -3
  116. package/lib/dependencies/ModuleDependency.js +5 -1
  117. package/lib/dependencies/ModuleHotAcceptDependency.js +1 -1
  118. package/lib/dependencies/WorkerPlugin.js +2 -2
  119. package/lib/dependencies/getFunctionExpression.js +1 -1
  120. package/lib/esm/ExportWebpackRequireRuntimeModule.js +1 -8
  121. package/lib/esm/ModuleChunkFormatPlugin.js +5 -4
  122. package/lib/hmr/HotModuleReplacement.runtime.js +2 -1
  123. package/lib/hmr/LazyCompilationPlugin.js +5 -3
  124. package/lib/ids/IdHelpers.js +20 -8
  125. package/lib/index.js +6 -0
  126. package/lib/javascript/ChunkHelpers.js +16 -5
  127. package/lib/javascript/JavascriptGenerator.js +105 -104
  128. package/lib/javascript/JavascriptModulesPlugin.js +80 -37
  129. package/lib/javascript/JavascriptParser.js +161 -44
  130. package/lib/json/JsonGenerator.js +5 -4
  131. package/lib/json/JsonParser.js +9 -2
  132. package/lib/library/AbstractLibraryPlugin.js +1 -1
  133. package/lib/library/AmdLibraryPlugin.js +4 -1
  134. package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
  135. package/lib/library/ModuleLibraryPlugin.js +41 -23
  136. package/lib/library/SystemLibraryPlugin.js +8 -1
  137. package/lib/library/UmdLibraryPlugin.js +2 -2
  138. package/lib/logging/Logger.js +5 -4
  139. package/lib/logging/createConsoleLogger.js +2 -2
  140. package/lib/node/NodeTargetPlugin.js +9 -1
  141. package/lib/node/ReadFileCompileWasmPlugin.js +0 -2
  142. package/lib/optimize/ConcatenatedModule.js +208 -167
  143. package/lib/optimize/ModuleConcatenationPlugin.js +5 -4
  144. package/lib/optimize/SideEffectsFlagPlugin.js +3 -2
  145. package/lib/optimize/SplitChunksPlugin.js +60 -46
  146. package/lib/rules/RuleSetCompiler.js +1 -1
  147. package/lib/runtime/AsyncModuleRuntimeModule.js +28 -18
  148. package/lib/runtime/AutoPublicPathRuntimeModule.js +8 -3
  149. package/lib/runtime/GetChunkFilenameRuntimeModule.js +3 -2
  150. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +89 -55
  151. package/lib/schemes/HttpUriPlugin.js +78 -7
  152. package/lib/serialization/AggregateErrorSerializer.js +1 -2
  153. package/lib/serialization/ObjectMiddleware.js +0 -2
  154. package/lib/serialization/SingleItemMiddleware.js +1 -1
  155. package/lib/sharing/ConsumeSharedModule.js +1 -1
  156. package/lib/sharing/ConsumeSharedPlugin.js +5 -3
  157. package/lib/sharing/ProvideSharedModule.js +1 -1
  158. package/lib/sharing/resolveMatchedConfigs.js +15 -9
  159. package/lib/sharing/utils.js +1 -1
  160. package/lib/stats/DefaultStatsFactoryPlugin.js +8 -5
  161. package/lib/stats/DefaultStatsPresetPlugin.js +1 -1
  162. package/lib/stats/DefaultStatsPrinterPlugin.js +1 -1
  163. package/lib/util/StringXor.js +1 -1
  164. package/lib/util/URLAbsoluteSpecifier.js +2 -2
  165. package/lib/util/binarySearchBounds.js +2 -2
  166. package/lib/util/comparators.js +54 -76
  167. package/lib/util/compileBooleanMatcher.js +78 -6
  168. package/lib/util/createHash.js +20 -199
  169. package/lib/util/deprecation.js +1 -1
  170. package/lib/util/deterministicGrouping.js +6 -3
  171. package/lib/util/fs.js +75 -75
  172. package/lib/util/hash/BatchedHash.js +10 -9
  173. package/lib/util/hash/BulkUpdateHash.js +138 -0
  174. package/lib/util/hash/DebugHash.js +75 -0
  175. package/lib/util/hash/hash-digest.js +216 -0
  176. package/lib/util/identifier.js +82 -17
  177. package/lib/util/internalSerializables.js +2 -6
  178. package/lib/util/runtime.js +3 -3
  179. package/lib/util/source.js +2 -2
  180. package/lib/wasm/EnableWasmLoadingPlugin.js +10 -4
  181. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +3 -2
  182. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +11 -7
  183. package/lib/wasm-sync/WebAssemblyGenerator.js +9 -6
  184. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +11 -6
  185. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +6 -2
  186. package/lib/web/FetchCompileWasmPlugin.js +0 -2
  187. package/lib/webpack.js +85 -82
  188. package/module.d.ts +5 -0
  189. package/package.json +34 -28
  190. package/schemas/WebpackOptions.check.js +1 -1
  191. package/schemas/WebpackOptions.json +160 -101
  192. package/schemas/plugins/{css/CssAutoParserOptions.check.d.ts → ManifestPlugin.check.d.ts} +1 -1
  193. package/schemas/plugins/ManifestPlugin.check.js +6 -0
  194. package/schemas/plugins/ManifestPlugin.json +98 -0
  195. package/schemas/plugins/SourceMapDevToolPlugin.check.js +1 -1
  196. package/schemas/plugins/SourceMapDevToolPlugin.json +16 -3
  197. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  198. package/schemas/plugins/container/ContainerReferencePlugin.json +4 -1
  199. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  200. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  201. package/schemas/plugins/container/ModuleFederationPlugin.json +4 -1
  202. package/schemas/plugins/css/CssModuleGeneratorOptions.check.js +1 -1
  203. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  204. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  205. package/schemas/plugins/json/JsonModulesPluginParser.check.js +1 -1
  206. package/types.d.ts +771 -436
  207. package/lib/ModuleSourceTypesConstants.js +0 -123
  208. package/lib/dependencies/CssLocalIdentifierDependency.js +0 -250
  209. package/lib/dependencies/CssSelfLocalIdentifierDependency.js +0 -112
  210. package/schemas/plugins/css/CssAutoGeneratorOptions.check.d.ts +0 -7
  211. package/schemas/plugins/css/CssAutoGeneratorOptions.check.js +0 -6
  212. package/schemas/plugins/css/CssAutoGeneratorOptions.json +0 -3
  213. package/schemas/plugins/css/CssAutoParserOptions.check.js +0 -6
  214. package/schemas/plugins/css/CssAutoParserOptions.json +0 -3
  215. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.d.ts +0 -7
  216. package/schemas/plugins/css/CssGlobalGeneratorOptions.check.js +0 -6
  217. package/schemas/plugins/css/CssGlobalGeneratorOptions.json +0 -3
  218. package/schemas/plugins/css/CssGlobalParserOptions.check.d.ts +0 -7
  219. package/schemas/plugins/css/CssGlobalParserOptions.check.js +0 -6
  220. package/schemas/plugins/css/CssGlobalParserOptions.json +0 -3
@@ -0,0 +1,216 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Alexander Akait @alexander-akait
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /** @typedef {import("../Hash")} Hash */
9
+ /** @typedef {import("../../../declarations/WebpackOptions").HashDigest} Encoding */
10
+
11
+ /** @typedef {"26" | "32" | "36" | "49" | "52" | "58" | "62"} Base */
12
+
13
+ /* cSpell:disable */
14
+
15
+ /** @type {Record<Base, string>} */
16
+ const ENCODE_TABLE = Object.freeze({
17
+ 26: "abcdefghijklmnopqrstuvwxyz",
18
+ 32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
19
+ 36: "0123456789abcdefghijklmnopqrstuvwxyz",
20
+ 49: "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
21
+ 52: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
22
+ 58: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
23
+ 62: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
24
+ });
25
+
26
+ /* cSpell:enable */
27
+
28
+ const ZERO = BigInt("0");
29
+ const EIGHT = BigInt("8");
30
+ const FF = BigInt("0xff");
31
+
32
+ /**
33
+ * It encodes octet arrays by doing long divisions on all significant digits in the array, creating a representation of that number in the new base.
34
+ * Then for every leading zero in the input (not significant as a number) it will encode as a single leader character.
35
+ * This is the first in the alphabet and will decode as 8 bits. The other characters depend upon the base.
36
+ * For example, a base58 alphabet packs roughly 5.858 bits per character.
37
+ * This means the encoded string 000f (using a base16, 0-f alphabet) will actually decode to 4 bytes unlike a canonical hex encoding which uniformly packs 4 bits into each character.
38
+ * While unusual, this does mean that no padding is required, and it works for bases like 43.
39
+ * @param {Buffer} buffer buffer
40
+ * @param {Base} base base
41
+ * @returns {string} encoded buffer
42
+ */
43
+ const encode = (buffer, base) => {
44
+ if (buffer.length === 0) return "";
45
+ const bigIntBase = BigInt(ENCODE_TABLE[base].length);
46
+ // Convert buffer to BigInt efficiently using bitwise operations
47
+ let value = ZERO;
48
+ for (let i = 0; i < buffer.length; i++) {
49
+ value = (value << EIGHT) | BigInt(buffer[i]);
50
+ }
51
+ // Convert to baseX string efficiently using array
52
+ const digits = [];
53
+ if (value === ZERO) return ENCODE_TABLE[base][0];
54
+ while (value > ZERO) {
55
+ const remainder = Number(value % bigIntBase);
56
+ digits.push(ENCODE_TABLE[base][remainder]);
57
+ value /= bigIntBase;
58
+ }
59
+ return digits.reverse().join("");
60
+ };
61
+
62
+ /**
63
+ * @param {string} data string
64
+ * @param {Base} base base
65
+ * @returns {Buffer} buffer
66
+ */
67
+ const decode = (data, base) => {
68
+ if (data.length === 0) return Buffer.from("");
69
+ const bigIntBase = BigInt(ENCODE_TABLE[base].length);
70
+ // Convert the baseX string to a BigInt value
71
+ let value = ZERO;
72
+ for (let i = 0; i < data.length; i++) {
73
+ const digit = ENCODE_TABLE[base].indexOf(data[i]);
74
+ if (digit === -1) {
75
+ throw new Error(`Invalid character at position ${i}: ${data[i]}`);
76
+ }
77
+ value = value * bigIntBase + BigInt(digit);
78
+ }
79
+ // If value is 0, return a single-byte buffer with value 0
80
+ if (value === ZERO) {
81
+ return Buffer.alloc(1);
82
+ }
83
+ // Determine buffer size efficiently by counting bytes
84
+ let temp = value;
85
+ let byteLength = 0;
86
+ while (temp > ZERO) {
87
+ temp >>= EIGHT;
88
+ byteLength++;
89
+ }
90
+ // Create buffer and fill it from right to left
91
+ const buffer = Buffer.alloc(byteLength);
92
+ for (let i = byteLength - 1; i >= 0; i--) {
93
+ buffer[i] = Number(value & FF);
94
+ value >>= EIGHT;
95
+ }
96
+ return buffer;
97
+ };
98
+
99
+ // Compatibility with the old hash libraries, they can return different structures, so let's stringify them firstly
100
+
101
+ /**
102
+ * @param {string | { toString: (radix: number) => string }} value value
103
+ * @param {string} encoding encoding
104
+ * @returns {string} string
105
+ */
106
+ const toString = (value, encoding) =>
107
+ typeof value === "string"
108
+ ? value
109
+ : Buffer.from(value.toString(16), "hex").toString(
110
+ /** @type {NodeJS.BufferEncoding} */
111
+ (encoding)
112
+ );
113
+
114
+ /**
115
+ * @param {Buffer | { toString: (radix: number) => string }} value value
116
+ * @returns {Buffer} buffer
117
+ */
118
+ const toBuffer = (value) =>
119
+ Buffer.isBuffer(value) ? value : Buffer.from(value.toString(16), "hex");
120
+
121
+ let isBase64URLSupported = false;
122
+
123
+ try {
124
+ isBase64URLSupported = Boolean(Buffer.from("", "base64url"));
125
+ } catch (_err) {
126
+ // Nothing
127
+ }
128
+
129
+ /**
130
+ * @param {Hash} hash hash
131
+ * @param {string | Buffer} data data
132
+ * @param {Encoding=} encoding encoding of the return value
133
+ * @returns {void}
134
+ */
135
+ const update = (hash, data, encoding) => {
136
+ if (encoding === "base64url" && !isBase64URLSupported) {
137
+ const base64String = /** @type {string} */ (data)
138
+ .replace(/-/g, "+")
139
+ .replace(/_/g, "/");
140
+ const buf = Buffer.from(base64String, "base64");
141
+ hash.update(buf);
142
+ return;
143
+ } else if (
144
+ typeof data === "string" &&
145
+ encoding &&
146
+ typeof ENCODE_TABLE[/** @type {Base} */ (encoding.slice(4))] !== "undefined"
147
+ ) {
148
+ const buf = decode(data, /** @type {Base} */ (encoding.slice(4)));
149
+ hash.update(buf);
150
+ return;
151
+ }
152
+
153
+ if (encoding) {
154
+ hash.update(/** @type {string} */ (data), encoding);
155
+ } else {
156
+ hash.update(data);
157
+ }
158
+ };
159
+
160
+ /**
161
+ * @overload
162
+ * @param {Hash} hash hash
163
+ * @returns {Buffer} digest
164
+ */
165
+ /**
166
+ * @overload
167
+ * @param {Hash} hash hash
168
+ * @param {undefined} encoding encoding of the return value
169
+ * @param {boolean=} isSafe true when we await right types from digest(), otherwise false
170
+ * @returns {Buffer} digest
171
+ */
172
+ /**
173
+ * @overload
174
+ * @param {Hash} hash hash
175
+ * @param {Encoding} encoding encoding of the return value
176
+ * @param {boolean=} isSafe true when we await right types from digest(), otherwise false
177
+ * @returns {string} digest
178
+ */
179
+ /**
180
+ * @param {Hash} hash hash
181
+ * @param {Encoding=} encoding encoding of the return value
182
+ * @param {boolean=} isSafe true when we await right types from digest(), otherwise false
183
+ * @returns {string | Buffer} digest
184
+ */
185
+ const digest = (hash, encoding, isSafe) => {
186
+ if (typeof encoding === "undefined") {
187
+ return isSafe ? hash.digest() : toBuffer(hash.digest());
188
+ }
189
+
190
+ if (encoding === "base64url" && !isBase64URLSupported) {
191
+ const digest = isSafe
192
+ ? hash.digest("base64")
193
+ : toString(hash.digest("base64"), "base64");
194
+
195
+ return digest.replace(/\+/g, "-").replace(/\//g, "_").replace(/[=]+$/, "");
196
+ } else if (
197
+ typeof ENCODE_TABLE[/** @type {Base} */ (encoding.slice(4))] !== "undefined"
198
+ ) {
199
+ const buf = isSafe ? hash.digest() : toBuffer(hash.digest());
200
+
201
+ return encode(
202
+ buf,
203
+ /** @type {Base} */
204
+ (encoding.slice(4))
205
+ );
206
+ }
207
+
208
+ return isSafe
209
+ ? hash.digest(encoding)
210
+ : toString(hash.digest(encoding), encoding);
211
+ };
212
+
213
+ module.exports.decode = decode;
214
+ module.exports.digest = digest;
215
+ module.exports.encode = encode;
216
+ module.exports.update = update;
@@ -321,6 +321,7 @@ const absolutify = makeCacheableWithContext(_absolutify);
321
321
  const PATH_QUERY_FRAGMENT_REGEXP =
322
322
  /^((?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
323
323
  const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/;
324
+ const ZERO_ESCAPE_REGEXP = /\0(.)/g;
324
325
 
325
326
  /** @typedef {{ resource: string, path: string, query: string, fragment: string }} ParsedResource */
326
327
  /** @typedef {{ resource: string, path: string, query: string }} ParsedResourceWithoutFragment */
@@ -330,15 +331,56 @@ const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/;
330
331
  * @returns {ParsedResource} parsed parts
331
332
  */
332
333
  const _parseResource = (str) => {
333
- const match =
334
- /** @type {[string, string, string | undefined, string | undefined]} */
335
- (/** @type {unknown} */ (PATH_QUERY_FRAGMENT_REGEXP.exec(str)));
336
- return {
337
- resource: str,
338
- path: match[1].replace(/\0(.)/g, "$1"),
339
- query: match[2] ? match[2].replace(/\0(.)/g, "$1") : "",
340
- fragment: match[3] || ""
341
- };
334
+ const firstEscape = str.indexOf("\0");
335
+
336
+ // Handle `\0`
337
+ if (firstEscape !== -1) {
338
+ const match =
339
+ /** @type {[string, string, string | undefined, string | undefined]} */
340
+ (/** @type {unknown} */ (PATH_QUERY_FRAGMENT_REGEXP.exec(str)));
341
+
342
+ return {
343
+ resource: str,
344
+ path: match[1].replace(ZERO_ESCAPE_REGEXP, "$1"),
345
+ query: match[2] ? match[2].replace(ZERO_ESCAPE_REGEXP, "$1") : "",
346
+ fragment: match[3] || ""
347
+ };
348
+ }
349
+
350
+ /** @type {ParsedResource} */
351
+ const result = { resource: str, path: "", query: "", fragment: "" };
352
+ const queryStart = str.indexOf("?");
353
+ const fragmentStart = str.indexOf("#");
354
+
355
+ if (fragmentStart < 0) {
356
+ if (queryStart < 0) {
357
+ result.path = result.resource;
358
+
359
+ // No fragment, no query
360
+ return result;
361
+ }
362
+
363
+ result.path = str.slice(0, queryStart);
364
+ result.query = str.slice(queryStart);
365
+
366
+ // Query, no fragment
367
+ return result;
368
+ }
369
+
370
+ if (queryStart < 0 || fragmentStart < queryStart) {
371
+ result.path = str.slice(0, fragmentStart);
372
+ result.fragment = str.slice(fragmentStart);
373
+
374
+ // Fragment, no query
375
+ return result;
376
+ }
377
+
378
+ result.path = str.slice(0, queryStart);
379
+ result.query = str.slice(queryStart, fragmentStart);
380
+ result.fragment = str.slice(fragmentStart);
381
+
382
+ // Query and fragment
383
+ return result;
342
384
  };
343
385
 
344
386
  /**
@@ -347,14 +389,37 @@ const _parseResource = (str) => {
347
389
  * @returns {ParsedResourceWithoutFragment} parsed parts
348
390
  */
349
391
  const _parseResourceWithoutFragment = (str) => {
350
- const match =
351
- /** @type {[string, string, string | undefined]} */
352
- (/** @type {unknown} */ (PATH_QUERY_REGEXP.exec(str)));
353
- return {
354
- resource: str,
355
- path: match[1].replace(/\0(.)/g, "$1"),
356
- query: match[2] ? match[2].replace(/\0(.)/g, "$1") : ""
357
- };
392
+ const firstEscape = str.indexOf("\0");
393
+
394
+ // Handle `\0`
395
+ if (firstEscape !== -1) {
396
+ const match =
397
+ /** @type {[string, string, string | undefined]} */
398
+ (/** @type {unknown} */ (PATH_QUERY_REGEXP.exec(str)));
399
+
400
+ return {
401
+ resource: str,
402
+ path: match[1].replace(ZERO_ESCAPE_REGEXP, "$1"),
403
+ query: match[2] ? match[2].replace(ZERO_ESCAPE_REGEXP, "$1") : ""
404
+ };
405
+ }
406
+
407
+ /** @type {ParsedResourceWithoutFragment} */
408
+ const result = { resource: str, path: "", query: "" };
409
+ const queryStart = str.indexOf("?");
410
+
411
+ if (queryStart < 0) {
412
+ result.path = result.resource;
413
+
414
+ // No query
415
+ return result;
416
+ }
417
+
418
+ result.path = str.slice(0, queryStart);
419
+ result.query = str.slice(queryStart);
420
+
421
+ // Query
422
+ return result;
358
423
  };
359
424
 
360
425
  /**
@@ -73,16 +73,12 @@ module.exports = {
73
73
  require("../dependencies/CriticalDependencyWarning"),
74
74
  "dependencies/CssImportDependency": () =>
75
75
  require("../dependencies/CssImportDependency"),
76
- "dependencies/CssLocalIdentifierDependency": () =>
77
- require("../dependencies/CssLocalIdentifierDependency"),
78
- "dependencies/CssSelfLocalIdentifierDependency": () =>
79
- require("../dependencies/CssSelfLocalIdentifierDependency"),
76
+ "dependencies/CssUrlDependency": () =>
77
+ require("../dependencies/CssUrlDependency"),
80
78
  "dependencies/CssIcssImportDependency": () =>
81
79
  require("../dependencies/CssIcssImportDependency"),
82
80
  "dependencies/CssIcssExportDependency": () =>
83
81
  require("../dependencies/CssIcssExportDependency"),
84
- "dependencies/CssUrlDependency": () =>
85
- require("../dependencies/CssUrlDependency"),
86
82
  "dependencies/CssIcssSymbolDependency": () =>
87
83
  require("../dependencies/CssIcssSymbolDependency"),
88
84
  "dependencies/DelegatedSourceDependency": () =>
@@ -71,7 +71,7 @@ const forEachRuntime = (runtime, fn, deterministicOrder = false) => {
71
71
 
72
72
  /**
73
73
  * @template T
74
- * @param {SortableSet<T>} set set
74
+ * @param {Exclude<RuntimeSpec, undefined | string>} set set
75
75
  * @returns {string} runtime key
76
76
  */
77
77
  const getRuntimesKey = (set) => {
@@ -102,7 +102,7 @@ const keyToRuntime = (key) => {
102
102
 
103
103
  /**
104
104
  * @template T
105
- * @param {SortableSet<T>} set set
105
+ * @param {Exclude<RuntimeSpec, undefined | string>} set set
106
106
  * @returns {string} runtime string
107
107
  */
108
108
  const getRuntimesString = (set) => {
@@ -163,7 +163,7 @@ const runtimeEqual = (a, b) => {
163
163
  /**
164
164
  * @param {RuntimeSpec} a first
165
165
  * @param {RuntimeSpec} b second
166
- * @returns {-1|0|1} compare
166
+ * @returns {-1 | 0 | 1} compare
167
167
  */
168
168
  const compareRuntime = (a, b) => {
169
169
  if (a === b) {
@@ -17,9 +17,9 @@ const equalityCache = new WeakMap();
17
17
  */
18
18
  const _isSourceEqual = (a, b) => {
19
19
  // prefer .buffer(), it's called anyway during emit
20
- /** @type {Buffer|string} */
20
+ /** @type {Buffer | string} */
21
21
  let aSource = typeof a.buffer === "function" ? a.buffer() : a.source();
22
- /** @type {Buffer|string} */
22
+ /** @type {Buffer | string} */
23
23
  let bSource = typeof b.buffer === "function" ? b.buffer() : b.source();
24
24
  if (aSource === bSource) return true;
25
25
  if (typeof aSource === "string" && typeof bSource === "string") return false;
@@ -77,7 +77,6 @@ class EnableWasmLoadingPlugin {
77
77
  switch (type) {
78
78
  case "fetch": {
79
79
  if (compiler.options.experiments.syncWebAssembly) {
80
- // TODO webpack 6 remove FetchCompileWasmPlugin
81
80
  const FetchCompileWasmPlugin = require("../web/FetchCompileWasmPlugin");
82
81
 
83
82
  new FetchCompileWasmPlugin({
@@ -95,7 +94,6 @@ class EnableWasmLoadingPlugin {
95
94
  }
96
95
  case "async-node": {
97
96
  if (compiler.options.experiments.syncWebAssembly) {
98
- // TODO webpack 6 remove ReadFileCompileWasmPlugin
99
97
  const ReadFileCompileWasmPlugin = require("../node/ReadFileCompileWasmPlugin");
100
98
 
101
99
  new ReadFileCompileWasmPlugin({
@@ -119,9 +117,17 @@ class EnableWasmLoadingPlugin {
119
117
  break;
120
118
  }
121
119
  case "universal": {
122
- const UniversalCompileAsyncWasmPlugin = require("../wasm-async/UniversalCompileAsyncWasmPlugin");
120
+ if (compiler.options.experiments.syncWebAssembly) {
121
+ throw new Error(
122
+ "Universal wasm loading type is only supported by asynchronous web assembly."
123
+ );
124
+ }
123
125
 
124
- new UniversalCompileAsyncWasmPlugin().apply(compiler);
126
+ if (compiler.options.experiments.asyncWebAssembly) {
127
+ const UniversalCompileAsyncWasmPlugin = require("../wasm-async/UniversalCompileAsyncWasmPlugin");
128
+
129
+ new UniversalCompileAsyncWasmPlugin().apply(compiler);
130
+ }
125
131
  break;
126
132
  }
127
133
  default:
@@ -7,10 +7,11 @@
7
7
 
8
8
  const { RawSource } = require("webpack-sources");
9
9
  const Generator = require("../Generator");
10
- const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypesConstants");
10
+ const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypeConstants");
11
11
 
12
12
  /** @typedef {import("webpack-sources").Source} Source */
13
13
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
14
+ /** @typedef {import("../Module").SourceType} SourceType */
14
15
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
15
16
  /** @typedef {import("../NormalModule")} NormalModule */
16
17
 
@@ -38,7 +39,7 @@ class AsyncWebAssemblyGenerator extends Generator {
38
39
 
39
40
  /**
40
41
  * @param {NormalModule} module the module
41
- * @param {string=} type source type
42
+ * @param {SourceType=} type source type
42
43
  * @returns {number} estimate size of the module
43
44
  */
44
45
  getSize(module, type) {
@@ -8,7 +8,7 @@
8
8
  const { RawSource } = require("webpack-sources");
9
9
  const Generator = require("../Generator");
10
10
  const InitFragment = require("../InitFragment");
11
- const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypesConstants");
11
+ const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypeConstants");
12
12
  const RuntimeGlobals = require("../RuntimeGlobals");
13
13
  const Template = require("../Template");
14
14
  const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
@@ -16,11 +16,12 @@ const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDe
16
16
  /** @typedef {import("webpack-sources").Source} Source */
17
17
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
18
18
  /** @typedef {import("../Module")} Module */
19
+ /** @typedef {import("../Module").SourceType} SourceType */
19
20
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
20
21
  /** @typedef {import("../NormalModule")} NormalModule */
21
22
 
22
23
  /**
23
- * @typedef {{ request: string, importVar: string }} ImportObjRequestItem
24
+ * @typedef {{ request: string, importVar: string, dependency: WebAssemblyImportDependency }} ImportObjRequestItem
24
25
  */
25
26
 
26
27
  class AsyncWebAssemblyJavascriptGenerator extends Generator {
@@ -34,7 +35,7 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
34
35
 
35
36
  /**
36
37
  * @param {NormalModule} module the module
37
- * @param {string=} type source type
38
+ * @param {SourceType=} type source type
38
39
  * @returns {number} estimate size of the module
39
40
  */
40
41
  getSize(module, type) {
@@ -70,7 +71,8 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
70
71
  if (!depModules.has(module)) {
71
72
  depModules.set(module, {
72
73
  request: dep.request,
73
- importVar: `WEBPACK_IMPORTED_MODULE_${depModules.size}`
74
+ importVar: `WEBPACK_IMPORTED_MODULE_${depModules.size}`,
75
+ dependency: dep
74
76
  });
75
77
  }
76
78
  let list = wasmDepsByRequest.get(dep.request);
@@ -87,7 +89,7 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
87
89
 
88
90
  const importStatements = Array.from(
89
91
  depModules,
90
- ([importedModule, { request, importVar }]) => {
92
+ ([importedModule, { request, importVar, dependency }]) => {
91
93
  if (moduleGraph.isAsync(importedModule)) {
92
94
  promises.push(importVar);
93
95
  }
@@ -99,7 +101,8 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
99
101
  request,
100
102
  originModule: module,
101
103
  importVar,
102
- runtimeRequirements
104
+ runtimeRequirements,
105
+ dependency
103
106
  });
104
107
  }
105
108
  );
@@ -132,7 +135,8 @@ class AsyncWebAssemblyJavascriptGenerator extends Generator {
132
135
  importVar,
133
136
  initFragments,
134
137
  runtime,
135
- runtimeRequirements
138
+ runtimeRequirements,
139
+ dependency: dep
136
140
  })}`;
137
141
  });
138
142
  return Template.asString([
@@ -11,13 +11,14 @@ const { addWithAST, editWithAST } = require("@webassemblyjs/wasm-edit");
11
11
  const { decode } = require("@webassemblyjs/wasm-parser");
12
12
  const { RawSource } = require("webpack-sources");
13
13
  const Generator = require("../Generator");
14
- const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypesConstants");
14
+ const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypeConstants");
15
15
  const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
16
16
  const WebAssemblyUtils = require("./WebAssemblyUtils");
17
17
 
18
18
  /** @typedef {import("webpack-sources").Source} Source */
19
19
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
20
20
  /** @typedef {import("../Module")} Module */
21
+ /** @typedef {import("../Module").SourceType} SourceType */
21
22
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
22
23
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
23
24
  /** @typedef {import("../NormalModule")} NormalModule */
@@ -270,11 +271,13 @@ const rewriteExportNames =
270
271
  }
271
272
  });
272
273
 
274
+ /** @typedef {Map<string, UsedWasmDependency>} Mapping */
275
+
273
276
  /**
274
277
  * Mangle import names and modules
275
278
  * @param {object} state state
276
279
  * @param {AST} state.ast Module's ast
277
- * @param {Map<string, UsedWasmDependency>} state.usedDependencyMap mappings to mangle names
280
+ * @param {Mapping} state.usedDependencyMap mappings to mangle names
278
281
  * @returns {ArrayBufferTransform} transform
279
282
  */
280
283
  const rewriteImports =
@@ -383,11 +386,11 @@ const addInitFunction =
383
386
  * Extract mangle mappings from module
384
387
  * @param {ModuleGraph} moduleGraph module graph
385
388
  * @param {Module} module current module
386
- * @param {boolean | undefined} mangle mangle imports
387
- * @returns {Map<string, UsedWasmDependency>} mappings to mangled names
389
+ * @param {boolean=} mangle mangle imports
390
+ * @returns {Mapping} mappings to mangled names
388
391
  */
389
392
  const getUsedDependencyMap = (moduleGraph, module, mangle) => {
390
- /** @type {Map<string, UsedWasmDependency>} */
393
+ /** @type {Mapping} */
391
394
  const map = new Map();
392
395
  for (const usedDep of WebAssemblyUtils.getUsedDependencies(
393
396
  moduleGraph,
@@ -426,7 +429,7 @@ class WebAssemblyGenerator extends Generator {
426
429
 
427
430
  /**
428
431
  * @param {NormalModule} module the module
429
- * @param {string=} type source type
432
+ * @param {SourceType=} type source type
430
433
  * @returns {number} estimate size of the module
431
434
  */
432
435
  getSize(module, type) {
@@ -9,7 +9,7 @@ const { RawSource } = require("webpack-sources");
9
9
  const { UsageState } = require("../ExportsInfo");
10
10
  const Generator = require("../Generator");
11
11
  const InitFragment = require("../InitFragment");
12
- const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypesConstants");
12
+ const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypeConstants");
13
13
  const RuntimeGlobals = require("../RuntimeGlobals");
14
14
  const Template = require("../Template");
15
15
  const ModuleDependency = require("../dependencies/ModuleDependency");
@@ -19,6 +19,7 @@ const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDe
19
19
  /** @typedef {import("webpack-sources").Source} Source */
20
20
  /** @typedef {import("../Generator").GenerateContext} GenerateContext */
21
21
  /** @typedef {import("../Module")} Module */
22
+ /** @typedef {import("../Module").SourceType} SourceType */
22
23
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
23
24
  /** @typedef {import("../NormalModule")} NormalModule */
24
25
 
@@ -33,7 +34,7 @@ class WebAssemblyJavascriptGenerator extends Generator {
33
34
 
34
35
  /**
35
36
  * @param {NormalModule} module the module
36
- * @param {string=} type source type
37
+ * @param {SourceType=} type source type
37
38
  * @returns {number} estimate size of the module
38
39
  */
39
40
  getSize(module, type) {
@@ -71,6 +72,7 @@ class WebAssemblyJavascriptGenerator extends Generator {
71
72
  importedModules.set(
72
73
  moduleGraph.getModule(dep),
73
74
  (importData = {
75
+ dependency: moduleDep,
74
76
  importVar: `m${index}`,
75
77
  index,
76
78
  request: (moduleDep && moduleDep.userRequest) || undefined,
@@ -106,7 +108,8 @@ class WebAssemblyJavascriptGenerator extends Generator {
106
108
  defaultInterop: true,
107
109
  initFragments,
108
110
  runtime,
109
- runtimeRequirements
111
+ runtimeRequirements,
112
+ dependency: dep
110
113
  })
111
114
  );
112
115
  }
@@ -138,7 +141,8 @@ class WebAssemblyJavascriptGenerator extends Generator {
138
141
  defaultInterop: true,
139
142
  initFragments,
140
143
  runtime,
141
- runtimeRequirements
144
+ runtimeRequirements,
145
+ dependency: dep
142
146
  })};`,
143
147
  `if(WebAssembly.Global) ${exportProp} = ` +
144
148
  `new WebAssembly.Global({ value: ${JSON.stringify(
@@ -154,7 +158,7 @@ class WebAssemblyJavascriptGenerator extends Generator {
154
158
  const importsCode = Template.asString(
155
159
  Array.from(
156
160
  importedModules,
157
- ([module, { importVar, request, reexports }]) => {
161
+ ([module, { importVar, request, reexports, dependency }]) => {
158
162
  const importStatement = runtimeTemplate.importStatement({
159
163
  module,
160
164
  moduleGraph,
@@ -162,7 +166,8 @@ class WebAssemblyJavascriptGenerator extends Generator {
162
166
  request,
163
167
  importVar,
164
168
  originModule: module,
165
- runtimeRequirements
169
+ runtimeRequirements,
170
+ dependency
166
171
  });
167
172
  return importStatement[0] + importStatement[1] + reexports.join("\n");
168
173
  }
@@ -6,6 +6,10 @@
6
6
  "use strict";
7
7
 
8
8
  const Generator = require("../Generator");
9
+ const {
10
+ JAVASCRIPT_TYPE,
11
+ WEBASSEMBLY_TYPE
12
+ } = require("../ModuleSourceTypeConstants");
9
13
  const { WEBASSEMBLY_MODULE_TYPE_SYNC } = require("../ModuleTypeConstants");
10
14
  const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
11
15
  const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
@@ -74,8 +78,8 @@ class WebAssemblyModulesPlugin {
74
78
  const WebAssemblyGenerator = getWebAssemblyGenerator();
75
79
 
76
80
  return Generator.byType({
77
- javascript: new WebAssemblyJavascriptGenerator(),
78
- webassembly: new WebAssemblyGenerator(this.options)
81
+ [JAVASCRIPT_TYPE]: new WebAssemblyJavascriptGenerator(),
82
+ [WEBASSEMBLY_TYPE]: new WebAssemblyGenerator(this.options)
79
83
  });
80
84
  });
81
85
 
@@ -17,8 +17,6 @@ const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRunt
17
17
  * @property {boolean=} mangleImports mangle imports
18
18
  */
19
19
 
20
- // TODO webpack 6 remove
21
-
22
20
  const PLUGIN_NAME = "FetchCompileWasmPlugin";
23
21
 
24
22
  class FetchCompileWasmPlugin {