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,235 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Haijie Xie @hai-x
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const { RawSource } = require("webpack-sources");
9
+ const Compilation = require("./Compilation");
10
+ const HotUpdateChunk = require("./HotUpdateChunk");
11
+ const createSchemaValidation = require("./util/create-schema-validation");
12
+
13
+ /** @typedef {import("./Compiler")} Compiler */
14
+ /** @typedef {import("./Chunk")} Chunk */
15
+ /** @typedef {import("./Chunk").ChunkName} ChunkName */
16
+ /** @typedef {import("./Chunk").ChunkId} ChunkId */
17
+ /** @typedef {import("./Compilation").Asset} Asset */
18
+ /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
19
+
20
+ /** @typedef {import("../declarations/plugins/ManifestPlugin").ManifestPluginOptions} ManifestPluginOptions */
21
+ /** @typedef {import("../declarations/plugins/ManifestPlugin").ManifestObject} ManifestObject */
22
+ /** @typedef {import("../declarations/plugins/ManifestPlugin").ManifestEntrypoint} ManifestEntrypoint */
23
+ /** @typedef {import("../declarations/plugins/ManifestPlugin").ManifestItem} ManifestItem */
24
+
25
+ const PLUGIN_NAME = "ManifestPlugin";
26
+
27
+ const validate = createSchemaValidation(
28
+ require("../schemas/plugins/ManifestPlugin.check"),
29
+ () => require("../schemas/plugins/ManifestPlugin.json"),
30
+ {
31
+ name: "ManifestPlugin",
32
+ baseDataPath: "options"
33
+ }
34
+ );
35
+
36
+ /**
37
+ * @param {string} filename filename
38
+ * @returns {string} extname
39
+ */
40
+ const extname = (filename) => {
41
+ const replaced = filename.replace(/\?.*/, "");
42
+ const split = replaced.split(".");
43
+ const last = split.pop();
44
+ if (!last) return "";
45
+ return last && /^(gz|br|map)$/i.test(last) ? `${split.pop()}.${last}` : last;
46
+ };
47
+
48
+ class ManifestPlugin {
49
+ /**
50
+ * @param {ManifestPluginOptions} options options
51
+ */
52
+ constructor(options) {
53
+ validate(options);
54
+
55
+ /** @type {ManifestPluginOptions & Required<Omit<ManifestPluginOptions, "filter" | "generate">>} */
56
+ this.options = {
57
+ filename: "manifest.json",
58
+ prefix: "[publicpath]",
59
+ entrypoints: true,
60
+ serialize: (manifest) => JSON.stringify(manifest, null, 2),
61
+ ...options
62
+ };
63
+ }
64
+
65
+ /**
66
+ * Apply the plugin
67
+ * @param {Compiler} compiler the compiler instance
68
+ * @returns {void}
69
+ */
70
+ apply(compiler) {
71
+ compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
72
+ compilation.hooks.processAssets.tap(
73
+ {
74
+ name: PLUGIN_NAME,
75
+ stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
76
+ },
77
+ () => {
78
+ const hashDigestLength = compilation.outputOptions.hashDigestLength;
79
+ const publicPath = compilation.getPath(
80
+ compilation.outputOptions.publicPath
81
+ );
82
+
83
+ /**
84
+ * @param {string | string[]} value value
85
+ * @returns {RegExp} regexp to remove hash
86
+ */
87
+ const createHashRegExp = (value) =>
88
+ new RegExp(
89
+ `(?:\\.${Array.isArray(value) ? `(${value.join("|")})` : value})(?=\\.)`,
90
+ "gi"
91
+ );
92
+
93
+ /**
94
+ * @param {string} name name
95
+ * @param {AssetInfo | null} info asset info
96
+ * @returns {string} hash removed name
97
+ */
98
+ const removeHash = (name, info) => {
99
+ // Handles hashes that match configured `hashDigestLength`
100
+ // i.e. index.XXXX.html -> index.html (html-webpack-plugin)
101
+ if (hashDigestLength <= 0) return name;
102
+ const reg = createHashRegExp(`[a-f0-9]{${hashDigestLength},32}`);
103
+ return name.replace(reg, "");
104
+ };
105
+
106
+ /**
107
+ * @param {Chunk} chunk chunk
108
+ * @returns {ChunkName | ChunkId} chunk name or chunk id
109
+ */
110
+ const getName = (chunk) => {
111
+ if (chunk.name) return chunk.name;
112
+
113
+ return chunk.id;
114
+ };
115
+
116
+ /** @type {ManifestObject} */
117
+ let manifest = {};
118
+
119
+ if (this.options.entrypoints) {
120
+ /** @type {ManifestObject["entrypoints"]} */
121
+ const entrypoints = {};
122
+
123
+ for (const [name, entrypoint] of compilation.entrypoints) {
124
+ const imports = [];
125
+
126
+ for (const chunk of entrypoint.chunks) {
127
+ for (const file of chunk.files) {
128
+ const name = getName(chunk);
129
+
130
+ imports.push(name ? `${name}.${extname(file)}` : file);
131
+ }
132
+ }
133
+
134
+ /** @type {ManifestEntrypoint} */
135
+ const item = { imports };
136
+ const parents = entrypoint
137
+ .getParents()
138
+ .map((item) => /** @type {string} */ (item.name));
139
+
140
+ if (parents.length > 0) {
141
+ item.parents = parents;
142
+ }
143
+
144
+ entrypoints[name] = item;
145
+ }
146
+
147
+ manifest.entrypoints = entrypoints;
148
+ }
149
+
150
+ /** @type {ManifestObject["assets"]} */
151
+ const assets = {};
152
+
153
+ /** @type {Set<string>} */
154
+ const added = new Set();
155
+
156
+ /**
157
+ * @param {string} file file
158
+ * @param {string=} usedName usedName
159
+ * @returns {void}
160
+ */
161
+ const handleFile = (file, usedName) => {
162
+ if (added.has(file)) return;
163
+ added.add(file);
164
+
165
+ const asset = compilation.getAsset(file);
166
+ if (!asset) return;
167
+ const sourceFilename = asset.info.sourceFilename;
168
+ const name =
169
+ usedName ||
170
+ sourceFilename ||
171
+ // Fallback for unofficial plugins, just remove hash from filename
172
+ removeHash(file, asset.info);
173
+
174
+ const prefix = this.options.prefix.replace(
175
+ /\[publicpath\]/gi,
176
+ () => (publicPath === "auto" ? "/" : publicPath)
177
+ );
178
+ /** @type {ManifestItem} */
179
+ const item = { file: prefix + file };
180
+
181
+ if (sourceFilename) {
182
+ item.src = sourceFilename;
183
+ }
184
+
185
+ if (this.options.filter) {
186
+ const needKeep = this.options.filter(item);
187
+
188
+ if (!needKeep) {
189
+ return;
190
+ }
191
+ }
192
+
193
+ assets[name] = item;
194
+ };
195
+
196
+ for (const chunk of compilation.chunks) {
197
+ if (chunk instanceof HotUpdateChunk) continue;
198
+
199
+ for (const auxiliaryFile of chunk.auxiliaryFiles) {
200
+ handleFile(auxiliaryFile);
201
+ }
202
+
203
+ const name = getName(chunk);
204
+
205
+ for (const file of chunk.files) {
206
+ handleFile(file, name ? `${name}.${extname(file)}` : file);
207
+ }
208
+ }
209
+
210
+ for (const asset of compilation.getAssets()) {
211
+ if (asset.info.hotModuleReplacement) {
212
+ continue;
213
+ }
214
+
215
+ handleFile(asset.name);
216
+ }
217
+
218
+ manifest.assets = assets;
219
+
220
+ if (this.options.generate) {
221
+ manifest = this.options.generate(manifest);
222
+ }
223
+
224
+ compilation.emitAsset(
225
+ this.options.filename,
226
+ new RawSource(this.options.serialize(manifest)),
227
+ { manifest: true }
228
+ );
229
+ }
230
+ );
231
+ });
232
+ }
233
+ }
234
+
235
+ module.exports = ManifestPlugin;
package/lib/Module.js CHANGED
@@ -9,7 +9,11 @@ const util = require("util");
9
9
  const ChunkGraph = require("./ChunkGraph");
