Skip to content

[turbopack] Drop unused exports from a CJS module#95716

Open
sampoder wants to merge 1 commit into
canaryfrom
sp/turbopack/cjs-tree-shaking/drop-unused-exports
Open

[turbopack] Drop unused exports from a CJS module#95716
sampoder wants to merge 1 commit into
canaryfrom
sp/turbopack/cjs-tree-shaking/drop-unused-exports

Conversation

@sampoder

@sampoder sampoder commented Jul 12, 2026

Copy link
Copy Markdown
Member

This PR introduces two new things: a pass of the analyzer that recognises exports from CJS modules (in the form of exports.foo, see analyze_cjs_exports) and then a CodeGen that reads export_usage_info to remove CJS exports that are not used.

This PR does not add anything additionally to export_usage_info, so this means that we only trim from modules that are imported with import { foo } from "bar". The CodeGen depends on the output of analyze_cjs_exports for the locations of exports, whether it is safe to drop exports (its pretty conservative), and also whether __esModule is present.

@github-actions

github-actions Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Stats from current PR

🟢 1 improvement

Metric Canary PR Change Trend
Webpack Build Time (cached) 25.763s 25.114s 🟢 649ms (-3%) ▁▁▁▁█
📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 759ms 762ms ▁▁▁▂█
Cold (Ready in log) 752ms 758ms ▁▁▁▁█
Cold (First Request) 1.271s 1.278s ▁▁▁▁█
Warm (Listen) 759ms 760ms ▁▁▁▁▇
Warm (Ready in log) 745ms 752ms ▁▁▁▁█
Warm (First Request) 1.254s 1.268s ▁▁▁▁█
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 814ms 811ms ▂▁▂▁█
Cold (Ready in log) 784ms 776ms ▁▁▁▁█
Cold (First Request) 3.388s 3.292s ▁▁▂▁█
Warm (Listen) 813ms 812ms ▂▂▂▁█
Warm (Ready in log) 794ms 777ms ▁▁▁▁█
Warm (First Request) 3.452s 3.370s ▁▁▁▁█

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 4.880s 4.878s ▁▁▁▁█
Cached Build 4.930s 4.921s ▁▁▁▁█
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 25.294s 25.466s ▁▁▁▁█
Cached Build 25.763s 25.114s 🟢 649ms (-3%) ▁▁▁▁█
node_modules Size 516 MB 516 MB ▁▁▁▁█
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
00ef4gy8b5wxj.js gzip 157 B N/A -
03gmyxr7dukvs.js gzip 10.3 kB N/A -
040t1q17lo593.js gzip 8.77 kB N/A -
05js0p06ql0ws.js gzip 162 B N/A -
0bb0vtp8dwsez.js gzip 450 B N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0ga6rcwbclepx.js gzip 158 B N/A -
0l0sr6gau5xjn.js gzip 3.53 kB N/A -
0nxqz3q165m2l.js gzip 220 B N/A -
0qr-1tnb7_hg5.js gzip 5.72 kB N/A -
0su3oj-wd_f24.js gzip 157 B N/A -
0uilzvklme4ea.js gzip 8.76 kB N/A -
0v-to2419kft6.js gzip 71.1 kB N/A -
0z0y83mfx-l-h.js gzip 10 kB N/A -
15ok8ydf274z0.js gzip 10.6 kB N/A -
16cqqcigykfea.js gzip 13.6 kB N/A -
17h18zxt1915l.js gzip 1.46 kB N/A -
1b6ps5k3qbc9v.js gzip 168 B N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1fq1h7dxad4cv.js gzip 9.44 kB N/A -
1fym-adbk_69_.js gzip 13.1 kB N/A -
1g80-jfls9qyr.js gzip 13.2 kB N/A -
1gav7nfh4y8ln.js gzip 8.76 kB N/A -
22sajk3amu-ua.js gzip 45 kB N/A -
2c0gxmllklfeh.js gzip 155 B N/A -
2cm1e61lu646w.js gzip 155 B N/A -
2jicr0hpcz3x7.js gzip 154 B N/A -
2rypqffktcxqh.js gzip 8.69 kB N/A -
2te199gvbth0p.js gzip 154 B N/A -
2uqsizxtehgqf.js gzip 8.79 kB N/A -
2vzktdqwbijqc.js gzip 8.74 kB N/A -
2zh5c22cnhvwf.js gzip 155 B N/A -
304gh1b2mfj2-.js gzip 65.5 kB N/A -
35dyc4njdvspw.js gzip 156 B N/A -
39ubradsr4gzy.js gzip 158 B N/A -
3c244akvx_-be.js gzip 8.74 kB N/A -
3dmlemitx3ef3.js gzip 2.29 kB N/A -
3pq0q6ik66hi5.js gzip 162 B N/A -
3y193x4n8cq9d.js gzip 7.4 kB N/A -
4237vdj6eyslt.js gzip 8.7 kB N/A -
turbopack-06..2zci.js gzip 3.85 kB N/A -
turbopack-0l..vj57.js gzip 3.85 kB N/A -
turbopack-0y..khhu.js gzip 3.85 kB N/A -
turbopack-17..dk51.js gzip 3.83 kB N/A -
turbopack-1o..q_rp.js gzip 3.85 kB N/A -
turbopack-1t..txle.js gzip 3.85 kB N/A -
turbopack-1u..7swf.js gzip 3.85 kB N/A -
turbopack-1y..8r0a.js gzip 3.85 kB N/A -
turbopack-28..1rro.js gzip 3.85 kB N/A -
turbopack-29..--ev.js gzip 3.85 kB N/A -
turbopack-3-..e5qa.js gzip 3.85 kB N/A -
turbopack-31..np11.js gzip 3.85 kB N/A -
turbopack-3l..a73s.js gzip 3.86 kB N/A -
turbopack-3z..yq_9.js gzip 3.85 kB N/A -
00tc16j5-eos9.js gzip N/A 1.46 kB -
0130eu77j0h59.js gzip N/A 155 B -
04joe4zmayj76.js gzip N/A 159 B -
05efbq2jv23b5.js gzip N/A 155 B -
05tax4x1rfda4.js gzip N/A 7.4 kB -
0ci8x4-0ug8pc.js gzip N/A 71.1 kB -
0d9rpn6i4_zpx.js gzip N/A 160 B -
0gaw1gmpwuwkn.js gzip N/A 8.8 kB -
12zyjjmq4oad1.js gzip N/A 5.72 kB -
1hefztyduchdh.js gzip N/A 156 B -
1k9cc4-gpa2cx.js gzip N/A 13.1 kB -
1n59uogejo81s.js gzip N/A 10.3 kB -
1npei3hlexdv3.js gzip N/A 9.99 kB -
1ssuiwj_rnpc5.js gzip N/A 8.77 kB -
1zfs8m9y4a5zq.js gzip N/A 152 B -
2-phc9obd3nyd.js gzip N/A 8.77 kB -
24utq57r3l1cw.js gzip N/A 8.7 kB -
2d_vc-5g35ich.js gzip N/A 155 B -
2fhjq6ek3srmr.js gzip N/A 8.69 kB -
2g0t7upz_v-hf.js gzip N/A 10.6 kB -
2ia9qqy99aie9.js gzip N/A 13.6 kB -
2m58iubo3_2om.js gzip N/A 65.5 kB -
2oza28kdqvoay.js gzip N/A 8.74 kB -
2x7r100b7ytej.js gzip N/A 168 B -
2zh8-mmhujy8o.js gzip N/A 2.29 kB -
2znh0hgl912gx.js gzip N/A 155 B -
30h_m0irj-q6p.js gzip N/A 221 B -
359s3sl8kxyzu.js gzip N/A 156 B -
37ejbfx1u9lpo.js gzip N/A 8.75 kB -
38c63ct4i1cy7.js gzip N/A 449 B -
395ew4vducf52.js gzip N/A 154 B -
3bov4yvn0_jo5.js gzip N/A 157 B -
3hd1us4ordyc9.js gzip N/A 13.2 kB -
3jimpal3lvz5e.js gzip N/A 9.44 kB -
3ns42zoevxrsp.js gzip N/A 45 kB -
3su6a4l8jbfth.js gzip N/A 3.53 kB -
3wb4pe4z4-aim.js gzip N/A 8.77 kB -
41z0m_6bo_9ke.js gzip N/A 155 B -
turbopack-05..tq1r.js gzip N/A 3.85 kB -
turbopack-0c..kyer.js gzip N/A 3.84 kB -
turbopack-0o..5lk5.js gzip N/A 3.83 kB -
turbopack-0t..ilm7.js gzip N/A 3.85 kB -
turbopack-17..m634.js gzip N/A 3.85 kB -
turbopack-19..1c1c.js gzip N/A 3.85 kB -
turbopack-1j..f3i6.js gzip N/A 3.85 kB -
turbopack-1q..obpn.js gzip N/A 3.85 kB -
turbopack-1z..7kul.js gzip N/A 3.85 kB -
turbopack-2g..1qrh.js gzip N/A 3.85 kB -
turbopack-2m..1z1h.js gzip N/A 3.85 kB -
turbopack-2y..nv-r.js gzip N/A 3.85 kB -
turbopack-35..jnn_.js gzip N/A 3.86 kB -
turbopack-3j..38kl.js gzip N/A 3.85 kB -
Total 448 kB 448 kB ⚠️ +20 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 730 B 723 B
Total 730 B 723 B ✅ -7 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 434 B 433 B
Total 434 B 433 B ✅ -1 B

