Skip to content

Commit

Permalink
fix: warpped require module (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Oct 19, 2023
1 parent 22794c3 commit ee9a682
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 18 deletions.
33 changes: 20 additions & 13 deletions crates/rolldown/src/bundler/graph/linker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use index_vec::IndexVec;
use oxc::{semantic::ReferenceId, span::Atom};
use rolldown_common::{ExportsKind, LocalOrReExport, ModuleId, ResolvedExport, SymbolRef};
use rolldown_common::{
ExportsKind, ImportKind, LocalOrReExport, ModuleId, ResolvedExport, SymbolRef,
};
use rustc_hash::FxHashMap;

use super::graph::Graph;
Expand Down Expand Up @@ -59,6 +61,13 @@ impl<'graph> Linker<'graph> {
Module::Normal(module) => {
if module.exports_kind == ExportsKind::CommonJs {
wrap_module(self.graph, module.id, &mut module_to_wrapped);
} else {
// Should mark wrapped for require import module
module.import_records.iter().for_each(|record| {
if record.kind == ImportKind::Require {
wrap_module(self.graph, record.resolved_module, &mut module_to_wrapped);
}
});
}
}
Module::External(_) => {}
Expand Down Expand Up @@ -103,19 +112,17 @@ impl<'graph> Linker<'graph> {
if let Some(importee_warp_symbol) = importee.wrap_symbol {
imported_symbols.push((importer.id, importee_warp_symbol));
imported_symbols.push((importer.id, importee.namespace_symbol.0));
}

match (importer.exports_kind, importee.exports_kind) {
(ExportsKind::Esm, ExportsKind::CommonJs) => {
imported_symbols
.push((importer.id, self.graph.runtime.resolve_symbol(&"__toESM".into())));
imported_symbols.push((importer.id, importee.namespace_symbol.0));
}
(ExportsKind::CommonJs, ExportsKind::Esm) => {
imported_symbols
.push((importer.id, self.graph.runtime.resolve_symbol(&"__toCommonJS".into())));
match (importer.exports_kind, importee.exports_kind) {
(ExportsKind::Esm, ExportsKind::CommonJs) => {
imported_symbols
.push((importer.id, self.graph.runtime.resolve_symbol(&"__toESM".into())));
}
(_, ExportsKind::Esm) => {
imported_symbols
.push((importer.id, self.graph.runtime.resolve_symbol(&"__toCommonJS".into())));
}
_ => {}
}
_ => {}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ impl<'ast> EsmWrapSourceRender<'ast> {
let program = self.ctx.module.ast.program();
self.visit_program(program);
self.hoisted_functions.iter().for_each(|f| {
// TODO: remove this hack
// here move end of function to the keep "\n"
self.ctx.source.relocate(f.start, f.end + 1, 0);
// Improve: multiply functions should separate by "\n"
self.ctx.source.relocate(f.start, f.end, 0);
self.ctx.source.append_right(0, "\n");
});
if !self.hoisted_vars.is_empty() {
self.ctx.source.append_right(0, format!("var {};\n", self.hoisted_vars.join(",")));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/esbuild/default/common_js_from_es6
---
# main.js

```js
// bar.js
function bar$1() {
return 'bar'
}
var bar_ns = {
get bar() { return bar$1 }
};
var init_bar = __esm({
'bar.js'() {

}
});
// foo.js
function foo$1() {
return 'foo'
}
var foo_ns = {
get foo() { return foo$1 }
};
var init_foo = __esm({
'foo.js'() {

}
});
// main.js
const {foo} = (init_foo(), __toCommonJS(foo_ns))
console.log(foo(), bar())
const {bar} = (init_bar(), __toCommonJS(bar_ns)) // This should not be hoisted
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/esbuild/default/simple_common_js
---
# main.js

```js
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
module.exports = function() {
return 123
}
}
});
// main.js
const fn = require_foo()
console.log(fn())
```
6 changes: 4 additions & 2 deletions crates/rolldown/tests/fixtures/basic_commonjs/artifacts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ input_file: crates/rolldown/tests/fixtures/basic_commonjs

```js
// esm.js
function esm_default_fn() {}
function esm_named_fn() {}
function esm_default_fn() {}function esm_named_fn() {}

var esm_named_var,esm_named_class;
var esm_ns = {
get esm_named_var() { return esm_named_var },
Expand All @@ -18,7 +18,9 @@ var esm_ns = {
};
var init_esm = __esm({
'esm.js'() {

esm_named_var = 1;

esm_named_class = class esm_named_class {}
}
});
Expand Down

0 comments on commit ee9a682

Please sign in to comment.