Skip to content

Turbopack: properly set NODE_ENV for SWC plugins#92579

Merged
mischnic merged 3 commits into
canaryfrom
mischnic/swc-plugin-env
Apr 15, 2026
Merged

Turbopack: properly set NODE_ENV for SWC plugins#92579
mischnic merged 3 commits into
canaryfrom
mischnic/swc-plugin-env

Conversation

@mischnic
Copy link
Copy Markdown
Member

@mischnic mischnic commented Apr 9, 2026

What?

Closes #92650
Closes #92547

When Turbopack runs SWC transform plugins, it passes a TransformPluginMetadataContext that includes the current NODE_ENV. Previously this value was hardcoded to "development" regardless of the actual build mode, causing SWC plugins to misbehave during production builds.

Why?

SwcPluginMetadataContext is used by SWC plugins (e.g. babel-plugin-styled-components, @swc/plugin-emotion) to conditionally produce development vs. production output — for instance, injecting display names and data attributes only in development. With NODE_ENV always set to "development", plugins could never produce their optimised production output, resulting in larger bundles and missing minification in next build.

Fixes #92547

How?

NODE_ENV is now derived from the process.env.NODE_ENV compile-time define that is already part of CompileTimeInfo (the same source of truth used for dead-code elimination). The resolution happens in EcmascriptModuleAsset::parse() via a new shared helper node_env_from_compile_time_info() in parse.rs, and the value is stored on TransformContext so every CustomTransformer implementation can read it from ctx.node_env without needing access to the broader compilation context.

Key design decisions:

  • TransformContext is the right place: it is the per-file context passed to every CustomTransformer::transform() call, so storing node_env there follows the existing pattern and avoids coupling the plugin struct to the build mode.
  • Webpack analysis path: compile_time_info is now threaded through webpack_runtime(), module_references(), and the webpack reference structs (WebpackModuleAsset, WebpackChunkAssetReference, WebpackEntryAssetReference, WebpackRuntimeAssetReference) so that webpack-bundled files with SWC plugin transforms also receive the correct NODE_ENV.
  • segment_config.rs: passes a placeholder but includes a comment explaining it is irrelevant since EcmascriptInputTransforms::empty() means no transforms run.

@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js. labels Apr 9, 2026
@nextjs-bot
Copy link
Copy Markdown
Contributor

nextjs-bot commented Apr 9, 2026

Stats from current PR

✅ No significant changes detected

