Visitar URL original
fix: include inlined constants in namespace object (#8099) · rolldown/rolldown@d4a32e9 · GitHub
Skip to content

Commit d4a32e9

Browse files
authored
fix: include inlined constants in namespace object (#8099)
This was not passing: ```js // index.js import assert from 'node:assert'; import * as ns from './bar'; assert.deepEqual(ns, { [Symbol.toStringTag]: 'Module', bar: 'bar', }); // bar.js export const bar = "bar" ```
1 parent dd1b8aa commit d4a32e9

6 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎crates/rolldown/src/module_finalizers/mod.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,16 @@ impl<'me, 'ast> ScopeHoistingFinalizer<'me, 'ast> {
509509
// https://github.com/rolldown/rolldown/blob/d6d65f9080e427cd9feef56eb7a110fbcf6c1414/crates/rolldown/src/stages/generate_stage/chunk_optimizer.rs#L347-L354
510510
arg_obj_expr.properties.extend(self.ctx.linking_info.canonical_exports(false).filter_map(
511511
|(export, resolved_export)| {
512-
if !self.ctx.used_symbol_refs.contains(&resolved_export.symbol_ref) {
512+
// Even if the symbol is not marked as used (generated inside module),
513+
// it should be included in the namespace export.
514+
let is_inlinable_constant = self
515+
.ctx
516+
.constant_value_map
517+
.get(&self.ctx.symbol_db.canonical_ref_for(resolved_export.symbol_ref))
518+
.is_some_and(|meta| !meta.commonjs_export);
519+
if !self.ctx.used_symbol_refs.contains(&resolved_export.symbol_ref)
520+
&& !is_inlinable_constant
521+
{
513522
return None;
514523
}
515524
// prop_name: () => returned
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"config": {
3+
"optimization": {
4+
"inlineConst": { "mode": "all" }
5+
}
6+
}
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: crates/rolldown_testing/src/integration_test.rs
3+
---
4+
# Assets
5+
6+
## main.js
7+
8+
```js
9+
import assert from "node:assert";
10+
11+
// HIDDEN [\0rolldown/runtime.js]
12+
//#region bar.js
13+
var bar_exports = /* @__PURE__ */ __exportAll({ bar: () => "bar" });
14+
const bar = "bar";
15+
16+
//#endregion
17+
//#region main.js
18+
assert.deepEqual(bar_exports, {
19+
[Symbol.toStringTag]: "Module",
20+
bar: "bar"
21+
});
22+
23+
//#endregion
24+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const bar = "bar"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import assert from 'node:assert';
2+
import * as ns from './bar';
3+
4+
assert.deepEqual(ns, {
5+
[Symbol.toStringTag]: 'Module',
6+
bar: 'bar',
7+
});

‎crates/rolldown/tests/snapshots/integration_rolldown__filename_with_hash.snap‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5445,6 +5445,10 @@ expression: output
54455445
54465446
- main-!~{000}~.js => main-jzXyUv2k.js
54475447
5448+
# tests/rolldown/optimization/inline_const/namespace_object_export
5449+
5450+
- main-!~{000}~.js => main-_gmHMUS3.js
5451+
54485452
# tests/rolldown/optimization/inline_const/ns_chain
54495453
54505454
- main-!~{000}~.js => main-Brb9u1JP.js

0 commit comments

Comments
 (0)