webpack 5.108.3 → 5.109.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 (191) hide show
  1. package/lib/APIPlugin.js +48 -29
  2. package/lib/CacheFacade.js +8 -0
  3. package/lib/ChunkGroup.js +1 -0
  4. package/lib/CompatibilityPlugin.js +31 -29
  5. package/lib/Compilation.js +50 -8
  6. package/lib/ConstPlugin.js +11 -11
  7. package/lib/ContextModule.js +239 -6
  8. package/lib/ContextModuleFactory.js +139 -14
  9. package/lib/DefinePlugin.js +396 -101
  10. package/lib/Entrypoint.js +1 -1
  11. package/lib/ExportsInfoApiPlugin.js +2 -2
  12. package/lib/ExternalModule.js +107 -5
  13. package/lib/ExternalModuleFactoryPlugin.js +20 -1
  14. package/lib/FileSystemInfo.js +130 -74
  15. package/lib/FlagDependencyExportsPlugin.js +55 -8
  16. package/lib/HotModuleReplacementPlugin.js +18 -15
  17. package/lib/LazyBarrel.js +30 -3
  18. package/lib/Module.js +3 -0
  19. package/lib/ModuleFilenameHelpers.js +2 -2
  20. package/lib/ModuleTypeConstants.js +8 -1
  21. package/lib/MultiCompiler.js +13 -2
  22. package/lib/MultiWatching.js +1 -0
  23. package/lib/NodeStuffPlugin.js +51 -33
  24. package/lib/NormalModule.js +169 -405
  25. package/lib/NormalModuleFactory.js +76 -2
  26. package/lib/ProgressPlugin.js +211 -119
  27. package/lib/ProvidePlugin.js +2 -2
  28. package/lib/RuntimeGlobals.js +47 -0
  29. package/lib/RuntimePlugin.js +33 -0
  30. package/lib/RuntimeTemplate.js +304 -16
  31. package/lib/UseStrictPlugin.js +1 -1
  32. package/lib/WebpackIsIncludedPlugin.js +1 -1
  33. package/lib/WebpackOptionsApply.js +24 -19
  34. package/lib/asset/AssetGenerator.js +110 -28
  35. package/lib/asset/AssetModulesPlugin.js +92 -2
  36. package/lib/asset/WebManifestGenerator.js +164 -0
  37. package/lib/asset/WebManifestParser.js +130 -0
  38. package/lib/async-modules/AwaitDependenciesInitFragment.js +14 -4
  39. package/lib/async-modules/isGeneratorLowered.js +26 -0
  40. package/lib/cache/PackFileCacheStrategy.js +9 -3
  41. package/lib/config/browserslistTargetHandler.js +40 -0
  42. package/lib/config/defaults.js +222 -32
  43. package/lib/config/normalization.js +26 -0
  44. package/lib/config/target.js +9 -1
  45. package/lib/css/CssGenerator.js +73 -73
  46. package/lib/css/CssLoadingRuntimeModule.js +5 -1
  47. package/lib/css/CssModulesPlugin.js +92 -53
  48. package/lib/css/CssParser.js +843 -302
  49. package/lib/css/syntax.js +1219 -483
  50. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +6 -6
  51. package/lib/dependencies/AMDPlugin.js +2 -2
  52. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +13 -13
  53. package/lib/dependencies/CommonJsDependencyHelpers.js +39 -0
  54. package/lib/dependencies/CommonJsExportsDependency.js +47 -2
  55. package/lib/dependencies/CommonJsExportsParserPlugin.js +10 -10
  56. package/lib/dependencies/CommonJsImportsParserPlugin.js +18 -18
  57. package/lib/dependencies/CommonJsPlugin.js +2 -2
  58. package/lib/dependencies/CommonJsRequireDependency.js +26 -1
  59. package/lib/dependencies/CommonJsSelfReferenceDependency.js +32 -1
  60. package/lib/dependencies/ContextDependencyHelpers.js +3 -3
  61. package/lib/dependencies/CreateRequireParserPlugin.js +6 -6
  62. package/lib/dependencies/CssIcssExportDependency.js +190 -87
  63. package/lib/dependencies/CssUrlDependency.js +35 -4
  64. package/lib/dependencies/ExternalModuleInitFragmentDependency.js +2 -2
  65. package/lib/dependencies/HarmonyCompatibilityDependency.js +32 -14
  66. package/lib/dependencies/HarmonyDetectionParserPlugin.js +43 -13
  67. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +7 -19
  68. package/lib/dependencies/HarmonyImportDependency.js +12 -1
  69. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +34 -107
  70. package/lib/dependencies/HarmonyImportGuard.js +192 -20
  71. package/lib/dependencies/HarmonyModulesPlugin.js +6 -0
  72. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +1 -1
  73. package/lib/dependencies/HtmlEntryDependency.js +797 -172
  74. package/lib/dependencies/HtmlSourceDependency.js +26 -0
  75. package/lib/dependencies/ImportDependency.js +43 -1
  76. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +31 -18
  77. package/lib/dependencies/ImportMetaContextPlugin.js +38 -7
  78. package/lib/dependencies/ImportMetaGlobDependency.js +81 -0
  79. package/lib/dependencies/ImportMetaGlobDependencyParserPlugin.js +78 -0
  80. package/lib/dependencies/ImportMetaGlobHelpers.js +660 -0
  81. package/lib/dependencies/ImportMetaPlugin.js +532 -180
  82. package/lib/dependencies/ImportMetaResolveDependency.js +90 -0
  83. package/lib/dependencies/ImportParserPlugin.js +87 -26
  84. package/lib/dependencies/ImportPhase.js +10 -8
  85. package/lib/dependencies/LoaderPlugin.js +5 -1
  86. package/lib/dependencies/RequireContextDependencyParserPlugin.js +1 -1
  87. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +3 -6
  88. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +4 -11
  89. package/lib/dependencies/SystemPlugin.js +2 -4
  90. package/lib/dependencies/TopLevelAwaitDependency.js +93 -0
  91. package/lib/dependencies/URLDependency.js +141 -21
  92. package/lib/dependencies/{WorkerPlugin.js → WorkerAndWorkletPlugin.js} +324 -112
  93. package/lib/dependencies/WorkerDependency.js +86 -18
  94. package/lib/dependencies/WorkletDependency.js +278 -0
  95. package/lib/dll/DelegatedModuleFactoryPlugin.js +19 -0
  96. package/lib/errors/EnvironmentNotSupportAsyncWarning.js +1 -1
  97. package/lib/errors/LoaderLoadingError.js +20 -0
  98. package/lib/errors/ModuleParseError.js +39 -22
  99. package/lib/errors/NodeStuffInWebError.js +1 -1
  100. package/lib/esm/ModuleChunkFormatPlugin.js +47 -14
  101. package/lib/esm/ModuleChunkLoadingPlugin.js +8 -1
  102. package/lib/esm/ModuleChunkLoadingRuntimeModule.js +29 -1
  103. package/lib/hmr/lazyCompilationBackend.js +32 -8
  104. package/lib/html/HtmlGenerator.js +940 -91
  105. package/lib/html/HtmlModule.js +3 -2
  106. package/lib/html/HtmlModulesPlugin.js +625 -170
  107. package/lib/html/HtmlParser.js +812 -340
  108. package/lib/html/favicon.svg +1 -0
  109. package/lib/html/syntax.js +2116 -834
  110. package/lib/ids/IdHelpers.js +1 -1
  111. package/lib/index.js +6 -0
  112. package/lib/javascript/ArrayPushCallbackChunkFormatPlugin.js +4 -5
  113. package/lib/javascript/BasicEvaluatedExpression.js +366 -48
  114. package/lib/javascript/JavascriptGenerator.js +197 -49
  115. package/lib/javascript/JavascriptModulesPlugin.js +178 -24
  116. package/lib/javascript/JavascriptParser.js +711 -355
  117. package/lib/javascript/JavascriptParserHelpers.js +3 -6
  118. package/lib/javascript/syntax.js +4380 -0
  119. package/lib/library/ModuleLibraryPlugin.js +38 -1
  120. package/lib/loaders/LoaderRunner.js +714 -0
  121. package/lib/loaders/loadLoader.js +100 -0
  122. package/lib/node/ReadFileCompileAsyncWasmPlugin.js +19 -0
  123. package/lib/node/ReadFileCompileWasmPlugin.js +14 -0
  124. package/lib/optimize/ConcatenatedModule.js +120 -16
  125. package/lib/optimize/FlagIncludedChunksPlugin.js +13 -3
  126. package/lib/optimize/InnerGraphPlugin.js +50 -30
  127. package/lib/optimize/ModuleConcatenationPlugin.js +69 -3
  128. package/lib/optimize/SideEffectsFlagPlugin.js +86 -43
  129. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +15 -0
  130. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +24 -13
  131. package/lib/prefetch/ResourceHintPlugin.js +663 -0
  132. package/lib/prefetch/ResourceHintRuntimeModule.js +149 -0
  133. package/lib/prefetch/StartupAssetHintRuntimeModule.js +210 -0
  134. package/lib/prefetch/parseResourceHintOptions.js +103 -0
  135. package/lib/rules/RuleSetCompiler.js +114 -0
  136. package/lib/runtime/AsyncModuleGeneratorRuntimeModule.js +56 -0
  137. package/lib/runtime/AsyncModuleRuntimeModule.js +24 -8
  138. package/lib/runtime/AutoPublicPathRuntimeModule.js +7 -1
  139. package/lib/runtime/CommonJsWrapRuntimeModule.js +37 -0
  140. package/lib/runtime/GetWorkletBootstrapRuntimeModule.js +59 -0
  141. package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +204 -8
  142. package/lib/runtime/SetAnonymousDefaultNameRuntimeModule.js +4 -1
  143. package/lib/schemes/HttpUriPlugin.js +40 -1
  144. package/lib/schemes/VirtualUrlPlugin.js +1 -2
  145. package/lib/serialization/FileMiddleware.js +29 -3
  146. package/lib/stats/DefaultStatsFactoryPlugin.js +32 -3
  147. package/lib/stats/DefaultStatsPresetPlugin.js +2 -0
  148. package/lib/stats/DefaultStatsPrinterPlugin.js +12 -2
  149. package/lib/stats/StatsFactory.js +15 -8
  150. package/lib/url/URLParserPlugin.js +98 -19
  151. package/lib/util/AsyncQueue.js +122 -72
  152. package/lib/util/LocConverter.js +9 -8
  153. package/lib/util/SetHelpers.js +1 -1
  154. package/lib/util/SourceProcessor.js +41 -28
  155. package/lib/util/StackedMap.js +122 -48
  156. package/lib/util/binarySearchBounds.js +2 -2
  157. package/lib/util/concatenate.js +40 -14
  158. package/lib/util/createHooksRegistry.js +13 -5
  159. package/lib/util/dataURL.js +2 -2
  160. package/lib/util/deterministicGrouping.js +7 -5
  161. package/lib/util/formatSize.js +6 -2
  162. package/lib/util/globUtils.js +710 -0
  163. package/lib/util/identifier.js +23 -6
  164. package/lib/util/implicitTypeLoaderFallback.js +62 -0
  165. package/lib/util/internalSerializables.js +88 -71
  166. package/lib/util/magicComment.js +121 -25
  167. package/lib/util/numberHash.js +2 -2
  168. package/lib/wasm-async/AsyncWasmCompileRuntimeModule.js +24 -18
  169. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +33 -22
  170. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +79 -16
  171. package/lib/web/FetchCompileAsyncWasmPlugin.js +19 -0
  172. package/lib/web/FetchCompileWasmPlugin.js +14 -0
  173. package/lib/web/JsonpChunkLoadingRuntimeModule.js +2 -0
  174. package/lib/webpack.js +65 -0
  175. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +3 -0
  176. package/module.d.ts +21 -0
  177. package/package.json +14 -13
  178. package/schemas/WebpackOptions.check.js +1 -1
  179. package/schemas/WebpackOptions.json +759 -36
  180. package/schemas/plugins/HtmlParserOptions.check.js +1 -1
  181. package/schemas/plugins/ProgressPlugin.check.js +1 -1
  182. package/schemas/plugins/ProgressPlugin.json +17 -1
  183. package/schemas/plugins/container/ContainerReferencePlugin.check.js +1 -1
  184. package/schemas/plugins/container/ContainerReferencePlugin.json +1 -0
  185. package/schemas/plugins/container/ExternalsType.check.js +1 -1
  186. package/schemas/plugins/container/ModuleFederationPlugin.check.js +1 -1
  187. package/schemas/plugins/container/ModuleFederationPlugin.json +1 -0
  188. package/schemas/plugins/css/CssAutoOrModuleParserOptions.check.js +1 -1
  189. package/schemas/plugins/css/CssModuleParserOptions.check.js +1 -1
  190. package/schemas/plugins/css/CssParserOptions.check.js +1 -1
  191. package/types.d.ts +1300 -325
