Skip to content

Commit 45b2358

Browse files
feat: handle more expression types in isPure for better tree-shaking (#20723)
1 parent ffbf36f commit 45b2358

18 files changed

Lines changed: 245 additions & 55 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Improve tree-shaking by handling more expression types in `isPure`: `ArrayExpression`, `ObjectExpression` and `NewExpression`.

‎lib/CompatibilityPlugin.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,14 @@ class CompatibilityPlugin {
186186
(declarator.id.name === RuntimeGlobals.exports ||
187187
declarator.id.name === RuntimeGlobals.require)
188188
) {
189-
const { name, declaration } =
190-
/** @type {CompatibilitySettings} */ (
191-
parser.getTagData(
192-
declarator.id.name,
193-
nestedWebpackIdentifierTag
194-
)
195-
);
189+
const tagData = /** @type {CompatibilitySettings | undefined} */ (
190+
parser.getTagData(
191+
declarator.id.name,
192+
nestedWebpackIdentifierTag
193+
)
194+
);
195+
if (!tagData) return;
196+
const { name, declaration } = tagData;
196197
if (!declaration.updated) {
197198
const dep = new ConstDependency(name, declaration.range);
198199
dep.loc = declaration.loc;

‎lib/javascript/JavascriptParser.js‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4956,8 +4956,8 @@ class JavascriptParser extends Parser {
49564956
.for(expr.type)
49574957
.call(expr, commentsStartPos);
49584958
if (typeof result === "boolean") return result;
4959+
// TODO handle more cases
49594960
switch (expr.type) {
4960-
// TODO handle more cases
49614961
case "ClassDeclaration":
49624962
case "ClassExpression": {
49634963
if (expr.body.type !== "ClassBody") return false;
@@ -5027,6 +5027,37 @@ class JavascriptParser extends Parser {
50275027
this.isPure(decl.init, /** @type {Range} */ (decl.range)[0])
50285028
);
50295029

5030+
case "ArrayExpression":
5031+
return expr.elements.every((element) => {
5032+
if (element === null) return true;
5033+
if (element.type === "SpreadElement") return false;
5034+
const pureFlag = this.isPure(element, commentsStartPos);
5035+
commentsStartPos = /** @type {Range} */ (element.range)[1];
5036+
return pureFlag;
5037+
});
5038+
5039+
case "ObjectExpression": {
5040+
return expr.properties.every((property) => {
5041+
if (property.type === "SpreadElement") return false;
5042+
const end = /** @type {Range} */ (property.key.range)[1];
5043+
5044+
if (
5045+
property.computed &&
5046+
!this.isPure(property.key, commentsStartPos)
5047+
) {
5048+
return false;
5049+
}
5050+
5051+
const pureFlag = this.isPure(
5052+
/** @type {Exclude<Property["value"], AssignmentPattern | ObjectPattern | ArrayPattern | RestElement>} */
5053+
(property.value),
5054+
end
5055+
);
5056+
commentsStartPos = end;
5057+
return pureFlag;
5058+
});
5059+
}
5060+
50305061
case "ConditionalExpression":
50315062
return (
50325063
this.isPure(expr.test, commentsStartPos) &&
@@ -5073,6 +5104,27 @@ class JavascriptParser extends Parser {
50735104
return pureFlag;
50745105
});
50755106
}
5107+
5108+
case "NewExpression": {
5109+
const pureFlag =
5110+
/** @type {Range} */ (expr.range)[0] - commentsStartPos > 12 &&
5111+
this.getComments([
5112+
commentsStartPos,
5113+
/** @type {Range} */ (expr.range)[0]
5114+
]).some(
5115+
(comment) =>
5116+
comment.type === "Block" &&
5117+
/^\s*(?:#|@)__PURE__\s*$/.test(comment.value)
5118+
);
5119+
if (!pureFlag) return false;
5120+
commentsStartPos = /** @type {Range} */ (expr.callee.range)[1];
5121+
return expr.arguments.every((arg) => {
5122+
if (arg.type === "SpreadElement") return false;
5123+
const pureFlag = this.isPure(arg, commentsStartPos);
5124+
commentsStartPos = /** @type {Range} */ (arg.range)[1];
5125+
return pureFlag;
5126+
});
5127+
}
50765128
}
50775129
const evaluated = this.evaluateExpression(expr);
50785130
return !evaluated.couldHaveSideEffects();

‎lib/optimize/InnerGraphPlugin.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
JAVASCRIPT_MODULE_TYPE_AUTO,
1010
JAVASCRIPT_MODULE_TYPE_ESM
1111
} = require("../ModuleTypeConstants");
12+
const RuntimeGlobals = require("../RuntimeGlobals");
1213
const PureExpressionDependency = require("../dependencies/PureExpressionDependency");
1314
const InnerGraph = require("./InnerGraph");
1415

@@ -208,6 +209,13 @@ class InnerGraphPlugin {
208209
decl.id.type === "Identifier"
209210
) {
210211
const name = decl.id.name;
212+
// Skip webpack runtime variables handled by CompatibilityPlugin
213+
if (
214+
name === RuntimeGlobals.require ||
215+
name === RuntimeGlobals.exports
216+
) {
217+
return;
218+
}
211219
if (
212220
decl.init.type === "ClassExpression" &&
213221
parser.isPure(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { o2, o3 } from "./module";
2+
3+
import {
4+
exportUsed,
5+
export2Used,
6+
export3Used,
7+
export4Used,
8+
export5Used,
9+
export6Used
10+
} from "./inner";
11+
12+
it("some exports should be unused when no array expression use it", () => {
13+
expect(o2[0]).toBe(42);
14+
expect(o3[0].EXPORT5()).toBe(42);
15+
expect(o3[0].EXPORT6()).toBe(42);
16+
if (process.env.NODE_ENV === "production") {
17+
expect(exportUsed).toBe(false);
18+
expect(export2Used).toBe(false);
19+
expect(export3Used).toBe(false);
20+
expect(export4Used).toBe(true);
21+
expect(export5Used).toBe(true);
22+
expect(export6Used).toBe(true);
23+
}
24+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const EXPORT = 42;
2+
export const EXPORT2 = 42;
3+
export const EXPORT3 = 42;
4+
export const EXPORT4 = 42;
5+
export const EXPORT5 = () => 42;
6+
export const EXPORT6 = () => 42;
7+
8+
export const exportUsed = __webpack_exports_info__.EXPORT.used;
9+
export const export2Used = __webpack_exports_info__.EXPORT2.used;
10+
export const export3Used = __webpack_exports_info__.EXPORT3.used;
11+
export const export4Used = __webpack_exports_info__.EXPORT4.used;
12+
export const export5Used = __webpack_exports_info__.EXPORT5.used;
13+
export const export6Used = __webpack_exports_info__.EXPORT6.used;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { EXPORT, EXPORT2, EXPORT3, EXPORT4, EXPORT5, EXPORT6 } from "./inner";
2+
3+
const o1 = [EXPORT2, EXPORT3];
4+
5+
const o2 = [EXPORT4];
6+
7+
const o3 = [{ EXPORT5, EXPORT6 }];
8+
9+
export { o1, o2, o3 };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { o2, o3 } from "./module";
2+
3+
import {
4+
exportUsed,
5+
export2Used,
6+
export3Used,
7+
export4Used,
8+
export5Used,
9+
export6Used
10+
} from "./inner";
11+
12+
it("some exports should be unused when no object expression use it", () => {
13+
expect(o2.EXPORT4).toBe(42);
14+
expect(o3.EXPORT5()).toBe(42);
15+
expect(o3.EXPORT6()).toBe(42);
16+
if (process.env.NODE_ENV === "production") {
17+
expect(exportUsed).toBe(false);
18+
expect(export2Used).toBe(false);
19+
expect(export3Used).toBe(false);
20+
expect(export4Used).toBe(true);
21+
expect(export5Used).toBe(true);
22+
expect(export6Used).toBe(true);
23+
}
24+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const EXPORT = 42;
2+
export const EXPORT2 = 42;
3+
export const EXPORT3 = 42;
4+
export const EXPORT4 = 42;
5+
export const EXPORT5 = () => 42;
6+
export const EXPORT6 = () => 42;
7+
8+
export const exportUsed = __webpack_exports_info__.EXPORT.used;
9+
export const export2Used = __webpack_exports_info__.EXPORT2.used;
10+
export const export3Used = __webpack_exports_info__.EXPORT3.used;
11+
export const export4Used = __webpack_exports_info__.EXPORT4.used;
12+
export const export5Used = __webpack_exports_info__.EXPORT5.used;
13+
export const export6Used = __webpack_exports_info__.EXPORT6.used;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { EXPORT, EXPORT2, EXPORT3, EXPORT4, EXPORT5, EXPORT6 } from "./inner";
2+
3+
const o1 = {
4+
EXPORT2: EXPORT2,
5+
EXPORT3: EXPORT3
6+
};
7+
8+
const o2 = {
9+
EXPORT4: EXPORT4
10+
};
11+
12+
const o3 = {
13+
EXPORT5: EXPORT5,
14+
EXPORT6: EXPORT6
15+
};
16+
17+
export { o1, o2, o3 };

0 commit comments

Comments
 (0)