10
10
  const DependenciesBlock = require("./DependenciesBlock");
11
11
  const ModuleGraph = require("./ModuleGraph");
12
- const { JS_TYPES } = require("./ModuleSourceTypesConstants");
12
+ const {
13
+ JAVASCRIPT_TYPE,
14
+ UNKNOWN_TYPE
15
+ } = require("./ModuleSourceTypeConstants");
16
+ const { JAVASCRIPT_TYPES } = require("./ModuleSourceTypeConstants");
13
17
  const RuntimeGlobals = require("./RuntimeGlobals");
14
18
  const { first } = require("./util/SetHelpers");
15
19
  const { compareChunksById } = require("./util/comparators");
@@ -24,12 +28,14 @@ const makeSerializable = require("./util/makeSerializable");
24
28
  /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */
25
29
  /** @typedef {import("./Compilation")} Compilation */
26
30
  /** @typedef {import("./Compilation").AssetInfo} AssetInfo */
31
+ /** @typedef {import("./Compilation").FileSystemDependencies} FileSystemDependencies */
27
32
  /** @typedef {import("./Compilation").UnsafeCacheData} UnsafeCacheData */
28
33
  /** @typedef {import("./ConcatenationScope")} ConcatenationScope */
29
34
  /** @typedef {import("./Dependency")} Dependency */
