Skip to content

Commit

Permalink
fix(es): Resolve jsc.baseUrl for .swcrc specified by `--config-fi…
Browse files Browse the repository at this point in the history
…le` (#7801)

**Related issue:**

 - Closes #7800
  • Loading branch information
kdy1 authored Aug 14, 2023
1 parent 0f66ee3 commit fe1ca26
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 63 deletions.
2 changes: 1 addition & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ impl Options {
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum RootMode {
#[serde(rename = "root")]
Root,
Expand Down
133 changes: 78 additions & 55 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ use swc_common::{
BytePos, FileName, Mark, SourceFile, SourceMap, Spanned, GLOBALS,
};
pub use swc_config::config_types::{BoolConfig, BoolOr, BoolOrDataConfig};
use swc_config::merge::Merge;
use swc_ecma_ast::{EsVersion, Ident, Program};
use swc_ecma_codegen::{self, text_writer::WriteJs, Emitter, Node};
use swc_ecma_loader::resolvers::{
Expand Down Expand Up @@ -755,67 +754,73 @@ impl Compiler {

let root = root.as_ref().unwrap_or(&CUR_DIR);

let config_file = match config_file {
Some(ConfigFile::Str(ref s)) => Some(load_swcrc(Path::new(&s))?),
_ => None,
let swcrc_path = match config_file {
Some(ConfigFile::Str(s)) => Some(PathBuf::from(s.clone())),
_ => {
if *swcrc {
if let FileName::Real(ref path) = name {
find_swcrc(path, root, *root_mode)
} else {
None
}
} else {
None
}
}
};

if let FileName::Real(ref path) = name {
if *swcrc {
let mut parent = path.parent();
while let Some(dir) = parent {
let swcrc = dir.join(".swcrc");

if swcrc.exists() {
let config = load_swcrc(&swcrc)?;

let mut config = config
.into_config(Some(path))
.context("failed to process config file")?;

if let Some(config_file) = config_file {
config.merge(config_file.into_config(Some(path))?)
}

if let Some(c) = &mut config {
if c.jsc.base_url != PathBuf::new() {
let joined = dir.join(&c.jsc.base_url);
c.jsc.base_url = if cfg!(target_os = "windows")
&& c.jsc.base_url.as_os_str() == "."
{
dir.canonicalize().with_context(|| {
format!(
"failed to canonicalize base url using the path \
of .swcrc\nDir: {}\n(Used logic for windows)",
dir.display(),
)
})?
} else {
joined.canonicalize().with_context(|| {
format!(
"failed to canonicalize base url using the path \
of .swcrc\nPath: {}\nDir: {}\nbaseUrl: {}",
joined.display(),
dir.display(),
c.jsc.base_url.display()
)
})?
};
}
}

return Ok(config);
}
let config_file = match swcrc_path.as_deref() {
Some(s) => Some(load_swcrc(s)?),
_ => None,
};
let filename_path = match name {
FileName::Real(p) => Some(&**p),
_ => None,
};

if dir == root && *root_mode == RootMode::Root {
break;
if let Some(filename_path) = filename_path {
if let Some(config) = config_file {
let dir = swcrc_path
.as_deref()
.and_then(|p| p.parent())
.expect(".swcrc path should have parent dir");

let mut config = config
.into_config(Some(filename_path))
.context("failed to process config file")?;

if let Some(c) = &mut config {
if c.jsc.base_url != PathBuf::new() {
let joined = dir.join(&c.jsc.base_url);
c.jsc.base_url = if cfg!(target_os = "windows")
&& c.jsc.base_url.as_os_str() == "."
{
dir.canonicalize().with_context(|| {
format!(
"failed to canonicalize base url using the path of \
.swcrc\nDir: {}\n(Used logic for windows)",
dir.display(),
)
})?
} else {
joined.canonicalize().with_context(|| {
format!(
"failed to canonicalize base url using the path of \
.swcrc\nPath: {}\nDir: {}\nbaseUrl: {}",
joined.display(),
dir.display(),
c.jsc.base_url.display()
)
})?
};
}
parent = dir.parent();
}

return Ok(config);
}

let config_file = config_file.unwrap_or_default();
let config = config_file.into_config(Some(path))?;
let config = config_file.into_config(Some(filename_path))?;

return Ok(config);
}
Expand All @@ -832,7 +837,7 @@ impl Compiler {
}
}
})
.with_context(|| format!("failed to read swcrc file ({})", name))
.with_context(|| format!("failed to read .swcrc file for input file at `{}`", name))
}

/// This method returns [None] if a file should be skipped.
Expand Down Expand Up @@ -1258,6 +1263,24 @@ impl Compiler {
}
}

fn find_swcrc(path: &Path, root: &Path, root_mode: RootMode) -> Option<PathBuf> {
let mut parent = path.parent();
while let Some(dir) = parent {
let swcrc = dir.join(".swcrc");

if swcrc.exists() {
return Some(swcrc);
}

if dir == root && root_mode == RootMode::Root {
break;
}
parent = dir.parent();
}

None
}

#[tracing::instrument(level = "info", skip_all)]
fn load_swcrc(path: &Path) -> Result<Rc, Error> {
let content = read_to_string(path).context("failed to read config (.swcrc) file")?;
Expand Down
2 changes: 1 addition & 1 deletion node-swc/__tests__/error_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it("should work", () => {
it("should report good error", () => {
expect(() => {
swc.transformFileSync(__dirname + "/../tests/error/simple.js");
}).toThrow("failed to read swcrc file");
}).toThrow("failed to deserialize .swcrc (json) file");
});

it("should report good error (handler)", () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"@swc/plugin-jest": "latest",
"@taplo/cli": "^0.3.2",
"@types/jest": "^28.1.4",
"@types/node": "^14.14.41",
"@types/node": "^20.5.0",
"@types/terser": "^3.12.0",
"acorn": "^8.6.0",
"acorn-jsx": "^5.3.2",
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,7 @@ __metadata:
"@swc/plugin-jest": latest
"@taplo/cli": ^0.3.2
"@types/jest": ^28.1.4
"@types/node": ^14.14.41
"@types/node": ^20.5.0
"@types/terser": ^3.12.0
acorn: ^8.6.0
acorn-jsx: ^5.3.2
Expand Down Expand Up @@ -2785,10 +2785,10 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^14.14.41":
version: 14.18.48
resolution: "@types/node@npm:14.18.48"
checksum: bc410153cd12b5f5218c2c2a5ab4a65e8dc99ebffe98261a30e9d4beb8f9a0e4ee48c41b850f8cb80da6ed1024e42bba8dee1592869aa5b1826ddb7c29ad6899
"@types/node@npm:^20.5.0":
version: 20.5.0
resolution: "@types/node@npm:20.5.0"
checksum: 659bc5fc93b5c02bd88ca4bfae4f6b9dc307d45884d1dd9d69df85819a9943cdc00cd3c87eec3048866df6a67f52297f74d170e47a44f61edb3e8f770d94e85e
languageName: node
linkType: hard

Expand Down

1 comment on commit fe1ca26

@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: fe1ca26 Previous: e527c12 Ratio
es/full/bugs-1 296665 ns/iter (± 5591) 297853 ns/iter (± 8847) 1.00
es/full/minify/libraries/antd 1417616450 ns/iter (± 12880450) 1310765582 ns/iter (± 4435084) 1.08
es/full/minify/libraries/d3 300195188 ns/iter (± 5734109) 274788092 ns/iter (± 2473237) 1.09
es/full/minify/libraries/echarts 1167013457 ns/iter (± 21327701) 1056011049 ns/iter (± 5136094) 1.11
es/full/minify/libraries/jquery 87845045 ns/iter (± 644684) 85172699 ns/iter (± 135928) 1.03
es/full/minify/libraries/lodash 101009767 ns/iter (± 1487556) 98459040 ns/iter (± 346164) 1.03
es/full/minify/libraries/moment 51024735 ns/iter (± 94607) 50223077 ns/iter (± 80212) 1.02
es/full/minify/libraries/react 18296757 ns/iter (± 186704) 18002768 ns/iter (± 65107) 1.02
es/full/minify/libraries/terser 230045951 ns/iter (± 4161414) 219888250 ns/iter (± 733433) 1.05
es/full/minify/libraries/three 415789690 ns/iter (± 4263690) 386834233 ns/iter (± 1417125) 1.07
es/full/minify/libraries/typescript 2805731536 ns/iter (± 11567293) 2661688155 ns/iter (± 11433731) 1.05
es/full/minify/libraries/victory 622509144 ns/iter (± 19057283) 573750764 ns/iter (± 4753642) 1.08
es/full/minify/libraries/vue 124486106 ns/iter (± 1844332) 120717205 ns/iter (± 605973) 1.03
es/full/codegen/es3 34443 ns/iter (± 74) 35215 ns/iter (± 106) 0.98
es/full/codegen/es5 34518 ns/iter (± 103) 35057 ns/iter (± 101) 0.98
es/full/codegen/es2015 34419 ns/iter (± 125) 35164 ns/iter (± 65) 0.98
es/full/codegen/es2016 34521 ns/iter (± 346) 35177 ns/iter (± 99) 0.98
es/full/codegen/es2017 34326 ns/iter (± 102) 35221 ns/iter (± 83) 0.97
es/full/codegen/es2018 34372 ns/iter (± 148) 35116 ns/iter (± 82) 0.98
es/full/codegen/es2019 34352 ns/iter (± 139) 35295 ns/iter (± 142) 0.97
es/full/codegen/es2020 34357 ns/iter (± 95) 35219 ns/iter (± 51) 0.98
es/full/all/es3 172803410 ns/iter (± 852403) 167873349 ns/iter (± 800478) 1.03
es/full/all/es5 163915784 ns/iter (± 1349704) 159957927 ns/iter (± 989092) 1.02
es/full/all/es2015 123374710 ns/iter (± 601065) 119717466 ns/iter (± 595030) 1.03
es/full/all/es2016 122477574 ns/iter (± 1326481) 119122449 ns/iter (± 777458) 1.03
es/full/all/es2017 122339539 ns/iter (± 604776) 117980371 ns/iter (± 834533) 1.04
es/full/all/es2018 119241749 ns/iter (± 731825) 116599886 ns/iter (± 398948) 1.02
es/full/all/es2019 118281645 ns/iter (± 725933) 115893928 ns/iter (± 995573) 1.02
es/full/all/es2020 113935645 ns/iter (± 477055) 110893589 ns/iter (± 532487) 1.03
es/full/parser 503563 ns/iter (± 5652) 496107 ns/iter (± 5778) 1.02
es/full/base/fixer 17483 ns/iter (± 70) 18502 ns/iter (± 69) 0.94
es/full/base/resolver_and_hygiene 79112 ns/iter (± 161) 81608 ns/iter (± 192) 0.97
serialization of serde 295 ns/iter (± 2) 299 ns/iter (± 0) 0.99
css/minify/libraries/bootstrap 28874055 ns/iter (± 425853) 29072137 ns/iter (± 49942) 0.99
css/visitor/compare/clone 1638739 ns/iter (± 9596) 1631787 ns/iter (± 1355) 1.00
css/visitor/compare/visit_mut_span 1774332 ns/iter (± 3885) 1754399 ns/iter (± 2513) 1.01
css/visitor/compare/visit_mut_span_panic 1851417 ns/iter (± 5499) 1827899 ns/iter (± 3139) 1.01
css/visitor/compare/fold_span 2580863 ns/iter (± 16883) 2587703 ns/iter (± 13837) 1.00
css/visitor/compare/fold_span_panic 2767776 ns/iter (± 15272) 2740419 ns/iter (± 17520) 1.01
css/lexer/bootstrap_5_1_3 4493075 ns/iter (± 5295) 4441576 ns/iter (± 4999) 1.01
css/lexer/foundation_6_7_4 3778608 ns/iter (± 4569) 3733921 ns/iter (± 125161) 1.01
css/lexer/tailwind_3_1_1 717159 ns/iter (± 788) 712834 ns/iter (± 626) 1.01
css/parser/bootstrap_5_1_3 19664743 ns/iter (± 112028) 19817590 ns/iter (± 54116) 0.99
css/parser/foundation_6_7_4 15533572 ns/iter (± 273780) 15952394 ns/iter (± 22214) 0.97
css/parser/tailwind_3_1_1 3016064 ns/iter (± 6707) 3026839 ns/iter (± 10151) 1.00
es/codegen/colors 727456 ns/iter (± 395578) 738916 ns/iter (± 405942) 0.98
es/codegen/large 3146727 ns/iter (± 1677202) 3151157 ns/iter (± 1682542) 1.00
es/codegen/with-parser/colors 46440 ns/iter (± 209) 45629 ns/iter (± 485) 1.02
es/codegen/with-parser/large 491542 ns/iter (± 979) 489536 ns/iter (± 777) 1.00
es/minify/libraries/antd 1196686730 ns/iter (± 28294856) 1139865278 ns/iter (± 7750642) 1.05
es/minify/libraries/d3 245533920 ns/iter (± 1594102) 241198406 ns/iter (± 352949) 1.02
es/minify/libraries/echarts 951450407 ns/iter (± 23733309) 902422607 ns/iter (± 4077635) 1.05
es/minify/libraries/jquery 75413031 ns/iter (± 682065) 74246995 ns/iter (± 92568) 1.02
es/minify/libraries/lodash 90508309 ns/iter (± 671207) 88766260 ns/iter (± 190484) 1.02
es/minify/libraries/moment 44659277 ns/iter (± 368815) 43734505 ns/iter (± 64773) 1.02
es/minify/libraries/react 16246300 ns/iter (± 119562) 16106186 ns/iter (± 77217) 1.01
es/minify/libraries/terser 190892146 ns/iter (± 2584495) 187594086 ns/iter (± 376803) 1.02
es/minify/libraries/three 340658726 ns/iter (± 13937443) 322440538 ns/iter (± 811938) 1.06
es/minify/libraries/typescript 2351135169 ns/iter (± 25629848) 2273444472 ns/iter (± 11389614) 1.03
es/minify/libraries/victory 498727882 ns/iter (± 6309830) 481876406 ns/iter (± 1826121) 1.03
es/minify/libraries/vue 108969721 ns/iter (± 714696) 107429706 ns/iter (± 194663) 1.01
es/visitor/compare/clone 1942619 ns/iter (± 8690) 1937392 ns/iter (± 9051) 1.00
es/visitor/compare/visit_mut_span 2243940 ns/iter (± 4040) 2264187 ns/iter (± 2437) 0.99
es/visitor/compare/visit_mut_span_panic 2314505 ns/iter (± 8377) 2324664 ns/iter (± 2902) 1.00
es/visitor/compare/fold_span 3366617 ns/iter (± 9921) 3354564 ns/iter (± 8955) 1.00
es/visitor/compare/fold_span_panic 3469188 ns/iter (± 6554) 3510530 ns/iter (± 6658) 0.99
es/lexer/colors 13305 ns/iter (± 60) 13385 ns/iter (± 61) 0.99
es/lexer/angular 6200450 ns/iter (± 25721) 6224795 ns/iter (± 5435) 1.00
es/lexer/backbone 807744 ns/iter (± 475) 805421 ns/iter (± 1399) 1.00
es/lexer/jquery 4550202 ns/iter (± 7037) 4552794 ns/iter (± 7889) 1.00
es/lexer/jquery mobile 7013218 ns/iter (± 4487) 6970063 ns/iter (± 8957) 1.01
es/lexer/mootools 3643983 ns/iter (± 9135) 3605187 ns/iter (± 6096) 1.01
es/lexer/underscore 678020 ns/iter (± 873) 680627 ns/iter (± 1363) 1.00
es/lexer/three 21690225 ns/iter (± 51334) 21765516 ns/iter (± 21613) 1.00
es/lexer/yui 3929042 ns/iter (± 2203) 3904730 ns/iter (± 5425) 1.01
es/parser/colors 27100 ns/iter (± 84) 27188 ns/iter (± 63) 1.00
es/parser/angular 13527382 ns/iter (± 66339) 13373655 ns/iter (± 91902) 1.01
es/parser/backbone 1991018 ns/iter (± 11542) 1971983 ns/iter (± 7954) 1.01
es/parser/jquery 10863778 ns/iter (± 110072) 10799851 ns/iter (± 28545) 1.01
es/parser/jquery mobile 16683481 ns/iter (± 48465) 16444644 ns/iter (± 55974) 1.01
es/parser/mootools 8363031 ns/iter (± 31488) 8297126 ns/iter (± 21124) 1.01
es/parser/underscore 1717267 ns/iter (± 9296) 1712344 ns/iter (± 11295) 1.00
es/parser/three 47566231 ns/iter (± 827096) 46415565 ns/iter (± 228027) 1.02
es/parser/yui 8299270 ns/iter (± 41665) 8184245 ns/iter (± 28875) 1.01
es/preset-env/usage/builtin_type 135719 ns/iter (± 31524) 135746 ns/iter (± 33467) 1.00
es/preset-env/usage/property 16692 ns/iter (± 79) 16673 ns/iter (± 41) 1.00
es/resolver/typescript 89680036 ns/iter (± 1643505) 90069429 ns/iter (± 675043) 1.00
es/fixer/typescript 64996440 ns/iter (± 1449749) 64269461 ns/iter (± 463233) 1.01
es/hygiene/typescript 141248546 ns/iter (± 1985704) 131766512 ns/iter (± 527431) 1.07
es/resolver_with_hygiene/typescript 257038535 ns/iter (± 3802440) 246050781 ns/iter (± 711301) 1.04
es/visitor/base-perf/module_clone 59622 ns/iter (± 255) 60134 ns/iter (± 341) 0.99
es/visitor/base-perf/fold_empty 63862 ns/iter (± 276) 64091 ns/iter (± 414) 1.00
es/visitor/base-perf/fold_noop_impl_all 64069 ns/iter (± 221) 64292 ns/iter (± 243) 1.00
es/visitor/base-perf/fold_noop_impl_vec 64093 ns/iter (± 575) 64368 ns/iter (± 447) 1.00
es/visitor/base-perf/boxing_boxed_clone 57 ns/iter (± 0) 56 ns/iter (± 0) 1.02
es/visitor/base-perf/boxing_unboxed_clone 40 ns/iter (± 0) 40 ns/iter (± 0) 1
es/visitor/base-perf/boxing_boxed 105 ns/iter (± 0) 108 ns/iter (± 0) 0.97
es/visitor/base-perf/boxing_unboxed 75 ns/iter (± 0) 78 ns/iter (± 0) 0.96
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2564 ns/iter (± 10) 2579 ns/iter (± 7) 0.99
es/base/parallel/resolver/typescript 3804184787 ns/iter (± 286215483) 4124500282 ns/iter (± 228152147) 0.92
es/base/parallel/hygiene/typescript 1436214226 ns/iter (± 18912821) 1430346648 ns/iter (± 18166792) 1.00
misc/visitors/time-complexity/time 5 137 ns/iter (± 1) 151 ns/iter (± 0) 0.91
misc/visitors/time-complexity/time 10 399 ns/iter (± 0) 432 ns/iter (± 0) 0.92
misc/visitors/time-complexity/time 15 683 ns/iter (± 24) 810 ns/iter (± 1) 0.84
misc/visitors/time-complexity/time 20 1052 ns/iter (± 4) 1300 ns/iter (± 25) 0.81
misc/visitors/time-complexity/time 40 4145 ns/iter (± 6) 5028 ns/iter (± 2) 0.82
misc/visitors/time-complexity/time 60 8798 ns/iter (± 3) 9059 ns/iter (± 2) 0.97
es/full-target/es2016 228049 ns/iter (± 429) 233973 ns/iter (± 2595) 0.97
es/full-target/es2017 215598 ns/iter (± 394) 223535 ns/iter (± 911) 0.96
es/full-target/es2018 205035 ns/iter (± 718) 211017 ns/iter (± 703) 0.97
es2020_nullish_coalescing 71016 ns/iter (± 422) 71509 ns/iter (± 636) 0.99
es2020_optional_chaining 82618 ns/iter (± 236) 80828 ns/iter (± 402) 1.02
es2022_class_properties 114113 ns/iter (± 464) 116920 ns/iter (± 606) 0.98
es2018_object_rest_spread 75168 ns/iter (± 271) 76448 ns/iter (± 343) 0.98
es2019_optional_catch_binding 65448 ns/iter (± 192) 65622 ns/iter (± 245) 1.00
es2017_async_to_generator 65585 ns/iter (± 220) 66146 ns/iter (± 357) 0.99
es2016_exponentiation 69894 ns/iter (± 218) 69912 ns/iter (± 159) 1.00
es2015_arrow 72777 ns/iter (± 172) 73140 ns/iter (± 222) 1.00
es2015_block_scoped_fn 69894 ns/iter (± 146) 70715 ns/iter (± 455) 0.99
es2015_block_scoping 125212 ns/iter (± 380) 124669 ns/iter (± 1956) 1.00

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

Please sign in to comment.