Skip to content

Commit

Permalink
fix: determine call expression result is used to render require call (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Nov 2, 2023
1 parent 026a3c2 commit 85224ea
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 21 deletions.
31 changes: 24 additions & 7 deletions crates/rolldown/src/bundler/visitors/renderer_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct RendererBase<'ast> {
pub default_symbol_name: Option<&'ast Atom>,
// Used to hoisted declaration for import module, including import declaration and export declaration which has source imported
pub first_stmt_start: Option<u32>,
// Used to determine whether the call result is used.
pub call_result_un_used: bool,
}

impl<'ast> RendererBase<'ast> {
Expand Down Expand Up @@ -51,6 +53,7 @@ impl<'ast> RendererBase<'ast> {
wrap_symbol_name,
default_symbol_name,
first_stmt_start: None,
call_result_un_used: false,
}
}

Expand Down Expand Up @@ -262,20 +265,27 @@ impl<'ast> RendererBase<'ast> {
let namespace_name = self.canonical_name_for(importee.namespace_symbol);
let to_commonjs_runtime_symbol_name =
self.canonical_name_for_runtime(&"__toCommonJS".into());
self.source.update(
expr.span.start,
expr.span.end,
format!(
"({wrap_symbol_name}(), {to_commonjs_runtime_symbol_name}({namespace_name}))"
),
);

if self.call_result_un_used {
self.source.update(expr.span.start, expr.span.end, format!("{wrap_symbol_name}()"));
} else {
self.source.update(
expr.span.start,
expr.span.end,
format!(
"({wrap_symbol_name}(), {to_commonjs_runtime_symbol_name}({namespace_name}))"
),
);
}
}
}
}
}
self.call_result_un_used = false;
}

pub fn visit_statement(&mut self, stmt: &'ast oxc::ast::ast::Statement<'ast>) {
// Mark the start position for place hoisted module declarations.
if self.first_stmt_start.is_none() {
let hoisted_decl = if let oxc::ast::ast::Statement::ModuleDeclaration(decl) = stmt {
match &decl.0 {
Expand All @@ -291,5 +301,12 @@ impl<'ast> RendererBase<'ast> {
self.first_stmt_start = Some(stmt.span().start);
}
}

// only direct call is result unused eg `init()`
if let oxc::ast::ast::Statement::ExpressionStatement(expr) = stmt {
if let oxc::ast::ast::Expression::CallExpression(_) = &expr.expression {
self.call_result_un_used = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ abc = undefined
function Fn() {}
var commonjs_default,v,l,c,Class;
var commonjs_ns = {
get Fn() { return Fn },
get Class() { return Class },
get Fn() { return Fn },
get abc() { return abc },
get v() { return v },
get default() { return commonjs_default },
get c() { return c },
get b() { return b_ns },
get l() { return l }
get c() { return c },
get default() { return commonjs_default },
get l() { return l },
get v() { return v }
};
var init_commonjs = __esm({
'commonjs.js'() {
Expand All @@ -118,11 +118,11 @@ Class = class Class {}
}
});
// entry.js
(init_commonjs(), __toCommonJS(commonjs_ns))
(init_c(), __toCommonJS(c_ns))
(init_d(), __toCommonJS(d_ns))
(init_e(), __toCommonJS(e_ns))
(init_f(), __toCommonJS(f_ns))
(init_g(), __toCommonJS(g_ns))
(init_h(), __toCommonJS(h_ns))
init_commonjs()
init_c()
init_d()
init_e()
init_f()
init_g()
init_h()
```
4 changes: 2 additions & 2 deletions crates/rolldown/tests/fixtures/mix-cjs-esm/artifacts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = 1;
// esm_import_cjs_require.js
var import_cjs = __toESM(require_cjs());
(init_foo(), __toCommonJS(foo_ns))
init_foo()
console.log(import_cjs.a)
// esm_import_cjs_export.js
var require_esm_import_cjs_export = __commonJS({
Expand All @@ -37,7 +37,7 @@ module.exports = 1
}
});
// esm_export_cjs_require.js
(init_foo(), __toCommonJS(foo_ns))
init_foo()
const value$1 = 1;
// esm_export_cjs_export.js
module.exports = 1;
Expand Down

0 comments on commit 85224ea

Please sign in to comment.