next.js
f01e47e4 - Turbopack: list ESM namespace keys in code unit order for the general export registration (#101749)

Commit
8 hours ago
Turbopack: list ESM namespace keys in code unit order for the general export registration (#101749) ### What? Turbopack now lists the keys of a module namespace object in code unit order for modules registered through the general (non-compact) ESM export registration. Before, a namespace with export names such as `'😀'` and `'。'` listed `'。'` first. ### Why? The spec requires a module namespace object's `[[OwnPropertyKeys]]` to return its exports sorted by UTF-16 code units. #101494 fixed this for the compact re-export registration. The general registration still emitted its entries in the order of the exports map, which is Rust string (UTF-8 byte) order. The two orders differ when a character outside the Basic Multilingual Plane (a surrogate pair in UTF-16) meets a BMP character at or above U+E000. The runtime defines namespace properties in list order, so `Object.keys` showed the UTF-8 order. This applies to local exports, and to local exports combined with `export *` names, whenever a module goes through the general registration. That includes scope-hoisted modules and modules compiled without following re-exports. ### How? The sort happens at build time, so there is no runtime change and no runtime cost. The general registration in `EsmExports::code_generation` now collects each export with the key it is emitted under (the mangled name when export names are mangled, otherwise the original name). It sorts that list with the UTF-16 comparator added in #101494, then emits the bindings. Name resolution still uses the original export name, so only the order of the emitted entries changes. Two new execution fixtures under `turbopack-tests/tests/execution/turbopack/exports/` cover this: - `namespace-key-order` uses the default options (scope hoisting). - `namespace-key-order-no-hoisting` disables scope hoisting and re-export following, so the namespace goes through the general registration rather than the compact one. Each checks a module with out-of-order local exports (including `default`, a `let`, a function, `'😀'` and `'。'`), and a module combining local exports with an `export *`. Both fail without the change. The emitted output of existing snapshot tests is unchanged. <!-- NEXT_JS_LLM --> <!-- fleet 6f5bd9f1-0c8e-48ce-a331-1466eb7242a6 --> --------- Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>
Author
Parents
Loading