AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack: ProductionBrowserSourceMaps Disables Dead-Import Elimination For Compiler.Define-Folded Typeof Guards

Enabling productionBrowserSourceMaps in Turbopack prevents tree-shaking of side-effect-free imports referenced only in typeof-guarded dead branches after compiler.define replacement, causing unreachable modules to be bundled.

highConfidence 85%Next.jsAffected V16.3.0Affected V16.3.2Affected V16.4.0-Canary.3

Origin Analysis

Turbopack's source map generation treats original import bindings as live references for mapping generated code, preventing dead-code elimination of modules whose only use is inside typeof __FLAG__ === 'undefined' || __FLAG__ guards folded by compiler.define. The import is kept in the module graph even though the call site is eliminated, resulting in the module subtree being emitted.
1. Create a Next.js app with a local package fake-sdk with sideEffects: false, containing import { feature } from './feature.js' and a guard if (typeof __MY_FLAG__ === 'undefined' || __MY_FLAG__) { integrations.push(feature()); }. 2. Set productionBrowserSourceMaps: true and compiler: { define: { __MY_FLAG__: false } } in next.config.mjs. 3. Run next build. 4. Grep for FEATURE_MARKER_SHOULD_BE_TREESHAKEN in .next/static/chunks/; it is present when source maps are enabled, absent when disabled.

Fixing Code Block

Edge Case Audit

Disabling Turbopack may reduce build performance and remove access to Turbopack-specific optimizations. It may also be unsupported in future Next.js versions if webpack fallback is removed. Use only as a temporary hotfix; revert to Turbopack once the underlying tree-shaking bug is fixed upstream. Test build output and source maps after applying.

Ecosystem Topology