Skip to content

Commit b44f77d

Browse files
fix: apply topLevelVar to exported const/let declarations (#8507)
closed #8506
1 parent 8a28ad6 commit b44f77d

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

crates/rolldown/src/module_finalizers/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,8 +1541,9 @@ impl<'me, 'ast> ScopeHoistingFinalizer<'me, 'ast> {
15411541
return;
15421542
}
15431543
}
1544-
} else if self.ctx.options.top_level_var {
1545-
// Here we should find if it's a "VariableDeclaration" and switch it to "Var."
1544+
}
1545+
1546+
if self.ctx.options.top_level_var {
15461547
if let Statement::VariableDeclaration(var_decl) = &mut top_stmt {
15471548
var_decl.kind = ast::VariableDeclarationKind::Var;
15481549
for decl in &mut var_decl.declarations {

crates/rolldown/tests/rolldown/function/top_level_var/enabled/artifacts.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ var firstLevelVar = "var";
1212
var firstLevelConst = "const";
1313
var FirstLevelClass = class {};
1414
console.log(firstLevelLet, firstLevelVar, firstLevelConst, new FirstLevelClass());
15+
var exportedConst = "exported_const";
16+
var exportedLet = "exported_let";
17+
var ExportedClass = class {};
18+
function exportedFunction() {}
1519
console.log("let");
1620
function second_level() {
1721
let secondLevelLet = "let";
@@ -22,5 +26,6 @@ function second_level() {
2226
}
2327
second_level();
2428
//#endregion
29+
export { ExportedClass, exportedConst, exportedFunction, exportedLet };
2530
2631
```

crates/rolldown/tests/rolldown/function/top_level_var/enabled/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const firstLevelConst = 'const';
44
class FirstLevelClass {}
55
console.log(firstLevelLet, firstLevelVar, firstLevelConst, new FirstLevelClass());
66

7+
export const exportedConst = 'exported_const';
8+
export let exportedLet = 'exported_let';
9+
export class ExportedClass {}
10+
export function exportedFunction() {}
11+
712
if (true) {
813
let shouldNotBeSubstitutedLet = 'let';
914
console.log(shouldNotBeSubstitutedLet);

crates/rolldown/tests/snapshots/integration_rolldown__filename_with_hash.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4498,7 +4498,7 @@ expression: output
44984498
44994499
# tests/rolldown/function/top_level_var/enabled
45004500
4501-
- main-!~{000}~.js => main-fX6SLPHl.js
4501+
- main-!~{000}~.js => main--DMU1jVn.js
45024502
45034503
# tests/rolldown/hash/content_include_placeholder
45044504

0 commit comments

Comments
 (0)