You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/blog/2026-03-13-webpack-5-106.mdx
+36-5Lines changed: 36 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,17 +95,48 @@ module.exports = {
95
95
96
96
## Better Tree Shaking for CommonJS Destructuring
97
97
98
-
Webpack can now statically analyze **destructuring assignments directly from CommonJS `require`** (and `module.require`) and treat only the destructured properties as “referenced exports”, instead of conservatively assuming the whole `exports` object is used. This improves dead-code elimination in optimized builds and can reduce bundle size in codebases that still consume CommonJS modules.
98
+
Webpack can now statically analyze **destructuring assignments directly from CommonJS `require`** (and `module.require`) and treat only the destructured properties as "referenced exports", instead of conservatively assuming the whole `exports` object is used. This improves dead-code elimination in optimized builds and can reduce bundle size in codebases that still consume CommonJS modules.
99
99
100
-
**Examples**
100
+
Consider a module that exports multiple functions, and a consumer that only destructures one of them:
101
101
102
102
```js
103
-
// Only `a` is considered referenced
104
-
const { a } =require("./module");
103
+
// math.js
104
+
exports.add= (a, b) => a + b;
105
+
exports.divide= (a, b) => a / b;
106
+
exports.multiply= (a, b) => a * b;
107
+
exports.subtract= (a, b) => a - b;
105
108
```
106
109
107
110
```js
108
-
// Also supported
111
+
// app.js
112
+
const { add } =require("./math");
113
+
114
+
console.log(add(2, 3));
115
+
```
116
+
117
+
In previous versions, webpack treated `require("./math")` as referencing the entire exports object. All four functions were included in the bundle even though only `add` is used:
118
+
119
+
```js
120
+
// Bundled output (5.105 — simplified)
121
+
constmath= {
122
+
add: (a, b) => a + b,
123
+
subtract: (a, b) => a - b,
124
+
multiply: (a, b) => a * b,
125
+
divide: (a, b) => a / b,
126
+
};
127
+
```
128
+
129
+
Starting with 5.106, webpack recognizes the destructuring pattern and marks only `add` as referenced. The unused exports are eliminated during optimization:
0 commit comments