webpack 5.104.1 → 5.105.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 (279) hide show
  1. package/bin/webpack.js +6 -3
  2. package/lib/APIPlugin.js +85 -15
  3. package/lib/AbstractMethodError.js +1 -0
  4. package/lib/AsyncDependenciesBlock.js +13 -1
  5. package/lib/AsyncDependencyToInitialChunkError.js +1 -0
  6. package/lib/AutomaticPrefetchPlugin.js +1 -1
  7. package/lib/BannerPlugin.js +2 -0
  8. package/lib/CacheFacade.js +2 -2
  9. package/lib/CaseSensitiveModulesWarning.js +1 -0
  10. package/lib/Chunk.js +8 -4
  11. package/lib/ChunkGraph.js +30 -9
  12. package/lib/ChunkGroup.js +6 -3
  13. package/lib/ChunkRenderError.js +2 -0
  14. package/lib/CleanPlugin.js +4 -1
  15. package/lib/CodeGenerationError.js +7 -1
  16. package/lib/CodeGenerationResults.js +10 -3
  17. package/lib/CommentCompilationWarning.js +2 -1
  18. package/lib/Compilation.js +280 -236
  19. package/lib/Compiler.js +3 -2
  20. package/lib/ConcatenationScope.js +2 -1
  21. package/lib/ConcurrentCompilationError.js +3 -3
  22. package/lib/ContextModule.js +248 -111
  23. package/lib/ContextModuleFactory.js +14 -2
  24. package/lib/DefinePlugin.js +3 -1
  25. package/lib/DelegatedModule.js +6 -0
  26. package/lib/DelegatedModuleFactoryPlugin.js +2 -0
  27. package/lib/Dependency.js +6 -1
  28. package/lib/DependencyTemplates.js +3 -2
  29. package/lib/DllModule.js +2 -0
  30. package/lib/DllReferencePlugin.js +1 -0
  31. package/lib/DotenvPlugin.js +11 -5
  32. package/lib/DynamicEntryPlugin.js +1 -0
  33. package/lib/EnvironmentNotSupportAsyncWarning.js +1 -0
  34. package/lib/EvalSourceMapDevToolPlugin.js +17 -13
  35. package/lib/ExportsInfo.js +8 -4
  36. package/lib/ExternalModule.js +16 -0
  37. package/lib/FalseIIFEUmdWarning.js +1 -0
  38. package/lib/FileSystemInfo.js +49 -25
  39. package/lib/FlagDependencyExportsPlugin.js +8 -1
  40. package/lib/FlagDependencyUsagePlugin.js +9 -7
  41. package/lib/HarmonyLinkingError.js +1 -0
  42. package/lib/HookWebpackError.js +2 -0
  43. package/lib/HotModuleReplacementPlugin.js +16 -2
  44. package/lib/InitFragment.js +1 -0
  45. package/lib/InvalidDependenciesModuleWarning.js +1 -0
  46. package/lib/LibManifestPlugin.js +1 -0
  47. package/lib/ManifestPlugin.js +4 -1
  48. package/lib/Module.js +7 -4
  49. package/lib/ModuleBuildError.js +3 -1
  50. package/lib/ModuleDependencyError.js +1 -0
  51. package/lib/ModuleDependencyWarning.js +1 -0
  52. package/lib/ModuleError.js +2 -1
  53. package/lib/ModuleFilenameHelpers.js +2 -1
  54. package/lib/ModuleGraph.js +3 -0
  55. package/lib/ModuleGraphConnection.js +10 -1
  56. package/lib/ModuleHashingError.js +2 -0
  57. package/lib/ModuleInfoHeaderPlugin.js +6 -1
  58. package/lib/ModuleNotFoundError.js +1 -0
  59. package/lib/ModuleParseError.js +5 -1
  60. package/lib/ModuleProfile.js +1 -1
  61. package/lib/ModuleRestoreError.js +2 -0
  62. package/lib/ModuleSourceTypeConstants.js +1 -11
  63. package/lib/ModuleStoreError.js +2 -0
  64. package/lib/ModuleWarning.js +3 -1
  65. package/lib/MultiCompiler.js +11 -7
  66. package/lib/MultiWatching.js +2 -2
  67. package/lib/NoModeWarning.js +1 -0
  68. package/lib/NodeStuffInWebError.js +1 -0
  69. package/lib/NormalModule.js +26 -5
  70. package/lib/NormalModuleFactory.js +20 -4
  71. package/lib/OptionsApply.js +3 -1
  72. package/lib/ProgressPlugin.js +4 -0
  73. package/lib/RawModule.js +2 -0
  74. package/lib/RuntimeModule.js +10 -0
  75. package/lib/RuntimePlugin.js +7 -9
  76. package/lib/RuntimeTemplate.js +14 -7
  77. package/lib/SourceMapDevToolPlugin.js +30 -23
  78. package/lib/Template.js +10 -5
  79. package/lib/TemplatedPathPlugin.js +4 -2
  80. package/lib/UnhandledSchemeError.js +1 -0
  81. package/lib/UnsupportedFeatureWarning.js +1 -0
  82. package/lib/WarnDeprecatedOptionPlugin.js +1 -0
  83. package/lib/WatchIgnorePlugin.js +1 -1
  84. package/lib/Watching.js +5 -0
  85. package/lib/WebpackError.js +4 -0
  86. package/lib/WebpackOptionsApply.js +60 -40
  87. package/lib/asset/AssetBytesGenerator.js +1 -0
  88. package/lib/asset/AssetGenerator.js +9 -3
  89. package/lib/asset/AssetModulesPlugin.js +33 -2
  90. package/lib/asset/AssetSourceGenerator.js +1 -0
  91. package/lib/asset/RawDataUrlModule.js +4 -0
  92. package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -1
  93. package/lib/buildChunkGraph.js +31 -2
  94. package/lib/cache/MemoryWithGcCachePlugin.js +1 -0
  95. package/lib/cache/PackFileCacheStrategy.js +41 -8
  96. package/lib/cache/getLazyHashedEtag.js +13 -4
  97. package/lib/cache/mergeEtags.js +3 -1
  98. package/lib/cli.js +3 -1
  99. package/lib/config/defaults.js +65 -44
  100. package/lib/config/normalization.js +96 -0
  101. package/lib/container/ContainerEntryModule.js +4 -0
  102. package/lib/container/FallbackModule.js +2 -0
  103. package/lib/container/HoistContainerReferencesPlugin.js +6 -0
  104. package/lib/container/RemoteModule.js +19 -0
  105. package/lib/css/CssGenerator.js +31 -4
  106. package/lib/css/CssModulesPlugin.js +26 -30
  107. package/lib/css/CssParser.js +12 -9
  108. package/lib/debug/ProfilingPlugin.js +1 -0
  109. package/lib/dependencies/AMDDefineDependency.js +3 -1
  110. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +6 -0
  111. package/lib/dependencies/AMDPlugin.js +1 -1
  112. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +7 -1
  113. package/lib/dependencies/CachedConstDependency.js +1 -0
  114. package/lib/dependencies/CommonJsDependencyHelpers.js +2 -0
  115. package/lib/dependencies/CommonJsExportRequireDependency.js +1 -0
  116. package/lib/dependencies/CommonJsFullRequireDependency.js +2 -0
  117. package/lib/dependencies/CommonJsImportsParserPlugin.js +3 -0
  118. package/lib/dependencies/CommonJsRequireContextDependency.js +2 -2
  119. package/lib/dependencies/CommonJsSelfReferenceDependency.js +1 -0
  120. package/lib/dependencies/ConstDependency.js +1 -0
  121. package/lib/dependencies/ContextDependencyHelpers.js +11 -4
  122. package/lib/dependencies/ContextElementDependency.js +1 -0
  123. package/lib/dependencies/CriticalDependencyWarning.js +1 -0
  124. package/lib/dependencies/CssIcssExportDependency.js +5 -4
  125. package/lib/dependencies/CssIcssSymbolDependency.js +1 -0
  126. package/lib/dependencies/CssUrlDependency.js +2 -3
  127. package/lib/dependencies/ExternalModuleInitFragment.js +4 -1
  128. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +0 -9
  129. package/lib/dependencies/HarmonyAcceptDependency.js +3 -0
  130. package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +1 -0
  131. package/lib/dependencies/HarmonyExportExpressionDependency.js +4 -0
  132. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +8 -1
  133. package/lib/dependencies/HarmonyExportInitFragment.js +17 -3
  134. package/lib/dependencies/HarmonyExportSpecifierDependency.js +4 -0
  135. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +266 -5
  136. package/lib/dependencies/HarmonyImportSpecifierDependency.js +22 -3
  137. package/lib/dependencies/ImportDependency.js +1 -0
  138. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +1 -0
  139. package/lib/dependencies/ImportMetaPlugin.js +53 -11
  140. package/lib/dependencies/ImportParserPlugin.js +7 -12
  141. package/lib/dependencies/LoaderPlugin.js +3 -0
  142. package/lib/dependencies/ModuleDecoratorDependency.js +1 -0
  143. package/lib/dependencies/ModuleDependency.js +1 -0
  144. package/lib/dependencies/ModuleInitFragmentDependency.js +87 -0
  145. package/lib/dependencies/ProvidedDependency.js +1 -0
  146. package/lib/dependencies/RequireContextPlugin.js +1 -0
  147. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +7 -2
  148. package/lib/dependencies/RuntimeRequirementsDependency.js +1 -0
  149. package/lib/dependencies/WorkerDependency.js +9 -3
  150. package/lib/dependencies/WorkerPlugin.js +1 -0
  151. package/lib/dependencies/getFunctionExpression.js +3 -1
  152. package/lib/errors/BuildCycleError.js +4 -1
  153. package/lib/esm/ModuleChunkFormatPlugin.js +6 -4
  154. package/lib/esm/ModuleChunkLoadingPlugin.js +1 -0
  155. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +1 -0
  156. package/lib/hmr/LazyCompilationPlugin.js +5 -0
  157. package/lib/hmr/lazyCompilationBackend.js +3 -2
  158. package/lib/ids/ChunkModuleIdRangePlugin.js +2 -0
  159. package/lib/ids/IdHelpers.js +10 -8
  160. package/lib/ids/OccurrenceModuleIdsPlugin.js +15 -7
  161. package/lib/ids/SyncModuleIdsPlugin.js +5 -1
  162. package/lib/javascript/BasicEvaluatedExpression.js +1 -0
  163. package/lib/javascript/EnableChunkLoadingPlugin.js +5 -2
  164. package/lib/javascript/JavascriptModulesPlugin.js +29 -8
  165. package/lib/javascript/JavascriptParser.js +47 -14
  166. package/lib/json/JsonParser.js +6 -0
  167. package/lib/library/AbstractLibraryPlugin.js +4 -1
  168. package/lib/library/AmdLibraryPlugin.js +2 -1
  169. package/lib/library/AssignLibraryPlugin.js +6 -2
  170. package/lib/library/EnableLibraryPlugin.js +1 -0
  171. package/lib/library/ExportPropertyLibraryPlugin.js +1 -1
  172. package/lib/library/JsonpLibraryPlugin.js +1 -1
  173. package/lib/library/ModuleLibraryPlugin.js +130 -13
  174. package/lib/library/SystemLibraryPlugin.js +4 -1
  175. package/lib/library/UmdLibraryPlugin.js +4 -3
  176. package/lib/logging/createConsoleLogger.js +3 -0
  177. package/lib/logging/truncateArgs.js +1 -1
  178. package/lib/node/CommonJsChunkLoadingPlugin.js +1 -0
  179. package/lib/node/NodeTargetPlugin.js +1 -0
  180. package/lib/node/NodeWatchFileSystem.js +3 -0
  181. package/lib/node/nodeConsole.js +18 -1
  182. package/lib/optimize/AggressiveMergingPlugin.js +2 -1
  183. package/lib/optimize/AggressiveSplittingPlugin.js +10 -6
  184. package/lib/optimize/ConcatenatedModule.js +33 -19
  185. package/lib/optimize/FlagIncludedChunksPlugin.js +11 -1
  186. package/lib/optimize/InnerGraph.js +5 -3
  187. package/lib/optimize/InnerGraphPlugin.js +25 -25
  188. package/lib/optimize/MangleExportsPlugin.js +14 -2
  189. package/lib/optimize/MergeDuplicateChunksPlugin.js +3 -0
  190. package/lib/optimize/MinChunkSizePlugin.js +5 -2
  191. package/lib/optimize/ModuleConcatenationPlugin.js +19 -4
  192. package/lib/optimize/RealContentHashPlugin.js +35 -27
  193. package/lib/optimize/RemoveParentModulesPlugin.js +12 -3
  194. package/lib/optimize/SideEffectsFlagPlugin.js +3 -0
  195. package/lib/optimize/SplitChunksPlugin.js +14 -3
  196. package/lib/performance/AssetsOverSizeLimitWarning.js +6 -2
  197. package/lib/performance/EntrypointsOverSizeLimitWarning.js +6 -2
  198. package/lib/performance/NoAsyncChunksWarning.js +1 -0
  199. package/lib/rules/BasicEffectRulePlugin.js +2 -0
  200. package/lib/rules/BasicMatcherRulePlugin.js +3 -0
  201. package/lib/rules/ObjectMatcherRulePlugin.js +5 -1
  202. package/lib/rules/RuleSetCompiler.js +2 -0
  203. package/lib/runtime/AsyncModuleRuntimeModule.js +1 -0
  204. package/lib/runtime/ChunkNameRuntimeModule.js +1 -0
  205. package/lib/runtime/CompatRuntimeModule.js +1 -0
  206. package/lib/runtime/EnsureChunkRuntimeModule.js +1 -0
  207. package/lib/runtime/GetChunkFilenameRuntimeModule.js +4 -0
  208. package/lib/runtime/GetMainFilenameRuntimeModule.js +2 -0
  209. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -0
  210. package/lib/runtime/LoadScriptRuntimeModule.js +2 -0
  211. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +15 -32
  212. package/lib/runtime/PublicPathRuntimeModule.js +1 -0
  213. package/lib/runtime/StartupChunkDependenciesPlugin.js +2 -0
  214. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +1 -0
  215. package/lib/runtime/StartupEntrypointRuntimeModule.js +1 -0
  216. package/lib/schemes/HttpUriPlugin.js +27 -7
  217. package/lib/schemes/VirtualUrlPlugin.js +7 -2
  218. package/lib/serialization/BinaryMiddleware.js +4 -0
  219. package/lib/serialization/FileMiddleware.js +9 -4
  220. package/lib/serialization/ObjectMiddleware.js +34 -11
  221. package/lib/serialization/PlainObjectSerializer.js +2 -0
  222. package/lib/sharing/ConsumeSharedModule.js +60 -0
  223. package/lib/sharing/ConsumeSharedPlugin.js +5 -4
  224. package/lib/sharing/ProvideSharedModule.js +4 -0
  225. package/lib/sharing/ProvideSharedPlugin.js +24 -25
  226. package/lib/sharing/resolveMatchedConfigs.js +2 -2
  227. package/lib/sharing/utils.js +8 -6
  228. package/lib/stats/DefaultStatsFactoryPlugin.js +14 -12
  229. package/lib/stats/DefaultStatsPrinterPlugin.js +6 -5
  230. package/lib/stats/StatsFactory.js +2 -2
  231. package/lib/url/URLParserPlugin.js +4 -3
  232. package/lib/util/AppendOnlyStackedSet.js +57 -0
  233. package/lib/util/Hash.js +1 -0
  234. package/lib/util/IterableHelpers.js +1 -0
  235. package/lib/util/LazyBucketSortedSet.js +1 -0
  236. package/lib/util/LazySet.js +1 -0
  237. package/lib/util/ParallelismFactorCalculator.js +1 -0
  238. package/lib/util/SortableSet.js +1 -0
  239. package/lib/util/StringXor.js +1 -0
  240. package/lib/util/binarySearchBounds.js +15 -8
  241. package/lib/util/chainedImports.js +1 -1
  242. package/lib/util/cleverMerge.js +6 -1
  243. package/lib/util/comparators.js +21 -1
  244. package/lib/util/compileBooleanMatcher.js +3 -3
  245. package/lib/util/concatenate.js +3 -2
  246. package/lib/util/conventions.js +3 -2
  247. package/lib/util/deterministicGrouping.js +13 -1
  248. package/lib/util/extractSourceMap.js +12 -13
  249. package/lib/util/fs.js +15 -19
  250. package/lib/util/hash/BatchedHash.js +3 -0
  251. package/lib/util/hash/BulkUpdateHash.js +7 -1
  252. package/lib/util/hash/hash-digest.js +1 -0
  253. package/lib/util/hash/wasm-hash.js +16 -1
  254. package/lib/util/identifier.js +7 -2
  255. package/lib/util/magicComment.js +1 -1
  256. package/lib/util/propertyName.js +1 -1
  257. package/lib/util/registerExternalSerializer.js +1 -0
  258. package/lib/util/runtime.js +15 -1
  259. package/lib/util/semver.js +1 -0
  260. package/lib/util/serialization.js +2 -2
  261. package/lib/wasm/EnableWasmLoadingPlugin.js +1 -0
  262. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +11 -3
  263. package/lib/wasm-async/AsyncWebAssemblyGenerator.js +1 -0
  264. package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +3 -2
  265. package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +5 -1
  266. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +3 -0
  267. package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +2 -0
  268. package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +7 -3
  269. package/lib/wasm-sync/WebAssemblyModulesPlugin.js +2 -2
  270. package/lib/wasm-sync/WebAssemblyParser.js +3 -1
  271. package/lib/web/FetchCompileWasmPlugin.js +1 -0
  272. package/lib/web/JsonpChunkLoadingPlugin.js +1 -0
  273. package/lib/web/JsonpChunkLoadingRuntimeModule.js +1 -0
  274. package/lib/webpack.js +13 -3
  275. package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +1 -0
  276. package/package.json +20 -28
  277. package/schemas/WebpackOptions.check.js +1 -1
  278. package/schemas/WebpackOptions.json +71 -5
  279. package/types.d.ts +339 -93