📦 Webpack

Client

Main Bundles
Canary PR Change
2486.HASH.js gzip 169 B N/A -
3146-HASH.js gzip 64 kB N/A -
39fcf99b-HASH.js gzip 62.9 kB N/A -
8443-HASH.js gzip 4.68 kB N/A -
9431-HASH.js gzip 5.62 kB N/A -
framework-HASH.js gzip 59.8 kB 59.8 kB
main-app-HASH.js gzip 255 B 254 B
main-HASH.js gzip 39.6 kB 40.1 kB 🔴 +455 B (+1%)
webpack-HASH.js gzip 1.68 kB 1.68 kB
6105-HASH.js gzip N/A 5.63 kB -
764.HASH.js gzip N/A 169 B -
8898-HASH.js gzip N/A 63.4 kB -
9597-HASH.js gzip N/A 4.65 kB -
e1ccab69-HASH.js gzip N/A 62.9 kB -
Total 239 kB 239 kB ✅ -141 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB
Total 39.4 kB 39.4 kB
Pages
Canary PR Change
_app-HASH.js gzip 194 B 194 B
_error-HASH.js gzip 183 B 182 B
css-HASH.js gzip 335 B 335 B
dynamic-HASH.js gzip 1.8 kB 1.8 kB
edge-ssr-HASH.js gzip 255 B 254 B
head-HASH.js gzip 351 B 349 B
hooks-HASH.js gzip 384 B 384 B
image-HASH.js gzip 580 B 580 B
index-HASH.js gzip 259 B 259 B
link-HASH.js gzip 2.49 kB 2.49 kB
routerDirect..HASH.js gzip 319 B 319 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 315 B 313 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.96 kB 7.95 kB ✅ -7 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 128 kB 127 kB
page.js gzip 282 kB 283 kB
Total 409 kB 411 kB ⚠️ +1.5 kB
Middleware
Canary PR Change
middleware-b..fest.js gzip 614 B 615 B
middleware-r..fest.js gzip 156 B 155 B
middleware.js gzip 45.2 kB 45.2 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 46.8 kB 46.8 kB ⚠️ +29 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 719 B 720 B
Total 719 B 720 B ⚠️ +1 B
Build Cache
Canary PR Change
0.pack gzip 4.78 MB 4.78 MB 🟢 7.53 kB (0%)
index.pack gzip 118 kB 118 kB
index.pack.old gzip 117 kB 117 kB
Total 5.02 MB 5.01 MB ✅ -7.33 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 363 kB 363 kB
app-page-exp..prod.js gzip 201 kB 201 kB
app-page-tur...dev.js gzip 363 kB 363 kB
app-page-tur..prod.js gzip 201 kB 201 kB
app-page-tur...dev.js gzip 359 kB 359 kB
app-page-tur..prod.js gzip 199 kB 199 kB
app-page.run...dev.js gzip 360 kB 360 kB
app-page.run..prod.js gzip 199 kB 199 kB
app-route-ex...dev.js gzip 81.1 kB 81.1 kB
app-route-ex..prod.js gzip 55.2 kB 55.2 kB
app-route-tu...dev.js gzip 81.1 kB 81.1 kB
app-route-tu..prod.js gzip 55.2 kB 55.2 kB
app-route-tu...dev.js gzip 80.7 kB 80.7 kB
app-route-tu..prod.js gzip 55 kB 55 kB
app-route.ru...dev.js gzip 80.7 kB 80.7 kB
app-route.ru..prod.js gzip 54.9 kB 54.9 kB
dist_client_...dev.js gzip 324 B 324 B
dist_client_...dev.js gzip 326 B 326 B
dist_client_...dev.js gzip 318 B 318 B
dist_client_...dev.js gzip 317 B 317 B
pages-api-tu...dev.js gzip 45.2 kB 45.2 kB
pages-api-tu..prod.js gzip 34 kB 34 kB
pages-api.ru...dev.js gzip 45.2 kB 45.2 kB
pages-api.ru..prod.js gzip 33.9 kB 33.9 kB
pages-turbo....dev.js gzip 54.7 kB 54.7 kB
pages-turbo...prod.js gzip 39.6 kB 39.6 kB
pages.runtim...dev.js gzip 54.7 kB 54.7 kB
pages.runtim..prod.js gzip 39.6 kB 39.6 kB
server.runti..prod.js gzip 65.3 kB 65.3 kB
use-cache-pr...dev.js gzip 71.4 kB 71.4 kB
use-cache-pr...dev.js gzip 71.4 kB 71.4 kB
use-cache-pr...dev.js gzip 69.7 kB 69.7 kB
use-cache-pr...dev.js gzip 69.7 kB 69.7 kB
Total 3.48 MB 3.48 MB
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/5fb6e118e5954d0e6e1d24e272e66d290e3c069a/next

