webpack 5.86.0 → 5.87.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.

Potentially problematic release.


This version of webpack might be problematic. Click here for more details.

Files changed (105) hide show
  1. package/README.md +5 -2
  2. package/lib/CaseSensitiveModulesWarning.js +3 -1
  3. package/lib/ChunkGroup.js +1 -0
  4. package/lib/Compiler.js +3 -1
  5. package/lib/ConcatenationScope.js +2 -2
  6. package/lib/ConditionalInitFragment.js +11 -1
  7. package/lib/ConstPlugin.js +57 -29
  8. package/lib/FlagEntryExportAsUsedPlugin.js +4 -0
  9. package/lib/ModuleDependencyError.js +4 -2
  10. package/lib/ModuleDependencyWarning.js +4 -2
  11. package/lib/ModuleGraph.js +31 -24
  12. package/lib/ModuleGraphConnection.js +19 -6
  13. package/lib/ModuleInfoHeaderPlugin.js +9 -2
  14. package/lib/ModuleNotFoundError.js +5 -2
  15. package/lib/ModuleStoreError.js +2 -1
  16. package/lib/ModuleTypeConstants.js +7 -0
  17. package/lib/MultiWatching.js +4 -0
  18. package/lib/RuntimeGlobals.js +5 -0
  19. package/lib/RuntimePlugin.js +2 -1
  20. package/lib/RuntimeTemplate.js +20 -2
  21. package/lib/WebpackError.js +6 -5
  22. package/lib/WebpackOptionsApply.js +1 -1
  23. package/lib/WebpackOptionsDefaulter.js +10 -3
  24. package/lib/config/defaults.js +31 -2
  25. package/lib/css/CssModulesPlugin.js +5 -2
  26. package/lib/css/CssParser.js +12 -0
  27. package/lib/dependencies/ConstDependency.js +2 -2
  28. package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +19 -0
  29. package/lib/dependencies/ImportParserPlugin.js +25 -1
  30. package/lib/ids/SyncModuleIdsPlugin.js +1 -0
  31. package/lib/javascript/BasicEvaluatedExpression.js +23 -15
  32. package/lib/javascript/CommonJsChunkFormatPlugin.js +6 -2
  33. package/lib/javascript/JavascriptParser.js +118 -58
  34. package/lib/javascript/JavascriptParserHelpers.js +37 -15
  35. package/lib/performance/SizeLimitsPlugin.js +7 -4
  36. package/lib/prefetch/ChunkPrefetchFunctionRuntimeModule.js +3 -1
  37. package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +3 -1
  38. package/lib/prefetch/ChunkPrefetchStartupRuntimeModule.js +5 -2
  39. package/lib/prefetch/ChunkPrefetchTriggerRuntimeModule.js +3 -1
  40. package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +3 -1
  41. package/lib/rules/BasicEffectRulePlugin.js +4 -0
  42. package/lib/rules/BasicMatcherRulePlugin.js +5 -0
  43. package/lib/rules/RuleSetCompiler.js +3 -3
  44. package/lib/rules/UseEffectRulePlugin.js +6 -4
  45. package/lib/runtime/AsyncModuleRuntimeModule.js +4 -1
  46. package/lib/runtime/AutoPublicPathRuntimeModule.js +3 -1
  47. package/lib/runtime/BaseUriRuntimeModule.js +2 -2
  48. package/lib/runtime/CompatGetDefaultExportRuntimeModule.js +4 -1
  49. package/lib/runtime/CompatRuntimeModule.js +6 -1
  50. package/lib/runtime/CreateFakeNamespaceObjectRuntimeModule.js +4 -1
  51. package/lib/runtime/CreateScriptRuntimeModule.js +3 -1
  52. package/lib/runtime/CreateScriptUrlRuntimeModule.js +3 -1
  53. package/lib/runtime/DefinePropertyGettersRuntimeModule.js +4 -1
  54. package/lib/runtime/EnsureChunkRuntimeModule.js +14 -3
  55. package/lib/runtime/GetChunkFilenameRuntimeModule.js +5 -9
  56. package/lib/runtime/GetFullHashRuntimeModule.js +3 -2
  57. package/lib/runtime/GetMainFilenameRuntimeModule.js +4 -1
  58. package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +3 -1
  59. package/lib/runtime/HasOwnPropertyRuntimeModule.js +4 -1
  60. package/lib/runtime/LoadScriptRuntimeModule.js +63 -47
  61. package/lib/runtime/MakeNamespaceObjectRuntimeModule.js +4 -1
  62. package/lib/runtime/OnChunksLoadedRuntimeModule.js +3 -1
  63. package/lib/runtime/PublicPathRuntimeModule.js +3 -1
  64. package/lib/runtime/RelativeUrlRuntimeModule.js +4 -1
  65. package/lib/runtime/RuntimeIdRuntimeModule.js +5 -1
  66. package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +8 -2
  67. package/lib/runtime/StartupEntrypointRuntimeModule.js +2 -1
  68. package/lib/serialization/ObjectMiddleware.js +6 -3
  69. package/lib/sharing/ConsumeSharedFallbackDependency.js +3 -0
  70. package/lib/sharing/ConsumeSharedRuntimeModule.js +13 -4
  71. package/lib/sharing/ProvideSharedDependency.js +11 -0
  72. package/lib/sharing/ProvideSharedModule.js +4 -0
  73. package/lib/sharing/ProvideSharedPlugin.js +22 -21
  74. package/lib/sharing/ShareRuntimeModule.js +11 -4
  75. package/lib/sharing/resolveMatchedConfigs.js +1 -1
  76. package/lib/sharing/utils.js +32 -5
  77. package/lib/util/AsyncQueue.js +4 -2
  78. package/lib/util/ParallelismFactorCalculator.js +10 -0
  79. package/lib/util/Semaphore.js +1 -1
  80. package/lib/util/createHash.js +30 -9
  81. package/lib/util/deprecation.js +10 -3
  82. package/lib/util/deterministicGrouping.js +50 -11
  83. package/lib/util/findGraphRoots.js +4 -2
  84. package/lib/util/memoize.js +3 -3
  85. package/lib/util/processAsyncTree.js +7 -1
  86. package/lib/util/registerExternalSerializer.js +1 -1
  87. package/lib/util/runtime.js +14 -1
  88. package/lib/util/smartGrouping.js +1 -1
  89. package/lib/validateSchema.js +6 -2
  90. package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +5 -1
  91. package/lib/wasm-async/AsyncWebAssemblyParser.js +7 -3
  92. package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +13 -6
  93. package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +3 -1
  94. package/lib/wasm-sync/WebAssemblyGenerator.js +1 -0
  95. package/lib/wasm-sync/WebAssemblyParser.js +6 -2
  96. package/lib/web/JsonpChunkLoadingRuntimeModule.js +13 -3
  97. package/lib/webpack.js +11 -2
  98. package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +14 -15
  99. package/module.d.ts +1 -0
  100. package/package.json +4 -4
  101. package/schemas/WebpackOptions.check.js +1 -1
  102. package/schemas/WebpackOptions.json +39 -5
  103. package/schemas/plugins/css/CssGeneratorOptions.json +1 -1
  104. package/schemas/plugins/css/CssParserOptions.json +1 -1
  105. package/types.d.ts +433 -184
