webpack 5.97.1 → 5.99.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (262) hide show
  1. package/README.md +345 -392
  2. package/lib/AsyncDependenciesBlock.js +3 -1
  3. package/lib/BannerPlugin.js +1 -1
  4. package/lib/Cache.js +9 -7
  5. package/lib/CacheFacade.js +5 -5
  6. package/lib/Chunk.js +2 -2
  7. package/lib/ChunkGraph.js +21 -16
  8. package/lib/ChunkTemplate.js +6 -6
  9. package/lib/CleanPlugin.js +10 -10
  10. package/lib/CodeGenerationResults.js +4 -3
  11. package/lib/CompatibilityPlugin.js +4 -1
  12. package/lib/Compilation.js +343 -167
  13. package/lib/Compiler.js +13 -18
  14. package/lib/ConditionalInitFragment.js +1 -1
  15. package/lib/ConstPlugin.js +5 -3
  16. package/lib/ContextModule.js +4 -2
  17. package/lib/ContextModuleFactory.js +3 -3
  18. package/lib/ContextReplacementPlugin.js +43 -16
  19. package/lib/DefinePlugin.js +25 -24
  20. package/lib/DelegatedModule.js +4 -2
  21. package/lib/DelegatedModuleFactoryPlugin.js +2 -1
  22. package/lib/Dependency.js +19 -13
  23. package/lib/DependencyTemplates.js +4 -3
  24. package/lib/DllModule.js +4 -2
  25. package/lib/DllModuleFactory.js +2 -2
  26. package/lib/DllReferencePlugin.js +2 -1
  27. package/lib/DynamicEntryPlugin.js +1 -1
  28. package/lib/EnvironmentPlugin.js +4 -2
  29. package/lib/ExportsInfo.js +72 -40
  30. package/lib/ExternalModule.js +14 -5
  31. package/lib/ExternalModuleFactoryPlugin.js +24 -12
  32. package/lib/FileSystemInfo.js +129 -94
  33. package/lib/FlagDependencyExportsPlugin.js +6 -4
  34. package/lib/FlagDependencyUsagePlugin.js +1 -1
  35. package/lib/Generator.js +29 -1
  36. package/lib/HookWebpackError.js +2 -2
  37. package/lib/HotModuleReplacementPlugin.js +3 -9
  38. package/lib/IgnoreErrorModuleFactory.js +2 -2
  39. package/lib/IgnorePlugin.js +0 -5
  40. package/lib/InitFragment.js +1 -1
  41. package/lib/LoaderOptionsPlugin.js +8 -5
  42. package/lib/MainTemplate.js +7 -7
  43. package/lib/Module.js +44 -21
  44. package/lib/ModuleBuildError.js +3 -1
  45. package/lib/ModuleDependencyError.js +4 -3
  46. package/lib/ModuleDependencyWarning.js +4 -3
  47. package/lib/ModuleFactory.js +9 -3
  48. package/lib/ModuleFilenameHelpers.js +21 -39
  49. package/lib/ModuleGraph.js +20 -14
  50. package/lib/ModuleGraphConnection.js +7 -13
  51. package/lib/ModuleNotFoundError.js +1 -1
  52. package/lib/ModuleParseError.js +2 -1
  53. package/lib/ModuleSourceTypesConstants.js +11 -0
  54. package/lib/ModuleTemplate.js +5 -5
  55. package/lib/ModuleTypeConstants.js +15 -0
  56. package/lib/MultiCompiler.js +4 -4
  57. package/lib/MultiStats.js +1 -1
  58. package/lib/NormalModule.js +101 -54
  59. package/lib/NormalModuleFactory.js +38 -33
  60. package/lib/NormalModuleReplacementPlugin.js +3 -2
  61. package/lib/NullFactory.js +2 -2
  62. package/lib/Parser.js +4 -3
  63. package/lib/ProgressPlugin.js +1 -2
  64. package/lib/RawModule.js +4 -2
  65. package/lib/RecordIdsPlugin.js +6 -2
  66. package/lib/RequestShortener.js +3 -1
  67. package/lib/ResolverFactory.js +12 -9
  68. package/lib/RuntimeModule.js +4 -2
  69. package/lib/RuntimeTemplate.js +2 -1
  70. package/lib/SelfModuleFactory.js +2 -2
  71. package/lib/SourceMapDevToolPlugin.js +0 -8
  72. package/lib/Template.js +6 -5
  73. package/lib/TemplatedPathPlugin.js +15 -12
  74. package/lib/WebpackOptionsApply.js +1 -1
  75. package/lib/asset/AssetGenerator.js +237 -170
  76. package/lib/asset/AssetModulesPlugin.js +52 -10
  77. package/lib/asset/AssetSourceGenerator.js +18 -0
  78. package/lib/asset/RawDataUrlModule.js +4 -2
  79. package/lib/buildChunkGraph.js +14 -4
  80. package/lib/cache/MemoryWithGcCachePlugin.js +6 -2
  81. package/lib/cache/PackFileCacheStrategy.js +137 -121
  82. package/lib/cache/ResolverCachePlugin.js +15 -7
  83. package/lib/cache/getLazyHashedEtag.js +4 -3
  84. package/lib/cli.js +23 -15
  85. package/lib/config/defaults.js +104 -28
  86. package/lib/config/normalization.js +14 -13
  87. package/lib/config/target.js +8 -8
  88. package/lib/container/ContainerEntryModule.js +4 -2
  89. package/lib/container/ContainerEntryModuleFactory.js +2 -2
  90. package/lib/container/FallbackModule.js +4 -2
  91. package/lib/container/FallbackModuleFactory.js +2 -2
  92. package/lib/container/RemoteModule.js +4 -2
  93. package/lib/container/options.js +5 -5
  94. package/lib/css/CssGenerator.js +71 -9
  95. package/lib/css/CssLoadingRuntimeModule.js +5 -2
  96. package/lib/css/CssModulesPlugin.js +41 -9
  97. package/lib/css/CssParser.js +37 -17
  98. package/lib/css/walkCssTokens.js +17 -17
  99. package/lib/debug/ProfilingPlugin.js +98 -38
  100. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +10 -4
  101. package/lib/dependencies/AMDPlugin.js +5 -2
  102. package/lib/dependencies/AMDRequireArrayDependency.js +4 -3
  103. package/lib/dependencies/AMDRequireContextDependency.js +2 -1
  104. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +15 -7
  105. package/lib/dependencies/AMDRuntimeModules.js +3 -1
  106. package/lib/dependencies/CommonJsExportRequireDependency.js +4 -5
  107. package/lib/dependencies/CommonJsExportsParserPlugin.js +3 -3
  108. package/lib/dependencies/CommonJsImportsParserPlugin.js +38 -7
  109. package/lib/dependencies/CommonJsRequireContextDependency.js +2 -1
  110. package/lib/dependencies/ContextDependencyHelpers.js +13 -6
  111. package/lib/dependencies/CssIcssExportDependency.js +15 -12
  112. package/lib/dependencies/CssIcssImportDependency.js +4 -1
  113. package/lib/dependencies/CssLocalIdentifierDependency.js +18 -15
  114. package/lib/dependencies/ExportsInfoDependency.js +6 -1
  115. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +5 -5
  116. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +22 -15
  117. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +23 -8
  118. package/lib/dependencies/HarmonyExportSpecifierDependency.js +2 -2
  119. package/lib/dependencies/HarmonyImportDependency.js +8 -6
  120. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +19 -14
  121. package/lib/dependencies/HarmonyImportSpecifierDependency.js +5 -5
  122. package/lib/dependencies/ImportContextDependency.js +2 -1
  123. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +8 -4
  124. package/lib/dependencies/JsonExportsDependency.js +49 -27
  125. package/lib/dependencies/LoaderPlugin.js +4 -14
  126. package/lib/dependencies/RequireContextDependency.js +2 -1
  127. package/lib/dependencies/RequireContextDependencyParserPlugin.js +6 -3
  128. package/lib/dependencies/RequireEnsureDependenciesBlock.js +3 -2
  129. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +11 -5
  130. package/lib/dependencies/RequireResolveContextDependency.js +1 -1
  131. package/lib/dependencies/WebAssemblyExportImportedDependency.js +1 -1
  132. package/lib/dependencies/WorkerDependency.js +6 -3
  133. package/lib/dependencies/WorkerPlugin.js +100 -41
  134. package/lib/esm/ModuleChunkFormatPlugin.js +5 -0
  135. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +3 -2
  136. package/lib/hmr/HotModuleReplacement.runtime.js +1 -0
  137. package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +1 -0
  138. package/lib/hmr/LazyCompilationPlugin.js +32 -24
  139. package/lib/hmr/lazyCompilationBackend.js +1 -1
  140. package/lib/ids/DeterministicModuleIdsPlugin.js +1 -1
  141. package/lib/ids/HashedModuleIdsPlugin.js +2 -1
  142. package/lib/ids/IdHelpers.js +15 -14
  143. package/lib/ids/SyncModuleIdsPlugin.js +9 -5
  144. package/lib/index.js +5 -5
  145. package/lib/javascript/BasicEvaluatedExpression.js +6 -6
  146. package/lib/javascript/JavascriptGenerator.js +11 -1
  147. package/lib/javascript/JavascriptModulesPlugin.js +54 -34
  148. package/lib/javascript/JavascriptParser.js +272 -188
  149. package/lib/javascript/JavascriptParserHelpers.js +10 -9
  150. package/lib/javascript/StartupHelpers.js +4 -1
  151. package/lib/json/JsonData.js +4 -4
  152. package/lib/json/JsonGenerator.js +54 -22
  153. package/lib/json/JsonModulesPlugin.js +16 -3
  154. package/lib/json/JsonParser.js +10 -4
  155. package/lib/library/AbstractLibraryPlugin.js +7 -3
  156. package/lib/library/AssignLibraryPlugin.js +29 -1
  157. package/lib/library/EnableLibraryPlugin.js +7 -10
  158. package/lib/library/ExportPropertyLibraryPlugin.js +4 -1
  159. package/lib/library/ModuleLibraryPlugin.js +121 -15
  160. package/lib/logging/Logger.js +2 -2
  161. package/lib/logging/createConsoleLogger.js +4 -4
  162. package/lib/node/NodeEnvironmentPlugin.js +6 -2
  163. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +1 -2
  164. package/lib/node/ReadFileCompileWasmPlugin.js +1 -2
  165. package/lib/node/nodeConsole.js +3 -1
  166. package/lib/optimize/AggressiveSplittingPlugin.js +1 -1
  167. package/lib/optimize/ConcatenatedModule.js +19 -12
  168. package/lib/optimize/InnerGraph.js +3 -2
  169. package/lib/optimize/InnerGraphPlugin.js +13 -7
  170. package/lib/optimize/LimitChunkCountPlugin.js +20 -0
  171. package/lib/optimize/ModuleConcatenationPlugin.js +10 -7
  172. package/lib/optimize/RealContentHashPlugin.js +8 -4
  173. package/lib/optimize/SideEffectsFlagPlugin.js +4 -2
  174. package/lib/optimize/SplitChunksPlugin.js +90 -65
  175. package/lib/rules/BasicEffectRulePlugin.js +9 -1
  176. package/lib/rules/BasicMatcherRulePlugin.js +15 -4
  177. package/lib/rules/ObjectMatcherRulePlugin.js +12 -3
  178. package/lib/rules/RuleSetCompiler.js +25 -14
  179. package/lib/rules/UseEffectRulePlugin.js +47 -17
  180. package/lib/runtime/AutoPublicPathRuntimeModule.js +1 -1
  181. package/lib/runtime/GetChunkFilenameRuntimeModule.js +6 -6
  182. package/lib/schemes/HttpUriPlugin.js +38 -17
  183. package/lib/serialization/BinaryMiddleware.js +52 -19
  184. package/lib/serialization/FileMiddleware.js +78 -48
  185. package/lib/serialization/ObjectMiddleware.js +78 -29
  186. package/lib/serialization/PlainObjectSerializer.js +1 -1
  187. package/lib/serialization/Serializer.js +15 -10
  188. package/lib/serialization/SerializerMiddleware.js +80 -41
  189. package/lib/serialization/SingleItemMiddleware.js +10 -7
  190. package/lib/serialization/types.js +1 -1
  191. package/lib/sharing/ConsumeSharedModule.js +4 -2
  192. package/lib/sharing/ProvideSharedModule.js +4 -2
  193. package/lib/sharing/ProvideSharedModuleFactory.js +5 -3
  194. package/lib/sharing/utils.js +2 -2
  195. package/lib/stats/DefaultStatsFactoryPlugin.js +81 -79
  196. package/lib/stats/DefaultStatsPresetPlugin.js +43 -23
  197. package/lib/stats/DefaultStatsPrinterPlugin.js +85 -43
  198. package/lib/stats/StatsFactory.js +11 -11
  199. package/lib/stats/StatsPrinter.js +7 -7
  200. package/lib/util/ArrayHelpers.js +2 -4
  201. package/lib/util/ArrayQueue.js +1 -1
  202. package/lib/util/AsyncQueue.js +4 -4
  203. package/lib/util/IterableHelpers.js +1 -1
  204. package/lib/util/LazyBucketSortedSet.js +41 -23
  205. package/lib/util/LazySet.js +3 -2
  206. package/lib/util/MapHelpers.js +1 -1
  207. package/lib/util/ParallelismFactorCalculator.js +1 -1
  208. package/lib/util/Semaphore.js +3 -3
  209. package/lib/util/SetHelpers.js +1 -1
  210. package/lib/util/SortableSet.js +9 -7
  211. package/lib/util/TupleQueue.js +9 -8
  212. package/lib/util/TupleSet.js +2 -2
  213. package/lib/util/WeakTupleMap.js +12 -11
  214. package/lib/util/binarySearchBounds.js +2 -1
  215. package/lib/util/cleverMerge.js +84 -54
  216. package/lib/util/comparators.js +22 -21
  217. package/lib/util/compileBooleanMatcher.js +3 -3
  218. package/lib/util/concatenate.js +6 -4
  219. package/lib/util/create-schema-validation.js +4 -4
  220. package/lib/util/createHash.js +2 -2
  221. package/lib/util/deprecation.js +35 -33
  222. package/lib/util/deterministicGrouping.js +6 -6
  223. package/lib/util/findGraphRoots.js +1 -1
  224. package/lib/util/fs.js +39 -39
  225. package/lib/util/hash/wasm-hash.js +2 -2
  226. package/lib/util/identifier.js +15 -18
  227. package/lib/util/makeSerializable.js +1 -1
  228. package/lib/util/memoize.js +4 -1
  229. package/lib/util/objectToMap.js +3 -2
  230. package/lib/util/processAsyncTree.js +2 -2
  231. package/lib/util/propertyName.js +0 -1
  232. package/lib/util/registerExternalSerializer.js +15 -18
  233. package/lib/util/removeBOM.js +25 -0
  234. package/lib/util/runtime.js +34 -27
  235. package/lib/util/semver.js +18 -8
  236. package/lib/util/serialization.js +5 -16
  237. package/lib/util/smartGrouping.js +3 -3
  238. package/lib/validateSchema.js +4 -7
  239. package/lib/wasm/EnableWasmLoadingPlugin.js +2 -2
  240. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +3 -3
  241. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +11 -0
  242. package/lib/wasm-async/AsyncWebAssemblyJavascriptGenerator.js +10 -0
  243. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +2 -2
  244. package/lib/wasm-async/UniversalCompileAsyncWasmPlugin.js +4 -0
  245. package/lib/wasm-sync/WebAssemblyGenerator.js +28 -12
  246. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +10 -0
  247. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +2 -2
  248. package/lib/wasm-sync/WebAssemblyParser.js +9 -4
  249. package/lib/web/JsonpChunkLoadingRuntimeModule.js +4 -2
  250. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +3 -0
  251. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +30 -25
  252. package/package.json +27 -26
  253. package/schemas/WebpackOptions.check.js +1 -1
  254. package/schemas/WebpackOptions.json +48 -13
  255. package/schemas/plugins/JsonModulesPluginGenerator.check.d.ts +7 -0
  256. package/schemas/plugins/JsonModulesPluginGenerator.check.js +6 -0
  257. package/schemas/plugins/JsonModulesPluginGenerator.json +11 -0
  258. package/schemas/plugins/JsonModulesPluginParser.check.js +1 -1
  259. package/schemas/plugins/JsonModulesPluginParser.json +4 -0
  260. package/schemas/plugins/SourceMapDevToolPlugin.json +2 -2
  261. package/types.d.ts +888 -583
  262. package/lib/library/ModernModuleLibraryPlugin.js +0 -144