@@ -0,0 +1,90 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Alexander Akait @alexander-akait
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const RuntimeGlobals = require("../RuntimeGlobals");
9
+ const makeSerializable = require("../util/makeSerializable");
10
+ const ModuleDependency = require("./ModuleDependency");
11
+ const URLDependency = require("./URLDependency");
12
+
13
+ /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
14
+ /** @typedef {import("../Dependency")} Dependency */
15
+ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
16
+ /** @typedef {import("../javascript/JavascriptParser").Range} Range */
17
+
18
+ // `import.meta.resolve(specifier)` is the string form of
19
+ // `new URL(specifier, import.meta.url)`, so it reuses the URL asset pipeline
20
+ // but renders to the resolved URL string (`.href`) over the whole call.
21
+ class ImportMetaResolveDependency extends URLDependency {
22
+ get type() {
23
+ return "import.meta.resolve()";
24
+ }
25
+ }
26
+
27
+ ImportMetaResolveDependency.Template = class ImportMetaResolveDependencyTemplate extends (
28
+ ModuleDependency.Template
29
+ ) {
30
+ /**
31
+ * Applies the plugin by registering its hooks on the compiler.
32
+ * @param {Dependency} dependency the dependency for which the template should be applied
33
+ * @param {ReplaceSource} source the current replace source which can be modified
34
+ * @param {DependencyTemplateContext} templateContext the context object
35
+ * @returns {void}
36
+ */
37
+ apply(dependency, source, templateContext) {
38
+ const {
39
+ chunkGraph,
40
+ moduleGraph,
41
+ runtimeRequirements,
42
+ runtimeTemplate,
43
+ runtime
44
+ } = templateContext;
45
+ const dep = /** @type {ImportMetaResolveDependency} */ (dependency);
46
+ const connection = moduleGraph.getConnection(dep);
47
+ // Skip rendering the dependency when it is conditional and inactive.
48
+ if (connection && !connection.isTargetActive(runtime)) {
49
+ source.replace(
50
+ dep.outerRange[0],
51
+ dep.outerRange[1] - 1,
52
+ "/* unused asset import */ undefined"
53
+ );
54
+ return;
55
+ }
56
+
57
+ runtimeRequirements.add(RuntimeGlobals.require);
58
+
59
+ const moduleRaw = runtimeTemplate.moduleRaw({
60
+ chunkGraph,
61
+ module: moduleGraph.getModule(dep),
62
+ request: dep.request,
63
+ runtimeRequirements,
64
+ weak: false
65
+ });
66
+
67
+ if (dep.relative) {
68
+ runtimeRequirements.add(RuntimeGlobals.relativeUrl);
69
+ source.replace(
70
+ dep.outerRange[0],
71
+ dep.outerRange[1] - 1,
72
+ `(new ${RuntimeGlobals.relativeUrl}(/* asset import */ ${moduleRaw})).href`
73
+ );
74
+ } else {
75
+ runtimeRequirements.add(RuntimeGlobals.baseURI);
76
+ source.replace(
77
+ dep.outerRange[0],
78
+ dep.outerRange[1] - 1,
79
+ `(new URL(/* asset import */ ${moduleRaw}, ${RuntimeGlobals.baseURI})).href`
80
+ );
81
+ }
82
+ }
83
+ };
84
+
85
+ makeSerializable(
86
+ ImportMetaResolveDependency,
87
+ "webpack/lib/dependencies/ImportMetaResolveDependency"
88
+ );
89
+
90
+ module.exports = ImportMetaResolveDependency;
@@ -174,6 +174,47 @@ const exportsFromEnumerable = (enumerable) =>
174
174
 