📊 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) 455ms 455ms ▁▁█▁▁
Cold (Ready in log) 439ms 439ms ▂▁▅▁▂
Cold (First Request) 833ms 822ms ▁▇▂██
Warm (Listen) 456ms 456ms ▅▅▅██
Warm (Ready in log) 438ms 439ms ▂▃▅▃▃
Warm (First Request) 337ms 345ms ▂▁▅▂▂
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 455ms 455ms ▅▅▅▁█
Cold (Ready in log) 437ms 438ms ▁▃▂▂█
Cold (First Request) 1.949s 1.932s ▁▄▃▁█
Warm (Listen) 456ms 456ms ▅▅█▅▁
Warm (Ready in log) 436ms 435ms ▁▃▂▂█
Warm (First Request) 1.952s 1.950s ▁▄▄▂█

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 3.955s 3.935s ▃▁█▂▂
Cached Build 3.975s 3.992s ▂▂▇▂▂
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.403s 14.339s ▂▃▃▂█
Cached Build 14.516s 14.494s ▁▃▂▂█
node_modules Size 493 MB 493 MB █▁███
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
053b9v5j96uu_.js gzip 8.56 kB N/A -
07p3thve3usj7.js gzip 166 B N/A -
07rxhp_1_g4mu.js gzip 13.1 kB N/A -
096aa2uod0_wa.js gzip 9.82 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0ea8vo2o6kp_s.js gzip 13.3 kB N/A -
0egovw6-5i1e3.js gzip 8.57 kB N/A -
0fli3_wppnim5.js gzip 12.9 kB N/A -
0k09jwjeb-tki.js gzip 13.8 kB N/A -
0kb7_ep3r1z0_.js gzip 10.1 kB N/A -
0kmgih16pb2zy.js gzip 9.24 kB N/A -
0wxpyd8r-vipl.js gzip 1.47 kB N/A -
0y65wbx0qseem.js gzip 152 B N/A -
1-z_xm7pzpbeh.js gzip 49.2 kB N/A -
10ya12vguoq-y.js gzip 158 B N/A -
16lhqjoqbznyg.js gzip 220 B 220 B
16vepdkipri3r.js gzip 8.51 kB N/A -
17n96uu6y1pxq.js gzip 8.6 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1hf51c3eumcxi.js gzip 154 B N/A -
1ke9exnwf9qrv.js gzip 154 B N/A -
1xvs50ql8hjy-.js gzip 155 B N/A -
1xz7fhgdco-x_.js gzip 8.59 kB N/A -
2__-e_ym8n788.js gzip 450 B N/A -
22o6xd9_ywdu6.js gzip 233 B N/A -
22wiu3_w41px6.js gzip 154 B N/A -
27acvxqigrsj2.js gzip 156 B N/A -
2copo157fzx7p.js gzip 155 B N/A -
2k1q_gre6hp0-.js gzip 8.56 kB N/A -
2kvj8yrfznmwx.js gzip 5.69 kB N/A -
2sgqisp4604si.js gzip 8.51 kB N/A -
342ijzvrpe53h.js gzip 2.29 kB N/A -
37rwzi4-whe5o.js gzip 155 B N/A -
3aae8vl16eepq.js gzip 159 B N/A -
3d3clma6-4r23.js gzip 65.5 kB N/A -
3nv9kgq774d9_.js gzip 154 B N/A -
3on3bb1ndhdz5.js gzip 70.8 kB N/A -
3v9s-fz24f8oo.js gzip 10.4 kB N/A -
3z6plg323x4y-.js gzip 155 B N/A -
420pn_b9x-3ir.js gzip 8.62 kB N/A -
44un3--wmqiyh.js gzip 7.61 kB N/A -
turbopack-0r..2k03.js gzip 4.19 kB N/A -
turbopack-1-..1ran.js gzip 4.2 kB N/A -
turbopack-11..vj4e.js gzip 4.19 kB N/A -
turbopack-1a..h9x-.js gzip 4.19 kB N/A -
turbopack-1i.._4ap.js gzip 4.19 kB N/A -
turbopack-1j..n9t-.js gzip 4.19 kB N/A -
turbopack-1l..rpeq.js gzip 4.19 kB N/A -
turbopack-1v.._r7v.js gzip 4.19 kB N/A -
turbopack-23..bd9g.js gzip 4.17 kB N/A -
turbopack-2i..h9xj.js gzip 4.19 kB N/A -
turbopack-37..oumk.js gzip 4.19 kB N/A -
turbopack-38..olal.js gzip 4.19 kB N/A -
turbopack-3o.._3ij.js gzip 4.19 kB N/A -
turbopack-3t..kvh8.js gzip 4.19 kB N/A -
03_szppy5nakp.js gzip N/A 49.2 kB -
05nuzdn63lp28.js gzip N/A 155 B -
0arkbdqpxc37i.js gzip N/A 8.6 kB -
0tvekitj587fh.js gzip N/A 8.51 kB -
0z83a1om5rvtt.js gzip N/A 7.61 kB -
1-jqyfc89tixo.js gzip N/A 1.46 kB -
14j1fphnrcumo.js gzip N/A 156 B -
14t1kneseb8th.js gzip N/A 2.3 kB -
167oy01_iobb5.js gzip N/A 154 B -
192q2suy7rwb_.js gzip N/A 8.56 kB -
1ab2xruymo-oj.js gzip N/A 449 B -
1c_z8-k7lq1ex.js gzip N/A 70.8 kB -
1c5bs0lbhv0ju.js gzip N/A 65.5 kB -
1d5u5zkyjpm-c.js gzip N/A 156 B -
1wzrm0xjjbzn5.js gzip N/A 10.1 kB -
1xxdq0mlixixb.js gzip N/A 152 B -
1z-4q0hxrcdpe.js gzip N/A 157 B -
25a1yz7zua29z.js gzip N/A 13.8 kB -
2azcc-6jyax2b.js gzip N/A 157 B -
2ccdrb-lh2w0q.js gzip N/A 158 B -
2f2bp_kexa37v.js gzip N/A 13.3 kB -
2i5v1taq97gql.js gzip N/A 8.59 kB -
2lgky8-8vt_pi.js gzip N/A 162 B -
2n3vh0w0tnb_2.js gzip N/A 8.52 kB -
2r1_yk9dpqcg4.js gzip N/A 156 B -
2u_rpxq3tzytl.js gzip N/A 233 B -
2wfbykz1avdns.js gzip N/A 8.56 kB -
32sifemopndov.js gzip N/A 10.4 kB -
368lim5wq0o0r.js gzip N/A 12.9 kB -
37c5_hyr_xcxo.js gzip N/A 155 B -
3cjel_mp63mjd.js gzip N/A 166 B -
3de283pazbu99.js gzip N/A 9.81 kB -
3drqjohogojbw.js gzip N/A 5.69 kB -
3g8l1m2-o-ewi.js gzip N/A 13.1 kB -
3gp2o_v7jcw6s.js gzip N/A 8.57 kB -
3itv8l0nc6am0.js gzip N/A 153 B -
3qu7g1q2l_x2n.js gzip N/A 9.24 kB -
43j6fjudq0a3_.js gzip N/A 8.62 kB -
turbopack-0e..ymsk.js gzip N/A 4.19 kB -
turbopack-0k..y5p0.js gzip N/A 4.19 kB -
turbopack-0t..1-6l.js gzip N/A 4.19 kB -
turbopack-0x..avr6.js gzip N/A 4.19 kB -
turbopack-1g..303t.js gzip N/A 4.2 kB -
turbopack-1j..v-je.js gzip N/A 4.19 kB -
turbopack-1w..p32p.js gzip N/A 4.19 kB -
turbopack-1x..bzds.js gzip N/A 4.19 kB -
turbopack-2-..rz8q.js gzip N/A 4.19 kB -
turbopack-29..95qv.js gzip N/A 4.19 kB -
turbopack-2e..ehma.js gzip N/A 4.17 kB -
turbopack-2l..lpt0.js gzip N/A 4.19 kB -
turbopack-3-..a3i-.js gzip N/A 4.19 kB -
turbopack-36..qzj9.js gzip N/A 4.19 kB -
Total 465 kB 465 kB ⚠️ +36 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 716 B 719 B
Total 716 B 719 B ⚠️ +3 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 430 B 435 B 🔴 +5 B (+1%)
Total 430 B 435 B ⚠️ +5 B