30
35
  /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
31
36
  /** @typedef {import("./DependencyTemplate").CssData} CssData */
32
37
  /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
38
+ /** @typedef {import("./ModuleSourceTypeConstants").AllTypes} AllTypes */
33
39
  /** @typedef {import("./FileSystemInfo")} FileSystemInfo */
34
40
  /** @typedef {import("./FileSystemInfo").Snapshot} Snapshot */
35
41
  /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
@@ -39,6 +45,8 @@ const makeSerializable = require("./util/makeSerializable");
39
45
  /** @typedef {import("./RequestShortener")} RequestShortener */
40
46
  /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
41
47
  /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
48
+ /** @typedef {import("../declarations/WebpackOptions").CssParserExportType} CssParserExportType */
49
+
42
50
  /**
43
51
  * @template T
44
52
  * @typedef {import("./InitFragment")<T>} InitFragment
@@ -68,7 +76,9 @@ const makeSerializable = require("./util/makeSerializable");
68
76
  * @property {string=} type the type of source that should be generated
69
77
  */
70
78
 
71
- /** @typedef {ReadonlySet<string>} SourceTypes */
79
+ /** @typedef {AllTypes} KnownSourceType */
80
+ /** @typedef {KnownSourceType | string} SourceType */
81
+ /** @typedef {ReadonlySet<SourceType>} SourceTypes */
72
82
 
73
83
  // TODO webpack 6: compilation will be required in CodeGenerationContext
