Unverified Commit 6db7b1f4 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Avoid facade splits solely for export mangling (#99285)

## Summary

A dynamically imported local-only ESM module could load through a facade
while static named imports resolved to its locals module. For shared
singletons, that meant two runtime identities. This backs off the facade
split introduced for export mangling in #97770, addressing the
remote-components regression reproduced in #99279.

Modules with actual re-exports still split as before, and unsplit
modules remain eligible for export-name mangling when their names can be
changed safely. Escaping namespaces keep their original export names.
Focused execution coverage checks that mixed dynamic/static imports
register a single module identity; the affected namespace and snapshot
expectations are updated.

## Verification

- `cargo test -p turbopack-tests --test execution` — 311 passed.
- `cargo test -p turbopack-tests --test snapshot` — 132 passed.
- Turbopack ECMAScript unit suite — 590 passed.
- `pnpm build-all`, `cargo fmt --all -- --check`, Prettier and
lint-staged checks passed.
- Both #99279 production Turbopack fixtures passed with shared-singleton
assertions in a disposable checkout; those unmerged fixtures are not
included here.

<!-- NEXT_JS_LLM -->


<!-- fleet faf1202d-c919-4f7d-bb74-d3fe954bbc57 -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 1cc0f661
...@@ -136,7 +136,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -136,7 +136,7 @@ import { nextTestSetup } from 'e2e-utils'
"[project]/src/app/client/shared-app-client.js [app-ssr] (ecmascript)", "[project]/src/app/client/shared-app-client.js [app-ssr] (ecmascript)",
"[project]/src/app/layout-target.js [app-rsc] (ecmascript)", "[project]/src/app/layout-target.js [app-rsc] (ecmascript)",
"[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)",
"[project]/src/collect-result.js [app-rsc] (ecmascript) <locals>", "[project]/src/collect-result.js [app-rsc] (ecmascript)",
"[project]/src/shared-pages-client.js [client] (ecmascript)", "[project]/src/shared-pages-client.js [client] (ecmascript)",
"[project]/src/shared-pages-client.js [ssr] (ecmascript)", "[project]/src/shared-pages-client.js [ssr] (ecmascript)",
] ]
...@@ -198,7 +198,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -198,7 +198,7 @@ import { nextTestSetup } from 'e2e-utils'
"[project]/src/app/client/shared-app-client.js [app-ssr] (ecmascript)", "[project]/src/app/client/shared-app-client.js [app-ssr] (ecmascript)",
"[project]/src/app/layout-target.js [app-rsc] (ecmascript)", "[project]/src/app/layout-target.js [app-rsc] (ecmascript)",
"[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)",
"[project]/src/collect-result.js [app-rsc] (ecmascript) <locals>", "[project]/src/collect-result.js [app-rsc] (ecmascript)",
"[project]/src/shared-pages-client.js [client] (ecmascript)", "[project]/src/shared-pages-client.js [client] (ecmascript)",
"[project]/src/shared-pages-client.js [ssr] (ecmascript)", "[project]/src/shared-pages-client.js [ssr] (ecmascript)",
] ]
...@@ -260,7 +260,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -260,7 +260,7 @@ import { nextTestSetup } from 'e2e-utils'
"[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)",
"[project]/src/app/rsc/shared-page/index.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-page/index.js [app-rsc] (ecmascript)",
"[project]/src/app/rsc/shared-page/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-page/target.js [app-rsc] (ecmascript)",
"[project]/src/collect-result.js [app-rsc] (ecmascript) <locals>", "[project]/src/collect-result.js [app-rsc] (ecmascript)",
"[project]/src/shared-pages-client.js [client] (ecmascript)", "[project]/src/shared-pages-client.js [client] (ecmascript)",
"[project]/src/shared-pages-client.js [ssr] (ecmascript)", "[project]/src/shared-pages-client.js [ssr] (ecmascript)",
] ]
...@@ -322,7 +322,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -322,7 +322,7 @@ import { nextTestSetup } from 'e2e-utils'
"[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)",
"[project]/src/app/rsc/shared-page/index.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-page/index.js [app-rsc] (ecmascript)",
"[project]/src/app/rsc/shared-page/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-page/target.js [app-rsc] (ecmascript)",
"[project]/src/collect-result.js [app-rsc] (ecmascript) <locals>", "[project]/src/collect-result.js [app-rsc] (ecmascript)",
"[project]/src/shared-pages-client.js [client] (ecmascript)", "[project]/src/shared-pages-client.js [client] (ecmascript)",
"[project]/src/shared-pages-client.js [ssr] (ecmascript)", "[project]/src/shared-pages-client.js [ssr] (ecmascript)",
] ]
...@@ -380,7 +380,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -380,7 +380,7 @@ import { nextTestSetup } from 'e2e-utils'
"[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)",
"[project]/src/app/rsc/shared-page/index.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-page/index.js [app-rsc] (ecmascript)",
"[project]/src/app/rsc/shared-page/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-page/target.js [app-rsc] (ecmascript)",
"[project]/src/collect-result.js [app-rsc] (ecmascript) <locals>", "[project]/src/collect-result.js [app-rsc] (ecmascript)",
"[project]/src/shared-pages-client.js [client] (ecmascript)", "[project]/src/shared-pages-client.js [client] (ecmascript)",
"[project]/src/shared-pages-client.js [ssr] (ecmascript)", "[project]/src/shared-pages-client.js [ssr] (ecmascript)",
] ]
...@@ -432,7 +432,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -432,7 +432,7 @@ import { nextTestSetup } from 'e2e-utils'
"[project]/src/app/client/shared-app-client.js [app-client] (ecmascript)", "[project]/src/app/client/shared-app-client.js [app-client] (ecmascript)",
"[project]/src/app/client/shared-app-client.js [app-ssr] (ecmascript)", "[project]/src/app/client/shared-app-client.js [app-ssr] (ecmascript)",
"[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)",
"[project]/src/collect-result.js [ssr] (ecmascript) <locals>", "[project]/src/collect-result.js [ssr] (ecmascript)",
"[project]/src/pages-lib/a/lib.js [ssr] (ecmascript)", "[project]/src/pages-lib/a/lib.js [ssr] (ecmascript)",
"[project]/src/pages-lib/a/unique.js [ssr] (ecmascript)", "[project]/src/pages-lib/a/unique.js [ssr] (ecmascript)",
"[project]/src/pages/pages/a.js [ssr] (ecmascript, collect, my-test)", "[project]/src/pages/pages/a.js [ssr] (ecmascript, collect, my-test)",
...@@ -487,7 +487,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -487,7 +487,7 @@ import { nextTestSetup } from 'e2e-utils'
"[project]/src/app/client/shared-app-client.js [app-client] (ecmascript)", "[project]/src/app/client/shared-app-client.js [app-client] (ecmascript)",
"[project]/src/app/client/shared-app-client.js [app-ssr] (ecmascript)", "[project]/src/app/client/shared-app-client.js [app-ssr] (ecmascript)",
"[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)", "[project]/src/app/rsc/shared-app/target.js [app-rsc] (ecmascript)",
"[project]/src/collect-result.js [ssr] (ecmascript) <locals>", "[project]/src/collect-result.js [ssr] (ecmascript)",
"[project]/src/pages-lib/client-only/lib.js [ssr] (ecmascript, next/dynamic entry, async loader)", "[project]/src/pages-lib/client-only/lib.js [ssr] (ecmascript, next/dynamic entry, async loader)",
"[project]/src/pages-lib/client-only/unique.js [ssr] (ecmascript)", "[project]/src/pages-lib/client-only/unique.js [ssr] (ecmascript)",
"[project]/src/pages/pages/client-only.js [ssr] (ecmascript, collect, my-test)", "[project]/src/pages/pages/client-only.js [ssr] (ecmascript, collect, my-test)",
......
...@@ -322,21 +322,11 @@ impl EcmascriptExports { ...@@ -322,21 +322,11 @@ impl EcmascriptExports {
/// Returns whether this module should be split into separate locals and facade modules. /// Returns whether this module should be split into separate locals and facade modules.
/// ///
/// Splitting happens for either of two reasons: /// Splitting is enabled for modules with re-exports (star exports or imported bindings),
/// /// allowing tree shaking to separate local definitions from re-exports. Do not split a
/// 1. The module has re-exports (star exports or imported bindings), which lets the /// local-only module merely to mangle its export names: dynamic imports would resolve to the
/// tree-shaking optimization separate local definitions from re-exports. /// facade while static named imports follow through to the locals module, giving a shared
/// 2. Its export names may be mangled. The facade then keeps the original names for whatever /// module two different runtime identities.
/// reads this module from outside, while the locals module carries the mangled ones — see
/// the `references::esm::mangle` module.
///
/// A module with **no exports at all** is never split, even when mangling is enabled: there is
/// nothing to mangle or tree shake, and splitting is actively unsafe. Such a facade's only edge
/// to the original code would be the `ExportUsage::evaluation` reference from
/// `EcmascriptModuleFacadeModule::specific_references`, which `BindingUsageInfo` prunes when
/// the target looks side-effect free — as a `"sideEffects": false` package declaration
/// makes it, even for a body that has side effects. Unsplit, the module runs as a chunk
/// group entry regardless.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn split_locals_and_reexports(&self) -> Result<Vc<bool>> { pub async fn split_locals_and_reexports(&self) -> Result<Vc<bool>> {
Ok(match self { Ok(match self {
...@@ -349,11 +339,9 @@ impl EcmascriptExports { ...@@ -349,11 +339,9 @@ impl EcmascriptExports {
EsmExport::ImportedBinding(..) | EsmExport::ImportedNamespace(_) EsmExport::ImportedBinding(..) | EsmExport::ImportedNamespace(_)
) )
}); });
// `has_reexports` already implies a non-empty export list, so the emptiness check // TODO: Re-enable mangling-only facade splits once remote-components consumers
// only needs to guard the mangling case. // can share a singleton across dynamic facade and static locals imports (#99279).
Vc::cell( Vc::cell(has_reexports)
has_reexports || (exports.mangle_export_names && !exports.exports.is_empty()),
)
} }
_ => Vc::cell(false), _ => Vc::cell(false),
}) })
......
...@@ -33,49 +33,35 @@ it('exported lets are live', () => { ...@@ -33,49 +33,35 @@ it('exported lets are live', () => {
}) })
// Whether a binding is emitted as a plain value or as a getter is decided by the module that // Whether a binding is emitted as a plain value or as a getter is decided by the module that
// *owns* the binding. These modules have their exports mangled, which splits each of them into a // *owns* it. A local-only module is no longer split merely for export mangling, so its namespace
// facade plus a `<locals>` module (see `EcmascriptExports::split_locals_and_reexports`); the // is the right place to inspect the emitted property descriptors. Look up its exact module ID to
// bindings live on the locals module, and the facade only re-exposes them under their original // avoid accidentally testing a re-export facade instead of the original module.
// names. function moduleNamespaceOf(fileName) {
// const suffix = `exports/live/input/${fileName} [test] (ecmascript)`
// That re-exposure is unconditionally a getter -- a facade forwards a name it does not own, so it
// cannot know the binding is never reassigned. Asserting on the namespace object of `import * as`
// therefore only ever observes the facade and says nothing about the optimization. These tests
// reach past it to the locals module, which is what actually decides value-vs-getter.
function localsNamespaceOf(fileName) {
const suffix = `exports/live/input/${fileName} [test] (ecmascript) <locals>`
const id = Array.from(__turbopack_modules__.keys()).find((m) => const id = Array.from(__turbopack_modules__.keys()).find((m) =>
m.endsWith(suffix) m.endsWith(suffix)
) )
// Not a soft check: without the split there is no locals module to inspect, and every
// assertion below would be silently testing the facade instead.
expect(id).toEqual(expect.stringContaining(suffix)) expect(id).toEqual(expect.stringContaining(suffix))
return __turbopack_import__(id) return __turbopack_import__(id)
} }
it('exported bindings that are not mutated are not live', () => { it('exported bindings that are not mutated are not live', () => {
const locals = localsNamespaceOf('live_exports.js') const ns = moduleNamespaceOf('live_exports.js')
// Mangled keys, so look up what each original name was emitted as.
const info = liveExports.exportsInfo const info = liveExports.exportsInfo
// This module's export keys can still be mangled when all reads are statically known.
expectValue(locals, info.neverMutated.mangledName, 'neverMutated') expectValue(ns, info.neverMutated.mangledName, 'neverMutated')
expectValue( expectValue(
locals, ns,
info.obviouslyneverMutated.mangledName, info.obviouslyneverMutated.mangledName,
'obviouslyneverMutated' 'obviouslyneverMutated'
) )
// `const_default_export_function.js` exports only `default`, and a module with a single export const constDefaultNs = moduleNamespaceOf('const_default_export_function.js')
// to mangle always emits it under the same fixed key, so its namespace has exactly one own const keys = Object.keys(constDefaultNs)
// enumerable property and no lookup table is needed to find it.
const constDefaultLocals = localsNamespaceOf(
'const_default_export_function.js'
)
const keys = Object.keys(constDefaultLocals)
expect(keys).toHaveLength(1) expect(keys).toHaveLength(1)
expectValue(constDefaultLocals, keys[0], expect.any(Function)) expectValue(constDefaultNs, keys[0], expect.any(Function))
// The values are still reachable under the original names through the facade. // The values are still reachable under the original names.
expect(liveExports.neverMutated).toBe('neverMutated') expect(liveExports.neverMutated).toBe('neverMutated')
expect(liveExports.obviouslyneverMutated).toBe('obviouslyneverMutated') expect(liveExports.obviouslyneverMutated).toBe('obviouslyneverMutated')
expect(constDefaultExportFunction.default).toEqual(expect.any(Function)) expect(constDefaultExportFunction.default).toEqual(expect.any(Function))
...@@ -83,12 +69,11 @@ it('exported bindings that are not mutated are not live', () => { ...@@ -83,12 +69,11 @@ it('exported bindings that are not mutated are not live', () => {
it('exported bindings that are free vars are live', () => { it('exported bindings that are free vars are live', () => {
// Reading `g` here is also what keeps it alive: export usage is tracked per name, so an export // Reading `g` here is also what keeps it alive: export usage is tracked per name, so an export
// this file never mentions is dropped from the locals module altogether and would have no // this file never mentions can be dropped and would have no descriptor left to inspect.
// descriptor left to inspect.
expect(liveExports.g).toBe(globalThis) expect(liveExports.g).toBe(globalThis)
const locals = localsNamespaceOf('live_exports.js') const ns = moduleNamespaceOf('live_exports.js')
expectGetter(locals, liveExports.exportsInfo.g.mangledName) expectGetter(ns, liveExports.exportsInfo.g.mangledName)
}) })
function expectValue(ns, propName, value) { function expectValue(ns, propName, value) {
......
...@@ -26,9 +26,7 @@ export function setDefaultFunction(f) { ...@@ -26,9 +26,7 @@ export function setDefaultFunction(f) {
defaultFunction = f defaultFunction = f
} }
// Test-only. `index.js` inspects this module's *locals* module directly (see the comment there), // Test-only. `index.js` inspects this module's namespace descriptors directly. The keys may
// whose export keys are mangled. `__webpack_exports_info__` is the only way to learn which key an // still be mangled when every read is statically known; `__webpack_exports_info__` reports the
// original export name ended up under: it is keyed by the original names and reports each one's // emitted key for each original name.
// emitted key as `mangledName`. Evaluated here rather than in `index.js` because it always
// describes the exports of the module it appears in.
export const exportsInfo = __webpack_exports_info__ export const exportsInfo = __webpack_exports_info__
...@@ -8,10 +8,9 @@ it('should keep a CommonJS consumer of an ESM module working', () => { ...@@ -8,10 +8,9 @@ it('should keep a CommonJS consumer of an ESM module working', () => {
expect(someLongExportName).toBe('esm-1') expect(someLongExportName).toBe('esm-1')
}) })
it('should keep a CommonJS consumer working against a mangled module', () => { it('should keep a CommonJS consumer working with original export names', () => {
// The `esm.someLongExportName` accesses in `cjs-consumer.js` are user source, so what the // The `esm.someLongExportName` accesses in `cjs-consumer.js` are user source, so the unsplit
// CommonJS module receives has to keep carrying the original names. It does: the facade // module must keep the original keys on its namespace object.
// materializes them, forwarding to the mangled keys of the locals module underneath. expect(exportsInfo.someLongExportName.canMangle).toBe(false)
expect(exportsInfo.someLongExportName.canMangle).toBe(true)
expect(keys()).toContain('someLongExportName') expect(keys()).toContain('someLongExportName')
}) })
...@@ -21,16 +21,9 @@ it('should handle `default` when destructuring a namespace', () => { ...@@ -21,16 +21,9 @@ it('should handle `default` when destructuring a namespace', () => {
expect(value).toBe('default-value') expect(value).toBe('default-value')
}) })
it('should mangle a module read through a namespace binding', () => { it('should preserve original names when a namespace binding escapes', () => {
// A namespace-imported module is still mangled internally. The facade keeps the original names // Without a facade split, the module keeps its public keys because callers can read the
// for whoever reads the namespace object, and forwards them to the mangled keys of the locals // namespace object by its original export names.
// module — so the reads above (member access, destructuring, `default`) all stay correct while expect(mod.exportsInfo.aVeryLongExportName.canMangle).toBe(false)
// the emitted keys get shorter. expect(mod.exportsInfo.aVeryLongExportName.mangledName).toBeNull()
expect(mod.exportsInfo.aVeryLongExportName.canMangle).toBe(true)
expect(mod.exportsInfo.aVeryLongExportName.mangledName).toEqual(
expect.any(String)
)
expect(
mod.exportsInfo.aVeryLongExportName.mangledName.length
).toBeLessThanOrEqual(2)
}) })
...@@ -64,12 +64,10 @@ it('should preserve namespace reads through an export-star forwarding edge', () ...@@ -64,12 +64,10 @@ it('should preserve namespace reads through an export-star forwarding edge', ()
it('should preserve namespace names through a multi-hop diamond', () => { it('should preserve namespace names through a multi-hop diamond', () => {
expect(readMultiHopDiamond()).toEqual(['multi-hop', 'multi-hop']) expect(readMultiHopDiamond()).toEqual(['multi-hop', 'multi-hop'])
// The materialized facade preserves the original public name, so the locals module remains // Without a mangling-only facade, the escaped namespace and named consumers use the original
// mangleable even though the namespace escapes through multiple forwarding edges. // export keys from the same source module.
expect(multiHopExportsInfo.veryLongMultiHopExportName.canMangle).toBe(true) expect(multiHopExportsInfo.veryLongMultiHopExportName.canMangle).toBe(false)
expect(multiHopExportsInfo.veryLongMultiHopExportName.mangledName).not.toBe( expect(multiHopExportsInfo.veryLongMultiHopExportName.mangledName).toBeNull()
'veryLongMultiHopExportName'
)
// The escaped namespace must still expose the ORIGINAL names, both by enumeration and by // The escaped namespace must still expose the ORIGINAL names, both by enumeration and by
// dynamic (non-statically-analyzable) key access. // dynamic (non-statically-analyzable) key access.
expect(enumerateMultiHopNamespace()).toEqual([ expect(enumerateMultiHopNamespace()).toEqual([
......
it('should mangle a module imported with webpackExports', async () => { it('should preserve names for a module imported with webpackExports', async () => {
const { usedName, exportsInfo } = await import( const { usedName, exportsInfo } = await import(
/* webpackExports: ["usedName", "exportsInfo"] */ './lazy' /* webpackExports: ["usedName", "exportsInfo"] */ './lazy'
) )
expect(usedName).toBe('used') expect(usedName).toBe('used')
// The magic comment narrows which exports are used. The module is still mangled internally — // The comment narrows which exports are used, but the promise still exposes a namespace
// the namespace object the `import()` resolves to is materialized by the facade under the // object with the original names. Without a facade, the target cannot mangle those keys.
// original names, forwarding to the mangled keys. expect(exportsInfo.usedName.canMangle).toBe(false)
expect(exportsInfo.usedName.canMangle).toBe(true) expect(exportsInfo.usedName.mangledName).toBeNull()
expect(exportsInfo.usedName.mangledName).toEqual(expect.any(String))
expect(exportsInfo.usedName.mangledName).not.toBe('usedName')
}) })
it('should mangle a module imported with turbopackExports', async () => { it('should preserve names for a module imported with turbopackExports', async () => {
const ns = await import( const ns = await import(
/* turbopackExports: ["otherUsedName", "exportsInfo"] */ './lazy' /* turbopackExports: ["otherUsedName", "exportsInfo"] */ './lazy'
) )
expect(ns.otherUsedName).toBe('other-used') expect(ns.otherUsedName).toBe('other-used')
expect(ns.exportsInfo.otherUsedName.canMangle).toBe(true) expect(ns.exportsInfo.otherUsedName.canMangle).toBe(false)
}) })
it('should keep a plain dynamic import working', async () => { it('should keep a plain dynamic import working', async () => {
const ns = await import('./lazy') const ns = await import('./lazy')
expect(ns.usedName).toBe('used') expect(ns.usedName).toBe('used')
// The materialized namespace object still exposes the original names. // The unsplit namespace object still exposes the original names.
expect(Object.keys(ns)).toContain('usedName') expect(Object.keys(ns)).toContain('usedName')
}) })
...@@ -31,12 +31,12 @@ it('should keep all 60 values correct through the mangled keys', () => { ...@@ -31,12 +31,12 @@ it('should keep all 60 values correct through the mangled keys', () => {
expect(values()).toEqual(expected) expect(values()).toEqual(expected)
}) })
it('should keep computed-key reads working on a mangled module', () => { it('should keep computed-key namespace reads working without mangling', () => {
for (let i = 0; i < 60; i++) { for (let i = 0; i < 60; i++) {
const n = String(i).padStart(2, '0') const n = String(i).padStart(2, '0')
expect(manyAsNamespace[`exportNumber${n}`]).toBe(`value-${n}`) expect(manyAsNamespace[`exportNumber${n}`]).toBe(`value-${n}`)
} }
// The module is mangled internally, but the computed reads go through the namespace object the // The namespace-imported module keeps its public names; the separate named-only module
// facade materializes, which still carries every original name. // checked above remains eligible for mangling.
expect(manyAsNamespace.exportsInfo.exportNumber00.canMangle).toBe(true) expect(manyAsNamespace.exportsInfo.exportNumber00.canMangle).toBe(false)
}) })
// A module whose namespace object escapes is still mangled, the way webpack does it: the internal // A local-only module should keep one identity when its namespace escapes: splitting it merely
// export keys are shortened, and the namespace object handed to user code is *materialized* by the // for export mangling makes a dynamic import target a facade while named imports use locals.
// facade, mapping the original names onto those keys. One `import * as ns` in a dependency no // The public namespace must still expose the original export names.
// longer un-mangles the whole module.
//
// Compare webpack's `test/configCases/mangle/mangle-escaping-namespace`, which asserts both halves:
// the raw export is not defined under its original name, and the materialized namespace object
// exposes the original name.
import { ENUM_A, exportsInfo } from './enums'
import { getEnums } from './provider' import { getEnums } from './provider'
it('should still mangle a module whose namespace escapes', () => { it('should preserve an escaped namespace without mangling its local exports', () => {
const ns = getEnums() const ns = getEnums()
// The namespace object keeps the original names... // The namespace object keeps the original names...
expect(ns.ENUM_A).toBe('a-value') expect(ns.ENUM_A).toBe('a-value')
...@@ -22,10 +18,32 @@ it('should still mangle a module whose namespace escapes', () => { ...@@ -22,10 +18,32 @@ it('should still mangle a module whose namespace escapes', () => {
'exportsInfo', 'exportsInfo',
]) ])
// ...while the module's own export keys are shortened underneath. // A named import and the namespace refer to the same local value. The namespace escape
expect(ns.exportsInfo.ENUM_A.canMangle).toBe(true) // prevents the unsplit module's export keys from being mangled.
expect(ns.exportsInfo.ENUM_A.mangledName).not.toBe('ENUM_A') expect(ENUM_A).toBe(ns.ENUM_A)
expect(ns.exportsInfo.ENUM_B.mangledName).not.toBe( expect(exportsInfo).toBe(ns.exportsInfo)
ns.exportsInfo.ENUM_A.mangledName expect(exportsInfo.ENUM_A.canMangle).toBe(false)
) expect(exportsInfo.ENUM_A.mangledName).toBeNull()
expect(exportsInfo.ENUM_B.canMangle).toBe(false)
})
it('should keep dynamic and static imports of local exports consistent', async () => {
const dynamic = await import('./enums')
// The dynamic import and named import must not create facade and locals IDs for this file.
const moduleName =
'exports/mangle-materialized-namespace/input/enums.js [test] (ecmascript)'
expect(
Array.from(__turbopack_modules__.keys()).filter((id) =>
id.includes(moduleName)
)
).toEqual([expect.stringContaining(moduleName)])
expect(dynamic.ENUM_A).toBe(ENUM_A)
expect(dynamic.exportsInfo).toBe(exportsInfo)
expect(Object.keys(dynamic).sort()).toEqual([
'ENUM_A',
'ENUM_B',
'default',
'exportsInfo',
])
expect(dynamic.exportsInfo.ENUM_A.canMangle).toBe(false)
}) })
(()=>{"use strict";module.exports=["[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js [test] (ecmascript)",s=>{s.S(["[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js [test] (ecmascript) <locals>","anotherLongExportName,T,someLongExportName,z"])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js [test] (ecmascript) <locals>",s=>{s.s(["T",0,"long-2","z",0,"long-1"])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/index.js [test] (ecmascript)",s=>{var t=s.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js [test] (ecmascript)"),e=s.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/mangled.js [test] (ecmascript) <locals>");console.log(t,e.z,e.T),s.s([])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/mangled.js [test] (ecmascript) <locals>",s=>{s.s(["T",0,"mangled-2","z",0,"mangled-1"])}]})();
//# sourceMappingURL=1jsg_tests_snapshot_mangle-exports_escaping-namespace_input_1bvx6nn6m4t6e._.js.map
\ No newline at end of file
(()=>{"use strict";module.exports=["[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js [test] (ecmascript)",t=>{t.s(["anotherLongExportName",0,"long-2","someLongExportName",0,"long-1"])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/index.js [test] (ecmascript)",t=>{var s=t.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js [test] (ecmascript)"),e=t.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/mangled.js [test] (ecmascript)");console.log(s,e.z,e.T),t.s([])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/mangled.js [test] (ecmascript)",t=>{t.s(["T",0,"mangled-2","z",0,"mangled-1"])}]})();
//# sourceMappingURL=1jsg_tests_snapshot_mangle-exports_escaping-namespace_input_1ysta134r3dyz._.js.map
\ No newline at end of file
{"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/index.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/mangled.js"],"sourcesContent":["// A namespace object of this module escapes, so it keeps its original export names.\nexport const someLongExportName = 'long-1'\nexport const anotherLongExportName = 'long-2'\n","import * as escaping from './escaping.js'\nimport { someLongExportName, anotherLongExportName } from './mangled.js'\n\nconst leak = () => escaping\n\nconsole.log(leak(), someLongExportName, anotherLongExportName)\n","// Nothing escapes here, so this module in the same graph is still mangled.\nexport const someLongExportName = 'mangled-1'\nexport const anotherLongExportName = 'mangled-2'\n"],"names":["someLongExportName","anotherLongExportName","leak","imported module [project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js [test] (ecmascript)","console","log"],"mappings":"2gBAEqC,eADH,iJCDlC,IAAA,EAAA,EAAA,CAAA,CAAA,qIACA,EAAA,EAAA,CAAA,CAAA,6IAIAI,QAAQC,GAAG,CAFQF,AAEPD,EAAQ,EAAA,CAAkB,CAAE,EAAA,CAAqB,sKCHxB,kBADH"} {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/index.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/mangled.js"],"sourcesContent":["// A namespace object of this module escapes, so it keeps its original export names.\nexport const someLongExportName = 'long-1'\nexport const anotherLongExportName = 'long-2'\n","import * as escaping from './escaping.js'\nimport { someLongExportName, anotherLongExportName } from './mangled.js'\n\nconst leak = () => escaping\n\nconsole.log(leak(), someLongExportName, anotherLongExportName)\n","// Nothing escapes here, so this module in the same graph is still mangled.\nexport const someLongExportName = 'mangled-1'\nexport const anotherLongExportName = 'mangled-2'\n"],"names":["someLongExportName","anotherLongExportName","leak","imported module [project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/escaping.js [test] (ecmascript)","console","log"],"mappings":"0MAEqC,gCADH,iJCDlC,IAAA,EAAA,EAAA,CAAA,CAAA,qIACA,EAAA,EAAA,CAAA,CAAA,oIAIAI,QAAQC,GAAG,CAFQF,AAEPD,EAAQ,EAAA,CAAkB,CAAE,EAAA,CAAqB,6JCHxB,kBADH"}
\ No newline at end of file \ No newline at end of file
var R=require("./[turbopack]_runtime.js")("output/index.entry.js") var R=require("./[turbopack]_runtime.js")("output/index.entry.js")
R.c("output/1jsg_tests_snapshot_mangle-exports_escaping-namespace_input_1bvx6nn6m4t6e._.js") R.c("output/1jsg_tests_snapshot_mangle-exports_escaping-namespace_input_1ysta134r3dyz._.js")
R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/index.js [test] (ecmascript)") R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/index.js [test] (ecmascript)")
module.exports=R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/index.js [test] (ecmascript)").exports module.exports=R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/escaping-namespace/input/index.js [test] (ecmascript)").exports
\ No newline at end of file
(()=>{"use strict";module.exports=["[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/index.js [test] (ecmascript)",t=>{var s=t.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/lib.js [test] (ecmascript) <locals>");console.log(s.a,s.$,s.z,s.T,(0,s.X)()),t.s([])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/lib.js [test] (ecmascript) <locals>",t=>{t.s(["a",0,"a","X",0,function(){return"fn"},"T",0,"long-2","$",0,"bb","z",0,"long-1"])}]})(); (()=>{"use strict";module.exports=["[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/index.js [test] (ecmascript)",t=>{var e=t.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/lib.js [test] (ecmascript)");console.log(e.a,e.$,e.z,e.T,(0,e.X)()),t.s([])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/lib.js [test] (ecmascript)",t=>{t.s(["a",0,"a","X",0,function(){return"fn"},"T",0,"long-2","$",0,"bb","z",0,"long-1"])}]})();
//# sourceMappingURL=1jsg_tests_snapshot_mangle-exports_named-and-preserved_input_1si6yfvbtud7_._.js.map //# sourceMappingURL=1jsg_tests_snapshot_mangle-exports_named-and-preserved_input_0pbreftkp_5pc._.js.map
\ No newline at end of file \ No newline at end of file
{"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/index.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/lib.js"],"sourcesContent":["import {\n a,\n bb,\n someLongExportName,\n anotherLongExportName,\n aLongFunctionName,\n} from './lib.js'\n\nconsole.log(\n a,\n bb,\n someLongExportName,\n anotherLongExportName,\n aLongFunctionName()\n)\n","export const a = 'a'\nexport const bb = 'bb'\nexport const someLongExportName = 'long-1'\nexport const anotherLongExportName = 'long-2'\nexport const unusedLongExportName = 'unused'\n\nexport function aLongFunctionName() {\n return 'fn'\n}\n"],"names":["console","log","a","bb","someLongExportName","anotherLongExportName","unusedLongExportName","aLongFunctionName"],"mappings":"yKAAA,IAAA,EAAA,EAAA,CAAA,CAAA,0IAQAA,QAAQC,GAAG,CACT,EAAA,CAAC,CACD,EAAA,CAAE,CACF,EAAA,CAAkB,CAClB,EAAA,CAAqB,CACrB,CAAA,EAAA,EAAA,CAAA,AAAiB,sKCbF,UAMV,SAASM,EACd,MAAO,IACT,QALqC,eAFnB,WACgB"} {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/index.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/lib.js"],"sourcesContent":["import {\n a,\n bb,\n someLongExportName,\n anotherLongExportName,\n aLongFunctionName,\n} from './lib.js'\n\nconsole.log(\n a,\n bb,\n someLongExportName,\n anotherLongExportName,\n aLongFunctionName()\n)\n","export const a = 'a'\nexport const bb = 'bb'\nexport const someLongExportName = 'long-1'\nexport const anotherLongExportName = 'long-2'\nexport const unusedLongExportName = 'unused'\n\nexport function aLongFunctionName() {\n return 'fn'\n}\n"],"names":["console","log","a","bb","someLongExportName","anotherLongExportName","unusedLongExportName","aLongFunctionName"],"mappings":"yKAAA,IAAA,EAAA,EAAA,CAAA,CAAA,iIAQAA,QAAQC,GAAG,CACT,EAAA,CAAC,CACD,EAAA,CAAE,CACF,EAAA,CAAkB,CAClB,EAAA,CAAqB,CACrB,CAAA,EAAA,EAAA,CAAA,AAAiB,6JCbF,UAMV,SAASM,EACd,MAAO,IACT,QALqC,eAFnB,WACgB"}
\ No newline at end of file \ No newline at end of file
var R=require("./[turbopack]_runtime.js")("output/index.entry.js") var R=require("./[turbopack]_runtime.js")("output/index.entry.js")
R.c("output/1jsg_tests_snapshot_mangle-exports_named-and-preserved_input_1si6yfvbtud7_._.js") R.c("output/1jsg_tests_snapshot_mangle-exports_named-and-preserved_input_0pbreftkp_5pc._.js")
R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/index.js [test] (ecmascript)") R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/index.js [test] (ecmascript)")
module.exports=R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/index.js [test] (ecmascript)").exports module.exports=R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/named-and-preserved/input/index.js [test] (ecmascript)").exports
\ No newline at end of file
(()=>{"use strict";module.exports=["[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/index.js [test] (ecmascript)",t=>{console.log((0,t.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/lone.js [test] (ecmascript) <locals>").f)()),t.s([])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/lone.js [test] (ecmascript) <locals>",t=>{t.s(["f",0,function(){return"lone"}])}]})(); (()=>{"use strict";module.exports=["[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/index.js [test] (ecmascript)",t=>{console.log((0,t.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/lone.js [test] (ecmascript)").f)()),t.s([])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/lone.js [test] (ecmascript)",t=>{t.s(["f",0,function(){return"lone"}])}]})();
//# sourceMappingURL=0_9x_turbopack-tests_tests_snapshot_mangle-exports_single-export_input_1xad2kk3iii3x._.js.map //# sourceMappingURL=0_9x_turbopack-tests_tests_snapshot_mangle-exports_single-export_input_1mf78g4ogdhgv._.js.map
\ No newline at end of file \ No newline at end of file
{"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/index.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/lone.js"],"sourcesContent":["import { aLongFunctionNameNobodyWantsInTheBundle } from './lone'\n\nconsole.log(aLongFunctionNameNobodyWantsInTheBundle())\n","// The only export of this module. It is emitted under the fixed single-export key rather than a\n// hashed one, so that every single-export module in the graph produces the same `.f` byte sequence\n// and gzip can share it. Visible in the committed output below.\nexport function aLongFunctionNameNobodyWantsInTheBundle() {\n return 'lone'\n}\n"],"names":["console","log","aLongFunctionNameNobodyWantsInTheBundle"],"mappings":"mKAEAA,QAAQC,GAAG,CAAC,CAAA,EAFZ,AAEY,EAFZ,CAAA,CAAA,qIAEY,CAAA,AAAuC,iKCC5C,SAASC,EACd,MAAO,MACT"} {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/index.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/lone.js"],"sourcesContent":["import { aLongFunctionNameNobodyWantsInTheBundle } from './lone'\n\nconsole.log(aLongFunctionNameNobodyWantsInTheBundle())\n","// The only export of this module. It is emitted under the fixed single-export key rather than a\n// hashed one, so that every single-export module in the graph produces the same `.f` byte sequence\n// and gzip can share it. Visible in the committed output below.\nexport function aLongFunctionNameNobodyWantsInTheBundle() {\n return 'lone'\n}\n"],"names":["console","log","aLongFunctionNameNobodyWantsInTheBundle"],"mappings":"mKAEAA,QAAQC,GAAG,CAAC,CAAA,EAFZ,AAEY,EAFZ,CAAA,CAAA,4HAEY,CAAA,AAAuC,wJCC5C,SAASC,EACd,MAAO,MACT"}
\ No newline at end of file \ No newline at end of file
var R=require("./[turbopack]_runtime.js")("output/index.entry.js") var R=require("./[turbopack]_runtime.js")("output/index.entry.js")
R.c("output/0_9x_turbopack-tests_tests_snapshot_mangle-exports_single-export_input_1xad2kk3iii3x._.js") R.c("output/0_9x_turbopack-tests_tests_snapshot_mangle-exports_single-export_input_1mf78g4ogdhgv._.js")
R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/index.js [test] (ecmascript)") R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/index.js [test] (ecmascript)")
module.exports=R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/index.js [test] (ecmascript)").exports module.exports=R.m("[project]/turbopack/crates/turbopack-tests/tests/snapshot/mangle-exports/single-export/input/index.js [test] (ecmascript)").exports
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment