next.js
74bb4384 - Turbopack: keep mangled compact re-exports in import order (#101920)

Commit
7 hours ago
Turbopack: keep mangled compact re-exports in import order (#101920) ### What? A compact re-export registration (`TURBOPACK_ESM_REEXPORT`) whose export names are mangled now keeps its exports in import order instead of sorting them by key. ### Why? Follow-up to #101494. That PR sorts the registration by key, because a module namespace object must list its keys in code unit order. When sorting interleaves the sources or reverses their order, the separate imports can no longer be subsumed into the registration. Export names are only mangled when the namespace object cannot escape, so nothing can observe its key order. For those modules, every source can keep a single group in import order. Where the existing subsumption checks allow it (no scope hoisting, known positions, no locally bound imports), each group is then headed by its module id and the imports are subsumed. ### How? When the module has mangled names, `build_compact_reexports` sorts the pairs by `(source import order, source index)` instead of `cmp_code_units`. The existing grouping and subsumption checks then see one group per source in import order. For example, with `export { y, z } from './first'` and `export { a } from './second'`, when every export is only imported by name: ```js // before var ns1 = __turbopack_context__.i("first.js"); var ns2 = __turbopack_context__.i("second.js"); __turbopack_context__.S([ns2, "a,a", 0, ns1, "y,y,z,z"]); // after __turbopack_context__.S(["first.js", "y,y,z,z", 0, "second.js", "a,a"]); ``` The new execution fixture `reexport-registration/mangled-reordered-sources` covers this case and checks values and evaluation order. The unmangled `reordered-sources` fixture still checks the code unit key order. ### Verification - `cargo nextest run -p turbopack-tests -p turbopack-ecmascript --cargo-profile release-with-assertions`: 1107 passed, no snapshot changes. - Checked the emitted code for the new fixture: the imports are subsumed with the change and kept without it. - `cargo clippy -p turbopack-ecmascript` and `cargo fmt --check` are clean. <!-- NEXT_JS_LLM --> <!-- fleet 6f5bd9f1-0c8e-48ce-a331-1466eb7242a6 --> --------- 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>
Author
Parents
Loading