next.js
4b34b108 - Turbopack: tree-shake imports re-exported through local export lists (#102777)

Commit
8 hours ago
Turbopack: tree-shake imports re-exported through local export lists (#102777) Base of a stack; #102776 builds on it. ### What? Tree-shake imported bindings forwarded through a local export list (`import { x } from './x'; export { x }`) the same way as direct re-exports (`export { x } from './x'`): the import is only kept when the re-export is used, or when the module uses `x` itself. Previously the export specifier counted as a top-level read, so the import was always kept. ### How? All changes are in the `turbopack-ecmascript` analyzer: - `visit_named_export` rewrites such an export into a re-export with its **own** reference (at the export's span, with the import's module, symbol and annotations), and records its liveness in `named_reexports`, exactly like `export { x } from`. The import's own reference is only used for the module's own reads of `x`. With side-effect optimization this keeps the re-export reference in the facade module and the local reads in the locals module. - The import-usage analyzer and the value-graph visitor no longer treat the identifier in such an export specifier as a read. - Re-exporting `env` from `process` previously relied on that read to mark all env vars as used. This is now detected from the re-exports directly, which also covers `export { env } from 'process'`. ### Verification - New execution fixture `remove-unused-imports/local-reexport-import` (named, aliased, default and namespace imports; imports used locally, re-exported, or both; side-effectful module still evaluated). Its removal assertions fail on canary. - New execution fixture `remove-unused-imports/local-reexport-import-shared` (scope hoisting on): an import read during module evaluation and in a function, and re-exported, with consumers using only one of those. This is the shape of `packages/next/src/shared/lib/router/utils/is-bot.ts` and fails with `ReferenceError` with an earlier version of this PR that shared one reference. - `cargo test -p turbopack-tests --test execution` (459 passed; 460 with #102776), `--test snapshot` (27), `cargo test -p turbopack-ecmascript --lib` (694, incl. `env-var-info/dynamic-named-import-reexport`), clippy. - With a locally built native binding: `pnpm test-start-turbo test/e2e/404-page-custom-error/404-page-custom-error.test.ts` passes; `test/production/pages-dir/production` builds and passes except the two "navigate to external site" tests, which need internet access. <!-- NEXT_JS_LLM --> <!-- fleet d2155176-7097-444a-9a94-a9e73d2ad3aa --> Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Author
Parents
Loading