175
175
  const PLUGIN_NAME = "ImportParserPlugin";
176
176
 
177
+ /**
178
+ * Whether an `import()` second argument is a fully static attributes object
179
+ * (every `with`/`assert` value is a string literal). Only then can webpack use
180
+ * the attributes at build time; otherwise the argument must be evaluated and
181
+ * validated at runtime per spec.
182
+ * @param {import("estree").Expression | null | undefined} node the second-argument AST node
183
+ * @returns {boolean} true if statically extractable
184
+ */
185
+ const isStaticStringAttributes = (node) => {
186
+ if (!node || node.type !== "ObjectExpression") return false;
187
+ for (const prop of node.properties) {
188
+ if (prop.type !== "Property" || prop.kind !== "init" || prop.computed) {
189
+ return false;
190
+ }
191
+ const key = prop.key;
192
+ const keyName =
193
+ key.type === "Identifier"
194
+ ? key.name
195
+ : key.type === "Literal"
196
+ ? key.value
197
+ : undefined;
198
+ if (keyName === "with" || keyName === "assert") {
199
+ const value = prop.value;
200
+ if (value.type !== "ObjectExpression") return false;
201
+ for (const attr of value.properties) {
202
+ if (attr.type !== "Property" || attr.kind !== "init" || attr.computed) {
203
+ return false;
204
+ }
205
+ const attrValue = attr.value;
206
+ if (
207
+ attrValue.type !== "Literal" ||
208
+ typeof attrValue.value !== "string"
209
+ ) {
210
+ return false;
211
+ }
212
+ }
213
+ }
214
+ }
215
+ return true;
216
+ };
217
+
177
218
  class ImportParserPlugin {
178
219
  /**
179
220
  * Creates an instance of ImportParserPlugin.
@@ -277,6 +318,7 @@ class ImportParserPlugin {
277
318
 
278
319
  const {
279
320
  dynamicImportPreload,
321
+ dynamicImportCssPreload,
280
322
  dynamicImportPrefetch,
281
323
  dynamicImportFetchPriority
282
324
  } = this.options;
@@ -287,6 +329,13 @@ class ImportParserPlugin {
287
329
  groupOptions.preloadOrder =
288
330
  dynamicImportPreload === true ? 0 : dynamicImportPreload;
289
331
  }
332
+ if (
333
+ dynamicImportCssPreload !== undefined &&
334
+ dynamicImportCssPreload !== false
335
+ ) {
336
+ groupOptions.cssPreloadOrder =
337
+ dynamicImportCssPreload === true ? 0 : dynamicImportCssPreload;
338
+ }
290
339
  if (
291
340
  dynamicImportPrefetch !== undefined &&
292
341
  dynamicImportPrefetch !== false
@@ -310,7 +359,7 @@ class ImportParserPlugin {
310
359
  parser.state.module.addWarning(
311
360
  new CommentCompilationWarning(
312
361
  `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
313
- /** @type {DependencyLocation} */ (comment.loc)
362
+ parser.getLocation(comment)
314
363
  )