📦 Webpack

Client

Main Bundles
Canary PR Change
2637-HASH.js gzip 4.63 kB N/A -
7724.HASH.js gzip 169 B N/A -
8274-HASH.js gzip 61.3 kB N/A -
8817-HASH.js gzip 5.59 kB N/A -
c3500254-HASH.js gzip 62.8 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 254 B 255 B
main-HASH.js gzip 39.4 kB 39.4 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
5887-HASH.js gzip N/A 5.61 kB -
6522-HASH.js gzip N/A 60.7 kB -
6779-HASH.js gzip N/A 4.63 kB -
8854.HASH.js gzip N/A 169 B -
eab920f9-HASH.js gzip N/A 62.8 kB -
Total 235 kB 235 kB ✅ -619 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 193 B 193 B
_error-HASH.js gzip 182 B 182 B
css-HASH.js gzip 333 B 334 B
dynamic-HASH.js gzip 1.81 kB 1.8 kB
edge-ssr-HASH.js gzip 255 B 255 B
head-HASH.js gzip 353 B 349 B 🟢 4 B (-1%)
hooks-HASH.js gzip 384 B 382 B
image-HASH.js gzip 581 B 581 B
index-HASH.js gzip 260 B 259 B
link-HASH.js gzip 2.51 kB 2.51 kB
routerDirect..HASH.js gzip 316 B 318 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 313 B 314 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.98 kB 7.97 kB ✅ -10 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 126 kB 126 kB
page.js gzip 273 kB 273 kB
Total 398 kB 398 kB ✅ -320 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 618 B 618 B
middleware-r..fest.js gzip 156 B 156 B
middleware.js gzip 44.2 kB 44.2 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.9 kB 45.9 kB ⚠️ +5 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 721 B 720 B
Total 721 B 720 B ✅ -1 B
Build Cache
Canary PR Change
0.pack gzip 4.37 MB 4.38 MB
index.pack gzip 113 kB 113 kB
index.pack.old gzip 114 kB 114 kB
Total 4.6 MB 4.6 MB ⚠️ +2.96 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 346 kB 346 kB
app-page-exp..prod.js gzip 192 kB 192 kB
app-page-tur...dev.js gzip 346 kB 346 kB
app-page-tur..prod.js gzip 192 kB 192 kB
app-page-tur...dev.js gzip 342 kB 342 kB
app-page-tur..prod.js gzip 190 kB 190 kB
app-page.run...dev.js gzip 343 kB 343 kB
app-page.run..prod.js gzip 190 kB 190 kB
app-route-ex...dev.js gzip 77 kB 77 kB
app-route-ex..prod.js gzip 52.5 kB 52.5 kB
app-route-tu...dev.js gzip 77.1 kB 77.1 kB
app-route-tu..prod.js gzip 52.6 kB 52.6 kB
app-route-tu...dev.js gzip 76.7 kB 76.7 kB
app-route-tu..prod.js gzip 52.3 kB 52.3 kB
app-route.ru...dev.js gzip 76.6 kB 76.6 kB
app-route.ru..prod.js gzip 52.3 kB 52.3 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 43.9 kB 43.9 kB
pages-api-tu..prod.js gzip 33.5 kB 33.5 kB
pages-api.ru...dev.js gzip 43.9 kB 43.9 kB
pages-api.ru..prod.js gzip 33.5 kB 33.5 kB
pages-turbo....dev.js gzip 53.3 kB 53.3 kB
pages-turbo...prod.js gzip 39.1 kB 39.1 kB
pages.runtim...dev.js gzip 53.3 kB 53.3 kB
pages.runtim..prod.js gzip 39.1 kB 39.1 kB
server.runti..prod.js gzip 62.9 kB 62.9 kB
Total 3.06 MB 3.06 MB ✅ -3 B
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/4db429240dd852bf3d4f2c0d9231655e48a4db75/next

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 9, 2026

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing mischnic/swc-plugin-env (4db4292) with canary (56e5acc)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Comment thread turbopack/crates/turbopack-ecmascript/src/webpack/parse.rs Outdated
@mischnic mischnic marked this pull request as ready for review April 13, 2026 21:25
@mischnic mischnic force-pushed the mischnic/swc-plugin-env branch from a0e02f4 to 99ddbb7 Compare April 13, 2026 21:25
Copy link
Copy Markdown
Member Author