@@ -47,6 +47,7 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
47
47
  /** @typedef {import("estree").MetaProperty} MetaProperty */
48
48
  /** @typedef {import("estree").Property} Property */
49
49
  /** @typedef {import("estree").AssignmentPattern} AssignmentPattern */
50
+ /** @typedef {import("estree").ChainElement} ChainElement */
50
51
  /** @typedef {import("estree").Pattern} Pattern */
51
52
  /** @typedef {import("estree").UpdateExpression} UpdateExpression */
52
53
  /** @typedef {import("estree").ObjectExpression} ObjectExpression */
@@ -105,7 +106,7 @@ const parser = AcornParser.extend(importAssertions);
105
106
  class VariableInfo {
106
107
  /**
107
108
  * @param {ScopeInfo} declaredScope scope in which the variable is declared
108
- * @param {string | true} freeName which free name the variable aliases, or true when none
109
+ * @param {string | true | undefined} freeName which free name the variable aliases, or true when none
109
110
  * @param {TagInfo | undefined} tagInfo info about tags
110
111
  */
111
112
  constructor(declaredScope, freeName, tagInfo) {
@@ -404,9 +405,9 @@ class JavascriptParser extends Parser {
404
405
  unhandledExpressionMemberChain: new HookMap(
405
406
  () => new SyncBailHook(["expression", "members"])
406
407
  ),
407
- /** @type {SyncBailHook<[Expression], boolean | void>} */
408
+ /** @type {SyncBailHook<[ConditionalExpression], boolean | void>} */
408
409
  expressionConditionalOperator: new SyncBailHook(["expression"]),
409
- /** @type {SyncBailHook<[Expression], boolean | void>} */
410
+ /** @type {SyncBailHook<[LogicalExpression], boolean | void>} */
410
411
  expressionLogicalOperator: new SyncBailHook(["expression"]),
411
412
  /** @type {SyncBailHook<[Program, Comment[]], boolean | void>} */
412
413
  program: new SyncBailHook(["ast", "comments"]),
@@ -422,7 +423,7 @@ class JavascriptParser extends Parser {
422
423
  this.semicolons = undefined;
423
424
  /** @type {(Statement | ModuleDeclaration | Expression)[]} */
424
425
  this.statementPath = undefined;
425
- /** @type {Statement | ModuleDeclaration | Expression} */
426
+ /** @type {Statement | ModuleDeclaration | Expression | undefined} */
426
427
  this.prevStatement = undefined;
427
428
  /** @type {WeakMap<Expression, Set<string>>} */
428
429
  this.destructuringAssignmentProperties = undefined;
@@ -438,27 +439,29 @@ class JavascriptParser extends Parser {
438
439
  case "number":
439
440
  return new BasicEvaluatedExpression()
440
441
  .setNumber(expr.value)
441
- .setRange(expr.range);
442
+ .setRange(/** @type {Range} */ (expr.range));
442
443
  case "bigint":
443
444
  return new BasicEvaluatedExpression()
444
445
  .setBigInt(expr.value)
445
- .setRange(expr.range);
446
+ .setRange(/** @type {Range} */ (expr.range));
446
447
  case "string":
447
448
  return new BasicEvaluatedExpression()
448
449
  .setString(expr.value)
449
- .setRange(expr.range);
450
+ .setRange(/** @type {Range} */ (expr.range));
450
451
  case "boolean":
451
452
  return new BasicEvaluatedExpression()
452
453
  .setBoolean(expr.value)
453
- .setRange(expr.range);
454
+ .setRange(/** @type {Range} */ (expr.range));
454
455
  }
455
456
  if (expr.value === null) {
456
- return new BasicEvaluatedExpression().setNull().setRange(expr.range);
457
+ return new BasicEvaluatedExpression()
458
+ .setNull()
459
+ .setRange(/** @type {Range} */ (expr.range));
457
460
  }
458
461
  if (expr.value instanceof RegExp) {
459
462
  return new BasicEvaluatedExpression()
460
463
  .setRegExp(expr.value)
461
- .setRange(expr.range);
464
+ .setRange(/** @type {Range} */ (expr.range));
462
465
  }
463
466
  });
464
467
  this.hooks.evaluate.for("NewExpression").tap("JavascriptParser", _expr => {
@@ -493,7 +496,7 @@ class JavascriptParser extends Parser {
493
496
  } else {
494
497
  return new BasicEvaluatedExpression()
495
498
  .setRegExp(new RegExp(""))
496
- .setRange(expr.range);
499
+ .setRange(/** @type {Range} */ (expr.range));
497
500
  }
498
501
 
499
502
  const arg2 = expr.arguments[1];
@@ -518,7 +521,7 @@ class JavascriptParser extends Parser {
518
521
 
519
522
  return new BasicEvaluatedExpression()
520
523
  .setRegExp(flags ? new RegExp(regExp, flags) : new RegExp(regExp))
521
- .setRange(expr.range);
524
+ .setRange(/** @type {Range} */ (expr.range));
522
525
  });
523
526
  this.hooks.evaluate
524
527
  .for("LogicalExpression")
@@ -584,7 +587,7 @@ class JavascriptParser extends Parser {
584
587
  * @param {boolean | number | BigInt | string} value the value to convert to an expression
585
588
  * @param {BinaryExpression | UnaryExpression} expr the expression being evaluated
586
589
  * @param {boolean} sideEffects whether the expression has side effects
587
- * @returns {BasicEvaluatedExpression} the evaluated expression
590
+ * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
588
591
  * @example
589
592
  *
590
593
  * ```js
@@ -611,22 +614,22 @@ class JavascriptParser extends Parser {
611
614
  return new BasicEvaluatedExpression()
612
615
  .setBoolean(value)
613
616
  .setSideEffects(sideEffects)
614
- .setRange(expr.range);
617
+ .setRange(/** @type {Range} */ (expr.range));
615
618
  case "number":
616
619
  return new BasicEvaluatedExpression()
617
620
  .setNumber(value)
618
621
  .setSideEffects(sideEffects)
619
- .setRange(expr.range);
622
+ .setRange(/** @type {Range} */ (expr.range));
620
623
  case "bigint":
621
624
  return new BasicEvaluatedExpression()
622
625
  .setBigInt(value)
623
626
  .setSideEffects(sideEffects)
624
- .setRange(expr.range);
627
+ .setRange(/** @type {Range} */ (expr.range));
625
628
  case "string":
626
629
  return new BasicEvaluatedExpression()
627
630
  .setString(value)
628
631
  .setSideEffects(sideEffects)
629
- .setRange(expr.range);
632
+ .setRange(/** @type {Range} */ (expr.range));
630
633
  }
631
634
  };
632
635
 
@@ -720,7 +723,7 @@ class JavascriptParser extends Parser {
720
723
  const left = this.evaluateExpression(expr.left);
721
724
  const right = this.evaluateExpression(expr.right);
722
725
  const res = new BasicEvaluatedExpression();
723
- res.setRange(expr.range);
726
+ res.setRange(/** @type {Range} */ (expr.range));
724
727
 
725
728
  const leftConst = left.isCompileTimeValue();
726
729
  const rightConst = right.isCompileTimeValue();
@@ -758,8 +761,14 @@ class JavascriptParser extends Parser {
758
761
  (rightPrimitive === false &&
759
762
  (rightConst || leftPrimitive === true)) ||
760
763
  // Different nullish or boolish status also means not equal
761
- isAlwaysDifferent(left.asBool(), right.asBool()) ||
762
- isAlwaysDifferent(left.asNullish(), right.asNullish())
764
+ isAlwaysDifferent(
765
+ /** @type {boolean} */ (left.asBool()),
766
+ /** @type {boolean} */ (right.asBool())
767
+ ) ||
768
+ isAlwaysDifferent(
769
+ /** @type {boolean} */ (left.asNullish()),
770
+ /** @type {boolean} */ (right.asNullish())
771
+ )
763
772
  ) {
764
773
  return res
765
774
  .setBoolean(!eql)
@@ -1363,7 +1372,7 @@ class JavascriptParser extends Parser {
1363
1372
 
1364
1373
  const part = new BasicEvaluatedExpression()
1365
1374
  .setString(quasi)
1366
- .setRange(quasiExpr.range)
1375
+ .setRange(/** @type {Range} */ (quasiExpr.range))
1367
1376
  .setExpression(quasiExpr);
1368
1377
  quasis.push(part);
1369
1378
  parts.push(part);
@@ -1381,11 +1390,11 @@ class JavascriptParser extends Parser {
1381
1390
 
1382
1391
  const { quasis, parts } = getSimplifiedTemplateResult("cooked", node);
1383
1392
  if (parts.length === 1) {
1384
- return parts[0].setRange(node.range);
1393
+ return parts[0].setRange(/** @type {Range} */ (node.range));
1385
1394
  }
1386
1395
  return new BasicEvaluatedExpression()
1387
1396
  .setTemplateString(quasis, parts, "cooked")
1388
- .setRange(node.range);
1397
+ .setRange(/** @type {Range} */ (node.range));
1389
1398
  });
1390
1399
  this.hooks.evaluate
1391
1400
  .for("TaggedTemplateExpression")
@@ -1400,7 +1409,7 @@ class JavascriptParser extends Parser {
1400
1409
  );
1401
1410
  return new BasicEvaluatedExpression()
1402
1411
  .setTemplateString(quasis, parts, "raw")
1403
- .setRange(node.range);
1412
+ .setRange(/** @type {Range} */ (node.range));
1404
1413
  }
1405
1414
  });
1406
1415
 
@@ -1490,7 +1499,7 @@ class JavascriptParser extends Parser {
1490
1499
  return new BasicEvaluatedExpression()
1491
1500
  .setArray(result)
1492
1501
  .setSideEffects(param.couldHaveSideEffects())
1493
- .setRange(expr.range);
1502
+ .setRange(/** @type {Range} */ (expr.range));
1494
1503
  });
1495
1504
  this.hooks.evaluate
1496
1505
  .for("ConditionalExpression")
@@ -1505,12 +1514,16 @@ class JavascriptParser extends Parser {
1505
1514
  const alternate = this.evaluateExpression(expr.alternate);
1506
1515
  res = new BasicEvaluatedExpression();
1507
1516
  if (consequent.isConditional()) {
1508
- res.setOptions(consequent.options);
1517
+ res.setOptions(
1518
+ /** @type {BasicEvaluatedExpression[]} */ (consequent.options)
1519
+ );
1509
1520
  } else {
1510
1521
  res.setOptions([consequent]);
1511
1522
  }
1512
1523
  if (alternate.isConditional()) {
1513
- res.addOptions(alternate.options);
1524
+ res.addOptions(
1525
+ /** @type {BasicEvaluatedExpression[]} */ (alternate.options)
1526
+ );
1514
1527
  } else {
1515
1528
  res.addOptions([alternate]);
1516
1529
  }
@@ -1520,7 +1533,7 @@ class JavascriptParser extends Parser {
1520
1533
  );
1521
1534
  if (condition.couldHaveSideEffects()) res.setSideEffects();
1522
1535
  }
1523
- res.setRange(expr.range);
1536
+ res.setRange(/** @type {Range} */ (expr.range));
1524
1537
  return res;
1525
1538
  });
1526
1539
  this.hooks.evaluate
@@ -1538,7 +1551,7 @@ class JavascriptParser extends Parser {
1538
1551
  if (!items.every(Boolean)) return;
1539
1552
  return new BasicEvaluatedExpression()
1540
1553
  .setItems(items)
1541
- .setRange(expr.range);
1554
+ .setRange(/** @type {Range} */ (expr.range));
1542
1555
  });
1543
1556
  this.hooks.evaluate
1544
1557
  .for("ChainExpression")
@@ -1573,11 +1586,13 @@ class JavascriptParser extends Parser {
1573
1586
  }
1574
1587
 
1575
1588
  while (optionalExpressionsStack.length > 0) {
1576
- const expression = optionalExpressionsStack.pop();
1589
+ const expression =
1590
+ /** @type {Expression} */
1591
+ (optionalExpressionsStack.pop());
1577
1592
  const evaluated = this.evaluateExpression(expression);
1578
1593
 
1579
1594
  if (evaluated.asNullish()) {
1580
- return evaluated.setRange(_expr.range);
1595
+ return evaluated.setRange(/** @type {Range} */ (_expr.range));
1581
1596
  }
1582
1597
  }
1583
1598
  return this.evaluateExpression(expr.expression);
@@ -2696,6 +2711,9 @@ class JavascriptParser extends Parser {
2696
2711
  }
2697
2712
  }
2698
2713
 
2714
+ /**
2715
+ * @param {TODO} expression expression
2716
+ */
2699
2717
  walkExpression(expression) {
2700
2718
  switch (expression.type) {
2701
2719
  case "ArrayExpression":
@@ -3423,10 +3441,10 @@ class JavascriptParser extends Parser {
3423
3441
  * @template R
3424
3442
  * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
3425
3443
  * @param {MemberExpression} expr expression info
3426
- * @param {function(string, string | ScopeInfo | VariableInfo, function(): string[]): any} fallback callback when variable in not handled by hooks
3427
- * @param {function(string): any} defined callback when variable is defined
3444
+ * @param {(function(string, string | ScopeInfo | VariableInfo, function(): string[]): any) | undefined} fallback callback when variable in not handled by hooks
3445
+ * @param {(function(string): any) | undefined} defined callback when variable is defined
3428
3446
  * @param {AsArray<T>} args args for the hook
3429
- * @returns {R} result of hook
3447
+ * @returns {R | undefined} result of hook
3430
3448
  */
3431
3449
  callHooksForExpressionWithFallback(
3432
3450
  hookMap,
@@ -3458,7 +3476,7 @@ class JavascriptParser extends Parser {
3458
3476
  * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
3459
3477
  * @param {string} name key in map
3460
3478
  * @param {AsArray<T>} args args for the hook
3461
- * @returns {R} result of hook
3479
+ * @returns {R | undefined} result of hook
3462
3480
  */
3463
3481
  callHooksForName(hookMap, name, ...args) {
3464
3482
  return this.callHooksForNameWithFallback(
@@ -3476,7 +3494,7 @@ class JavascriptParser extends Parser {
3476
3494
  * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks that should be called
3477
3495
  * @param {ExportedVariableInfo} info variable info
3478
3496
  * @param {AsArray<T>} args args for the hook
3479
- * @returns {R} result of hook
3497
+ * @returns {R | undefined} result of hook
3480
3498
  */
3481
3499
  callHooksForInfo(hookMap, info, ...args) {
3482
3500
  return this.callHooksForInfoWithFallback(
@@ -3493,10 +3511,10 @@ class JavascriptParser extends Parser {
3493
3511
  * @template R
3494
3512
  * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
3495
3513
  * @param {ExportedVariableInfo} info variable info
3496
- * @param {function(string): any} fallback callback when variable in not handled by hooks
3497
- * @param {function(): any} defined callback when variable is defined
3514
+ * @param {(function(string): any) | undefined} fallback callback when variable in not handled by hooks
3515
+ * @param {(function(): any) | undefined} defined callback when variable is defined
3498
3516
  * @param {AsArray<T>} args args for the hook
3499
- * @returns {R} result of hook
3517
+ * @returns {R | undefined} result of hook
3500
3518
  */
3501
3519
  callHooksForInfoWithFallback(hookMap, info, fallback, defined, ...args) {
3502
3520
  let name;
@@ -3543,10 +3561,10 @@ class JavascriptParser extends Parser {
3543
3561
  * @template R
3544
3562
  * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
3545
3563
  * @param {string} name key in map
3546
- * @param {function(string): any} fallback callback when variable in not handled by hooks
3547
- * @param {function(): any} defined callback when variable is defined
3564
+ * @param {(function(string): any) | undefined} fallback callback when variable in not handled by hooks
3565
+ * @param {(function(): any) | undefined} defined callback when variable is defined
3548
3566
  * @param {AsArray<T>} args args for the hook
3549
- * @returns {R} result of hook
3567
+ * @returns {R | undefined} result of hook
3550
3568
  */
3551
3569
  callHooksForNameWithFallback(hookMap, name, fallback, defined, ...args) {
3552
3570
  return this.callHooksForInfoWithFallback(
@@ -3586,6 +3604,12 @@ class JavascriptParser extends Parser {
3586
3604
  this.scope = oldScope;
3587
3605
  }
3588
3606
 
3607
+ /**
3608
+ * @param {boolean} hasThis true, when this is defined
3609
+ * @param {any} params scope params
3610
+ * @param {function(): void} fn inner function
3611
+ * @returns {void}
3612
+ */
3589
3613
  inClassScope(hasThis, params, fn) {
3590
3614
  const oldScope = this.scope;
3591
3615
  this.scope = {
@@ -3610,6 +3634,12 @@ class JavascriptParser extends Parser {
3610
3634
  this.scope = oldScope;
3611
3635
  }
3612
3636
 
3637
+ /**
3638
+ * @param {boolean} hasThis true, when this is defined
3639
+ * @param {any} params scope params
3640
+ * @param {function(): void} fn inner function
3641
+ * @returns {void}
3642
+ */
3613
3643
  inFunctionScope(hasThis, params, fn) {
3614
3644
  const oldScope = this.scope;
3615
3645
  this.scope = {
@@ -3634,6 +3664,10 @@ class JavascriptParser extends Parser {
3634
3664
  this.scope = oldScope;
3635
3665
  }
3636
3666
 
3667
+ /**
3668
+ * @param {function(): void} fn inner function
3669
+ * @returns {void}
3670
+ */
3637
3671
  inBlockScope(fn) {
3638
3672
  const oldScope = this.scope;
3639
3673
  this.scope = {
@@ -3650,15 +3684,28 @@ class JavascriptParser extends Parser {
3650
3684
  this.scope = oldScope;
3651
3685
  }
3652
3686
 
3687
+ /**
3688
+ * @param {Array<Directive | Statement | ModuleDeclaration>} statements statements
3689
+ */
3653
3690
  detectMode(statements) {
3654
3691
  const isLiteral =
3655
3692
  statements.length >= 1 &&
3656
3693
  statements[0].type === "ExpressionStatement" &&
3657
3694
  statements[0].expression.type === "Literal";
3658
- if (isLiteral && statements[0].expression.value === "use strict") {
3695
+ if (
3696
+ isLiteral &&
3697
+ /** @type {Literal} */
3698
+ (/** @type {ExpressionStatement} */ (statements[0]).expression).value ===
3699
+ "use strict"
3700
+ ) {
3659
3701
  this.scope.isStrict = true;
3660
3702
  }
3661
- if (isLiteral && statements[0].expression.value === "use asm") {
3703
+ if (
3704
+ isLiteral &&
3705
+ /** @type {Literal} */
3706
+ (/** @type {ExpressionStatement} */ (statements[0]).expression).value ===
3707
+ "use asm"
3708
+ ) {
3662
3709
  this.scope.isAsmJs = true;
3663
3710
  }
3664
3711
  }
@@ -3761,7 +3808,7 @@ class JavascriptParser extends Parser {
3761
3808
  }
3762
3809
 
3763
3810
  /**
3764
- * @param {Expression} expression expression node
3811
+ * @param {TODO} expression expression node
3765
3812
  * @returns {BasicEvaluatedExpression} evaluation result
3766
3813
  */
3767
3814
  evaluateExpression(expression) {
@@ -3779,7 +3826,7 @@ class JavascriptParser extends Parser {
3779
3826
  // ignore error
3780
3827
  }
3781
3828
  return new BasicEvaluatedExpression()
3782
- .setRange(expression.range)
3829
+ .setRange(/** @type {Range} */ (expression.range))
3783
3830
  .setExpression(expression);
3784
3831
  }
3785
3832
 
@@ -4024,43 +4071,52 @@ class JavascriptParser extends Parser {
4024
4071
 
4025
4072
  case "VariableDeclaration":
4026
4073
  return expr.declarations.every(decl =>
4027
- this.isPure(decl.init, decl.range[0])
4074
+ this.isPure(decl.init, /** @type {Range} */ (decl.range)[0])
4028
4075
  );
4029
4076
 
4030
4077
  case "ConditionalExpression":
4031
4078
  return (
4032
4079
  this.isPure(expr.test, commentsStartPos) &&
4033
- this.isPure(expr.consequent, expr.test.range[1]) &&
4034
- this.isPure(expr.alternate, expr.consequent.range[1])
4080
+ this.isPure(
4081
+ expr.consequent,
4082
+ /** @type {Range} */ (expr.test.range)[1]
4083
+ ) &&
4084
+ this.isPure(
4085
+ expr.alternate,
4086
+ /** @type {Range} */ (expr.consequent.range)[1]
4087
+ )
4035
4088
  );
4036
4089
 
4037
4090
  case "LogicalExpression":
4038
4091
  return (
4039
4092
  this.isPure(expr.left, commentsStartPos) &&
4040
- this.isPure(expr.right, expr.left.range[1])
4093
+ this.isPure(expr.right, /** @type {Range} */ (expr.left.range)[1])
4041
4094
  );
4042
4095
 
4043
4096
  case "SequenceExpression":
4044
4097
  return expr.expressions.every(expr => {
4045
4098
  const pureFlag = this.isPure(expr, commentsStartPos);
4046
- commentsStartPos = expr.range[1];
4099
+ commentsStartPos = /** @type {Range} */ (expr.range)[1];
4047
4100
  return pureFlag;
4048
4101
  });
4049
4102
 
4050
4103
  case "CallExpression": {
4051
4104
  const pureFlag =
4052
- expr.range[0] - commentsStartPos > 12 &&
4053
- this.getComments([commentsStartPos, expr.range[0]]).some(
4105
+ /** @type {Range} */ (expr.range)[0] - commentsStartPos > 12 &&
4106
+ this.getComments([
4107
+ commentsStartPos,
4108
+ /** @type {Range} */ (expr.range)[0]
4109
+ ]).some(
4054
4110
  comment =>
4055
4111
  comment.type === "Block" &&
4056
4112
  /^\s*(#|@)__PURE__\s*$/.test(comment.value)
4057
4113
  );
4058
4114
  if (!pureFlag) return false;
4059
- commentsStartPos = expr.callee.range[1];
4115
+ commentsStartPos = /** @type {Range} */ (expr.callee.range)[1];
4060
4116
  return expr.arguments.every(arg => {
4061
4117
  if (arg.type === "SpreadElement") return false;
4062
4118
  const pureFlag = this.isPure(arg, commentsStartPos);
4063
- commentsStartPos = arg.range[1];
4119
+ commentsStartPos = /** @type {Range} */ (arg.range)[1];
4064
4120
  return pureFlag;
4065
4121
  });
4066
4122
  }
@@ -4114,6 +4170,10 @@ class JavascriptParser extends Parser {
4114
4170
  this.semicolons.delete(pos);
4115
4171
  }
4116
4172
 
4173
+ /**
4174
+ * @param {Expression} expr expression
4175
+ * @returns {boolean} true, when the expression is a statement level expression
4176
+ */
4117
4177
  isStatementLevelExpression(expr) {
4118
4178
  const currentStatement = this.statementPath[this.statementPath.length - 1];
4119
4179
  return (
@@ -4303,7 +4363,7 @@ class JavascriptParser extends Parser {
4303
4363
 
4304
4364
  /**
4305
4365
  * @param {string} varName variable name
4306
- * @returns {{name: string, info: VariableInfo | string}} name of the free variable and variable info for that
4366
+ * @returns {{name: string, info: VariableInfo | string} | undefined} name of the free variable and variable info for that
4307
4367
  */
4308
4368
  getFreeInfoFromVariable(varName) {
4309
4369
  const info = this.getVariableInfo(varName);
@@ -4383,7 +4443,7 @@ class JavascriptParser extends Parser {
4383
4443
 
4384
4444
  /**
4385
4445
  * @param {MemberExpression} expression an expression
4386
- * @returns {{ name: string, rootInfo: ExportedVariableInfo, getMembers: () => string[]}} name info
4446
+ * @returns {{ name: string, rootInfo: ExportedVariableInfo, getMembers: () => string[]} | undefined} name info
4387
4447
  */
4388
4448
  getNameForExpression(expression) {
4389
4449
  return this.getMemberExpressionInfo(
@@ -4407,7 +4467,7 @@ class JavascriptParser extends Parser {
4407
4467
  sourceType: type === "auto" ? "module" : type
4408
4468
  };
4409
4469
 
4410
- /** @type {AnyNode} */
4470
+ /** @type {AnyNode | undefined} */
4411
4471
  let ast;
4412
4472
  let error;
4413
4473
  let threw = false;
@@ -9,20 +9,26 @@ const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
9
9
  const ConstDependency = require("../dependencies/ConstDependency");
10
10
  const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
11
11
 
12
- /** @typedef {import("estree").Expression} ExpressionNode */
12
+ /** @typedef {import("estree").Expression} Expression */
13
13
  /** @typedef {import("estree").Node} Node */
14
+ /** @typedef {import("estree").SourceLocation} SourceLocation */
14
15
  /** @typedef {import("./JavascriptParser")} JavascriptParser */
16
+ /** @typedef {import("./JavascriptParser").Range} Range */
15
17
 
16
18
  /**
17
19
  * @param {JavascriptParser} parser the parser
18
20
  * @param {string} value the const value
19
21
  * @param {string[]=} runtimeRequirements runtime requirements
20
- * @returns {function(ExpressionNode): true} plugin function
22
+ * @returns {function(Expression): true} plugin function
21
23
  */
22
24
  exports.toConstantDependency = (parser, value, runtimeRequirements) => {
23
25
  return function constDependency(expr) {
24
- const dep = new ConstDependency(value, expr.range, runtimeRequirements);
25
- dep.loc = expr.loc;
26
+ const dep = new ConstDependency(
27
+ value,
28
+ /** @type {Range} */ (expr.range),
29
+ runtimeRequirements
30
+ );
31
+ dep.loc = /** @type {SourceLocation} */ (expr.loc);
26
32
  parser.state.module.addPresentationalDependency(dep);
27
33
  return true;
28
34
  };
@@ -30,33 +36,37 @@ exports.toConstantDependency = (parser, value, runtimeRequirements) => {
30
36
 
31
37
  /**
32
38
  * @param {string} value the string value
33
- * @returns {function(ExpressionNode): BasicEvaluatedExpression} plugin function
39
+ * @returns {function(Expression): BasicEvaluatedExpression} plugin function
34
40
  */
35
41
  exports.evaluateToString = value => {
36
42
  return function stringExpression(expr) {
37
- return new BasicEvaluatedExpression().setString(value).setRange(expr.range);
43
+ return new BasicEvaluatedExpression()
44
+ .setString(value)
45
+ .setRange(/** @type {Range} */ (expr.range));
38
46
  };
39
47
  };
40
48
 
41
49
  /**
42
50
  * @param {number} value the number value
43
- * @returns {function(ExpressionNode): BasicEvaluatedExpression} plugin function
51
+ * @returns {function(Expression): BasicEvaluatedExpression} plugin function
44
52
  */
45
53
  exports.evaluateToNumber = value => {
46
54
  return function stringExpression(expr) {
47
- return new BasicEvaluatedExpression().setNumber(value).setRange(expr.range);
55
+ return new BasicEvaluatedExpression()
56
+ .setNumber(value)
57
+ .setRange(/** @type {Range} */ (expr.range));
48
58
  };
49
59
  };
50
60
 
51
61
  /**
52
62
  * @param {boolean} value the boolean value
53
- * @returns {function(ExpressionNode): BasicEvaluatedExpression} plugin function
63
+ * @returns {function(Expression): BasicEvaluatedExpression} plugin function
54
64
  */
55
65
  exports.evaluateToBoolean = value => {
56
66
  return function booleanExpression(expr) {
57
67
  return new BasicEvaluatedExpression()
58
68
  .setBoolean(value)
59
- .setRange(expr.range);
69
+ .setRange(/** @type {Range} */ (expr.range));
60
70
  };
61
71
  };
62
72
 
@@ -65,14 +75,14 @@ exports.evaluateToBoolean = value => {
65
75
  * @param {string} rootInfo rootInfo
66
76
  * @param {function(): string[]} getMembers getMembers
67
77
  * @param {boolean|null=} truthy is truthy, null if nullish
68
- * @returns {function(ExpressionNode): BasicEvaluatedExpression} callback
78
+ * @returns {function(Expression): BasicEvaluatedExpression} callback
69
79
  */
70
80
  exports.evaluateToIdentifier = (identifier, rootInfo, getMembers, truthy) => {
71
81
  return function identifierExpression(expr) {
72
82
  let evaluatedExpression = new BasicEvaluatedExpression()
73
83
  .setIdentifier(identifier, rootInfo, getMembers)
74
84
  .setSideEffects(false)
75
- .setRange(expr.range);
85
+ .setRange(/** @type {Range} */ (expr.range));
76
86
  switch (truthy) {
77
87
  case true:
78
88
  evaluatedExpression.setTruthy();
@@ -89,14 +99,26 @@ exports.evaluateToIdentifier = (identifier, rootInfo, getMembers, truthy) => {
89
99
  };
90
100
  };
91
101
 
102
+ /**
103
+ * @param {JavascriptParser} parser the parser
104
+ * @param {string} message the message
105
+ * @returns {function(Expression): boolean | undefined} callback to handle unsupported expression
106
+ */
92
107
  exports.expressionIsUnsupported = (parser, message) => {
93
108
  return function unsupportedExpression(expr) {
94
- const dep = new ConstDependency("(void 0)", expr.range, null);
95
- dep.loc = expr.loc;
109
+ const dep = new ConstDependency(
110
+ "(void 0)",
111
+ /** @type {Range} */ (expr.range),
112
+ null
113
+ );
114
+ dep.loc = /** @type {SourceLocation} */ (expr.loc);
96
115
  parser.state.module.addPresentationalDependency(dep);
97
116
  if (!parser.state.module) return;
98
117
  parser.state.module.addWarning(
99
- new UnsupportedFeatureWarning(message, expr.loc)
118
+ new UnsupportedFeatureWarning(
119
+ message,
120
+ /** @type {SourceLocation} */ (expr.loc)
121
+ )
100
122
  );
101
123
  return true;
102
124
  };
@@ -95,7 +95,7 @@ module.exports = class SizeLimitsPlugin {
95
95
  }
96
96
 
97
97
  const size = info.size || source.size();
98
- if (size > assetSizeLimit) {
98
+ if (size > /** @type {number} */ (assetSizeLimit)) {
99
99
  assetsOverSizeLimit.push({
100
100
  name,
101
101
  size
@@ -114,7 +114,7 @@ module.exports = class SizeLimitsPlugin {
114
114
  for (const [name, entry] of compilation.entrypoints) {
115
115
  const size = getEntrypointSize(entry);
116
116
 
117
- if (size > entrypointSizeLimit) {
117
+ if (size > /** @type {number} */ (entrypointSizeLimit)) {
118
118
  entrypointsOverLimit.push({
119
119
  name: name,
120
120
  size: size,
@@ -131,14 +131,17 @@ module.exports = class SizeLimitsPlugin {
131
131
  // if !1, then 2, if !2 return
132
132
  if (assetsOverSizeLimit.length > 0) {
133
133
  warnings.push(
134
- new AssetsOverSizeLimitWarning(assetsOverSizeLimit, assetSizeLimit)
134
+ new AssetsOverSizeLimitWarning(
135
+ assetsOverSizeLimit,
136
+ /** @type {number} */ (assetSizeLimit)
137
+ )
135
138
  );
136
139
  }
137
140
  if (entrypointsOverLimit.length > 0) {
138
141
  warnings.push(
139
142
  new EntrypointsOverSizeLimitWarning(
140
143
  entrypointsOverLimit,
141
- entrypointSizeLimit
144
+ /** @type {number} */ (entrypointSizeLimit)
142
145
  )
143
146
  );
144
147
  }
@@ -7,6 +7,7 @@
7
7
  const RuntimeModule = require("../RuntimeModule");
8
8
  const Template = require("../Template");
9
9
 
10
+ /** @typedef {import("../Compilation")} Compilation */
10
11
  /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
11
12
 
12
13
  class ChunkPrefetchFunctionRuntimeModule extends RuntimeModule {
@@ -27,7 +28,8 @@ class ChunkPrefetchFunctionRuntimeModule extends RuntimeModule {
27
28
  */
28
29
  generate() {
29
30
  const { runtimeFunction, runtimeHandlers } = this;
30
- const { runtimeTemplate } = this.compilation;
31
+ const compilation = /** @type {Compilation} */ (this.compilation);
32
+ const { runtimeTemplate } = compilation;
31
33
  return Template.asString([
32
34
  `${runtimeHandlers} = {};`,
33
35
  `${runtimeFunction} = ${runtimeTemplate.basicFunction("chunkId", [