[turbopack] Track members across namespace re-exports (#96396)
## What?
This PR closes a missed tree-shaking opportunity for libraries that
expose a module namespace as a named export. One real-world example is
Zod, whose public API has a structure similar to the following (and this
pattern also appears in v0's codebase):
```js
// user.js
import { z } from './lib.js'
console.log(z.object(), z.string(), z.defaultLocale)
```
```js
// lib.js — mirrors zod/index.js
import * as z from './external.js'
export * from './external.js'
export { z }
export default z
// external.js — mirrors zod/v4/classic/external.js
import en from './locales/en.js'
export * as locales from './locales/index.js'
export const object = () => 'object'
export const string = () => 'string'
export const defaultLocale = en
```
The analyzer can now preserve the information that only `object`,
`string`, and `defaultLocale` are read from `z`, and use it after
resolving that named export to the namespace of `external.js`.
This PR is now standalone on `canary`. Its two former prerequisites have
landed: #98680 introduced the independently reviewed
`EsmAssetReferenceOptions` constructor refactor, and #96282 integrated
CommonJS and ESM whole-namespace forwarding with canary's generic
`ExportUsage::Passthrough` fixed point.
## Why?
Previously, Turbopack knew that the named export `z` was used, but lost
the static member information from expressions such as `z.object()`.
Once `z` resolved to a namespace object, the optimizer therefore kept
every export of `external.js` — including `locales` and all of
`./locales/index.js` in this example.
The optimization must remain conservative in several cases:
- a computed property whose name cannot be determined statically, such
as `z[key]`;
- a namespace binding that escapes to unknown code, such as passing `z`
to another function;
- static and opaque uses of the same binding, where the opaque use must
dominate narrowing;
- live exports and method calls, whose binding and receiver semantics
must not change.
It must also avoid changing ordinary named-import behavior. A property
read on an imported value is not proof that the value is a module
namespace; narrowing and namespace provenance are valid only after
re-export resolution proves that fact.
## How?
### Represent namespace-member requests explicitly
The module-part model now has explicit states for this operation:
- `ExportedNamespaceMember { export, member }` represents a static
member read on a named export before re-export resolution;
- `RenamedNamespaceMember { export, member }` represents the case after
that export resolves to a module namespace.
This avoids overloading an ordinary `Export` or an optional `member`
field with two meanings. It also keeps existing `Export` and
`RenamedNamespace` identities unchanged; the new variants have distinct
hashes only for the new states.
Multiple static reads and multiple aliases are tracked as independently
keyed references, so `z.object`, `z.string`, and `z.defaultLocale` all
survive rather than overwriting one another.
### Narrow only after proving the export is a namespace
The analyzer records a statically known member on an imported binding
while leaving the emitted member expression intact. Re-export following
first resolves the outer named export:
1. If it resolves to a namespace object, Turbopack synthesizes a
namespace-member reference to the resolved module and contributes
`PartialNamespaceObject({ member })` there.
2. If it resolves to an ordinary named export, it continues to behave
like a normal named read of the outer module. It does not mark the
module as namespace-observable or unnecessarily disable export-name
mangling.
Dynamic property access and escaping bindings do not produce a narrowed
member part, so they continue to retain the full namespace. When static
and opaque uses are joined, the full-namespace use wins.
The explicit module-part variants are threaded through both fragment and
non-fragment infrastructure so they remain valid graph identities. The
new `ExportedNamespaceMember` rewrite itself currently runs only when
module fragments are disabled. A dedicated `moduleFragmentsEnabled:
true` fixture verifies the supported existing split behavior without
claiming that this new rewrite is active there.
While rebasing, canary moved follow-reexport canonicalization into
`turbopack-ecmascript` so lazy dynamic imports can carry the same
canonicalization policy. Namespace-member handling now lives in that
canonicalization helper as well, preserving both the new
lazy-compilation path and this PR's namespace rewrite.
## Testing
Enabled coverage includes:
- the Zod-shaped snapshot showing only `object`, `string`, and
`defaultLocale` retained while the unused locale namespace is removed;
- multiple static members on one namespace-valued export;
- dynamic access, escaping use, and mixed static/opaque use retaining
the full namespace;
- direct `export * as`, including an enabled used/unused precision
assertion;
- computed-string, parenthesized, optional, and missing-property reads;
- `this`-sensitive method calls;
- default-exported and renamed namespace values;
- live bindings read and mutated through multiple aliases, with single
evaluation;
- nested namespace runtime behavior and top-level-await targets;
- scope-hoisted, non-scope-hoisted, and module-fragment configurations;
- export-name mangling safety and non-pessimization across #96282's
forwarding edges.
Development coverage updates both a value behind the CommonJS forwarding
edge and a statically read namespace member, verifying HMR observes each
new value without stale exports or runtime errors.
One optimization-only limitation is documented under
`__skipped__/namespace-value-reexport-nested`: member narrowing
currently stops after the first namespace level for a read such as
`z.locales.en`. The full nested runtime behavior is covered by the
enabled `namespace-value-reexport` fixture.
Local verification on the standalone canary rebase passes affected-crate
and `next-core` checks, 14 focused execution/snapshot tests, the merged
passthrough/annotation unit tests, 60 analyzer graph tests, `build-all`,
and the development/HMR test. A throwaway ordinary object-valued
named-import fixture also verified that the pre-resolution path
preserves property values and `this` for getters and methods; it was
removed after review.
<!-- NEXT_JS_LLM -->
<!-- fleet 993ef5d6-cc80-4082-8e14-ff9ac871ce33 -->
Co-authored-by:
vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by:
Tobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by:
Luke Sandberg <210140+lukesandberg@users.noreply.github.com>
Showing
File moved
Please
register
or
sign in
to comment