Skip to content

Commit

Permalink
fix: should correctly render export default xxx (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Nov 29, 2023
1 parent 3cb39a2 commit 7fa642f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
27 changes: 17 additions & 10 deletions crates/rolldown/src/bundler/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod utils;
use std::fmt::Debug;

use oxc::{
ast::Visit,
ast::{ast::Expression, Visit},
span::{Atom, GetSpan, Span},
};
use rolldown_common::{ExportsKind, StmtInfo, StmtInfoId, StmtInfos, SymbolRef, WrapKind};
Expand Down Expand Up @@ -189,20 +189,27 @@ impl<'r> AstRenderer<'r> {
}
}

// hyf0: clippy::single_match_else: using match statement is more readable
#[allow(clippy::single_match_else)]
fn strip_export_keyword(
&mut self,
default_decl: &oxc::ast::ast::ExportDefaultDeclaration,
) -> RenderControl {
match &default_decl.declaration {
oxc::ast::ast::ExportDefaultDeclarationKind::Expression(exp) => {
let default_ref_name = self.ctx.default_ref_name.expect("Should generated a name");
self.ctx.source.overwrite(
default_decl.span.start,
exp.span().start,
format!("var {default_ref_name} = "),
);
self.visit_expression(exp);
}
oxc::ast::ast::ExportDefaultDeclarationKind::Expression(exp) => match exp {
Expression::Identifier(_) => {
self.ctx.remove_node(default_decl.span);
}
_ => {
let default_ref_name = self.ctx.default_ref_name.expect("Should generated a name");
self.ctx.source.overwrite(
default_decl.span.start,
exp.span().start,
format!("var {default_ref_name} = "),
);
self.visit_expression(exp);
}
},
oxc::ast::ast::ExportDefaultDeclarationKind::FunctionDeclaration(decl) => {
self.ctx.remove_node(Span::new(default_decl.span.start, decl.span.start));
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rolldown/tests/fixtures/misc/issue_376/_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import assert from 'node:assert'
import main from './dist/main.mjs'
assert.equal(main, 'hello world')
17 changes: 17 additions & 0 deletions crates/rolldown/tests/fixtures/misc/issue_376/artifacts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/fixtures/misc/issue_376
---
# Assets

## main.mjs

```js
// main.js
const test = 'hello world'
export { test as default };
```
3 changes: 3 additions & 0 deletions crates/rolldown/tests/fixtures/misc/issue_376/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const test = 'hello world'

export default test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 7fa642f

Please sign in to comment.