@@ -14,46 +14,66 @@ const NullDependency = require("./NullDependency");
14
14
  /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
15
15
  /** @typedef {import("../ModuleGraph")} ModuleGraph */
16
16
  /** @typedef {import("../json/JsonData")} JsonData */
17
- /** @typedef {import("../json/JsonData").RawJsonData} RawJsonData */
17
+ /** @typedef {import("../json/JsonData").JsonValue} JsonValue */
18
18
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
19
19
  /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
20
20
  /** @typedef {import("../util/Hash")} Hash */
21
21
 
22
22
  /**
23
- * @param {RawJsonData} data data
24
- * @returns {TODO} value
23
+ * @callback GetExportsFromDataFn
24
+ * @param {JsonValue} data raw json data
25
+ * @param {number} [curDepth] current depth
26
+ * @returns {ExportSpec[] | null} export spec or nothing
25
27
  */
26
- const getExportsFromData = data => {
27
- if (data && typeof data === "object") {
28
- if (Array.isArray(data)) {
29
- return data.length < 100
30
- ? data.map((item, idx) => ({
31
- name: `${idx}`,
32
- canMangle: true,
33
- exports: getExportsFromData(item)
34
- }))
35
- : undefined;
28
+
29
+ /**
30
+ * @param {number} exportsDepth exportsDepth
31
+ * @returns {GetExportsFromDataFn} value
32
+ */
33
+ const getExportsWithDepth = exportsDepth =>
34
+ /** @type {GetExportsFromDataFn} */
35
+ function getExportsFromData(data, curDepth = 1) {
36
+ if (curDepth > exportsDepth) {
37
+ return null;
36
38
  }
37
- const exports = [];
38
- for (const key of Object.keys(data)) {
39
- exports.push({
40
- name: key,
41
- canMangle: true,
42
- exports: getExportsFromData(data[key])
43
- });
39
+
40
+ if (data && typeof data === "object") {
41
+ if (Array.isArray(data)) {
42
+ return data.length < 100
43
+ ? data.map((item, idx) => ({
44
+ name: `${idx}`,
45
+ canMangle: true,
46
+ exports: getExportsFromData(item, curDepth + 1) || undefined
47
+ }))
48
+ : null;
49
+ }
50
+
51
+ /** @type {ExportSpec[]} */
52
+ const exports = [];
53
+
54
+ for (const key of Object.keys(data)) {
55
+ exports.push({
56
+ name: key,
57
+ canMangle: true,
58
+ exports: getExportsFromData(data[key], curDepth + 1) || undefined
59
+ });
60
+ }
61
+
62
+ return exports;
44
63
  }
45
- return exports;
46
- }
47
- return undefined;
48
- };
64
+
65
+ return null;
66
+ };
49
67
 
50
68
  class JsonExportsDependency extends NullDependency {
51
69
  /**
52
70
  * @param {JsonData} data json data
71
+ * @param {number} exportsDepth the depth of json exports to analyze
53
72
  */
54
- constructor(data) {
73
+ constructor(data, exportsDepth) {
55
74
  super();
56
75
  this.data = data;
76
+ this.exportsDepth = exportsDepth;
57
77
  }
58
78
 
59
79
  get type() {
@@ -67,8 +87,8 @@ class JsonExportsDependency extends NullDependency {
67
87
  */
68
88
  getExports(moduleGraph) {
69
89
  return {
70
- exports: getExportsFromData(
71
- this.data && /** @type {RawJsonData} */ (this.data.get())
90
+ exports: getExportsWithDepth(this.exportsDepth)(
91
+ this.data && /** @type {JsonValue} */ (this.data.get())
72
92
  ),
73
93
  dependencies: undefined
74
94
  };
@@ -90,6 +110,7 @@ class JsonExportsDependency extends NullDependency {
90
110
  serialize(context) {
91
111
  const { write } = context;
92
112
  write(this.data);
113
+ write(this.exportsDepth);
93
114
  super.serialize(context);
94
115
  }
95
116
 
@@ -99,6 +120,7 @@ class JsonExportsDependency extends NullDependency {
99
120
  deserialize(context) {
100
121
  const { read } = context;
101
122
  this.data = read();
123
+ this.exportsDepth = read();
102
124
  super.deserialize(context);
103
125
  }
104
126
  }
@@ -12,6 +12,7 @@ const LoaderImportDependency = require("./LoaderImportDependency");
12
12
 
13
13
  /** @typedef {import("../../declarations/LoaderContext").LoaderPluginLoaderContext} LoaderPluginLoaderContext */
14
14
  /** @typedef {import("../Compilation").DepConstructor} DepConstructor */
15
+ /** @typedef {import("../Compilation").ExecuteModuleExports} ExecuteModuleExports */
15
16
  /** @typedef {import("../Compilation").ExecuteModuleResult} ExecuteModuleResult */
16
17
  /** @typedef {import("../Compiler")} Compiler */
17
18
  /** @typedef {import("../Module")} Module */
@@ -20,7 +21,7 @@ const LoaderImportDependency = require("./LoaderImportDependency");
20
21
  /**
21
22
  * @callback ImportModuleCallback
22
23
  * @param {(Error | null)=} err error object
23
- * @param {any=} exports exports of the evaluated module
24
+ * @param {ExecuteModuleExports=} exports exports of the evaluated module
24
25
  */
25
26
 
26
27
  /**
@@ -31,11 +32,6 @@ const LoaderImportDependency = require("./LoaderImportDependency");
31
32
  */
32
33
 
33
34
  class LoaderPlugin {
34
- /**
35
- * @param {object} options options
36
- */
37
- constructor(options = {}) {}
38
-
39
35
  /**
40
36
  * Apply the plugin
41
37
  * @param {Compiler} compiler the compiler instance
@@ -150,12 +146,7 @@ class LoaderPlugin {
150
146
  for (const d of buildDependencies) {
151
147
  loaderContext.addBuildDependency(d);
152
148
  }
153
- return callback(
154
- null,
155
- source,
156
- /** @type {object | null} */ (map),
157
- referencedModule
158
- );
149
+ return callback(null, source, map, referencedModule);
159
150
  }
160
151
  );
161
152
  };
@@ -271,8 +262,7 @@ class LoaderPlugin {
271
262
  );
272
263
  };
273
264
 
274
- // eslint-disable-next-line no-warning-comments
275
- // @ts-ignore Overloading doesn't work
265
+ // @ts-expect-error overloading doesn't work
276
266
  loaderContext.importModule = (request, options, callback) => {
277
267
  if (!callback) {
278
268
  return new Promise((resolve, reject) => {
@@ -10,10 +10,11 @@ const ContextDependency = require("./ContextDependency");
10
10
  const ModuleDependencyTemplateAsRequireId = require("./ModuleDependencyTemplateAsRequireId");
11
11
 
12
12
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
13
+ /** @typedef {import("./ContextDependency").ContextDependencyOptions} ContextDependencyOptions */
13
14
 
14
15
  class RequireContextDependency extends ContextDependency {
15
16
  /**
16
- * @param {TODO} options options
17
+ * @param {ContextDependencyOptions} options options
17
18
  * @param {Range} range range
18
19
  */
19
20
  constructor(options, range) {
@@ -7,6 +7,7 @@
7
7
 
8
8
  const RequireContextDependency = require("./RequireContextDependency");
9
9
 
10
+ /** @typedef {import("../ContextModule").ContextMode} ContextMode */
10
11
  /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
11
12
  /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
12
13
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
@@ -22,12 +23,13 @@ module.exports = class RequireContextDependencyParserPlugin {
22
23
  .tap("RequireContextDependencyParserPlugin", expr => {
23
24
  let regExp = /^\.\/.*$/;
24
25
  let recursive = true;
26
+ /** @type {ContextMode} */
25
27
  let mode = "sync";
26
28
  switch (expr.arguments.length) {
27
29
  case 4: {
28
30
  const modeExpr = parser.evaluateExpression(expr.arguments[3]);
29
31
  if (!modeExpr.isString()) return;
30
- mode = /** @type {string} */ (modeExpr.string);
32
+ mode = /** @type {ContextMode} */ (modeExpr.string);
31
33
  }
32
34
  // falls through
33
35
  case 3: {
@@ -47,13 +49,14 @@ module.exports = class RequireContextDependencyParserPlugin {
47
49
  if (!requestExpr.isString()) return;
48
50
  const dep = new RequireContextDependency(
49
51
  {
50
- request: requestExpr.string,
52
+ request: /** @type {string} */ (requestExpr.string),
51
53
  recursive,
52
54
  regExp,
53
55
  mode,
54
56
  category: "commonjs"
55
57
  },
56
- /** @type {Range} */ (expr.range)
58
+ /** @type {Range} */
59
+ (expr.range)
57
60
  );
58
61
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
59
62
  dep.optional = Boolean(parser.scope.inTry);
@@ -8,13 +8,14 @@
8
8
  const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
9
9
  const makeSerializable = require("../util/makeSerializable");
10
10
 
11
+ /** @typedef {import("../AsyncDependenciesBlock").GroupOptions} GroupOptions */
11
12
  /** @typedef {import("../ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
12
13
  /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
13
14
 
14
15
  class RequireEnsureDependenciesBlock extends AsyncDependenciesBlock {
15
16
  /**
16
- * @param {ChunkGroupOptions & { entryOptions?: TODO }} chunkName chunk name
17
- * @param {DependencyLocation} loc location info
17
+ * @param {GroupOptions | null} chunkName chunk name
18
+ * @param {(DependencyLocation | null)=} loc location info
18
19
  */
19
20
  constructor(chunkName, loc) {
20
21
  super(chunkName, loc, null);
@@ -10,6 +10,7 @@ const RequireEnsureDependency = require("./RequireEnsureDependency");
10
10
  const RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
11
11
  const getFunctionExpression = require("./getFunctionExpression");
12
12
 
13
+ /** @typedef {import("../AsyncDependenciesBlock").GroupOptions} GroupOptions */
13
14
  /** @typedef {import("../ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */
14
15
  /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
15
16
  /** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
@@ -25,6 +26,7 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
25
26
  parser.hooks.call
26
27
  .for("require.ensure")
27
28
  .tap("RequireEnsureDependenciesBlockParserPlugin", expr => {
29
+ /** @type {string | GroupOptions | null} */
28
30
  let chunkName = null;
29
31
  let errorExpressionArg = null;
30
32
  let errorExpression = null;
@@ -32,7 +34,9 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
32
34
  case 4: {
33
35
  const chunkNameExpr = parser.evaluateExpression(expr.arguments[3]);
34
36
  if (!chunkNameExpr.isString()) return;
35
- chunkName = chunkNameExpr.string;
37
+ chunkName =
38
+ /** @type {string} */
39
+ (chunkNameExpr.string);
36
40
  }
37
41
  // falls through
38
42
  case 3: {
@@ -44,7 +48,9 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
44
48
  expr.arguments[2]
45
49
  );
46
50
  if (!chunkNameExpr.isString()) return;
47
- chunkName = chunkNameExpr.string;
51
+ chunkName =
52
+ /** @type {string} */
53
+ (chunkNameExpr.string);
48
54
  }
49
55
  }
50
56
  // falls through
@@ -70,9 +76,9 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
70
76
  }
71
77
 
72
78
  const depBlock = new RequireEnsureDependenciesBlock(
73
- /** @type {ChunkGroupOptions & { entryOptions?: TODO }} */
74
- (chunkName),
75
- /** @type {DependencyLocation} */ (expr.loc)
79
+ chunkName,
80
+ /** @type {DependencyLocation} */
81
+ (expr.loc)
76
82
  );
77
83
  const errorCallbackExists =
78
84
  expr.arguments.length === 4 ||
@@ -19,7 +19,7 @@ class RequireResolveContextDependency extends ContextDependency {
19
19
  * @param {ContextDependencyOptions} options options
20
20
  * @param {Range} range range
21
21
  * @param {Range} valueRange value range
22
- * @param {TODO} context context
22
+ * @param {string=} context context
23
23
  */
24
24
  constructor(options, range, valueRange, context) {
25
25
  super(options, context);
@@ -21,7 +21,7 @@ class WebAssemblyExportImportedDependency extends ModuleDependency {
21
21
  * @param {string} exportName export name
22
22
  * @param {string} request request
23
23
  * @param {string} name name
24
- * @param {TODO} valueType value type
24
+ * @param {string} valueType value type
25
25
  */
26
26
  constructor(exportName, request, name, valueType) {
27
27
  super(request);
@@ -30,6 +30,7 @@ class WorkerDependency extends ModuleDependency {
30
30
  * @param {Range} range range
31
31
  * @param {object} workerDependencyOptions options
32
32
  * @param {string=} workerDependencyOptions.publicPath public path for the worker
33
+ * @param {boolean=} workerDependencyOptions.needNewUrl need generate `new URL(...)`
33
34
  */
34
35
  constructor(request, range, workerDependencyOptions) {
35
36
  super(request);
@@ -118,12 +119,14 @@ WorkerDependency.Template = class WorkerDependencyTemplate extends (
118
119
  runtimeRequirements.add(RuntimeGlobals.baseURI);
119
120
  runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
120
121
 
122
+ const workerImportStr = `/* worker import */ ${workerImportBaseUrl} + ${
123
+ RuntimeGlobals.getChunkScriptFilename
124
+ }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`;
125
+
121
126
  source.replace(
122
127
  dep.range[0],
123
128
  dep.range[1] - 1,
124
- `/* worker import */ ${workerImportBaseUrl} + ${
125
- RuntimeGlobals.getChunkScriptFilename
126
- }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`
129
+ dep.options.needNewUrl ? `new URL(${workerImportStr})` : workerImportStr
127
130
  );
128
131
  }
129
132
  };
@@ -27,6 +27,8 @@ const WorkerDependency = require("./WorkerDependency");
27
27
 
28
28
  /** @typedef {import("estree").CallExpression} CallExpression */
29
29
  /** @typedef {import("estree").Expression} Expression */
30
+ /** @typedef {import("estree").Identifier} Identifier */
31
+ /** @typedef {import("estree").MemberExpression} MemberExpression */
30
32
  /** @typedef {import("estree").ObjectExpression} ObjectExpression */
31
33
  /** @typedef {import("estree").Pattern} Pattern */
32
34
  /** @typedef {import("estree").Property} Property */
@@ -117,45 +119,81 @@ class WorkerPlugin {
117
119
  /**
118
120
  * @param {JavascriptParser} parser the parser
119
121
  * @param {Expression} expr expression
120
- * @returns {[BasicEvaluatedExpression, [number, number]] | void} parsed
122
+ * @returns {[string, [number, number]] | void} parsed
121
123
  */
122
124
  const parseModuleUrl = (parser, expr) => {
123
- if (
124
- expr.type !== "NewExpression" ||
125
- expr.callee.type === "Super" ||
126
- expr.arguments.length !== 2
127
- )
125
+ if (expr.type !== "NewExpression" || expr.callee.type === "Super")
128
126
  return;
129
- const [arg1, arg2] = expr.arguments;
130
- if (arg1.type === "SpreadElement") return;
131
- if (arg2.type === "SpreadElement") return;
132
- const callee = parser.evaluateExpression(expr.callee);
133
- if (!callee.isIdentifier() || callee.identifier !== "URL") return;
134
- const arg2Value = parser.evaluateExpression(arg2);
135
127
  if (
136
- !arg2Value.isString() ||
137
- !(/** @type {string} */ (arg2Value.string).startsWith("file://")) ||
138
- arg2Value.string !== getUrl(parser.state.module)
128
+ expr.arguments.length === 1 &&
129
+ expr.arguments[0].type === "MemberExpression" &&
130
+ isMetaUrl(parser, expr.arguments[0])
139
131
  ) {
140
- return;
132
+ const arg1 = expr.arguments[0];
133
+ return [
134
+ getUrl(parser.state.module),
135
+ [
136
+ /** @type {Range} */ (arg1.range)[0],
137
+ /** @type {Range} */ (arg1.range)[1]
138
+ ]
139
+ ];
140
+ } else if (expr.arguments.length === 2) {
141
+ const [arg1, arg2] = expr.arguments;
142
+ if (arg1.type === "SpreadElement") return;
143
+ if (arg2.type === "SpreadElement") return;
144
+ const callee = parser.evaluateExpression(expr.callee);
145
+ if (!callee.isIdentifier() || callee.identifier !== "URL") return;
146
+ const arg2Value = parser.evaluateExpression(arg2);
147
+ if (
148
+ !arg2Value.isString() ||
149
+ !(
150
+ /** @type {string} */ (arg2Value.string).startsWith("file://")
151
+ ) ||
152
+ arg2Value.string !== getUrl(parser.state.module)
153
+ ) {
154
+ return;
155
+ }
156
+ const arg1Value = parser.evaluateExpression(arg1);
157
+ if (!arg1Value.isString()) return;
158
+ return [
159
+ /** @type {string} */ (arg1Value.string),
160
+ [
161
+ /** @type {Range} */ (arg1.range)[0],
162
+ /** @type {Range} */ (arg2.range)[1]
163
+ ]
164
+ ];
141
165
  }
142
- const arg1Value = parser.evaluateExpression(arg1);
143
- return [
144
- arg1Value,
145
- [
146
- /** @type {Range} */ (arg1.range)[0],
147
- /** @type {Range} */ (arg2.range)[1]
148
- ]
149
- ];
150
166
  };
151
167
 
168
+ /**
169
+ * @param {JavascriptParser} parser the parser
170
+ * @param {MemberExpression} expr expression
171
+ * @returns {boolean} is `import.meta.url`
172
+ */
173
+ const isMetaUrl = (parser, expr) => {
174
+ const chain = parser.extractMemberExpressionChain(expr);
175
+
176
+ if (
177
+ chain.members.length !== 1 ||
178
+ chain.object.type !== "MetaProperty" ||
179
+ chain.object.meta.name !== "import" ||
180
+ chain.object.property.name !== "meta" ||
181
+ chain.members[0] !== "url"
182
+ )
183
+ return false;
184
+
185
+ return true;
186
+ };
187
+
188
+ /** @typedef {Record<string, EXPECTED_ANY>} Values */
189
+
152
190
  /**
153
191
  * @param {JavascriptParser} parser the parser
154
192
  * @param {ObjectExpression} expr expression
155
- * @returns {{ expressions: Record<string, Expression | Pattern>, otherElements: (Property | SpreadElement)[], values: Record<string, any>, spread: boolean, insertType: "comma" | "single", insertLocation: number }} parsed object
193
+ * @returns {{ expressions: Record<string, Expression | Pattern>, otherElements: (Property | SpreadElement)[], values: Values, spread: boolean, insertType: "comma" | "single", insertLocation: number }} parsed object
156
194
  */
157
195
  const parseObjectExpression = (parser, expr) => {
158
- /** @type {Record<string, any>} */
196
+ /** @type {Values} */
159
197
  const values = {};
160
198
  /** @type {Record<string, Expression | Pattern>} */
161
199
  const expressions = {};
@@ -174,7 +212,8 @@ class WorkerPlugin {
174
212
  expressions[prop.key.name] = prop.value;
175
213
  if (!prop.shorthand && !prop.value.type.endsWith("Pattern")) {
176
214
  const value = parser.evaluateExpression(
177
- /** @type {Expression} */ (prop.value)
215
+ /** @type {Expression} */
216
+ (prop.value)
178
217
  );
179
218
  if (value.isCompileTimeValue())
180
219
  values[prop.key.name] = value.asCompileTimeValue();
@@ -217,10 +256,27 @@ class WorkerPlugin {
217
256
  const [arg1, arg2] = expr.arguments;
218
257
  if (arg1.type === "SpreadElement") return;
219
258
  if (arg2 && arg2.type === "SpreadElement") return;
220
- const parsedUrl = parseModuleUrl(parser, arg1);
221
- if (!parsedUrl) return;
222
- const [url, range] = parsedUrl;
223
- if (!url.isString()) return;
259
+
260
+ /** @type {string} */
261
+ let url;
262
+ /** @type {[number, number]} */
263
+ let range;
264
+ /** @type {boolean} */
265
+ let needNewUrl = false;
266
+
267
+ if (arg1.type === "MemberExpression" && isMetaUrl(parser, arg1)) {
268
+ url = getUrl(parser.state.module);
269
+ range = [
270
+ /** @type {Range} */ (arg1.range)[0],
271
+ /** @type {Range} */ (arg1.range)[1]
272
+ ];
273
+ needNewUrl = true;
274
+ } else {
275
+ const parsedUrl = parseModuleUrl(parser, arg1);
276
+ if (!parsedUrl) return;
277
+ [url, range] = parsedUrl;
278
+ }
279
+
224
280
  const {
225
281
  expressions,
226
282
  otherElements,
@@ -234,7 +290,7 @@ class WorkerPlugin {
234
290
  /** @type {Record<string, Expression | Pattern>} */
235
291
  expressions: {},
236
292
  otherElements: [],
237
- /** @type {Record<string, any>} */
293
+ /** @type {Values} */
238
294
  values: {},
239
295
  spread: false,
240
296
  insertType: arg2 ? "spread" : "argument",
@@ -342,13 +398,10 @@ class WorkerPlugin {
342
398
  }
343
399
  });
344
400
  block.loc = expr.loc;
345
- const dep = new WorkerDependency(
346
- /** @type {string} */ (url.string),
347
- range,
348
- {
349
- publicPath: this._workerPublicPath
350
- }
351
- );
401
+ const dep = new WorkerDependency(url, range, {
402
+ publicPath: this._workerPublicPath,
403
+ needNewUrl
404
+ });
352
405
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
353
406
  block.addDependency(dep);
354
407
  parser.state.module.addBlock(block);
@@ -370,7 +423,7 @@ class WorkerPlugin {
370
423
  );
371
424
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
372
425
  parser.state.module.addPresentationalDependency(dep);
373
- /** @type {TODO} */
426
+ /** @type {EXPECTED_ANY} */
374
427
  (expressions).type = undefined;
375
428
  }
376
429
  } else if (insertType === "comma") {
@@ -406,7 +459,13 @@ class WorkerPlugin {
406
459
 
407
460
  parser.walkExpression(expr.callee);
408
461
  for (const key of Object.keys(expressions)) {
409
- if (expressions[key]) parser.walkExpression(expressions[key]);
462
+ if (expressions[key]) {
463
+ if (expressions[key].type.endsWith("Pattern")) continue;
464
+ parser.walkExpression(
465
+ /** @type {Expression} */
466
+ (expressions[key])
467
+ );
468
+ }
410
469
  }
411
470
  for (const prop of otherElements) {
412
471
  parser.walkProperty(prop);
@@ -151,6 +151,11 @@ class ModuleChunkFormatPlugin {
151
151
  let index = 0;
152
152
  for (let i = 0; i < entries.length; i++) {
153
153
  const [module, entrypoint] = entries[i];
154
+ if (
155
+ !chunkGraph.getModuleSourceTypes(module).has("javascript")
156
+ ) {
157
+ continue;
158
+ }
154
159
  const final = i + 1 === entries.length;
155
160
  const moduleId = chunkGraph.getModuleId(module);
156
161
  const chunks = getAllChunks(
@@ -93,7 +93,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
93
93
  (compilation.outputOptions.environment);
94
94
  const {
95
95
  runtimeTemplate,
96
- outputOptions: { importFunctionName, crossOriginLoading }
96
+ outputOptions: { importFunctionName, crossOriginLoading, charset }
97
97
  } = compilation;
98
98
  const fn = RuntimeGlobals.ensureChunkHandlers;
99
99
  const withBaseURI = this._runtimeRequirements.has(RuntimeGlobals.baseURI);
@@ -261,6 +261,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
261
261
  linkPrefetch.call(
262
262
  Template.asString([
263
263
  "var link = document.createElement('link');",
264
+ charset ? "link.charset = 'utf-8';" : "",
264
265
  crossOriginLoading
265
266
  ? `link.crossOrigin = ${JSON.stringify(
266
267
  crossOriginLoading
@@ -300,7 +301,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
300
301
  linkPreload.call(
301
302
  Template.asString([
302
303
  "var link = document.createElement('link');",
303
- "link.charset = 'utf-8';",
304
+ charset ? "link.charset = 'utf-8';" : "",
304
305
  `if (${RuntimeGlobals.scriptNonce}) {`,
305
306
  Template.indent(
306
307
  `link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
@@ -69,6 +69,7 @@ module.exports = function () {
69
69
  me.children.push(request);
70
70
  }
71
71
  } else {
72
+ // eslint-disable-next-line no-console
72
73
  console.warn(
73
74
  "[HMR] unexpected require(" +
74
75
  request +
@@ -109,6 +109,7 @@ module.exports = function () {
109
109
  var appliedUpdate = {};
110
110
 
111
111
  var warnUnexpectedRequire = function warnUnexpectedRequire(module) {
112
+ // eslint-disable-next-line no-console
112
113
  console.warn(
113
114
  "[HMR] unexpected require(" + module.id + ") to disposed module"
114
115
  );