Commit: 5fb6e11

@github-actions

github-actions Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Failing CI jobs

Commit: 5fb6e11 | About building and testing Next.js

Comment thread turbopack/crates/turbopack-ecmascript/src/references/cjs_exports/recognize.rs Outdated
@sampoder sampoder force-pushed the sp/turbopack/cjs-tree-shaking/drop-unused-exports branch from 5255e39 to eaaf647 Compare July 12, 2026 18:06
@sampoder sampoder marked this pull request as ready for review July 12, 2026 18:21
@sampoder sampoder force-pushed the sp/turbopack/cjs-tree-shaking/drop-unused-exports branch from eaaf647 to 5fb6e11 Compare July 12, 2026 18:22
@sampoder sampoder requested a review from lukesandberg July 12, 2026 18:23
/// expression) — sets `is_unsafe`.
///
/// TODO (@sampoder): handle `Object.defineProperty(exports, "__esModule", { value: true })`
pub fn analyze_cjs_exports(program: &Program, unresolved_mark: Mark) -> CommonJsExportsAnalysis {

@mischnic mischnic Jul 13, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this need to be a separate AST pass?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try moving it into a pre-existing one. Might be a little less clean but it will probably run a lot faster. I also was thinking we might want to put this behind a flag so this method would make that easier. Is there a pass you had in mind that I could combine it with?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the natural place would be do it as part of the main Analyzer and then use the Effects system to report a droppable effect for every export

or really follow the pattern we have for ESM items: see add_esm_module_item

/// shadow. Prevents `let exports = {}; exports.foo = 'a'` from being treated as
/// a write to the global `exports`.
fn is_global(identifier: &Ident, name: &str, unresolved_mark: Mark) -> bool {
pub(crate) fn is_global(identifier: &Ident, name: &str, unresolved_mark: Mark) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these helpers should probably just move to a new module.... cjs_ast.rs?

name: RcStr,
/// Source position (`Stmt::span().lo`) of the `exports.NAME = …` statement,
/// used to locate it in the module body at code-gen time.
span_lo: u32,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typically we capture an AST path, is that possible in your traversal

Comment on lines +79 to +80
if matches!(*export_usage_info, ModuleExportUsageInfo::All) {
return Ok(CodeGeneration::empty());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this is more efficient... but this would fall out of the is_export_used checks below so you could just drop this special case

impl DropExportWrites {
fn drop_matching(&self, stmt: &mut Stmt) {
if self.unused.contains(&stmt.span().lo) {
*stmt = Stmt::Empty(EmptyStmt { span: DUMMY_SP });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we can drop the whole statement

exports.foo = bar() should become bar()

/// Whether the module touches `exports` / `module` outside the recognized
/// safe forms. When set, `names` may be incomplete and the module must be
/// treated as opaque CommonJS (no exports dropped).
pub is_unsafe: bool,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has_dynamic_exports would be a better name

Comment on lines +153 to +155
// Only side-effect-free values are droppable: removing the statement
// removes the evaluation of its RHS, so an impure RHS must be kept.
if !assign.right.may_have_side_effects(expr_ctx) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we need this condition as long as our rewrite preserves the LHS

we can let SWC decide the expression is deletable

/// expression) — sets `is_unsafe`.
///
/// TODO (@sampoder): handle `Object.defineProperty(exports, "__esModule", { value: true })`
pub fn analyze_cjs_exports(program: &Program, unresolved_mark: Mark) -> CommonJsExportsAnalysis {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the natural place would be do it as part of the main Analyzer and then use the Effects system to report a droppable effect for every export

or really follow the pattern we have for ESM items: see add_esm_module_item

self.fn_depth -= 1;
}

fn visit_this_expr(&mut self, _: &swc_core::ecma::ast::ThisExpr) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a reference to a this that is not rebound by another function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants