Turbopack: mangle exports behind an escaping namespace via the facade (#97770)
### What? Allows Turbopack to keep mangling a module's internal export keys when its namespace object escapes. A facade preserves the original public names while forwarding each one to the mangled key in the locals module, matching webpack's materialized-namespace behavior. This PR is stacked on #98617. That lower PR now owns the generic synthetic-facade compact registration, including empty evaluation groups, descriptor reuse, scope-hoisted factories, pruned references, and cycle/async fallbacks. This PR creates those facades for escaping namespaces, so the generic path is exercised at scale here. ### Why? A namespace import or dynamic import whose reads cannot be enumerated currently disables mangling for the entire target module. That makes one broad consumer lose the size benefit for every otherwise-static consumer of the module. Materializing the original namespace surface keeps ESM semantics intact — including live values and `Object.keys()` — while allowing internal consumers to use shorter names. ### How? The facade's synthesized per-export references participate in the module graph, so named usage propagates to the locals module. Its structural locals edge carries evaluation usage only, while each forwarded name carries its own export usage. Part-reference code generation also honors references pruned by the graph, matching ordinary ESM references. Modules with no exports are not split, async modules retain the existing path, and real export-cycle circuit breakers avoid compact registration because they require their getters to be visible before imports run. Namespace grouping includes the SWC syntax context so scope-hoisted modules cannot accidentally share the wrong generated binding. ### Testing - `cargo test -p turbopack-ecmascript -p turbopack-tests` — **590 unit, 303 execution, 3 auxiliary, and 131 snapshot tests passed** on the current stack - `cargo clippy -p turbopack-ecmascript -p turbopack-core -- -D warnings` - `cargo fmt --all -- --check` - `pnpm test-dev-experimental-turbo test/development/app-dir/lazy-dynamic-imports/lazy-dynamic-imports.test.ts` — 9/9 passed - `pnpm test-dev test/e2e/app-dir/instant-validation/suspense-boundaries.test.ts` — 38/38 passed - `pnpm test-start-turbo test/production/app-dir/barrel-optimization/mui/index.test.ts` — 1/1 passed - `pnpm test-start-turbo test/e2e/app-dir/cache-components-allow-otel-spans/cache-components-allow-otel-spans.test.ts` — 4/4 passed - `pnpm test-start-turbo test/e2e/404-page-custom-error/404-page-custom-error.test.ts test/e2e/app-dir/app-esm-js/index.test.ts test/e2e/app-dir/app-esm-js/standalone.test.ts` — 9/9 passed - Existing materialized-namespace fixtures verify namespace values, key enumeration, dynamic imports, CommonJS consumers, and liveness through the locals module - A callable CommonJS namespace fixture regression-tests the compact registration path after facade materialization - The full execution suite specifically caught and now covers pruned locals references and merged evaluation-only locals references in the generic #98617 implementation ### Bundle-size measurements Incremental effect of enabling `turbopackMangleExportNames` on this stack. Both sides use the same native binary and retain #98617's compact re-export registration; only mangling is toggled. Client reports raw and deterministic gzip (`gzip -9 -n`, sorted files); server reports raw only because Node.js consumes server code uncompressed. JavaScript files only; source maps, CSS, and license sidecars are excluded. #### Scope hoisting disabled | benchmark | client raw | client gzip | server raw | | --- | ---: | ---: | ---: | | `basic-app` | −17,380 B | −1,131 B | −61,155 B | | `heavy-npm-deps` *(three-dependency variant: lodash + Mantine + Mermaid)* | −47,537 B | −6,720 B | −82,326 B | | `module-cost` | −223,816 B | −2,113 B | −596,419 B | #### Scope hoisting enabled | benchmark | client raw | client gzip | server raw | | --- | ---: | ---: | ---: | | `basic-app` | −22,658 B | −7,634 B | −32,805 B | | `heavy-npm-deps` *(three-dependency variant: lodash + Mantine + Mermaid)* | −14,118 B | +1,291 B | −20,956 B | | `module-cost` | +30 B | −785 B | −7,265 B | The `heavy-npm-deps` rows use a modified benchmark fixture with Mantine and Mermaid enabled alongside lodash; the committed fixture enables lodash only. Positive/near-zero configurations were repeated and reproduced byte-for-byte for every reported metric. <!-- NEXT_JS_LLM --> <!-- fleet ecdfa248-cd54-41ac-b4a2-c9d49e2a67ee --> --------- 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
This diff is collapsed.
File moved
File moved
File moved
Please
register
or
sign in
to comment