74
84
  /**
@@ -78,6 +88,7 @@ const makeSerializable = require("./util/makeSerializable");
78
88
  * @property {ModuleGraph} moduleGraph the module graph
79
89
  * @property {ChunkGraph} chunkGraph the chunk graph
80
90
  * @property {RuntimeSpec} runtime the runtimes code should be generated for
91
+ * @property {RuntimeSpec[]} runtimes all runtimes code should be generated for
81
92
  * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
82
93
  * @property {CodeGenerationResults | undefined} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
83
94
  * @property {Compilation=} compilation the compilation
@@ -101,7 +112,7 @@ const makeSerializable = require("./util/makeSerializable");
101
112
 
102
113
  /**
103
114
  * @typedef {object} CodeGenerationResult
104
- * @property {Map<string, Source>} sources the resulting sources for all source types
115
+ * @property {Map<SourceType, Source>} sources the resulting sources for all source types
105
116
  * @property {CodeGenerationResultData=} data the resulting data for all source types
106
117
  * @property {ReadOnlyRuntimeRequirements | null} runtimeRequirements the runtime requirements
107
118
  * @property {string=} hash a hash of the code generation result (will be automatically calculated from sources and runtimeRequirements if not provided)
@@ -116,6 +127,7 @@ const makeSerializable = require("./util/makeSerializable");
116
127
  /**
117
128
  * @typedef {object} KnownBuildMeta
118
129
  * @property {("default" | "namespace" | "flagged" | "dynamic")=} exportsType
130
+ * @property {CssParserExportType=} exportType
119
131
  * @property {(false | "redirect" | "redirect-warn")=} defaultObject
120
132
  * @property {boolean=} strictHarmonyModule
121
133
  * @property {boolean=} treatAsCommonJs
@@ -123,12 +135,10 @@ const makeSerializable = require("./util/makeSerializable");
123
135
  * @property {boolean=} sideEffectFree
124
136
  * @property {boolean=} isCSSModule
125
137
  * @property {Record<string, string>=} jsIncompatibleExports
126
- * @property {Record<string, string>=} exportsFinalName
127
- * @property {string=} factoryExportsBinding
138
+ * @property {Map<RuntimeSpec, Record<string, string>>=} exportsFinalNameByRuntime
139
+ * @property {Map<RuntimeSpec, string>=} exportsSourceByRuntime
128
140
  */
129
141
 
130
- /** @typedef {LazySet<string>} FileSystemDependencies */
131
-
132
142
  /**
133
143
  * @typedef {object} KnownBuildInfo
134
144
  * @property {boolean=} cacheable
@@ -185,7 +195,8 @@ const EMPTY_RESOLVE_OPTIONS = {};
185
195
 
186
196
  let debugId = 1000;
187
197
 
188
- const DEFAULT_TYPES_UNKNOWN = new Set(["unknown"]);
198
+ /** @type {SourceTypes} */
199
+ const DEFAULT_TYPES_UNKNOWN = new Set([UNKNOWN_TYPE]);
189
200
 