315
364
  );
316
365
  }
@@ -327,7 +376,7 @@ class ImportParserPlugin {
327
376
  parser.state.module.addWarning(
328
377
  new UnsupportedFeatureWarning(
329
378
  `\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
330
- /** @type {DependencyLocation} */ (expr.loc)
379
+ parser.getLocation(expr)
331
380
  )
332
381
  );
333
382
  } else if (importOptions.webpackIgnore) {
@@ -340,7 +389,7 @@ class ImportParserPlugin {
340
389
  parser.state.module.addWarning(
341
390
  new UnsupportedFeatureWarning(
342
391
  `\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
343
- /** @type {DependencyLocation} */ (expr.loc)
392
+ parser.getLocation(expr)
344
393
  )
345
394
  );
346
395
  } else {
@@ -352,7 +401,7 @@ class ImportParserPlugin {
352
401
  parser.state.module.addWarning(
353
402
  new UnsupportedFeatureWarning(
354
403
  `\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`,
355
- /** @type {DependencyLocation} */ (expr.loc)
404
+ parser.getLocation(expr)
356
405
  )
357
406
  );
358
407
  } else {
@@ -368,7 +417,7 @@ class ImportParserPlugin {
368
417
  parser.state.module.addWarning(
369
418
  new UnsupportedFeatureWarning(
370
419
  `\`webpackPrefetch\` expected true or a number, but received: ${importOptions.webpackPrefetch}.`,
371
- /** @type {DependencyLocation} */ (expr.loc)
420
+ parser.getLocation(expr)
372
421
  )
373
422
  );
374
423
  }
@@ -382,7 +431,7 @@ class ImportParserPlugin {
382
431
  parser.state.module.addWarning(
383
432
  new UnsupportedFeatureWarning(
384
433
  `\`webpackPreload\` expected true or a number, but received: ${importOptions.webpackPreload}.`,
385
- /** @type {DependencyLocation} */ (expr.loc)
434
+ parser.getLocation(expr)
386
435
  )
387
436
  );
388
437
  }
@@ -399,7 +448,7 @@ class ImportParserPlugin {
399
448
  parser.state.module.addWarning(
400
449
  new UnsupportedFeatureWarning(
401
450
  `\`webpackFetchPriority\` expected true or "low", "high" or "auto", but received: ${importOptions.webpackFetchPriority}.`,
402
- /** @type {DependencyLocation} */ (expr.loc)
451
+ parser.getLocation(expr)
403
452
  )
404
453
  );
405
454
  }
@@ -412,7 +461,7 @@ class ImportParserPlugin {
412
461
  parser.state.module.addWarning(
413
462
  new UnsupportedFeatureWarning(
414
463
  `\`webpackInclude\` expected a regular expression, but received: ${importOptions.webpackInclude}.`,
415
- /** @type {DependencyLocation} */ (expr.loc)
464
+ parser.getLocation(expr)
416
465
  )
417
466
  );
418
467
  } else {
@@ -427,7 +476,7 @@ class ImportParserPlugin {
427
476
  parser.state.module.addWarning(
428
477
  new UnsupportedFeatureWarning(
429
478
  `\`webpackExclude\` expected a regular expression, but received: ${importOptions.webpackExclude}.`,
430
- /** @type {DependencyLocation} */ (expr.loc)
479
+ parser.getLocation(expr)
431
480
  )
432
481
  );
433
482
  } else {
@@ -435,19 +484,17 @@ class ImportParserPlugin {
435
484
  }
436
485
  }
437
486
  if (importOptions.webpackExports !== undefined) {
438
- if (
439
- !(
440
- typeof importOptions.webpackExports === "string" ||
441
- (Array.isArray(importOptions.webpackExports) &&
442
- importOptions.webpackExports.every(
443
- (item) => typeof item === "string"
444
- ))
445
- )
446
- ) {
487
+ if (!(
488
+ typeof importOptions.webpackExports === "string" ||
489
+ (Array.isArray(importOptions.webpackExports) &&
490
+ importOptions.webpackExports.every(
491
+ (item) => typeof item === "string"
492
+ ))
493
+ )) {
447
494
  parser.state.module.addWarning(
448
495
  new UnsupportedFeatureWarning(
449
496
  `\`webpackExports\` expected a string or an array of strings, but received: ${importOptions.webpackExports}.`,
450
- /** @type {DependencyLocation} */ (expr.loc)
497
+ parser.getLocation(expr)
451
498
  )
452
499
  );
453
500
  } else if (typeof importOptions.webpackExports === "string") {
@@ -466,7 +513,7 @@ class ImportParserPlugin {
466
513
  parser.state.module.addWarning(
467
514
  new UnsupportedFeatureWarning(
468
515
  "`worker` entry option is not supported in `import()`, it only applies to workers (e.g. `new Worker(new URL(...))`).",
469
- /** @type {DependencyLocation} */ (expr.loc)
516
+ parser.getLocation(expr)
470
517
  )
471
518
  );
472
519
  }
@@ -481,7 +528,7 @@ class ImportParserPlugin {
481
528
  parser.state.module.addWarning(
482
529
  new UnsupportedFeatureWarning(
483
530
  `\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
484
- /** @type {DependencyLocation} */ (expr.loc)
531
+ parser.getLocation(expr)
485
532
  )
486
533
  );
487
534
  mode = "lazy";
@@ -502,7 +549,7 @@ class ImportParserPlugin {
502
549
  parser.state.module.addWarning(
503
550
  new UnsupportedFeatureWarning(
504
551
  "You don't need `webpackExports` if the usage of dynamic import is statically analyse-able. You can safely remove the `webpackExports` magic comment.",
505
- /** @type {DependencyLocation} */ (expr.loc)
552
+ parser.getLocation(expr)
506
553
  )
507
554
  );
508
555
  }
@@ -554,17 +601,31 @@ class ImportParserPlugin {
554
601
  ...groupOptions,
555
602
  name: chunkName
556
603
  },
557
- /** @type {DependencyLocation} */ (expr.loc),
604
+ parser.getLocation(expr),
558
605
  param.string
559
606
  );
607
+ // A second argument that isn't a statically extractable attributes
608
+ // object still has to be evaluated and validated at runtime, so
609
+ // drop the (unreliable) static attributes for it.
610
+ const optionsNode = expr.options;
611
+ const runtimeValidateOptions = Boolean(
612
+ optionsNode && !isStaticStringAttributes(optionsNode)
613
+ );
560
614
  const dep = new ImportDependency(
561
615
  /** @type {string} */ (param.string),
562
616
  /** @type {Range} */ (expr.range),
563
617
  exports,
564
618
  phase,
565
- attributes
619
+ runtimeValidateOptions ? undefined : attributes
566
620
  );
567
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
621
+ if (runtimeValidateOptions && optionsNode && optionsNode.range) {
622
+ dep.optionsRange = /** @type {Range} */ (optionsNode.range);
623
+ // The options expression stays in the output, so walk it to
624
+ // register its variable references and keep them from being
625
+ // tree-shaken away.
626
+ parser.walkExpression(optionsNode);
627
+ }
628
+ dep.loc = parser.getLocation(expr);
568
629
  dep.optional = Boolean(parser.scope.inTry);
569
630
  depBlock.addDependency(dep);
570
631
  parser.state.current.addBlock(depBlock);
@@ -601,7 +662,7 @@ class ImportParserPlugin {
601
662
  parser
602
663
  );
603
664
  if (!dep) return;
604
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
665
+ dep.loc = parser.getLocation(expr);
605
666
  dep.optional = Boolean(parser.scope.inTry);
606
667
  parser.state.current.addDependency(dep);
607
668
  }
@@ -103,15 +103,13 @@ function createGetImportPhase(enableDeferPhase, enableSourcePhase) {
103
103
  if (!node.range) return null;
104
104
  const { options, errors } = parser.parseCommentOptions(node.range);
105
105
  if (errors) {
106
+ const CommentCompilationWarning = getCommentCompilationWarning();
106
107
  for (const e of errors) {
107
108
  const { comment } = e;
108
- if (!comment.loc) continue;
109
-
110
- const CommentCompilationWarning = getCommentCompilationWarning();
111
109
  parser.state.module.addWarning(
112
110
  new CommentCompilationWarning(
113
111
  `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
114
- comment.loc
112
+ parser.getLocation(comment)
115
113
  )
116
114
  );
117
115
  }
@@ -134,13 +132,15 @@ function createGetImportPhase(enableDeferPhase, enableSourcePhase) {
134
132
  if (enableDeferPhase && typeof options.webpackDefer !== "undefined") {
135
133
  if (typeof webpackDefer === "boolean") {
136
134
  return webpackDefer ? ImportPhase.Defer : phaseBySyntax;
137
- } else if (node.loc) {
135
+ }
136
+ const loc = parser.getLocation(node);
137
+ if (loc) {
138
138
  const CommentCompilationWarning = getCommentCompilationWarning();
139
139
 
140
140
  parser.state.module.addWarning(
141
141
  new CommentCompilationWarning(
142
142
  "webpackDefer magic comment expected a boolean value.",
143
- node.loc
143
+ loc
144
144
  )
145
145
  );
146
146
  }
@@ -149,13 +149,15 @@ function createGetImportPhase(enableDeferPhase, enableSourcePhase) {
149
149
  if (enableSourcePhase && typeof options.webpackSource !== "undefined") {
150
150
  if (typeof webpackSource === "boolean") {
151
151
  return webpackSource ? ImportPhase.Source : phaseBySyntax;
152
- } else if (node.loc) {
152
+ }
153
+ const loc = parser.getLocation(node);
154
+ if (loc) {
153
155
  const CommentCompilationWarning = getCommentCompilationWarning();
154
156
 
155
157
  parser.state.module.addWarning(
156
158
  new CommentCompilationWarning(
157
159
  "webpackSource magic comment expected a boolean value.",
158
- node.loc
160
+ loc
159
161
  )
160
162
  );
161
163
  }
@@ -6,6 +6,7 @@
6
6
  "use strict";
7
7
 
8
8
  const NormalModule = require("../NormalModule");
9
+ const { markNotCacheable } = require("../loaders/LoaderRunner");
9
10
  const LazySet = require("../util/LazySet");
10
11
  const LoaderDependency = require("./LoaderDependency");
11
12
  const LoaderImportDependency = require("./LoaderImportDependency");
@@ -237,6 +238,7 @@ class LoaderPlugin {
237
238
  missingDependencies,
238
239
  buildDependencies,
239
240
  cacheable,
241
+ notCacheableReasons,
240
242
  assets,
241
243
  exports
242
244
  } = /** @type {ExecuteModuleResult} */ (result);
@@ -252,7 +254,9 @@ class LoaderPlugin {
252
254
  for (const d of buildDependencies) {
253
255
  loaderContext.addBuildDependency(d);
254
256
  }
255
- if (cacheable === false) loaderContext.cacheable(false);
257
+ if (cacheable === false) {
258
+ markNotCacheable(loaderContext, notCacheableReasons);
259
+ }
256
260
  for (const [name, { source, info }] of assets) {
257
261
  const buildInfo =
258
262
  /** @type {BuildInfo} */
@@ -59,7 +59,7 @@ module.exports = class RequireContextDependencyParserPlugin {
59
59
  /** @type {Range} */
60
60
  (expr.range)
61
61
  );
62
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
62
+ dep.loc = parser.getLocation(expr);
63
63
  dep.optional = Boolean(parser.scope.inTry);
64
64
  parser.state.current.addDependency(dep);
65
65
  return true;
@@ -76,8 +76,7 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
76
76
 
77
77
  const depBlock = new RequireEnsureDependenciesBlock(
78
78
  chunkName,
79
- /** @type {DependencyLocation} */
80
- (expr.loc)
79
+ parser.getLocation(expr)
81
80
  );
82
81
  const errorCallbackExists =
83
82
  expr.arguments.length === 4 ||
@@ -88,7 +87,7 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
88
87
  errorCallbackExists &&
89
88
  /** @type {Range} */ (expr.arguments[2].range)
90
89
  );
91
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
90
+ dep.loc = parser.getLocation(expr);
92
91
  depBlock.addDependency(dep);
93
92
  const old = parser.state.current;
94
93
  parser.state.current = /** @type {EXPECTED_ANY} */ (depBlock);
@@ -100,9 +99,7 @@ module.exports = class RequireEnsureDependenciesBlockParserPlugin {
100
99
  const ensureDependency = new RequireEnsureItemDependency(
101
100
  /** @type {string} */ (ee.string)
102
101
  );
103
- ensureDependency.loc =
104
- /** @type {DependencyLocation} */
105
- (expr.loc);
102
+ ensureDependency.loc = parser.getLocation(expr);
106
103
  depBlock.addDependency(ensureDependency);
107
104
  } else {
108
105
  failed = true;
@@ -43,10 +43,7 @@ module.exports = class RequireIncludeDependencyParserPlugin {
43
43
 
44
44
  if (warn) {
45
45
  parser.state.module.addWarning(
46
- new RequireIncludeDeprecationWarning(
47
- /** @type {DependencyLocation} */
48
- (expr.loc)
49
- )
46
+ new RequireIncludeDeprecationWarning(parser.getLocation(expr))
50
47
  );
51
48
  }
52
49
 
@@ -54,7 +51,7 @@ module.exports = class RequireIncludeDependencyParserPlugin {
54
51
  /** @type {string} */ (param.string),
55
52
  /** @type {Range} */ (expr.range)
56
53
  );
57
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
54
+ dep.loc = parser.getLocation(expr);
58
55
  parser.state.current.addDependency(dep);
59
56
  return true;
60
57
  });
@@ -63,9 +60,7 @@ module.exports = class RequireIncludeDependencyParserPlugin {
63
60
  .tap(PLUGIN_NAME, (expr) => {
64
61
  if (warn) {
65
62
  parser.state.module.addWarning(
66
- new RequireIncludeDeprecationWarning(
67
- /** @type {DependencyLocation} */ (expr.loc)
68
- )
63
+ new RequireIncludeDeprecationWarning(parser.getLocation(expr))
69
64
  );
70
65
  }
71
66
  return evaluateToString("function")(expr);
@@ -73,9 +68,7 @@ module.exports = class RequireIncludeDependencyParserPlugin {
73
68
  parser.hooks.typeof.for("require.include").tap(PLUGIN_NAME, (expr) => {
74
69
  if (warn) {
75
70
  parser.state.module.addWarning(
76
- new RequireIncludeDeprecationWarning(
77
- /** @type {DependencyLocation} */ (expr.loc)
78
- )
71
+ new RequireIncludeDeprecationWarning(parser.getLocation(expr))
79
72
  );
80
73
  }
81
74
  return toConstantDependency(parser, JSON.stringify("function"))(expr);
@@ -109,16 +109,14 @@ class SystemPlugin {
109
109
  /** @type {Range} */ (expr.range),
110
110
  [RuntimeGlobals.system]
111
111
  );
112
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
112
+ dep.loc = parser.getLocation(expr);
113
113
  parser.state.module.addPresentationalDependency(dep);
114
114
  return true;
115
115
  });
116
116
 
117
117
  parser.hooks.call.for("System.import").tap(PLUGIN_NAME, (expr) => {
118
118
  parser.state.module.addWarning(
119
- new SystemImportDeprecationWarning(
120
- /** @type {DependencyLocation} */ (expr.loc)
121
- )
119
+ new SystemImportDeprecationWarning(parser.getLocation(expr))
122
120
  );
123
121
 
124
122
  return parser.hooks.importCall.call({
@@ -0,0 +1,93 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ */
4
+
5
+ "use strict";
6
+
7
+ const isGeneratorLowered = require("../async-modules/isGeneratorLowered");
8
+ const makeSerializable = require("../util/makeSerializable");
9
+ const NullDependency = require("./NullDependency");
10
+
11
+ /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
12
+ /** @typedef {import("../Dependency")} Dependency */
13
+ /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
14
+ /** @typedef {import("../Module")} Module */
15
+ /** @typedef {import("../javascript/JavascriptParser").Range} Range */
16
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
17
+ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
18
+
19
+ /**
20
+ * Marks a top-level `await` expression so it can be rewritten to `(yield …)`
21
+ * when the module is lowered to a generator (target without `async`/`await`).
22
+ */
23
+ class TopLevelAwaitDependency extends NullDependency {
24
+ /**
25
+ * @param {Range} range range of the whole `await` expression
26
+ * @param {Range} argumentRange range of the awaited argument
27
+ */
28
+ constructor(range, argumentRange) {
29
+ super();
30
+ /** @type {Range} */
31
+ this.range = range;
32
+ /** @type {Range} */
33
+ this.argumentRange = argumentRange;
34
+ }
35
+
36
+ /**
37
+ * Serializes this instance into the provided serializer context.
38
+ * @param {ObjectSerializerContext} context context
39
+ */
40
+ serialize(context) {
41
+ const { write } = context;
42
+ write(this.range);
43
+ write(this.argumentRange);
44
+ super.serialize(context);
45
+ }
46
+
47
+ /**
48
+ * Restores this instance from the provided deserializer context.
49
+ * @param {ObjectDeserializerContext} context context
50
+ */
51
+ deserialize(context) {
52
+ const { read } = context;
53
+ this.range = read();
54
+ this.argumentRange = read();
55
+ super.deserialize(context);
56
+ }
57
+ }
58
+
59
+ makeSerializable(
60
+ TopLevelAwaitDependency,
61
+ "webpack/lib/dependencies/TopLevelAwaitDependency"
62
+ );
63
+
64
+ TopLevelAwaitDependency.Template = class TopLevelAwaitDependencyTemplate extends (
65
+ NullDependency.Template
66
+ ) {
67
+ /**
68
+ * Applies the plugin by registering its hooks on the compiler.
69
+ * @param {Dependency} dependency the dependency for which the template should be applied
70
+ * @param {ReplaceSource} source the current replace source which can be modified
71
+ * @param {DependencyTemplateContext} templateContext the context object
72
+ * @returns {void}
73
+ */
74
+ apply(dependency, source, { module, moduleGraph, runtimeTemplate }) {
75
+ // Only rewrite when the body is a generator (no `async`/`await`, but
76
+ // generators). `await` and `yield` differ in precedence, so wrap the
77
+ // whole expression: `await X` -> `(yield X)`.
78
+ if (
79
+ !isGeneratorLowered(
80
+ /** @type {Module} */ (module),
81
+ moduleGraph,
82
+ runtimeTemplate
83
+ )
84
+ ) {
85
+ return;
86
+ }
87
+ const dep = /** @type {TopLevelAwaitDependency} */ (dependency);
88
+ source.replace(dep.range[0], dep.argumentRange[0] - 1, "(yield ");
89
+ source.insert(dep.argumentRange[1], ")");
90
+ }
91
+ };
92
+
93
+ module.exports = TopLevelAwaitDependency;