Unverified Commit 614443ce authored by Andrew Imm's avatar Andrew Imm Committed by GitHub

Turbopack: add all keys to dynamic exports before sealing the object (#93334)

User surfaced an issue where a barrel file had `export * from` a chain
of CJS files. In this barrel-file-exports + dynamic-exports case, the
Turbopack runtime wraps `module.exports` in a `Proxy` whose `ownKeys`
trap synthesizes keys from the re-exported namespace. The constructed
proxy conflicts with `Object.seal`, which `esm()` calls to ensure the
namespace is immutable (per spec).
When you try to execute the code in the browser or for build, you get a
`TypeError: 'ownKeys' on proxy: trap returned extra keys but proxy
target is non-extensible`

I've constructed a test case that fails today. Nothing about this code
is "wrong" -- a CJS module gets imported and re-exported, and `Page.tsx`
does an `import *` from that file. But `ownKeys` returns something
different from the defined properties, which causes the JS engine to
trigger an exception.

The solution is to remove the Object.seal() for dynamic modules.
Seal does two things:
* Prevent extension of the object
* Set each key's [[Configurable]] property to false

We want this for true ESModules, but for dynamic exports we need to allow flexibility. We do this by passing the dynamic flag to the codgen, and avoiding the seal call in those cases. We use Proxy traps to ensure exported values can't be modified or deleted externally, while still allowing the original dynamic module to change them.
While investigating, I also found that Object.keys(importNamespace) would miss re-exported dynamic keys. Similarly, 'exportName' in starImports would always return false for dynamic imports. Since we're already touching this code, I also fixed those cases.

Test cases cover the scenarios that seemed relevant to real user code, as well as the invariants we care about.
parent 143926d6
...@@ -171,7 +171,7 @@ type EsmBindings = Array< ...@@ -171,7 +171,7 @@ type EsmBindings = Array<
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ */
function esm(exports: Exports, bindings: EsmBindings) { function esm(exports: Exports, bindings: EsmBindings, dynamic?: boolean) {
defineProp(exports, '__esModule', { value: true }) defineProp(exports, '__esModule', { value: true })
if (toStringTag) defineProp(exports, toStringTag, { value: 'Module' }) if (toStringTag) defineProp(exports, toStringTag, { value: 'Module' })
let i = 0 let i = 0
...@@ -205,7 +205,14 @@ function esm(exports: Exports, bindings: EsmBindings) { ...@@ -205,7 +205,14 @@ function esm(exports: Exports, bindings: EsmBindings) {
} }
} }
} }
Object.seal(exports) // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports)
} }
/** /**
...@@ -214,7 +221,8 @@ function esm(exports: Exports, bindings: EsmBindings) { ...@@ -214,7 +221,8 @@ function esm(exports: Exports, bindings: EsmBindings) {
function esmExport( function esmExport(
this: TurbopackBaseContext<Module>, this: TurbopackBaseContext<Module>,
bindings: EsmBindings, bindings: EsmBindings,
id: ModuleId | undefined id: ModuleId | undefined,
dynamic?: boolean
) { ) {
let module: Module let module: Module
let exports: Module['exports'] let exports: Module['exports']
...@@ -226,7 +234,7 @@ function esmExport( ...@@ -226,7 +234,7 @@ function esmExport(
exports = this.e exports = this.e
} }
module.namespaceObject = exports module.namespaceObject = exports
esm(exports, bindings) esm(exports, bindings, dynamic)
} }
contextPrototype.s = esmExport contextPrototype.s = esmExport
...@@ -240,6 +248,22 @@ function ensureDynamicExports( ...@@ -240,6 +248,22 @@ function ensureDynamicExports(
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, (reexportedObjects = [])) REEXPORTED_OBJECTS.set(module, (reexportedObjects = []))
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop: PropertyKey) => {
if (prop !== 'default') {
for (const obj of reexportedObjects!) {
if (hasOwnProperty.call(obj, prop)) return obj
}
}
return undefined
}
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get(target, prop) { get(target, prop) {
if ( if (
...@@ -249,12 +273,44 @@ function ensureDynamicExports( ...@@ -249,12 +273,44 @@ function ensureDynamicExports(
) { ) {
return Reflect.get(target, prop) return Reflect.get(target, prop)
} }
for (const obj of reexportedObjects!) { const obj = reexportOwning(prop)
const value = Reflect.get(obj, prop) return obj && Reflect.get(obj, prop)
if (value !== undefined) return value },
} // The namespace is read-only, like a real esm namespace object. The
return undefined // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set() {
return false
}, },
defineProperty() {
return false
},
deleteProperty() {
return false
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has(target, prop) {
if (Reflect.has(target, prop)) return true
if (prop === 'default' || prop === '__esModule') return false
return reexportOwning(prop) !== undefined
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys(target) { ownKeys(target) {
const keys = Reflect.ownKeys(target) const keys = Reflect.ownKeys(target)
for (const obj of reexportedObjects!) { for (const obj of reexportedObjects!) {
...@@ -264,6 +320,22 @@ function ensureDynamicExports( ...@@ -264,6 +320,22 @@ function ensureDynamicExports(
} }
return keys return keys
}, },
getOwnPropertyDescriptor(target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop)
if (own || prop === 'default' || prop === '__esModule') return own
const obj = reexportOwning(prop)
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: () => Reflect.get(obj, prop),
}
}
return undefined
},
}) })
} }
return reexportedObjects return reexportedObjects
......
...@@ -875,15 +875,33 @@ impl EsmExports { ...@@ -875,15 +875,33 @@ impl EsmExports {
vec![] vec![]
}; };
// When a module has dynamic re-exports (`export *` from a module whose
// exports are only known at runtime), its namespace object must stay
// extensible so the dynamic export proxy can surface those keys. Signal
// that to the runtime so it skips sealing the namespace.
let has_dynamic_exports = !expanded.dynamic_exports.is_empty();
let esm_exports = vec![CodeGenerationHoistedStmt::new( let esm_exports = vec![CodeGenerationHoistedStmt::new(
rcstr!("__turbopack_esm__"), rcstr!("__turbopack_esm__"),
if let Some(module) = scope_hoisting_context.module() { if let Some(module) = scope_hoisting_context.module() {
let id = module.chunk_item_id(chunking_context).await?; let id = module.chunk_item_id(chunking_context).await?;
if has_dynamic_exports {
quote!("$turbopack_esm($getters, $id, true);" as Stmt,
turbopack_esm: Expr = TURBOPACK_ESM.into(),
getters: Expr = getters,
id: Expr = module_id_to_lit(&id)
)
} else {
quote!("$turbopack_esm($getters, $id);" as Stmt, quote!("$turbopack_esm($getters, $id);" as Stmt,
turbopack_esm: Expr = TURBOPACK_ESM.into(), turbopack_esm: Expr = TURBOPACK_ESM.into(),
getters: Expr = getters, getters: Expr = getters,
id: Expr = module_id_to_lit(&id) id: Expr = module_id_to_lit(&id)
) )
}
} else if has_dynamic_exports {
quote!("$turbopack_esm($getters, undefined, true);" as Stmt,
turbopack_esm: Expr = TURBOPACK_ESM.into(),
getters: Expr = getters
)
} else { } else {
quote!("$turbopack_esm($getters);" as Stmt, quote!("$turbopack_esm($getters);" as Stmt,
turbopack_esm: Expr = TURBOPACK_ESM.into(), turbopack_esm: Expr = TURBOPACK_ESM.into(),
......
export const BUTTON = 'button'
export const PI = 3.14
export const COLOR = '#000000'
import * as barrelNs from './barrel.js'
it('re-exports from a CJS module through a barrel without proxy errors', () => {
// Forces `ownKeys` + per-key `getOwnPropertyDescriptor` on the dynamic-export
// proxy. Before the runtime fix this threw:
// TypeError: 'ownKeys' on proxy: trap returned extra keys but proxy
// target is non-extensible
expect(Object.keys(barrelNs).sort()).toEqual([
'AnotherThing',
'BUTTON',
'COLOR',
'LEGACY_CONST',
'PI',
'mutableValue',
'setMutable',
])
// Spread exercises the same ownKeys + descriptor path.
expect(Object.keys({ ...barrelNs }).sort()).toEqual(
Object.keys(barrelNs).sort()
)
})
it('reflects live value changes a CJS module makes to its own exports', () => {
expect(barrelNs.mutableValue).toBe('before')
barrelNs.setMutable('after')
expect(barrelNs.mutableValue).toBe('after')
})
it('rejects modifying an ESM re-export through the namespace', () => {
expect(() => {
barrelNs.BUTTON = 'changed'
}).toThrow()
expect(barrelNs.BUTTON).toBe('button')
})
it('rejects modifying a CJS re-export through the namespace', () => {
expect(() => {
barrelNs.LEGACY_CONST = 'changed'
}).toThrow()
expect(barrelNs.LEGACY_CONST).toBe('I am dynamic')
})
it('rejects defineProperty and delete through the namespace', () => {
expect(() => {
Object.defineProperty(barrelNs, 'BUTTON', { value: 'changed' })
}).toThrow()
expect(() => {
Object.defineProperty(barrelNs, 'NEW_PROP', { value: 1 })
}).toThrow()
expect(() => {
delete barrelNs.LEGACY_CONST
}).toThrow()
expect(barrelNs.BUTTON).toBe('button')
expect(barrelNs.LEGACY_CONST).toBe('I am dynamic')
expect('NEW_PROP' in barrelNs).toBe(false)
})
it('supports the `in` operator for static and dynamic keys (has trap)', () => {
expect('BUTTON' in barrelNs).toBe(true)
expect('LEGACY_CONST' in barrelNs).toBe(true)
expect('AnotherThing' in barrelNs).toBe(true)
expect('mutableValue' in barrelNs).toBe(true)
expect('DOES_NOT_EXIST' in barrelNs).toBe(false)
})
it('does not leak `default` from star-exported modules', () => {
expect(Object.keys(barrelNs)).not.toContain('default')
expect('default' in barrelNs).toBe(false)
})
it('keeps __esModule present but non-enumerable', () => {
expect(barrelNs.__esModule).toBe(true)
expect('__esModule' in barrelNs).toBe(true)
expect(Object.keys(barrelNs)).not.toContain('__esModule')
})
it('exposes dynamic keys via getOwnPropertyDescriptor', () => {
const desc = Object.getOwnPropertyDescriptor(barrelNs, 'LEGACY_CONST')
expect(desc).toBeDefined()
expect(desc.enumerable).toBe(true)
expect(typeof desc.get).toBe('function')
})
module.exports.LEGACY_CONST = 'I am dynamic'
module.exports.AnotherThing = function () {
return 'still dynamic'
}
// An export whose value can be reassigned by the CJS module itself at runtime.
module.exports.mutableValue = 'before'
module.exports.setMutable = function (value) {
module.exports.mutableValue = value
}
warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack/exports/re-export-dynamic/input/legacy.cjs unexpected export *
export * used with module [project]/turbopack/crates/turbopack-tests/tests/execution/turbopack/exports/re-export-dynamic/input/legacy.cjs [test] (ecmascript) which is a CommonJS module with exports only available at runtime
List all export names manually (`export { a, b, c } from "...") or rewrite the module to ESM, to avoid the additional runtime code.`
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/execution/turbopack/exports/re-export-dynamic/input/legacy.cjs
./turbopack/crates/turbopack-tests/tests/execution/turbopack/exports/re-export-dynamic/input/barrel.js
./turbopack/crates/turbopack-tests/tests/execution/turbopack/exports/re-export-dynamic/input/index.js
\ No newline at end of file
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="557d25bf-c2b0-8db1-31f5-73705b9713cf")}catch(e){}}(); ;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="15ac64a5-9f5e-fc28-a07d-303363f05eda")}catch(e){}}();
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([ (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js", "output/1i9t_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvs.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]} {"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvs.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]}
...@@ -105,7 +105,7 @@ function createModuleWithDirection(id) { ...@@ -105,7 +105,7 @@ function createModuleWithDirection(id) {
const BindingTag_Value = 0; const BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -143,11 +143,18 @@ const BindingTag_Value = 0; ...@@ -143,11 +143,18 @@ const BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
let module; let module;
let exports; let exports;
if (id != null) { if (id != null) {
...@@ -158,24 +165,72 @@ const BindingTag_Value = 0; ...@@ -158,24 +165,72 @@ const BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
let reexportedObjects = REEXPORTED_OBJECTS.get(module); let reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop)=>{
if (prop !== 'default') {
for (const obj of reexportedObjects){
if (hasOwnProperty.call(obj, prop)) return obj;
}
}
return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get (target, prop) { get (target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
for (const obj of reexportedObjects){ const obj = reexportOwning(prop);
const value = Reflect.get(obj, prop); return obj && Reflect.get(obj, prop);
if (value !== undefined) return value; },
} // The namespace is read-only, like a real esm namespace object. The
return undefined; // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set () {
return false;
},
defineProperty () {
return false;
}, },
deleteProperty () {
return false;
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has (target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys (target) { ownKeys (target) {
const keys = Reflect.ownKeys(target); const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects){ for (const obj of reexportedObjects){
...@@ -184,6 +239,22 @@ function ensureDynamicExports(module, exports) { ...@@ -184,6 +239,22 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor (target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
const obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: ()=>Reflect.get(obj, prop)
};
}
return undefined;
} }
}); });
} }
...@@ -2272,5 +2343,5 @@ chunkListsToRegister.forEach(registerChunkList); ...@@ -2272,5 +2343,5 @@ chunkListsToRegister.forEach(registerChunkList);
})(); })();
//# debugId=557d25bf-c2b0-8db1-31f5-73705b9713cf //# debugId=15ac64a5-9f5e-fc28-a07d-303363f05eda
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js.map //# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js.map
\ No newline at end of file
...@@ -90,7 +90,7 @@ function createModuleWithDirection(id) { ...@@ -90,7 +90,7 @@ function createModuleWithDirection(id) {
const BindingTag_Value = 0; const BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -128,11 +128,18 @@ const BindingTag_Value = 0; ...@@ -128,11 +128,18 @@ const BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
let module; let module;
let exports; let exports;
if (id != null) { if (id != null) {
...@@ -143,24 +150,72 @@ const BindingTag_Value = 0; ...@@ -143,24 +150,72 @@ const BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
let reexportedObjects = REEXPORTED_OBJECTS.get(module); let reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop)=>{
if (prop !== 'default') {
for (const obj of reexportedObjects){
if (hasOwnProperty.call(obj, prop)) return obj;
}
}
return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get (target, prop) { get (target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
for (const obj of reexportedObjects){ const obj = reexportOwning(prop);
const value = Reflect.get(obj, prop); return obj && Reflect.get(obj, prop);
if (value !== undefined) return value; },
} // The namespace is read-only, like a real esm namespace object. The
return undefined; // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set () {
return false;
},
defineProperty () {
return false;
},
deleteProperty () {
return false;
}, },
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has (target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys (target) { ownKeys (target) {
const keys = Reflect.ownKeys(target); const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects){ for (const obj of reexportedObjects){
...@@ -169,6 +224,22 @@ function ensureDynamicExports(module, exports) { ...@@ -169,6 +224,22 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor (target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
const obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: ()=>Reflect.get(obj, prop)
};
}
return undefined;
} }
}); });
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/b.js [test] (ecmascript)", ((__turbopack_context__) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/b.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict"; "use strict";
__turbopack_context__.s([]); __turbopack_context__.s([], undefined, true);
// b.js // b.js
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$c$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/c.js [test] (ecmascript)"); // This would not be handled, but still need __turbopack__cjs__ var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$c$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/c.js [test] (ecmascript)"); // This would not be handled, but still need __turbopack__cjs__
// as there are properties dynamically added by __turbopack__cjs__ in c.js // as there are properties dynamically added by __turbopack__cjs__ in c.js
...@@ -12,7 +12,7 @@ __turbopack_context__.j(__TURBOPACK__imported__module__$5b$project$5d2f$turbopac ...@@ -12,7 +12,7 @@ __turbopack_context__.j(__TURBOPACK__imported__module__$5b$project$5d2f$turbopac
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/c.js [test] (ecmascript)", ((__turbopack_context__) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/c.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict"; "use strict";
__turbopack_context__.s([]); __turbopack_context__.s([], undefined, true);
// c.js // c.js
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$commonjs$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/commonjs.js [test] (ecmascript)"); // This would be handled by existing logic var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$commonjs$2e$js__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-2/input/commonjs.js [test] (ecmascript)"); // This would be handled by existing logic
__turbopack_context__.j(__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$commonjs$2e$js__$5b$test$5d$__$28$ecmascript$29$__); __turbopack_context__.j(__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$2$2f$input$2f$commonjs$2e$js__$5b$test$5d$__$28$ecmascript$29$__);
......
...@@ -17,7 +17,7 @@ console.log(__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$ ...@@ -17,7 +17,7 @@ console.log(__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/mod.js [test] (ecmascript)", ((__turbopack_context__) => { "[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/mod.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict"; "use strict";
__turbopack_context__.s([]); __turbopack_context__.s([], undefined, true);
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$script$2f$input$2f$exported$2e$cjs__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/exported.cjs [test] (ecmascript)"); var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$script$2f$input$2f$exported$2e$cjs__$5b$test$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/export-alls/cjs-script/input/exported.cjs [test] (ecmascript)");
__turbopack_context__.j(__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$script$2f$input$2f$exported$2e$cjs__$5b$test$5d$__$28$ecmascript$29$__); __turbopack_context__.j(__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$export$2d$alls$2f$cjs$2d$script$2f$input$2f$exported$2e$cjs__$5b$test$5d$__$28$ecmascript$29$__);
; ;
......
...@@ -90,7 +90,7 @@ function createModuleWithDirection(id) { ...@@ -90,7 +90,7 @@ function createModuleWithDirection(id) {
const BindingTag_Value = 0; const BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -128,11 +128,18 @@ const BindingTag_Value = 0; ...@@ -128,11 +128,18 @@ const BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
let module; let module;
let exports; let exports;
if (id != null) { if (id != null) {
...@@ -143,24 +150,72 @@ const BindingTag_Value = 0; ...@@ -143,24 +150,72 @@ const BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
let reexportedObjects = REEXPORTED_OBJECTS.get(module); let reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop)=>{
if (prop !== 'default') {
for (const obj of reexportedObjects){
if (hasOwnProperty.call(obj, prop)) return obj;
}
}
return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get (target, prop) { get (target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
for (const obj of reexportedObjects){ const obj = reexportOwning(prop);
const value = Reflect.get(obj, prop); return obj && Reflect.get(obj, prop);
if (value !== undefined) return value; },
} // The namespace is read-only, like a real esm namespace object. The
return undefined; // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set () {
return false;
},
defineProperty () {
return false;
}, },
deleteProperty () {
return false;
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has (target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys (target) { ownKeys (target) {
const keys = Reflect.ownKeys(target); const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects){ for (const obj of reexportedObjects){
...@@ -169,6 +224,22 @@ function ensureDynamicExports(module, exports) { ...@@ -169,6 +224,22 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor (target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
const obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: ()=>Reflect.get(obj, prop)
};
}
return undefined;
} }
}); });
} }
......
...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) { ...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) {
const BindingTag_Value = 0; const BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -142,11 +142,18 @@ const BindingTag_Value = 0; ...@@ -142,11 +142,18 @@ const BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
let module; let module;
let exports; let exports;
if (id != null) { if (id != null) {
...@@ -157,24 +164,72 @@ const BindingTag_Value = 0; ...@@ -157,24 +164,72 @@ const BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
let reexportedObjects = REEXPORTED_OBJECTS.get(module); let reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop)=>{
if (prop !== 'default') {
for (const obj of reexportedObjects){
if (hasOwnProperty.call(obj, prop)) return obj;
}
}
return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get (target, prop) { get (target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
for (const obj of reexportedObjects){ const obj = reexportOwning(prop);
const value = Reflect.get(obj, prop); return obj && Reflect.get(obj, prop);
if (value !== undefined) return value; },
} // The namespace is read-only, like a real esm namespace object. The
return undefined; // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set () {
return false;
},
defineProperty () {
return false;
}, },
deleteProperty () {
return false;
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has (target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys (target) { ownKeys (target) {
const keys = Reflect.ownKeys(target); const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects){ for (const obj of reexportedObjects){
...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) { ...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor (target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
const obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: ()=>Reflect.get(obj, prop)
};
}
return undefined;
} }
}); });
} }
......
...@@ -236,7 +236,7 @@ function createModuleWithDirection(id) { ...@@ -236,7 +236,7 @@ function createModuleWithDirection(id) {
var BindingTag_Value = 0; var BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -274,11 +274,18 @@ var BindingTag_Value = 0; ...@@ -274,11 +274,18 @@ var BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
var module; var module;
var exports; var exports;
if (id != null) { if (id != null) {
...@@ -289,24 +296,24 @@ var BindingTag_Value = 0; ...@@ -289,24 +296,24 @@ var BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
var reexportedObjects = REEXPORTED_OBJECTS.get(module); var reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
module.exports = module.namespaceObject = new Proxy(exports, { // Returns the re-exported object that provides `prop` as an own property,
get: function get(target, prop) { // or `undefined` if none does. The traps share this logic so they always
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { // agree on which keys are synthesized from `reexportedObjects`. `default`
return Reflect.get(target, prop); // is never re-exported by `export *`, so it is never synthesized.
} var reexportOwning = function reexportOwning(prop) {
if (prop !== 'default') {
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
try { try {
for(var _iterator = reexportedObjects[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ for(var _iterator = reexportedObjects[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
var obj = _step.value; var obj = _step.value;
var value = Reflect.get(obj, prop); if (hasOwnProperty.call(obj, prop)) return obj;
if (value !== undefined) return value;
} }
} catch (err) { } catch (err) {
_didIteratorError = true; _didIteratorError = true;
...@@ -322,8 +329,56 @@ function ensureDynamicExports(module, exports) { ...@@ -322,8 +329,56 @@ function ensureDynamicExports(module, exports) {
} }
} }
} }
}
return undefined; return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, {
get: function get(target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop);
}
var obj = reexportOwning(prop);
return obj && Reflect.get(obj, prop);
}, },
// The namespace is read-only, like a real esm namespace object. The
// re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set: function set() {
return false;
},
defineProperty: function defineProperty() {
return false;
},
deleteProperty: function deleteProperty() {
return false;
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has: function has(target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys: function ownKeys(target) { ownKeys: function ownKeys(target) {
var keys = Reflect.ownKeys(target); var keys = Reflect.ownKeys(target);
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
...@@ -366,6 +421,24 @@ function ensureDynamicExports(module, exports) { ...@@ -366,6 +421,24 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(target, prop) {
var own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
var obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: function get() {
return Reflect.get(obj, prop);
}
};
}
return undefined;
} }
}); });
} }
......
...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) { ...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) {
const BindingTag_Value = 0; const BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -142,11 +142,18 @@ const BindingTag_Value = 0; ...@@ -142,11 +142,18 @@ const BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
let module; let module;
let exports; let exports;
if (id != null) { if (id != null) {
...@@ -157,24 +164,72 @@ const BindingTag_Value = 0; ...@@ -157,24 +164,72 @@ const BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
let reexportedObjects = REEXPORTED_OBJECTS.get(module); let reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop)=>{
if (prop !== 'default') {
for (const obj of reexportedObjects){
if (hasOwnProperty.call(obj, prop)) return obj;
}
}
return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get (target, prop) { get (target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
for (const obj of reexportedObjects){ const obj = reexportOwning(prop);
const value = Reflect.get(obj, prop); return obj && Reflect.get(obj, prop);
if (value !== undefined) return value; },
} // The namespace is read-only, like a real esm namespace object. The
return undefined; // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set () {
return false;
},
defineProperty () {
return false;
}, },
deleteProperty () {
return false;
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has (target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys (target) { ownKeys (target) {
const keys = Reflect.ownKeys(target); const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects){ for (const obj of reexportedObjects){
...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) { ...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor (target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
const obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: ()=>Reflect.get(obj, prop)
};
}
return undefined;
} }
}); });
} }
......
...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) { ...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) {
const BindingTag_Value = 0; const BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -142,11 +142,18 @@ const BindingTag_Value = 0; ...@@ -142,11 +142,18 @@ const BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
let module; let module;
let exports; let exports;
if (id != null) { if (id != null) {
...@@ -157,24 +164,72 @@ const BindingTag_Value = 0; ...@@ -157,24 +164,72 @@ const BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
let reexportedObjects = REEXPORTED_OBJECTS.get(module); let reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop)=>{
if (prop !== 'default') {
for (const obj of reexportedObjects){
if (hasOwnProperty.call(obj, prop)) return obj;
}
}
return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get (target, prop) { get (target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
for (const obj of reexportedObjects){ const obj = reexportOwning(prop);
const value = Reflect.get(obj, prop); return obj && Reflect.get(obj, prop);
if (value !== undefined) return value; },
} // The namespace is read-only, like a real esm namespace object. The
return undefined; // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set () {
return false;
},
defineProperty () {
return false;
}, },
deleteProperty () {
return false;
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has (target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys (target) { ownKeys (target) {
const keys = Reflect.ownKeys(target); const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects){ for (const obj of reexportedObjects){
...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) { ...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor (target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
const obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: ()=>Reflect.get(obj, prop)
};
}
return undefined;
} }
}); });
} }
......
...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) { ...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) {
const BindingTag_Value = 0; const BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -142,11 +142,18 @@ const BindingTag_Value = 0; ...@@ -142,11 +142,18 @@ const BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
let module; let module;
let exports; let exports;
if (id != null) { if (id != null) {
...@@ -157,24 +164,72 @@ const BindingTag_Value = 0; ...@@ -157,24 +164,72 @@ const BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
let reexportedObjects = REEXPORTED_OBJECTS.get(module); let reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop)=>{
if (prop !== 'default') {
for (const obj of reexportedObjects){
if (hasOwnProperty.call(obj, prop)) return obj;
}
}
return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get (target, prop) { get (target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
for (const obj of reexportedObjects){ const obj = reexportOwning(prop);
const value = Reflect.get(obj, prop); return obj && Reflect.get(obj, prop);
if (value !== undefined) return value; },
} // The namespace is read-only, like a real esm namespace object. The
return undefined; // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set () {
return false;
},
defineProperty () {
return false;
}, },
deleteProperty () {
return false;
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has (target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys (target) { ownKeys (target) {
const keys = Reflect.ownKeys(target); const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects){ for (const obj of reexportedObjects){
...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) { ...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor (target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
const obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: ()=>Reflect.get(obj, prop)
};
}
return undefined;
} }
}); });
} }
......
...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) { ...@@ -104,7 +104,7 @@ function createModuleWithDirection(id) {
const BindingTag_Value = 0; const BindingTag_Value = 0;
/** /**
* Adds the getters to the exports object. * Adds the getters to the exports object.
*/ function esm(exports, bindings) { */ function esm(exports, bindings, dynamic) {
defineProp(exports, '__esModule', { defineProp(exports, '__esModule', {
value: true value: true
}); });
...@@ -142,11 +142,18 @@ const BindingTag_Value = 0; ...@@ -142,11 +142,18 @@ const BindingTag_Value = 0;
} }
} }
} }
Object.seal(exports); // The properties defined above are already non-configurable and
// non-writable, so the namespace's existing exports are effectively
// immutable. Sealing additionally makes the object non-extensible, matching
// real ESM-namespace semantics. Modules with dynamic re-exports
// (`export *` from a CommonJS module) must stay extensible so the dynamic
// export proxy can surface keys discovered at runtime, so skip the seal for
// them.
if (!dynamic) Object.seal(exports);
} }
/** /**
* Makes the module an ESM with exports * Makes the module an ESM with exports
*/ function esmExport(bindings, id) { */ function esmExport(bindings, id, dynamic) {
let module; let module;
let exports; let exports;
if (id != null) { if (id != null) {
...@@ -157,24 +164,72 @@ const BindingTag_Value = 0; ...@@ -157,24 +164,72 @@ const BindingTag_Value = 0;
exports = this.e; exports = this.e;
} }
module.namespaceObject = exports; module.namespaceObject = exports;
esm(exports, bindings); esm(exports, bindings, dynamic);
} }
contextPrototype.s = esmExport; contextPrototype.s = esmExport;
function ensureDynamicExports(module, exports) { function ensureDynamicExports(module, exports) {
let reexportedObjects = REEXPORTED_OBJECTS.get(module); let reexportedObjects = REEXPORTED_OBJECTS.get(module);
if (!reexportedObjects) { if (!reexportedObjects) {
REEXPORTED_OBJECTS.set(module, reexportedObjects = []); REEXPORTED_OBJECTS.set(module, reexportedObjects = []);
// Returns the re-exported object that provides `prop` as an own property,
// or `undefined` if none does. The traps share this logic so they always
// agree on which keys are synthesized from `reexportedObjects`. `default`
// is never re-exported by `export *`, so it is never synthesized.
const reexportOwning = (prop)=>{
if (prop !== 'default') {
for (const obj of reexportedObjects){
if (hasOwnProperty.call(obj, prop)) return obj;
}
}
return undefined;
};
// Modules with dynamic re-exports are not sealed by `esm()`, so the
// target beneath the namespace stays extensible. That is what lets the
// `ownKeys` and `getOwnPropertyDescriptor` traps legally report keys that
// exist on `reexportedObjects` but not on the target itself.
module.exports = module.namespaceObject = new Proxy(exports, { module.exports = module.namespaceObject = new Proxy(exports, {
get (target, prop) { get (target, prop) {
if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') { if (hasOwnProperty.call(target, prop) || prop === 'default' || prop === '__esModule') {
return Reflect.get(target, prop); return Reflect.get(target, prop);
} }
for (const obj of reexportedObjects){ const obj = reexportOwning(prop);
const value = Reflect.get(obj, prop); return obj && Reflect.get(obj, prop);
if (value !== undefined) return value; },
} // The namespace is read-only, like a real esm namespace object. The
return undefined; // re-exported modules can still mutate their own exports (exposed live
// via `get`), but mutating the namespace itself is rejected. Refusing
// here, rather than forwarding to the extensible target, also prevents an
// assignment/definition from shadowing a dynamic re-export. It also
// prevents delete from removing a static export.
set () {
return false;
},
defineProperty () {
return false;
}, },
deleteProperty () {
return false;
},
// The `has` trap ensures that `'exportName' in starImports` will reflect
// the truth of whether a key is exported.
has (target, prop) {
if (Reflect.has(target, prop)) return true;
if (prop === 'default' || prop === '__esModule') return false;
return reexportOwning(prop) !== undefined;
},
// ownKeys and getOwnPropertyDescriptor together make the keys enumerable.
// If a value is returned from `ownKeys` but its property descriptor is
// not enumerable, it will not be visible to iterator methods.
// Collectively, they allow code like the following:
//
// ```
// // module.js re-exports dynamic CJS exports
// export * from './legacyModule.cjs'
//
// // from another JS file, reference the re-exported dynamic values
// import * as Namespace from './module.js'
// Object.keys(Namespace)
// ```
ownKeys (target) { ownKeys (target) {
const keys = Reflect.ownKeys(target); const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects){ for (const obj of reexportedObjects){
...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) { ...@@ -183,6 +238,22 @@ function ensureDynamicExports(module, exports) {
} }
} }
return keys; return keys;
},
getOwnPropertyDescriptor (target, prop) {
const own = Reflect.getOwnPropertyDescriptor(target, prop);
if (own || prop === 'default' || prop === '__esModule') return own;
const obj = reexportOwning(prop);
if (obj) {
// Synthetic keys don't exist on the target, so they MUST be
// reported as configurable. However the set/delete traps above will
// prevent them from actually being changed
return {
enumerable: true,
configurable: true,
get: ()=>Reflect.get(obj, prop)
};
}
return undefined;
} }
}); });
} }
......
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