mischnic commented Apr 13, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@nextjs-bot
Copy link
Copy Markdown
Contributor

nextjs-bot commented Apr 13, 2026

Tests Passed

@mischnic mischnic changed the base branch from canary to graphite-base/92579 April 14, 2026 08:46
@mischnic mischnic force-pushed the mischnic/swc-plugin-env branch from 99ddbb7 to 62adbc5 Compare April 14, 2026 08:46
@mischnic mischnic changed the base branch from graphite-base/92579 to mischnic/fix-webpack-swc-plugins-relative April 14, 2026 08:46
@mischnic mischnic force-pushed the mischnic/fix-webpack-swc-plugins-relative branch from 785c7ae to 415c9a8 Compare April 15, 2026 08:49
@mischnic mischnic force-pushed the mischnic/swc-plugin-env branch 2 times, most recently from c10ad97 to 424da41 Compare April 15, 2026 09:10
@mischnic mischnic force-pushed the mischnic/fix-webpack-swc-plugins-relative branch from 415c9a8 to 3773c9b Compare April 15, 2026 09:10
@mischnic mischnic force-pushed the mischnic/swc-plugin-env branch from 424da41 to 56a1f9f Compare April 15, 2026 09:21
@mischnic mischnic force-pushed the mischnic/fix-webpack-swc-plugins-relative branch from 3773c9b to 1d41e4b Compare April 15, 2026 09:21
@mischnic mischnic force-pushed the mischnic/swc-plugin-env branch from 56a1f9f to c413281 Compare April 15, 2026 09:22
@mischnic mischnic changed the base branch from mischnic/fix-webpack-swc-plugins-relative to graphite-base/92579 April 15, 2026 10:04
mischnic added a commit that referenced this pull request Apr 15, 2026
Webpack didn't correctly load relative paths for `swcPlugins`.

#92579 will add a test
mischnic and others added 2 commits April 15, 2026 10:05
…builds

Thread the actual build mode (NextMode) through to SwcEcmaTransformPluginsTransformer
so that TransformPluginMetadataContext receives "production" during `next build`
instead of the previously hardcoded "development" string.

Fixes #92547

Store node_env in TransformContext instead of SwcEcmaTransformPluginsTransformer

The SWC plugin transformer was storing node_env as a field on the struct,
which caused it to be locked in at plugin construction time from NextMode.
Instead, derive node_env from process.env.NODE_ENV compile-time defines in
EcmascriptModuleAsset::parse() and thread it through TransformContext so
each file transformation sees the correct environment value.

Thread compile_time_info through webpack path to eliminate hardcoded node_env strings

Extract node_env_from_compile_time_info() as a shared helper in parse.rs and use it
from both EcmascriptModuleAsset::parse() and the webpack analysis path. Thread
compile_time_info through WebpackModuleAsset, WebpackChunkAssetReference,
WebpackEntryAssetReference, WebpackRuntimeAssetReference, webpack_runtime(), and
module_references() so that SWC plugin transforms on webpack bundles also see the
correct NODE_ENV. Add a comment in segment_config.rs explaining why the node_env
value is irrelevant there (EcmascriptInputTransforms::empty() = no transforms run).

Co-Authored-By: Claude <noreply@anthropic.com>
@mischnic mischnic force-pushed the mischnic/swc-plugin-env branch from c413281 to 412a6d2 Compare April 15, 2026 10:05
@mischnic mischnic force-pushed the graphite-base/92579 branch from fe6d57b to 56e5acc Compare April 15, 2026 10:05
@graphite-app graphite-app Bot changed the base branch from graphite-base/92579 to canary April 15, 2026 10:05
@mischnic mischnic force-pushed the mischnic/swc-plugin-env branch from 412a6d2 to 4db4292 Compare April 15, 2026 10:05
@mischnic mischnic changed the title fix(turbopack): read NODE_ENV for SWC plugins from compile-time defines via TransformContext Turbopack: read NODE_ENV for SWC plugins from compile-time defines via TransformContext Apr 15, 2026
@mischnic mischnic changed the title Turbopack: read NODE_ENV for SWC plugins from compile-time defines via TransformContext Turbopack: properly set NODE_ENV for SWC plugins Apr 15, 2026
@mischnic mischnic merged commit 3f17705 into canary Apr 15, 2026
495 of 499 checks passed
@mischnic mischnic deleted the mischnic/swc-plugin-env branch April 15, 2026 12:55
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

created-by: Turbopack team PRs by the Turbopack team. locked tests Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Turbopack passes "development" to SWC plugins during next build

3 participants