Skip to content

Commit

Permalink
fix: should rewrite export default import(...) correctly (#220)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 committed Nov 11, 2023
1 parent 699fc5d commit cd8ad5e
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 15 deletions.
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/graph/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl<'graph> Linker<'graph> {
for module in &self.graph.modules {
match module {
Module::Normal(importer) => {
importer.import_records.iter().for_each(|r| {
importer.static_imports().for_each(|r| {
let importee_linking_info = &linking_infos[r.resolved_module];
let importee = &self.graph.modules[r.resolved_module];
let Module::Normal(importee) = importee else {
Expand Down
11 changes: 9 additions & 2 deletions crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use oxc::{
span::{Atom, Span},
};
use rolldown_common::{
ExportsKind, ImportRecord, ImportRecordId, LocalOrReExport, ModuleId, ModuleType, NamedImport,
ResolvedExport, ResourceId, StmtInfo, StmtInfos, SymbolRef,
ExportsKind, ImportKind, ImportRecord, ImportRecordId, LocalOrReExport, ModuleId, ModuleType,
NamedImport, ResolvedExport, ResourceId, StmtInfo, StmtInfos, SymbolRef,
};
use rolldown_oxc::OxcProgram;
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -54,6 +54,13 @@ impl NormalModule {
self.stmt_infos.get(0.into()).is_included
}

pub fn static_imports(&self) -> impl Iterator<Item = &ImportRecord> {
self
.import_records
.iter()
.filter(|rec| matches!(rec.kind, ImportKind::Import | ImportKind::Require))
}

#[allow(clippy::needless_pass_by_value)]
pub fn render(&self, ctx: ModuleRenderContext<'_>) -> Option<MagicString<'static>> {
let source = self.ast.source();
Expand Down
29 changes: 17 additions & 12 deletions crates/rolldown/src/bundler/renderer/impl_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,23 @@ impl<'ast, 'r> Visit<'ast> for AstRenderer<'r> {

fn visit_import_expression(&mut self, expr: &oxc::ast::ast::ImportExpression<'ast>) {
if let oxc::ast::ast::Expression::StringLiteral(str) = &expr.source {
if let Some(chunk_id) =
self.ctx.chunk_graph.module_to_chunk[self.ctx.module.importee_id_by_span(expr.span)]
{
let chunk = &self.ctx.chunk_graph.chunks[chunk_id];
self.overwrite(
str.span.start,
str.span.end,
// TODO: the path should be relative to the current importer chunk
format!("'./{}'", chunk.file_name.as_ref().unwrap()),
);
} else {
// external module doesn't belong to any chunk, just keep this as it is
let importee = self.ctx.module.importee_id_by_span(expr.span);
match self.ctx.graph.modules[importee] {
Module::Normal(_) => {
debug_assert!(matches!(self.ctx.graph.modules[importee], Module::Normal(_)));
let chunk_id = self.ctx.chunk_graph.module_to_chunk[importee]
.expect("Normal module should belong to a chunk");
let chunk = &self.ctx.chunk_graph.chunks[chunk_id];
self.overwrite(
str.span.start,
str.span.end,
// TODO: the path should be relative to the current importer chunk
format!("'./{}'", chunk.file_name.as_ref().unwrap()),
);
}
Module::External(_) => {
// external module doesn't belong to any chunk, just keep this as it is
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ impl<'r> AstRenderer<'r> {
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import assert from 'assert'
import main from './dist/main.mjs'

assert.equal((await main).default, 'cjs')
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/fixtures/compat/dynamic_cjs_entry
---
# Assets

## cjs_js.mjs

```js
import { __commonJS } from "./_rolldown_runtime.mjs";
// cjs.js
var require_cjs = __commonJS({
'cjs.js'(exports, module) {
module.exports = 'cjs'
}
});
export default require_cjs();
```
## main.mjs

```js
// main.js
var main_default = import('./cjs_js.mjs')
export { main_default as default };
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'cjs'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default import('./cjs.js')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit cd8ad5e

Please sign in to comment.