Skip to content

Commit

Permalink
fix(es/loader): Prefer ESM (#3089)
Browse files Browse the repository at this point in the history
swc_ecma_loader:
 - Check for `module` field and prefer it over `main`.
  • Loading branch information
kdy1 authored Dec 21, 2021
1 parent e33248c commit ba2563f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/swc_bundler/examples/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn do_test(_entry: &Path, entries: HashMap<String, FileName>, inline: bool, mini
NodeModulesResolver::new(TargetEnv::Node, Default::default()),
),
swc_bundler::Config {
require: true,
require: false,
disable_inliner: !inline,
external_modules: Default::default(),
disable_fixer: minify,
Expand Down
14 changes: 10 additions & 4 deletions crates/swc_ecma_loader/src/resolvers/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ struct PackageJson {
main: Option<String>,
#[serde(default)]
browser: Option<Browser>,
#[serde(default)]
module: Option<String>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -178,13 +180,17 @@ impl NodeModulesResolver {

let main_fields = match self.target_env {
TargetEnv::Node => {
vec![pkg.main.as_ref().clone()]
vec![pkg.module.as_ref().clone(), pkg.main.as_ref().clone()]
}
TargetEnv::Browser => {
if let Some(browser) = &pkg.browser {
match browser {
Browser::Str(path) => {
vec![Some(path), pkg.main.as_ref().clone()]
vec![
Some(path),
pkg.module.as_ref().clone(),
pkg.main.as_ref().clone(),
]
}
Browser::Obj(map) => {
let bucket = BROWSER_CACHE.entry(pkg_dir.to_path_buf()).or_default();
Expand Down Expand Up @@ -237,11 +243,11 @@ impl NodeModulesResolver {
}
}
}
vec![pkg.main.as_ref().clone()]
vec![pkg.module.as_ref().clone(), pkg.main.as_ref().clone()]
}
}
} else {
vec![pkg.main.as_ref().clone()]
vec![pkg.module.as_ref().clone(), pkg.main.as_ref().clone()]
}
}
};
Expand Down

1 comment on commit ba2563f

@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: ba2563f Previous: c9def54 Ratio
full_es2015 216390385 ns/iter (± 14649968) 183588506 ns/iter (± 5371293) 1.18
full_es2016 185160047 ns/iter (± 13503985) 151987907 ns/iter (± 5383572) 1.22
full_es2017 187724716 ns/iter (± 19813565) 156324968 ns/iter (± 6411452) 1.20
full_es2018 172649718 ns/iter (± 13651751) 156154979 ns/iter (± 3731519) 1.11
full_es2019 171821453 ns/iter (± 15037310) 153621108 ns/iter (± 7532837) 1.12
full_es2020 172426342 ns/iter (± 14886998) 153010972 ns/iter (± 4934926) 1.13
full_es3 237646080 ns/iter (± 20946292) 209931878 ns/iter (± 9927739) 1.13
full_es5 217306388 ns/iter (± 16446117) 195006307 ns/iter (± 10438175) 1.11
parser 791618 ns/iter (± 118346) 682190 ns/iter (± 19054) 1.16

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

Please sign in to comment.