Unverified Commit 2728966e authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Turbopack: stop reserving `default` once it is mangled

With `default` mangled like any other export name, nothing emits a property
under that key any more, so withholding the bucket has no purpose. The runtime
paths that look `'default'` up by name only see CommonJS targets and modules
with dynamic re-exports, both of which keep their original names.

`__esModule` stays reserved for the opposite reason: `esm()` in the runtime
does `defineProp(exports, '__esModule', …)` for every ESM module, so that
property exists whatever the module's own exports are called, and an export
assigned that key would collide with it.
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent d841b5a8
......@@ -31,10 +31,16 @@ use self::table::shorten_to_unique_names;
use crate::chunk::{EcmascriptChunkPlaceable, EcmascriptExports};
/// Export keys the runtime defines on a module's exports object itself, so an assigned name landing
/// on one of them would collide with it. They are never handed out — note that this is independent
/// of whether the export *named* `default` or `__esModule` is itself mangled, which it is: those
/// are ordinary ESM export names, and every read of them in an eligible module is generated by us.
const RESERVED_KEYS: &[&str] = &["default", "__esModule"];
/// on one of them would collide with it.
///
/// Only `__esModule` qualifies: `esm()` in the runtime does
/// `defineProp(exports, '__esModule', …)` for every ESM module, so that property is always there,
/// whatever the module's own exports are called. `default` is *not* here — nothing defines a
/// `default` property on a module that reaches this point, since the runtime paths that look it up
/// by name (the CommonJS interop in `esmNamespaceObject`, the proxy traps behind
/// `ensureDynamicExports`) only see CommonJS targets and modules with dynamic re-exports, and both
/// of those keep their original names via the checks below.
const RESERVED_KEYS: &[&str] = &["__esModule"];
/// A module's export name mapping: original export name -> the key actually used in the output.
///
......@@ -124,9 +130,9 @@ pub async fn mangled_export_names(
// by name — the CommonJS interop in `esmNamespaceObject` and the proxy traps behind
// `ensureDynamicExports` — only apply to CommonJS targets and to modules with dynamic
// re-exports, and both already keep their original names via the checks above. `__esModule` is
// how a CommonJS module signals interop *to* ESM; the runtime defines it on the exports object
// itself, so an ESM module that happens to export that name is just an ordinary export. Both
// strings stay in `RESERVED_KEYS` so that no *other* export is assigned them.
// how a CommonJS module signals interop *to* ESM, so an ESM module that happens to export that
// name is just an ordinary export. Only `__esModule` stays in `RESERVED_KEYS`, because the
// runtime defines that property on the exports object regardless; see there.
let names = expanded
.exports
.iter()
......
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