webpack 5.101.2 → 5.101.3

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.
@@ -180,7 +180,7 @@ class ConstPlugin {
180
180
  ? statement.alternate
181
181
  : statement.consequent;
182
182
  if (branchToRemove) {
183
- this.eliminateUnusedStatement(parser, branchToRemove);
183
+ this.eliminateUnusedStatement(parser, branchToRemove, true);
184
184
  }
185
185
  return bool;
186
186
  }
@@ -193,7 +193,7 @@ class ConstPlugin {
193
193
  ) {
194
194
  return;
195
195
  }
196
- this.eliminateUnusedStatement(parser, statement);
196
+ this.eliminateUnusedStatement(parser, statement, false);
197
197
  return true;
198
198
  });
199
199
  parser.hooks.expressionConditionalOperator.tap(
@@ -509,9 +509,10 @@ class ConstPlugin {
509
509
  * Eliminate an unused statement.
510
510
  * @param {JavascriptParser} parser the parser
511
511
  * @param {Statement} statement the statement to remove
512
+ * @param {boolean} alwaysInBlock whether to always generate curly brackets
512
513
  * @returns {void}
513
514
  */
514
- eliminateUnusedStatement(parser, statement) {
515
+ eliminateUnusedStatement(parser, statement, alwaysInBlock) {
515
516
  // Before removing the unused branch, the hoisted declarations
516
517
  // must be collected.
517
518
  //
@@ -545,8 +546,14 @@ class ConstPlugin {
545
546
  const declarations = parser.scope.isStrict
546
547
  ? getHoistedDeclarations(statement, false)
547
548
  : getHoistedDeclarations(statement, true);
548
- const replacement =
549
- declarations.length > 0 ? `{ var ${declarations.join(", ")}; }` : "{}";
549
+
550
+ const inBlock = alwaysInBlock || statement.type === "BlockStatement";
551
+
552
+ let replacement = inBlock ? "{" : "";
553
+ replacement +=
554
+ declarations.length > 0 ? ` var ${declarations.join(", ")}; ` : "";
555
+ replacement += inBlock ? "}" : "";
556
+
550
557
  const dep = new ConstDependency(
551
558
  `// removed by dead control flow\n${replacement}`,
552
559
  /** @type {Range} */ (statement.range)
@@ -489,6 +489,13 @@ class DefinePlugin {
489
489
  if (nested && !hooked.has(nested)) {
490
490
  // only detect the same nested key once
491
491
  hooked.add(nested);
492
+ parser.hooks.collectDestructuringAssignmentProperties.tap(
493
+ PLUGIN_NAME,
494
+ (expr) => {
495
+ const nameInfo = parser.getNameForExpression(expr);
496
+ if (nameInfo && nameInfo.name === nested) return true;
497
+ }
498
+ );
492
499
  parser.hooks.expression.for(nested).tap(
493
500
  {
494
501
  name: PLUGIN_NAME,
@@ -687,6 +694,13 @@ class DefinePlugin {
687
694
  PLUGIN_NAME,
688
695
  withValueDependency(key, evaluateToString("object"))
689
696
  );
697
+ parser.hooks.collectDestructuringAssignmentProperties.tap(
698
+ PLUGIN_NAME,
699
+ (expr) => {
700
+ const nameInfo = parser.getNameForExpression(expr);
701
+ if (nameInfo && nameInfo.name === key) return true;
702
+ }
703
+ );
690
704
  parser.hooks.expression.for(key).tap(PLUGIN_NAME, (expr) => {
691
705
  addValueDependency(key);
692
706
  let strCode = stringifyObj(
@@ -62,9 +62,12 @@ class AwaitDependenciesInitFragment extends InitFragment {
62
62
  this.dependencies.size === 1 ||
63
63
  !runtimeTemplate.supportsDestructuring()
64
64
  ) {
65
+ templateInput.push(
66
+ "var __webpack_async_dependencies_result__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);"
67
+ );
65
68
  for (const [index, importVar] of importVars.entries()) {
66
69
  templateInput.push(
67
- `${importVar} = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[${index}];`
70
+ `${importVar} = __webpack_async_dependencies_result__[${index}];`
68
71
  );
69
72
  }
70
73
  } else {
@@ -8,7 +8,10 @@
8
8
  const CommentCompilationWarning = require("../CommentCompilationWarning");
9
9
  const HotModuleReplacementPlugin = require("../HotModuleReplacementPlugin");
10
10
  const WebpackError = require("../WebpackError");
11
- const { getImportAttributes } = require("../javascript/JavascriptParser");
11
+ const {
12
+ VariableInfo,
13
+ getImportAttributes
14
+ } = require("../javascript/JavascriptParser");
12
15
  const InnerGraph = require("../optimize/InnerGraph");
13
16
  const ConstDependency = require("./ConstDependency");
14
17
  const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
@@ -211,6 +214,20 @@ module.exports = class HarmonyImportDependencyParserPlugin {
211
214
  InnerGraph.onUsage(parser.state, (e) => (dep.usedByExports = e));
212
215
  return true;
213
216
  });
217
+ parser.hooks.collectDestructuringAssignmentProperties.tap(
218
+ PLUGIN_NAME,
219
+ (expr) => {
220
+ const nameInfo = parser.getNameForExpression(expr);
221
+ if (
222
+ nameInfo &&
223
+ nameInfo.rootInfo instanceof VariableInfo &&
224
+ nameInfo.rootInfo.name &&
225
+ parser.getTagData(nameInfo.rootInfo.name, harmonySpecifierTag)
226
+ ) {
227
+ return true;
228
+ }
229
+ }
230
+ );
214
231
  parser.hooks.expression
215
232
  .for(harmonySpecifierTag)
216
233
  .tap(PLUGIN_NAME, (expr) => {
@@ -96,6 +96,12 @@ class ImportMetaPlugin {
96
96
  PLUGIN_NAME,
97
97
  toConstantDependency(parser, JSON.stringify("object"))
98
98
  );
99
+ parser.hooks.collectDestructuringAssignmentProperties.tap(
100
+ PLUGIN_NAME,
101
+ (expr) => {
102
+ if (expr.type === "MetaProperty") return true;
103
+ }
104
+ );
99
105
  parser.hooks.expression
100
106
  .for("import.meta")
101
107
  .tap(PLUGIN_NAME, (metaProperty) => {
@@ -46,6 +46,12 @@ class ImportParserPlugin {
46
46
  */
47
47
  const exportsFromEnumerable = (enumerable) =>
48
48
  Array.from(enumerable, (e) => [e]);
49
+ parser.hooks.collectDestructuringAssignmentProperties.tap(
50
+ PLUGIN_NAME,
51
+ (expr) => {
52
+ if (expr.type === "ImportExpression") return true;
53
+ }
54
+ );
49
55
  parser.hooks.importCall.tap(PLUGIN_NAME, (expr) => {
50
56
  const param = parser.evaluateExpression(expr.source);
51
57
 
@@ -522,6 +522,10 @@ class JavascriptParser extends Parser {
522
522
  varDeclarationVar: new HookMap(() => new SyncBailHook(["declaration"])),
523
523
  /** @type {HookMap<SyncBailHook<[Identifier], boolean | void>>} */
524
524
  pattern: new HookMap(() => new SyncBailHook(["pattern"])),
525
+ /** @type {SyncBailHook<[Expression], boolean | void>} */
526
+ collectDestructuringAssignmentProperties: new SyncBailHook([
527
+ "expression"
528
+ ]),
525
529
  /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
526
530
  canRename: new HookMap(() => new SyncBailHook(["initExpression"])),
527
531
  /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
@@ -2607,34 +2611,48 @@ class JavascriptParser extends Parser {
2607
2611
  * @param {AssignmentExpression} expression assignment expression
2608
2612
  */
2609
2613
  preWalkAssignmentExpression(expression) {
2614
+ this.enterDestructuringAssignment(expression.left, expression.right);
2615
+ }
2616
+
2617
+ /**
2618
+ * @param {Pattern} pattern pattern
2619
+ * @param {Expression} expression assignment expression
2620
+ * @returns {Expression | undefined} destructuring expression
2621
+ */
2622
+ enterDestructuringAssignment(pattern, expression) {
2610
2623
  if (
2611
- expression.left.type !== "ObjectPattern" ||
2624
+ pattern.type !== "ObjectPattern" ||
2612
2625
  !this.destructuringAssignmentProperties
2613
2626
  ) {
2614
2627
  return;
2615
2628
  }
2616
- const keys = this._preWalkObjectPattern(expression.left);
2617
- if (!keys) return;
2618
-
2619
- // check multiple assignments
2620
- if (this.destructuringAssignmentProperties.has(expression)) {
2621
- const set =
2622
- /** @type {Set<DestructuringAssignmentProperty>} */
2623
- (this.destructuringAssignmentProperties.get(expression));
2624
- this.destructuringAssignmentProperties.delete(expression);
2625
- for (const id of set) keys.add(id);
2626
- }
2627
2629
 
2628
- this.destructuringAssignmentProperties.set(
2629
- expression.right.type === "AwaitExpression"
2630
- ? expression.right.argument
2631
- : expression.right,
2632
- keys
2633
- );
2630
+ const expr =
2631
+ expression.type === "AwaitExpression" ? expression.argument : expression;
2632
+
2633
+ const destructuring =
2634
+ expr.type === "AssignmentExpression"
2635
+ ? this.enterDestructuringAssignment(expr.left, expr.right)
2636
+ : this.hooks.collectDestructuringAssignmentProperties.call(expr)
2637
+ ? expr
2638
+ : undefined;
2639
+
2640
+ if (destructuring) {
2641
+ const keys = this._preWalkObjectPattern(pattern);
2642
+ if (!keys) return;
2634
2643
 
2635
- if (expression.right.type === "AssignmentExpression") {
2636
- this.preWalkAssignmentExpression(expression.right);
2644
+ // check multiple assignments
2645
+ if (this.destructuringAssignmentProperties.has(destructuring)) {
2646
+ const set =
2647
+ /** @type {Set<DestructuringAssignmentProperty>} */
2648
+ (this.destructuringAssignmentProperties.get(destructuring));
2649
+ for (const id of keys) set.add(id);
2650
+ } else {
2651
+ this.destructuringAssignmentProperties.set(destructuring, keys);
2652
+ }
2637
2653
  }
2654
+
2655
+ return destructuring;
2638
2656
  }
2639
2657
 
2640
2658
  /**
@@ -2995,25 +3013,8 @@ class JavascriptParser extends Parser {
2995
3013
  * @param {VariableDeclarator} declarator variable declarator
2996
3014
  */
2997
3015
  preWalkVariableDeclarator(declarator) {
2998
- if (
2999
- !declarator.init ||
3000
- declarator.id.type !== "ObjectPattern" ||
3001
- !this.destructuringAssignmentProperties
3002
- ) {
3003
- return;
3004
- }
3005
- const keys = this._preWalkObjectPattern(declarator.id);
3006
-
3007
- if (!keys) return;
3008
- this.destructuringAssignmentProperties.set(
3009
- declarator.init.type === "AwaitExpression"
3010
- ? declarator.init.argument
3011
- : declarator.init,
3012
- keys
3013
- );
3014
-
3015
- if (declarator.init.type === "AssignmentExpression") {
3016
- this.preWalkAssignmentExpression(declarator.init);
3016
+ if (declarator.init) {
3017
+ this.enterDestructuringAssignment(declarator.id, declarator.init);
3017
3018
  }
3018
3019
  }
3019
3020
 
@@ -5179,7 +5180,7 @@ class JavascriptParser extends Parser {
5179
5180
  }
5180
5181
 
5181
5182
  /**
5182
- * @param {MemberExpression} expression an expression
5183
+ * @param {Expression} expression an expression
5183
5184
  * @returns {{ name: string, rootInfo: ExportedVariableInfo, getMembers: () => string[]} | undefined} name info
5184
5185
  */
5185
5186
  getNameForExpression(expression) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webpack",
3
- "version": "5.101.2",
3
+ "version": "5.101.3",
4
4
  "description": "Packs ECMAScript/CommonJs/AMD modules for the browser. Allows you to split your codebase into multiple bundles, which can be loaded on demand. Supports loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
5
5
  "homepage": "https://github.com/webpack/webpack",
6
6
  "bugs": "https://github.com/webpack/webpack/issues",
@@ -180,7 +180,7 @@
180
180
  "style-loader": "^4.0.0",
181
181
  "terser": "^5.43.1",
182
182
  "three": "^0.179.1",
183
- "tinybench": "^4.0.1",
183
+ "tinybench": "^5.0.0",
184
184
  "toml": "^3.0.0",
185
185
  "tooling": "webpack/tooling#v1.24.3",
186
186
  "ts-loader": "^9.5.1",
package/types.d.ts CHANGED
@@ -663,12 +663,12 @@ declare abstract class BasicEvaluatedExpression {
663
663
  | MethodDefinition
664
664
  | PropertyDefinition
665
665
  | VariableDeclarator
666
- | SwitchCase
667
- | CatchClause
668
666
  | ObjectPattern
669
667
  | ArrayPattern
670
668
  | RestElement
671
669
  | AssignmentPattern
670
+ | SwitchCase
671
+ | CatchClause
672
672
  | Property
673
673
  | AssignmentProperty
674
674
  | ClassBody
@@ -894,12 +894,12 @@ declare abstract class BasicEvaluatedExpression {
894
894
  | MethodDefinition
895
895
  | PropertyDefinition
896
896
  | VariableDeclarator
897
- | SwitchCase
898
- | CatchClause
899
897
  | ObjectPattern
900
898
  | ArrayPattern
901
899
  | RestElement
902
900
  | AssignmentPattern
901
+ | SwitchCase
902
+ | CatchClause
903
903
  | Property
904
904
  | AssignmentProperty
905
905
  | ClassBody
@@ -6855,6 +6855,10 @@ declare class JavascriptParser extends ParserClass {
6855
6855
  varDeclarationUsing: HookMap<SyncBailHook<[Identifier], boolean | void>>;
6856
6856
  varDeclarationVar: HookMap<SyncBailHook<[Identifier], boolean | void>>;
6857
6857
  pattern: HookMap<SyncBailHook<[Identifier], boolean | void>>;
6858
+ collectDestructuringAssignmentProperties: SyncBailHook<
6859
+ [Expression],
6860
+ boolean | void
6861
+ >;
6858
6862
  canRename: HookMap<SyncBailHook<[Expression], boolean | void>>;
6859
6863
  rename: HookMap<SyncBailHook<[Expression], boolean | void>>;
6860
6864
  assign: HookMap<SyncBailHook<[AssignmentExpression], boolean | void>>;
@@ -7329,6 +7333,38 @@ declare class JavascriptParser extends ParserClass {
7329
7333
  ): void;
7330
7334
  blockPreWalkExpressionStatement(statement: ExpressionStatement): void;
7331
7335
  preWalkAssignmentExpression(expression: AssignmentExpression): void;
7336
+ enterDestructuringAssignment(
7337
+ pattern: Pattern,
7338
+ expression: Expression
7339
+ ):
7340
+ | undefined
7341
+ | ImportExpressionImport
7342
+ | UnaryExpression
7343
+ | ArrayExpression
7344
+ | ArrowFunctionExpression
7345
+ | AssignmentExpression
7346
+ | AwaitExpression
7347
+ | BinaryExpression
7348
+ | SimpleCallExpression
7349
+ | NewExpression
7350
+ | ChainExpression
7351
+ | ClassExpression
7352
+ | ConditionalExpression
7353
+ | FunctionExpression
7354
+ | Identifier
7355
+ | SimpleLiteral
7356
+ | RegExpLiteral
7357
+ | BigIntLiteral
7358
+ | LogicalExpression
7359
+ | MemberExpression
7360
+ | MetaProperty
7361
+ | ObjectExpression
7362
+ | SequenceExpression
7363
+ | TaggedTemplateExpression
7364
+ | TemplateLiteral
7365
+ | ThisExpression
7366
+ | UpdateExpression
7367
+ | YieldExpression;
7332
7368
  modulePreWalkImportDeclaration(
7333
7369
  statement: ImportDeclarationJavascriptParser
7334
7370
  ): void;
@@ -7874,7 +7910,7 @@ declare class JavascriptParser extends ParserClass {
7874
7910
  allowedTypes: number
7875
7911
  ): undefined | CallExpressionInfo | ExpressionExpressionInfo;
7876
7912
  getNameForExpression(
7877
- expression: MemberExpression
7913
+ expression: Expression
7878
7914
  ):
7879
7915
  | undefined
7880
7916
  | {