190
201
  const deprecatedNeedRebuild = util.deprecate(
191
202
  /**
@@ -836,8 +847,8 @@ class Module extends DependenciesBlock {
836
847
 
837
848
  /**
838
849
  * @deprecated Use needBuild instead
839
- * @param {Map<string, number|null>} fileTimestamps timestamps of files
840
- * @param {Map<string, number|null>} contextTimestamps timestamps of directories
850
+ * @param {Map<string, number | null>} fileTimestamps timestamps of files
851
+ * @param {Map<string, number | null>} contextTimestamps timestamps of directories
841
852
  * @returns {boolean} true, if the module needs a rebuild
842
853
  */
843
854
  needRebuild(fileTimestamps, contextTimestamps) {
@@ -925,7 +936,7 @@ class Module extends DependenciesBlock {
925
936
  if (this.source === Module.prototype.source) {
926
937
  return DEFAULT_TYPES_UNKNOWN;
927
938
  }
928
- return JS_TYPES;
939
+ return JAVASCRIPT_TYPES;
929
940
  }
930
941
 
931
942
  /**
@@ -933,10 +944,10 @@ class Module extends DependenciesBlock {
933
944
  * @deprecated Use codeGeneration() instead
934
945
  * @param {DependencyTemplates} dependencyTemplates the dependency templates
935
946
  * @param {RuntimeTemplate} runtimeTemplate the runtime template
936
- * @param {string=} type the type of source that should be generated
947
+ * @param {SourceType=} type the type of source that should be generated
937
948
  * @returns {Source} generated source
938
949
  */
939
- source(dependencyTemplates, runtimeTemplate, type = "javascript") {
950
+ source(dependencyTemplates, runtimeTemplate, type = JAVASCRIPT_TYPE) {
940
951
  if (this.codeGeneration === Module.prototype.codeGeneration) {
941
952
  const AbstractMethodError = require("./AbstractMethodError");
942
953
 
@@ -954,6 +965,7 @@ class Module extends DependenciesBlock {
954
965
  moduleGraph: chunkGraph.moduleGraph,
955
966
  chunkGraph,
956
967
  runtime: undefined,
968
+ runtimes: [],
957
969
  codeGenerationResults: undefined
958
970
  };
959
971
  const sources = this.codeGeneration(codeGenContext).sources;
@@ -961,7 +973,7 @@ class Module extends DependenciesBlock {
961
973
  return /** @type {Source} */ (
962
974
  type
963
975
  ? sources.get(type)
964
- : sources.get(/** @type {string} */ (first(this.getSourceTypes())))
976
+ : sources.get(/** @type {SourceType} */ (first(this.getSourceTypes())))
965
977
  );
966
978
  }
967
979
 
@@ -1016,7 +1028,7 @@ class Module extends DependenciesBlock {
1016
1028
  // Best override this method
1017
1029
  const sources = new Map();
1018
1030
  for (const type of this.getSourceTypes()) {
1019
- if (type !== "unknown") {
1031
+ if (type !== UNKNOWN_TYPE) {
1020
1032
  sources.set(
1021
1033
  type,
1022
1034
  this.source(
@@ -17,7 +17,7 @@ const makeSerializable = require("./util/makeSerializable");
17
17
  class ModuleBuildError extends WebpackError {
18
18
  /**
19
19
  * @param {string | ErrorWithHideStack} err error thrown
20
- * @param {{from?: string|null}} info additional info
20
+ * @param {{from?: string | null}} info additional info
21
21
  */
22
22
  constructor(err, { from = null } = {}) {
23
23
  let message = "Module build failed";
@@ -15,7 +15,7 @@ const makeSerializable = require("./util/makeSerializable");
15
15
  class ModuleError extends WebpackError {
16
16
  /**
17
17
  * @param {Error} err error thrown
18
- * @param {{from?: string|null}} info additional info
18
+ * @param {{from?: string | null}} info additional info
19
19
  */
20
20
  constructor(err, { from = null } = {}) {
21
21
  let message = "Module Error";
@@ -281,7 +281,7 @@ ModuleFilenameHelpers.createFilename = (
281
281
  * @template T
282
282
  * @param {T[]} array the array with duplicates to be replaced
283
283
  * @param {(duplicateItem: T, duplicateItemIndex: number, numberOfTimesReplaced: number) => T} fn callback function to generate new values for the duplicate items
284
- * @param {(firstElement:T, nextElement:T) => -1 | 0 | 1=} comparator optional comparator function to sort the duplicate items
284
+ * @param {(firstElement: T, nextElement: T) => -1 | 0 | 1=} comparator optional comparator function to sort the duplicate items
285
285
  * @returns {T[]} the array with duplicates replaced
286
286
  * @example
287
287
  * ```js
@@ -9,6 +9,7 @@ const util = require("util");
9
9
  const ExportsInfo = require("./ExportsInfo");
10
10
  const ModuleGraphConnection = require("./ModuleGraphConnection");
11
11
  const HarmonyImportDependency = require("./dependencies/HarmonyImportDependency");
12
+ const { ImportPhaseUtils } = require("./dependencies/ImportPhase");
12
13
  const SortableSet = require("./util/SortableSet");
13
14
  const WeakTupleMap = require("./util/WeakTupleMap");
14
15
  const { sortWithSourceOrder } = require("./util/comparators");
@@ -168,6 +169,12 @@ class ModuleGraph {
168
169
  * @private
169
170
  */
170
171
  this._dependencySourceOrderMap = new WeakMap();
172
+
173
+ /**
174
+ * @type {Set<Module>}
175
+ * @private
176
+ */
177
+ this._modulesNeedingSort = new Set();
171
178
  }
172
179
 
173
180
  /**
@@ -304,17 +311,15 @@ class ModuleGraph {
304
311
  // import { a, b } from "lib" -> a and b have the same source order -> a = b = 1
305
312
  // import { d } from "lib/d" -> d = 2
306
313
  const currentSourceOrder =
307
- /** @type { HarmonyImportSideEffectDependency | HarmonyImportSpecifierDependency} */ (
308
- dependency
309
- ).sourceOrder;
314
+ /** @type {HarmonyImportSideEffectDependency | HarmonyImportSpecifierDependency} */
315
+ (dependency).sourceOrder;
310
316
 
311
317
  // lib/index.js (reexport)
312
318
  // import { a } from "lib/a" -> a = 0
313
319
  // import { b } from "lib/b" -> b = 1
314
320
  const originSourceOrder =
315
- /** @type { HarmonyImportSideEffectDependency | HarmonyImportSpecifierDependency} */ (
316
- originDependency
317
- ).sourceOrder;
321
+ /** @type {HarmonyImportSideEffectDependency | HarmonyImportSpecifierDependency} */
322
+ (originDependency).sourceOrder;
318
323
  if (
319
324
  typeof currentSourceOrder === "number" &&
320
325
  typeof originSourceOrder === "number"
@@ -329,17 +334,28 @@ class ModuleGraph {
329
334
  sub: originSourceOrder
330
335
  });
331
336
 
337
+ // Save for later batch sorting
338
+ this._modulesNeedingSort.add(parentModule);
339
+ }
340
+ }
341
+
342
+ /**
343
+ * @returns {void}
344
+ */
345
+ finishUpdateParent() {
346
+ if (this._modulesNeedingSort.size === 0) {
347
+ return;
348
+ }
349
+ for (const mod of this._modulesNeedingSort) {
332
350
  // If dependencies like HarmonyImportSideEffectDependency and HarmonyImportSpecifierDependency have a SourceOrder,
333
351
  // we sort based on it; otherwise, we preserve the original order.
334
352
  sortWithSourceOrder(
335
- parentModule.dependencies,
336
- this._dependencySourceOrderMap
353
+ mod.dependencies,
354
+ this._dependencySourceOrderMap,
355
+ (dep, index) => this.setParentDependenciesBlockIndex(dep, index)
337
356
  );
338
-
339
- for (const [index, dep] of parentModule.dependencies.entries()) {
340
- this.setParentDependenciesBlockIndex(dep, index);
341
- }
342
357
  }
358
+ this._modulesNeedingSort.clear();
343
359
  }
344
360
 
345
361
  /**
@@ -844,7 +860,7 @@ class ModuleGraph {
844
860
  ) {
845
861
  continue;
846
862
  }
847
- if (connection.dependency.defer) return true;
863
+ if (ImportPhaseUtils.isDefer(connection.dependency.phase)) return true;
848
864
  }
849
865
  return false;
850
866
  }
@@ -52,8 +52,8 @@ const intersectConnectionStates = (a, b) => {
52
52
 
53
53
  class ModuleGraphConnection {
54
54
  /**
55
- * @param {Module|null} originModule the referencing module
56
- * @param {Dependency|null} dependency the referencing dependency
55
+ * @param {Module | null} originModule the referencing module
56
+ * @param {Dependency | null} dependency the referencing dependency
57
57
  * @param {Module} module the referenced module
58
58
  * @param {string=} explanation some extra detail
59
59
  * @param {boolean=} weak the reference is weak