@@ -28,6 +28,7 @@ const propertyAccess = require("../util/propertyAccess");
28
28
  const ConstDependency = require("./ConstDependency");
29
29
 
30
30
  /** @typedef {import("estree").MemberExpression} MemberExpression */
31
+ /** @typedef {import("estree").Identifier} Identifier */
31
32
  /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
32
33
  /** @typedef {import("../Compiler")} Compiler */
33
34
  /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
@@ -44,18 +45,24 @@ const getCriticalDependencyWarning = memoize(() =>
44
45
 
45
46
  const PLUGIN_NAME = "ImportMetaPlugin";
46
47
 
48
+ /** @type {WeakMap<Compilation, { stringify: string, env: Record<string, string> }>} */
49
+ const compilationMetaEnvMap = new WeakMap();
50
+
47
51
  /**
48
52
  * Collect import.meta.env definitions from DefinePlugin and build JSON string
49
53
  * @param {Compilation} compilation the compilation
50
- * @returns {string} env object as JSON string
54
+ * @returns {{ stringify: string, env: Record<string, string> }} env object as JSON string
51
55
  */
52
56
  const collectImportMetaEnvDefinitions = (compilation) => {
53
- const definePluginHooks = DefinePlugin.getCompilationHooks(compilation);
54
- const definitions = definePluginHooks.definitions.call({});
55
- if (!definitions) {
56
- return "{}";
57
+ const cached = compilationMetaEnvMap.get(compilation);
58
+ if (cached) {
59
+ return cached;
57
60
  }
58
61
 
62
+ const definePluginHooks = DefinePlugin.getCompilationHooks(compilation);
63
+ const definitions = definePluginHooks.definitions.call({});
64
+ /** @type {Record<string, string>} */
65
+ const env = {};
59
66
  /** @type {string[]} */
60
67
  const pairs = [];
61
68
  for (const key of Object.keys(definitions)) {
@@ -63,10 +70,12 @@ const collectImportMetaEnvDefinitions = (compilation) => {
63
70
  const envKey = key.slice("import.meta.env.".length);
64
71
  const value = definitions[key];
65
72
  pairs.push(`${JSON.stringify(envKey)}:${value}`);
73
+ env[envKey] = /** @type {string} */ (value);
66
74
  }
67
75
  }
68
-
69
- return `{${pairs.join(",")}}`;
76
+ const result = { stringify: `{${pairs.join(",")}}`, env };
77
+ compilationMetaEnvMap.set(compilation, result);
78
+ return result;
70
79
  };
71
80
 
72
81
  /**
@@ -148,10 +157,15 @@ class ImportMetaPlugin {
148
157
  * @param {Members} members members
149
158
  * @returns {string} error message
150
159
  */
151
- const importMetaUnknownProperty = (members) =>
152
- `${Template.toNormalComment(
160
+ const importMetaUnknownProperty = (members) => {
161
+ if (importMeta === "preserve-unknown") {
162
+ return `import.meta${propertyAccess(members, 0)}`;
163
+ }
164
+ return `${Template.toNormalComment(
153
165
  `unsupported import.meta.${members.join(".")}`
154
166
  )} undefined${propertyAccess(members, 1)}`;
167
+ };
168
+
155
169
  parser.hooks.typeof
156
170
  .for("import.meta")
157
171
  .tap(
@@ -223,6 +237,9 @@ class ImportMetaPlugin {
223
237
  RuntimeGlobals.module
224
238
  );
225
239
  break;
240
+ case "env":
241
+ str += `env: ${collectImportMetaEnvDefinitions(compilation).stringify},`;
242
+ break;
226
243
  default:
227
244
  str += `[${JSON.stringify(
228
245
  prop.id
@@ -327,12 +344,31 @@ class ImportMetaPlugin {
327
344
  PLUGIN_NAME,
328
345
  toConstantDependency(parser, JSON.stringify("object"))
329
346
  );
347
+ parser.hooks.expressionMemberChain
348
+ .for("import.meta")
349
+ .tap(PLUGIN_NAME, (expr, members) => {
350
+ if (members[0] === "env" && members[1]) {
351
+ const name = members[1];
352
+ const { env } = collectImportMetaEnvDefinitions(compilation);
353
+ if (!Object.prototype.hasOwnProperty.call(env, name)) {
354
+ const dep = new ConstDependency(
355
+ "undefined",
356
+ /** @type {Range} */ (expr.range)
357
+ );
358
+ dep.loc = /** @type {DependencyLocation} */ (expr.loc);
359
+ parser.state.module.addPresentationalDependency(dep);
360
+ return true;
361
+ }
362
+ }
363
+ });
330
364
  parser.hooks.expression
331
365
  .for("import.meta.env")
332
366
  .tap(PLUGIN_NAME, (expr) => {
333
- const envCode = collectImportMetaEnvDefinitions(compilation);
367
+ const { stringify } =
368
+ collectImportMetaEnvDefinitions(compilation);
369
+
334
370
  const dep = new ConstDependency(
335
- envCode,
371
+ stringify,
336
372
  /** @type {Range} */ (expr.range)
337
373
  );
338
374
  dep.loc = /** @type {DependencyLocation} */ (expr.loc);
@@ -355,6 +391,11 @@ class ImportMetaPlugin {
355
391
  parser.hooks.unhandledExpressionMemberChain
356
392
  .for("import.meta")
357
393
  .tap(PLUGIN_NAME, (expr, members) => {
394
+ // unknown import.meta properties should be determined at runtime
395
+ if (importMeta === "preserve-unknown") {
396
+ return true;
397
+ }
398
+
358
399
  // keep import.meta.env unknown property
359
400
  // don't evaluate import.meta.env.UNKNOWN_PROPERTY -> undefined.UNKNOWN_PROPERTY
360
401
  // `dirname` and `filename` logic in NodeStuffPlugin
@@ -373,6 +414,7 @@ class ImportMetaPlugin {
373
414
  parser.state.module.addPresentationalDependency(dep);
374
415
  return true;
375
416
  });
417
+
376
418
  parser.hooks.evaluate
377
419
  .for("MemberExpression")
378
420
  .tap(PLUGIN_NAME, (expression) => {
@@ -17,7 +17,7 @@ const ContextDependencyHelpers = require("./ContextDependencyHelpers");
17
17
  const ImportContextDependency = require("./ImportContextDependency");
18
18
  const ImportDependency = require("./ImportDependency");
19
19
  const ImportEagerDependency = require("./ImportEagerDependency");
20
- const { ImportPhaseUtils, createGetImportPhase } = require("./ImportPhase");
20
+ const { createGetImportPhase } = require("./ImportPhase");
21
21
  const ImportWeakDependency = require("./ImportWeakDependency");
22
22
 
23
23
  /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
@@ -262,11 +262,14 @@ class ImportParserPlugin {
262
262
  parser.hooks.importCall.tap(PLUGIN_NAME, (expr, importThen) => {
263
263
  const param = parser.evaluateExpression(expr.source);
264
264
 
265
+ /** @type {null | string} */
265
266
  let chunkName = null;
266
267
  let mode = /** @type {ContextMode} */ (this.options.dynamicImportMode);
268
+ /** @type {null | RegExp} */
267
269
  let include = null;
270
+ /** @type {null | RegExp} */
268
271
  let exclude = null;
269
- /** @type {RawReferencedExports | null} */
272
+ /** @type {null | RawReferencedExports} */
270
273
  let exports = null;
271
274
  /** @type {RawChunkGroupOptions} */
272
275
  const groupOptions = {};
@@ -557,15 +560,6 @@ class ImportParserPlugin {
557
560
  mode = "async-weak";
558
561
  }
559
562
 
560
- if (ImportPhaseUtils.isDefer(phase)) {
561
- parser.state.module.addWarning(
562
- new UnsupportedFeatureWarning(
563
- "import.defer() is not yet supported for ContextModule (the import path is a dynamic expression).",
564
- /** @type {DependencyLocation} */ (expr.loc)
565
- )
566
- );
567
- }
568
-
569
563
  const dep = ContextDependencyHelpers.create(
570
564
  ImportContextDependency,
571
565
  /** @type {Range} */ (expr.range),
@@ -586,7 +580,8 @@ class ImportParserPlugin {
586
580
  typePrefix: "import()",
587
581
  category: "esm",
588
582
  referencedExports: exports,
589
- attributes: getImportAttributes(expr)
583
+ attributes: getImportAttributes(expr),
584
+ phase
590
585
  },
591
586
  parser
592
587
  );
@@ -10,6 +10,7 @@ const LazySet = require("../util/LazySet");
10
10
  const LoaderDependency = require("./LoaderDependency");
11
11
  const LoaderImportDependency = require("./LoaderImportDependency");
12
12
 
13
+ /** @typedef {import("webpack-sources").RawSourceMap} RawSourceMap */
13
14
  /** @typedef {import("../Compilation").DependencyConstructor} DependencyConstructor */
14
15
  /** @typedef {import("../Compilation").ExecuteModuleExports} ExecuteModuleExports */
15
16
  /** @typedef {import("../Compilation").ExecuteModuleResult} ExecuteModuleResult */
@@ -116,7 +117,9 @@ class LoaderPlugin {
116
117
  )
117
118
  );
118
119
  }
120
+ /** @type {null | RawSourceMap} */
119
121
  let map;
122
+ /** @type {string | Buffer | undefined} */
120
123
  let source;
121
124
  if (moduleSource.sourceAndMap) {
122
125
  const sourceAndMap = moduleSource.sourceAndMap();
@@ -30,6 +30,7 @@ class ModuleDecoratorDependency extends NullDependency {
30
30
  super();
31
31
  this.decorator = decorator;
32
32
  this.allowExportsAccess = allowExportsAccess;
33
+ /** @type {undefined | string} */
33
34
  this._hashUpdate = undefined;
34
35
  }
35
36
 
@@ -27,6 +27,7 @@ class ModuleDependency extends Dependency {
27
27
  this.sourceOrder = sourceOrder;
28
28
  /** @type {Range | undefined} */
29
29
  this.range = undefined;
30
+ /** @type {undefined | string} */
30
31
  this._context = undefined;
31
32
  }
32
33
 
@@ -0,0 +1,87 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Natsu @xiaoxiaojx
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const InitFragment = require("../InitFragment");
9
+ const makeSerializable = require("../util/makeSerializable");
10
+ const NullDependency = require("./NullDependency");
11
+
12
+ /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
13
+ /** @typedef {import("../Dependency")} Dependency */
14
+ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
15
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
16
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
17
+ /**
18
+ * A dependency that adds an init fragment to the module
19
+ */
20
+ class ModuleInitFragmentDependency extends NullDependency {
21
+ /**
22
+ * @param {string} initCode the initialization code
23
+ * @param {string[]} runtimeRequirements runtime requirements
24
+ * @param {string=} key unique key to avoid emitting the same initialization code twice
25
+ */
26
+ constructor(initCode, runtimeRequirements, key) {
27
+ super();
28
+ this.initCode = initCode;
29
+ this.runtimeRequirements = runtimeRequirements;
30
+ this.key = key;
31
+ }
32
+
33
+ /**
34
+ * @param {ObjectSerializerContext} context context
35
+ */
36
+ serialize(context) {
37
+ const { write } = context;
38
+ write(this.initCode);
39
+ write(this.runtimeRequirements);
40
+ write(this.key);
41
+ super.serialize(context);
42
+ }
43
+
44
+ /**
45
+ * @param {ObjectDeserializerContext} context context
46
+ */
47
+ deserialize(context) {
48
+ const { read } = context;
49
+ this.initCode = read();
50
+ this.runtimeRequirements = read();
51
+ this.key = read();
52
+ super.deserialize(context);
53
+ }
54
+ }
55
+
56
+ makeSerializable(
57
+ ModuleInitFragmentDependency,
58
+ "webpack/lib/dependencies/ModuleInitFragmentDependency"
59
+ );
60
+
61
+ ModuleInitFragmentDependency.Template = class ModuleInitFragmentDependencyTemplate extends (
62
+ NullDependency.Template
63
+ ) {
64
+ /**
65
+ * @param {Dependency} dependency the dependency for which the template should be applied
66
+ * @param {ReplaceSource} source the current replace source which can be modified
67
+ * @param {DependencyTemplateContext} templateContext the context object
68
+ * @returns {void}
69
+ */
70
+ apply(dependency, source, { initFragments, runtimeRequirements }) {
71
+ const dep = /** @type {ModuleInitFragmentDependency} */ (dependency);
72
+ for (const req of dep.runtimeRequirements) {
73
+ runtimeRequirements.add(req);
74
+ }
75
+ initFragments.push(
76
+ new InitFragment(
77
+ dep.initCode,
78
+ InitFragment.STAGE_CONSTANTS,
79
+ 0,
80
+ dep.key,
81
+ undefined
82
+ )
83
+ );
84
+ }
85
+ };
86
+
87
+ module.exports = ModuleInitFragmentDependency;
@@ -44,6 +44,7 @@ class ProvidedDependency extends ModuleDependency {
44
44
  this.identifier = identifier;
45
45
  this.ids = ids;
46
46
  this.range = range;
47
+ /** @type {undefined | string} */
47
48
  this._hashUpdate = undefined;
48
49
  }
49
50
 
@@ -86,6 +86,7 @@ class RequireContextPlugin {
86
86
  )
87
87
  ).options;
88
88
 
89
+ /** @type {{ context: string, request: string }[]} */
89
90
  let newItems;
90
91
  if (!finalResolveOptions.fullySpecified) {
91
92
  newItems = [];
@@ -10,11 +10,14 @@ const RequireEnsureDependency = require("./RequireEnsureDependency");
10
10
  const RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
11
11
  const getFunctionExpression = require("./getFunctionExpression");
12
12
 
13
+ /** @typedef {import("estree").Expression} Expression */
14
+ /** @typedef {import("estree").SpreadElement} SpreadElement */
13
15
  /** @typedef {import("../AsyncDependenciesBlock").GroupOptions} GroupOptions */
14
16
  /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
15
17
  /** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */
16
18
  /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
17
19
  /** @typedef {import("../javascript/JavascriptParser").Range} Range */
20
+ /** @typedef {import("./getFunctionExpression").FunctionExpressionResult} FunctionExpressionResult */
18
21
 
19
22
  const PLUGIN_NAME = "RequireEnsureDependenciesBlockParserPlugin";
20
23
 
@@ -27,8 +30,10 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
27
30
  parser.hooks.call.for("require.ensure").tap(PLUGIN_NAME, (expr) => {
28
31
  /** @type {string | GroupOptions | null} */
29
32
  let chunkName = null;
30
- let errorExpressionArg = null;
31
- let errorExpression = null;
33
+ /** @type {undefined | Expression | SpreadElement} */
34
+ let errorExpressionArg;
35
+ /** @type {undefined | FunctionExpressionResult} */
36
+ let errorExpression;
32
37
  switch (expr.arguments.length) {
33
38
  case 4: {
34
39
  const chunkNameExpr = parser.evaluateExpression(expr.arguments[3]);
@@ -24,6 +24,7 @@ class RuntimeRequirementsDependency extends NullDependency {
24
24
  constructor(runtimeRequirements) {
25
25
  super();
26
26
  this.runtimeRequirements = new Set(runtimeRequirements);
27
+ /** @type {undefined | string} */
27
28
  this._hashUpdate = undefined;
28
29
  }
29
30
 
@@ -23,20 +23,26 @@ const ModuleDependency = require("./ModuleDependency");
23
23
  /** @typedef {import("../util/Hash")} Hash */
24
24
  /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
25
25
 
26
+ /**
27
+ * @typedef {object} WorkerDependencyOptions
28
+ * @property {string=} publicPath public path for the worker
29
+ * @property {boolean=} needNewUrl true when need generate `new URL(...)`, otherwise false
30
+ */
31
+
26
32
  class WorkerDependency extends ModuleDependency {
27
33
  /**
28
34
  * @param {string} request request
29
35
  * @param {Range} range range
30
- * @param {object} workerDependencyOptions options
31
- * @param {string=} workerDependencyOptions.publicPath public path for the worker
32
- * @param {boolean=} workerDependencyOptions.needNewUrl need generate `new URL(...)`
36
+ * @param {WorkerDependencyOptions} workerDependencyOptions options
33
37
  */
34
38
  constructor(request, range, workerDependencyOptions) {
35
39
  super(request);
36
40
  this.range = range;
37
41
  // If options are updated, don't forget to update the hash and serialization functions
42
+ /** @type {WorkerDependencyOptions} */
38
43
  this.options = workerDependencyOptions;
39
44
  /** Cache the hash */
45
+ /** @type {undefined | string} */
40
46
  this._hashUpdate = undefined;
41
47
  }
42
48
 
@@ -387,6 +387,7 @@ class WorkerPlugin {
387
387
 
388
388
  const block = new AsyncDependenciesBlock({
389
389
  name: entryOptions.name,
390
+ circular: false,
390
391
  entryOptions: {
391
392
  chunkLoading: this._chunkLoading,
392
393
  wasmLoading: this._wasmLoading,
@@ -10,9 +10,11 @@
10
10
  /** @typedef {import("estree").FunctionExpression} FunctionExpression */
11
11
  /** @typedef {import("estree").SpreadElement} SpreadElement */
12
12
 
13
+ /** @typedef {{ fn: FunctionExpression | ArrowFunctionExpression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined }} FunctionExpressionResult */
14
+
13
15
  /**
14
16
  * @param {Expression | SpreadElement} expr expressions
15
- * @returns {{ fn: FunctionExpression | ArrowFunctionExpression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined } | undefined} function expression with additional information
17
+ * @returns {FunctionExpressionResult | undefined} function expression with additional information
16
18
  */
17
19
  module.exports = (expr) => {
18
20
  // <FunctionExpression>
@@ -11,7 +11,7 @@ const WebpackError = require("../WebpackError");
11
11
 
12
12
  class BuildCycleError extends WebpackError {
13
13
  /**
14
- * Creates an instance of ModuleDependencyError.
14
+ * Creates an instance of BuildCycleError.
15
15
  * @param {Module} module the module starting the cycle
16
16
  */
17
17
  constructor(module) {
@@ -19,9 +19,12 @@ class BuildCycleError extends WebpackError {
19
19
  "There is a circular build dependency, which makes it impossible to create this module"
20
20
  );
21
21
 
22
+ /** @type {string} */
22
23
  this.name = "BuildCycleError";
24
+ /** @type {Module} */
23
25
  this.module = module;
24
26
  }
25
27
  }
26
28
 
29
+ /** @type {typeof BuildCycleError} */
27
30
  module.exports = BuildCycleError;
@@ -212,6 +212,7 @@ class ModuleChunkFormatPlugin {
212
212
  )}\n`
213
213
  );
214
214
 
215
+ /** @type {Set<Chunk>} */
215
216
  const loadedChunks = new Set();
216
217
  for (let i = 0; i < entries.length; i++) {
217
218
  const [module, entrypoint] = entries[i];
@@ -225,13 +226,14 @@ class ModuleChunkFormatPlugin {
225
226
  /** @type {Chunk} */ (runtimeChunk),
226
227
  undefined
227
228
  );
229
+ /** @type {Set<Chunk>} */
228
230
  const processChunks = new Set();
229
- for (const _chunk of chunks) {
230
- if (loadedChunks.has(_chunk)) {
231
+ for (const chunk of chunks) {
232
+ if (loadedChunks.has(chunk)) {
231
233
  continue;
232
234
  }
233
- loadedChunks.add(_chunk);
234
- processChunks.add(_chunk);
235
+ loadedChunks.add(chunk);
236
+ processChunks.add(chunk);
235
237
  }
236
238
  const sourceWithDependentChunks = withDependentChunks(
237
239
  processChunks,
@@ -36,6 +36,7 @@ class ModuleChunkLoadingPlugin {
36
36
  : globalChunkLoading;
37
37
  return chunkLoading === "import";
38
38
  };
39
+ /** @type {WeakSet<Chunk>} */
39
40
  const onceForChunkSet = new WeakSet();
40
41
  /**
41
42
  * @param {Chunk} chunk chunk to check
@@ -60,6 +60,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
60
60
  */
61
61
  constructor(runtimeRequirements) {
62
62
  super("import chunk loading", RuntimeModule.STAGE_ATTACH);
63
+ /** @type {ReadOnlyRuntimeRequirements} */
63
64
  this._runtimeRequirements = runtimeRequirements;
64
65
  }
65
66
 
@@ -33,6 +33,8 @@ const { registerNotSerializable } = require("../util/serialization");
33
33
  /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
34
34
  /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
35
35
  /** @typedef {import("../Module").SourceTypes} SourceTypes */
36
+ /** @typedef {import("../Module").Sources} Sources */
37
+ /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
36
38
  /** @typedef {import("../ModuleFactory").ModuleFactoryCallback} ModuleFactoryCallback */
37
39
  /** @typedef {import("../ModuleFactory").ModuleFactoryCreateData} ModuleFactoryCreateData */
38
40
  /** @typedef {import("../RequestShortener")} RequestShortener */
@@ -225,7 +227,9 @@ class LazyCompilationProxyModule extends Module {
225
227
  * @returns {CodeGenerationResult} result
226
228
  */
227
229
  codeGeneration({ runtimeTemplate, chunkGraph, moduleGraph }) {
230
+ /** @type {Sources} */
228
231
  const sources = new Map();
232
+ /** @type {RuntimeRequirements} */
229
233
  const runtimeRequirements = new Set();
230
234
  runtimeRequirements.add(RuntimeGlobals.module);
231
235
  const clientDep = /** @type {CommonJsRequireDependency} */ (
@@ -247,6 +251,7 @@ class LazyCompilationProxyModule extends Module {
247
251
  Boolean(block)
248
252
  )}, module: module, onError: onError });`
249
253
  ]);
254
+ /** @type {string} */
250
255
  let source;
251
256
  if (block) {
252
257
  const dep = block.dependencies[0];
@@ -19,11 +19,12 @@
19
19
  /** @typedef {() => Server} CreateServerFunction */
20
20
 
21
21
  /**
22
- * @param {Omit<LazyCompilationDefaultBackendOptions, "client"> & { client: NonNullable<LazyCompilationDefaultBackendOptions["client"]>}} options additional options for the backend
22
+ * @param {Omit<LazyCompilationDefaultBackendOptions, "client"> & { client: NonNullable<LazyCompilationDefaultBackendOptions["client"]> }} options additional options for the backend
23
23
  * @returns {BackendHandler} backend
24
24
  */
25
25
  module.exports = (options) => (compiler, callback) => {
26
26
  const logger = compiler.getInfrastructureLogger("LazyCompilationBackend");
27
+ /** @type {Map<string, number>} */
27
28
  const activeModules = new Map();
28
29
  const prefix = "/lazy-compilation-using-";
29
30
 
@@ -152,7 +153,7 @@ module.exports = (options) => (compiler, callback) => {
152
153
  const key = `${encodeURIComponent(
153
154
  originalModule.identifier().replace(/\\/g, "/").replace(/@/g, "_")
154
155
  ).replace(/%(2F|3A|24|26|2B|2C|3B|3D)/g, decodeURIComponent)}`;
155
- const active = activeModules.get(key) > 0;
156
+ const active = /** @type {number} */ (activeModules.get(key)) > 0;
156
157
  return {
157
158
  client: `${options.client}?${encodeURIComponent(urlBase + prefix)}`,
158
159
  data: key,
@@ -13,6 +13,7 @@ const {
13
13
 
14
14
  /** @typedef {import("../Compiler")} Compiler */
15
15
  /** @typedef {import("../Module")} Module */
16
+ /** @typedef {import("../ChunkGraph").ModuleComparator} ModuleComparator */
16
17
 
17
18
  /**
18
19
  * @typedef {object} ChunkModuleIdRangePluginOptions
@@ -56,6 +57,7 @@ class ChunkModuleIdRangePlugin {
56
57
  /** @type {Module[]} */
57
58
  let chunkModules;
58
59
  if (options.order) {
60
+ /** @type {ModuleComparator} */
59
61
  let cmpFn;
60
62
  switch (options.order) {
61
63
  case "index":
@@ -13,14 +13,14 @@ const numberHash = require("../util/numberHash");
13
13
  /** @typedef {import("../ChunkGraph")} ChunkGraph */
14
14
  /** @typedef {import("../Compilation")} Compilation */
15
15
  /** @typedef {import("../Module")} Module */
16
- /** @typedef {typeof import("../util/Hash")} Hash */
16
+ /** @typedef {import("../util/Hash").HashFunction} HashFunction */
17
17
  /** @typedef {import("../util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
18
18
  /** @typedef {import("../Module").BuildMeta} BuildMeta */
19
19
 
20
20
  /**
21
21
  * @param {string} str string to hash
22
22
  * @param {number} len max length of the hash
23
- * @param {string | Hash} hashFunction hash function to use
23
+ * @param {HashFunction} hashFunction hash function to use
24
24
  * @returns {string} hash
25
25
  */
26
26
  const getHash = (str, len, hashFunction) => {
@@ -56,12 +56,12 @@ const avoidNumber = (str) => {
56
56
  * @returns {string} id representation
57
57
  */
58
58
  const requestToId = (request) =>
59
- request.replace(/^(\.\.?\/)+/, "").replace(/(^[.-]|[^a-zA-Z0-9_-])+/g, "_");
59
+ request.replace(/^(\.\.?\/)+/, "").replace(/(^[.-]|[^a-z0-9_-])+/gi, "_");
60
60
 
61
61
  /**
62
62
  * @param {string} string the string
63
63
  * @param {string} delimiter separator for string and hash
64
- * @param {string | Hash} hashFunction hash function to use
64
+ * @param {HashFunction} hashFunction hash function to use
65
65
  * @returns {string} string with limited max length to 100 chars
66
66
  */
67
67
  const shortenLongString = (string, delimiter, hashFunction) => {
@@ -95,7 +95,7 @@ const getShortModuleName = (module, context, associatedObjectForCache) => {
95
95
  * @param {string} shortName the short name
96
96
  * @param {Module} module the module
97
97
  * @param {string} context context directory
98
- * @param {string | Hash} hashFunction hash function to use
98
+ * @param {HashFunction} hashFunction hash function to use
99
99
  * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
100
100
  * @returns {string} long module name
101
101
  */
@@ -124,7 +124,7 @@ const getFullModuleName = (module, context, associatedObjectForCache) =>
124
124
  * @param {ChunkGraph} chunkGraph the chunk graph
125
125
  * @param {string} context context directory
126
126
  * @param {string} delimiter delimiter for names
127
- * @param {string | Hash} hashFunction hash function to use
127
+ * @param {HashFunction} hashFunction hash function to use
128
128
  * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
129
129
  * @returns {string} short chunk name
130
130
  */
@@ -152,7 +152,7 @@ const getShortChunkName = (
152
152
  * @param {ChunkGraph} chunkGraph the chunk graph
153
153
  * @param {string} context context directory
154
154
  * @param {string} delimiter delimiter for names
155
- * @param {string | Hash} hashFunction hash function to use
155
+ * @param {HashFunction} hashFunction hash function to use
156
156
  * @param {AssociatedObjectForCache=} associatedObjectForCache an object to which the cache will be attached
157
157
  * @returns {string} short chunk name
158
158
  */
@@ -231,7 +231,7 @@ const addToMapOfItems = (map, key, value) => {
231
231
  */
232
232
  const getUsedModuleIdsAndModules = (compilation, filter) => {
233
233
  const chunkGraph = compilation.chunkGraph;
234
-
234
+ /** @type {Module[]} */
235
235
  const modules = [];
236
236
 
237
237
  /** @type {UsedModuleIds} */
@@ -402,6 +402,7 @@ const assignDeterministicIds = (
402
402
 
403
403
  for (const item of items) {
404
404
  const ident = getName(item);
405
+ /** @type {number} */
405
406
  let id;
406
407
  let i = salt;
407
408
  do {
@@ -420,6 +421,7 @@ const assignAscendingModuleIds = (usedIds, modules, compilation) => {
420
421
  const chunkGraph = compilation.chunkGraph;
421
422
 
422
423
  let nextId = 0;
424
+ /** @type {(mod: Module) => void} */
423
425
  let assignId;
424
426
  if (usedIds.size > 0) {
425
427
  /**