Skip to content

Commit

Permalink
fix(bundler): Ignore require of external modules (#2840)
Browse files Browse the repository at this point in the history
swc_bundler:
 - Check for external modules for `require` calls`. (Closes #2275)
  • Loading branch information
ErDmKo authored Nov 23, 2021
1 parent d0cabc3 commit c8de935
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
5 changes: 4 additions & 1 deletion crates/swc_bundler/src/bundler/chunk/cjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ where
// TODO: Check for global mark
if i.sym == *"require" && node.args.len() == 1 {
match &*node.args[0].expr {
Expr::Lit(Lit::Str(..)) => {
Expr::Lit(Lit::Str(module_name)) => {
if self.bundler.is_external(&module_name.value) {
return;
}
let load = CallExpr {
span: node.span,
callee: Ident::new("load".into(), i.span).as_callee(),
Expand Down
26 changes: 6 additions & 20 deletions crates/swc_bundler/src/bundler/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,7 @@ where
/// Returns (local, export)
fn ctxt_for(&self, src: &JsWord) -> Option<(SyntaxContext, SyntaxContext)> {
// Don't apply mark if it's a core module.
if self
.bundler
.config
.external_modules
.iter()
.any(|v| v == src)
{
if self.bundler.is_external(src) {
return None;
}
let path = self.bundler.resolve(self.path, src).ok()?;
Expand All @@ -170,13 +164,7 @@ where

fn mark_as_wrapping_required(&self, src: &JsWord) {
// Don't apply mark if it's a core module.
if self
.bundler
.config
.external_modules
.iter()
.any(|v| v == src)
{
if self.bundler.is_external(src) {
return;
}
let path = self.bundler.resolve(self.path, src);
Expand Down Expand Up @@ -246,6 +234,9 @@ where
_ => false,
} =>
{
if self.bundler.is_external(&src.value) {
return;
}
match &mut **callee {
Expr::Ident(i) => {
self.mark_as_cjs(&src.value);
Expand Down Expand Up @@ -407,12 +398,7 @@ where

fn visit_mut_import_decl(&mut self, import: &mut ImportDecl) {
// Ignore if it's a core module.
if self
.bundler
.config
.external_modules
.contains(&import.src.value)
{
if self.bundler.is_external(&import.src.value) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion crates/swc_bundler/src/bundler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ where
}
})
}

pub(crate) fn is_external(&self, src: &JsWord) -> bool {
return self.config.external_modules.iter().any(|v| v == src);
}
///
///
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var f = require('fs');
console.log(f);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var f = require('fs');
console.log(f);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var f = require('fs');
console.log(f);

1 comment on commit c8de935

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: c8de935 Previous: d0cabc3 Ratio
base_tr_fixer 23530 ns/iter (± 312) 35581 ns/iter (± 5688) 0.66
base_tr_resolver_and_hygiene 134667 ns/iter (± 6063) 203378 ns/iter (± 49904) 0.66
codegen_es2015 51424 ns/iter (± 443) 71228 ns/iter (± 12479) 0.72
codegen_es2016 51412 ns/iter (± 425) 70251 ns/iter (± 7207) 0.73
codegen_es2017 51269 ns/iter (± 445) 71341 ns/iter (± 11148) 0.72
codegen_es2018 51406 ns/iter (± 408) 70740 ns/iter (± 10371) 0.73
codegen_es2019 51390 ns/iter (± 419) 71351 ns/iter (± 10574) 0.72
codegen_es2020 51778 ns/iter (± 467) 71596 ns/iter (± 11645) 0.72
codegen_es3 51751 ns/iter (± 758) 69980 ns/iter (± 5504) 0.74
codegen_es5 51398 ns/iter (± 346) 69919 ns/iter (± 6208) 0.74
full_es2015 180495258 ns/iter (± 8806278) 230304038 ns/iter (± 12187965) 0.78
full_es2016 143891553 ns/iter (± 6901884) 182062260 ns/iter (± 11175956) 0.79
full_es2017 155350323 ns/iter (± 8631936) 187889170 ns/iter (± 12497944) 0.83
full_es2018 153646317 ns/iter (± 6672768) 189012774 ns/iter (± 18599292) 0.81
full_es2019 157945835 ns/iter (± 11349004) 230066486 ns/iter (± 52212650) 0.69
full_es2020 154423053 ns/iter (± 7580218) 195235626 ns/iter (± 59072193) 0.79
full_es3 225540446 ns/iter (± 22510667) 273961200 ns/iter (± 174511502) 0.82
full_es5 202349797 ns/iter (± 8948752) 247778411 ns/iter (± 15264748) 0.82
parser 656935 ns/iter (± 22811) 835462 ns/iter (± 110680) 0.79
ser_ast_node 152 ns/iter (± 0) 188 ns/iter (± 34) 0.81
ser_serde 160 ns/iter (± 3) 205 ns/iter (± 31) 0.78
emit_colors 19435226 ns/iter (± 25720781) 15183246 ns/iter (± 21646959) 1.28
emit_large 122357435 ns/iter (± 188613070) 105353546 ns/iter (± 163141172) 1.16
base_clone 2402030 ns/iter (± 118342) 2972174 ns/iter (± 331381) 0.81
fold_span 4198956 ns/iter (± 509088) 5090971 ns/iter (± 832645) 0.82
fold_span_panic 5394560 ns/iter (± 1590989) 5485211 ns/iter (± 1475293) 0.98
visit_mut_span 4345727 ns/iter (± 1070230) 3534278 ns/iter (± 540709) 1.23
visit_mut_span_panic 4405983 ns/iter (± 1432067) 3608942 ns/iter (± 757899) 1.22
ast_clone 16904 ns/iter (± 238) 25849 ns/iter (± 4473) 0.65
ast_clone_to_stable 51315 ns/iter (± 412) 80228 ns/iter (± 8518) 0.64
ast_clone_to_stable_then_to_unstable 92930 ns/iter (± 668) 143086 ns/iter (± 19419) 0.65
json_deserialize 1931129 ns/iter (± 5036) 2786211 ns/iter (± 335181) 0.69
json_serialize 85026 ns/iter (± 421) 121780 ns/iter (± 21770) 0.70
boxing_boxed 144 ns/iter (± 1) 200 ns/iter (± 23) 0.72
boxing_boxed_clone 71 ns/iter (± 0) 89 ns/iter (± 17) 0.80
boxing_unboxed 130 ns/iter (± 0) 174 ns/iter (± 24) 0.75
boxing_unboxed_clone 63 ns/iter (± 0) 84 ns/iter (± 9) 0.75
time_10 331 ns/iter (± 2) 388 ns/iter (± 45) 0.85
time_15 694 ns/iter (± 2) 797 ns/iter (± 84) 0.87
time_20 1274 ns/iter (± 4) 1479 ns/iter (± 126) 0.86
time_40 7103 ns/iter (± 18) 5452 ns/iter (± 442) 1.30
time_5 101 ns/iter (± 1) 126 ns/iter (± 10) 0.80
time_60 16008 ns/iter (± 48) 11488 ns/iter (± 800) 1.39

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.