Skip to content

Commit

Permalink
fix(es): Allow missing .map file (#7141)
Browse files Browse the repository at this point in the history
  • Loading branch information
limerickgds authored Apr 2, 2023
1 parent f73f96d commit 3e6a186
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 32 deletions.
89 changes: 57 additions & 32 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,43 +324,68 @@ impl Compiler {
}
};

let path = match data_url {
Some(data_url) => dir.join(data_url).display().to_string(),
None => {
format!("{}.map", dir.join(filename).display())
}
};

let file = File::open(&path)
.or_else(|err| {
// Old behavior. This check would prevent
// regressions.
let f = format!("{}.map", filename.display());
let map_path = match data_url {
Some(data_url) => {
let mut map_path = dir.join(data_url);
if !map_path.exists() {
// Old behavior. This check would prevent
// regressions.
// Perhaps it shouldn't be supported. Sometimes
// developers don't want to expose their source code.
// Map files are for internal troubleshooting
// convenience.
map_path = PathBuf::from(format!(
"{}.map",
filename.display()
));
if !map_path.exists() {
bail!("failed to find input source map file")
}
}

match File::open(f) {
Ok(v) => Ok(v),
Err(_) => Err(err),
Some(map_path)
}
None => {
// Old behavior.
let map_path =
PathBuf::from(format!("{}.map", filename.display()));
if map_path.exists() {
Some(map_path)
} else {
None
}
})
.context("failed to open input source map file");

let file = if !is_default {
file?
} else {
match file {
Ok(v) => v,
Err(_) => return Ok(None),
}
};

Ok(Some(sourcemap::SourceMap::from_reader(file).with_context(
|| {
format!(
"failed to read input source map from file at {}",
path
)
},
)?))
match map_path {
Some(map_path) => {
let path = map_path.display().to_string();
let file = File::open(&path);

// Old behavior.
let file = if !is_default {
file?
} else {
match file {
Ok(v) => v,
Err(_) => return Ok(None),
}
};

Ok(Some(
sourcemap::SourceMap::from_reader(file).with_context(
|| {
format!(
"failed to read input source map
from file at {}",
path
)
},
)?,
))
}
None => Ok(None),
}
}
_ => {
tracing::error!("Failed to load source map for non-file input");
Expand Down
14 changes: 14 additions & 0 deletions crates/swc/tests/fixture/sourcemap/015/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"module": {
"type": "commonjs"
},
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false
},
"target": "es2016"
},
"sourceMaps": true,
"inputSourceMap": true
}
21 changes: 21 additions & 0 deletions crates/swc/tests/fixture/sourcemap/015/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe("multiline comments", () => {
it("test1", () => {
expect(false).toBe(true);
});

it("test2", () => {
/**/
expect(false).toBe(true);
});

it("test3", () => {
/*
*
*/
expect(false).toBe(true);
});

it("test4", () => {
expect(false).toBe(true);
});
});
17 changes: 17 additions & 0 deletions crates/swc/tests/fixture/sourcemap/015/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
describe("multiline comments", ()=>{
it("test1", ()=>{
expect(false).toBe(true);
});
it("test2", ()=>{
/**/ expect(false).toBe(true);
});
it("test3", ()=>{
/*
*
*/ expect(false).toBe(true);
});
it("test4", ()=>{
expect(false).toBe(true);
});
});
16 changes: 16 additions & 0 deletions crates/swc/tests/fixture/sourcemap/015/output/index.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"mappings": ";AAAAA,SAAS,sBAAsB,IAAM;IACjCC,GAAG,SAAS,IAAM;QACdC,OAAO,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B;IAEAF,GAAG,SAAS,IAAM;QACd,EAAE,GACFC,OAAO,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B;IAEAF,GAAG,SAAS,IAAM;QACd;;SAEC,GACDC,OAAO,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B;IAEAF,GAAG,SAAS,IAAM;QACdC,OAAO,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B;AACJ",
"names": [
"describe",
"it",
"expect",
"toBe"
],
"sources": [
"../../input/index.js"
],
"sourcesContent": [
"describe(\"multiline comments\", () => {\n it(\"test1\", () => {\n expect(false).toBe(true);\n });\n\n it(\"test2\", () => {\n /**/\n expect(false).toBe(true);\n });\n\n it(\"test3\", () => {\n /*\n *\n */\n expect(false).toBe(true);\n });\n\n it(\"test4\", () => {\n expect(false).toBe(true);\n });\n});\n"
],
"version": 3
}

1 comment on commit 3e6a186

@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: 3e6a186 Previous: 976667f Ratio
es/full/bugs-1 317035 ns/iter (± 10262) 298054 ns/iter (± 10323) 1.06
es/full/minify/libraries/antd 1698543792 ns/iter (± 32157292) 1595221496 ns/iter (± 17620009) 1.06
es/full/minify/libraries/d3 307545506 ns/iter (± 5010156) 291116201 ns/iter (± 7580105) 1.06
es/full/minify/libraries/echarts 1275224709 ns/iter (± 20989048) 1191995222 ns/iter (± 16608863) 1.07
es/full/minify/libraries/jquery 91291729 ns/iter (± 1655977) 89463557 ns/iter (± 1226184) 1.02
es/full/minify/libraries/lodash 104635009 ns/iter (± 761647) 105452875 ns/iter (± 13076392) 0.99
es/full/minify/libraries/moment 51635542 ns/iter (± 382175) 51503098 ns/iter (± 420571) 1.00
es/full/minify/libraries/react 19316357 ns/iter (± 275976) 18777514 ns/iter (± 119250) 1.03
es/full/minify/libraries/terser 241615873 ns/iter (± 3396312) 241597557 ns/iter (± 2308772) 1.00
es/full/minify/libraries/three 437207996 ns/iter (± 6394612) 429297842 ns/iter (± 7141073) 1.02
es/full/minify/libraries/typescript 3315511763 ns/iter (± 40535187) 3024264929 ns/iter (± 22596243) 1.10
es/full/minify/libraries/victory 665833093 ns/iter (± 10337008) 675830795 ns/iter (± 37814770) 0.99
es/full/minify/libraries/vue 128594805 ns/iter (± 866011) 132532429 ns/iter (± 3605162) 0.97
es/full/codegen/es3 28368 ns/iter (± 111) 28820 ns/iter (± 74) 0.98
es/full/codegen/es5 28556 ns/iter (± 37) 28972 ns/iter (± 81) 0.99
es/full/codegen/es2015 28457 ns/iter (± 89) 28931 ns/iter (± 77) 0.98
es/full/codegen/es2016 28260 ns/iter (± 158) 28970 ns/iter (± 616) 0.98
es/full/codegen/es2017 28496 ns/iter (± 68) 28930 ns/iter (± 65) 0.98
es/full/codegen/es2018 28240 ns/iter (± 150) 28938 ns/iter (± 62) 0.98
es/full/codegen/es2019 28113 ns/iter (± 171) 28969 ns/iter (± 185) 0.97
es/full/codegen/es2020 28205 ns/iter (± 219) 28947 ns/iter (± 57) 0.97
es/full/all/es3 182040064 ns/iter (± 2974136) 180262264 ns/iter (± 2044324) 1.01
es/full/all/es5 174799384 ns/iter (± 3348928) 170810683 ns/iter (± 2266063) 1.02
es/full/all/es2015 135156103 ns/iter (± 2932066) 130613481 ns/iter (± 1705982) 1.03
es/full/all/es2016 132090111 ns/iter (± 2400776) 130423760 ns/iter (± 1072599) 1.01
es/full/all/es2017 130577286 ns/iter (± 1228920) 127925073 ns/iter (± 1828929) 1.02
es/full/all/es2018 128071928 ns/iter (± 3549084) 122731709 ns/iter (± 1080514) 1.04
es/full/all/es2019 124351592 ns/iter (± 929221) 121281320 ns/iter (± 1051571) 1.03
es/full/all/es2020 117400340 ns/iter (± 757229) 116688934 ns/iter (± 987426) 1.01
es/full/parser 513829 ns/iter (± 12922) 515057 ns/iter (± 7206) 1.00
es/full/base/fixer 23681 ns/iter (± 137) 22888 ns/iter (± 27) 1.03
es/full/base/resolver_and_hygiene 83675 ns/iter (± 823) 83948 ns/iter (± 134) 1.00
serialization of serde 126 ns/iter (± 0) 127 ns/iter (± 0) 0.99
css/minify/libraries/bootstrap 26743558 ns/iter (± 190739) 27621105 ns/iter (± 160589) 0.97
css/visitor/compare/clone 2125420 ns/iter (± 4875) 2124865 ns/iter (± 7104) 1.00
css/visitor/compare/visit_mut_span 2278881 ns/iter (± 14065) 2305118 ns/iter (± 5290) 0.99
css/visitor/compare/visit_mut_span_panic 2358754 ns/iter (± 4714) 2340496 ns/iter (± 4765) 1.01
css/visitor/compare/fold_span 3083554 ns/iter (± 16267) 3104971 ns/iter (± 11311) 0.99
css/visitor/compare/fold_span_panic 3261700 ns/iter (± 10358) 3225749 ns/iter (± 17557) 1.01
css/lexer/bootstrap_5_1_3 5098060 ns/iter (± 25057) 5131904 ns/iter (± 13023) 0.99
css/lexer/foundation_6_7_4 4292790 ns/iter (± 23214) 4337864 ns/iter (± 4165) 0.99
css/lexer/tailwind_3_1_1 823851 ns/iter (± 1159) 822611 ns/iter (± 888) 1.00
css/parser/bootstrap_5_1_3 20617686 ns/iter (± 170431) 21117854 ns/iter (± 136273) 0.98
css/parser/foundation_6_7_4 16440526 ns/iter (± 54737) 16839177 ns/iter (± 38126) 0.98
css/parser/tailwind_3_1_1 3215052 ns/iter (± 16673) 3241642 ns/iter (± 4051) 0.99
es/codegen/colors 320527 ns/iter (± 181462) 327847 ns/iter (± 185563) 0.98
es/codegen/large 1228127 ns/iter (± 631088) 1246404 ns/iter (± 643828) 0.99
es/codegen/with-parser/colors 46833 ns/iter (± 758) 47109 ns/iter (± 468) 0.99
es/codegen/with-parser/large 502265 ns/iter (± 3149) 518557 ns/iter (± 11521) 0.97
es/minify/libraries/antd 1504764192 ns/iter (± 29044016) 1376435132 ns/iter (± 29787805) 1.09
es/minify/libraries/d3 248647791 ns/iter (± 4199240) 250579287 ns/iter (± 4688700) 0.99
es/minify/libraries/echarts 1078727190 ns/iter (± 9024794) 1043766651 ns/iter (± 20489258) 1.03
es/minify/libraries/jquery 78366581 ns/iter (± 1113673) 78385309 ns/iter (± 529122) 1.00
es/minify/libraries/lodash 93648756 ns/iter (± 1022341) 93340369 ns/iter (± 1385866) 1.00
es/minify/libraries/moment 45783687 ns/iter (± 502885) 45279645 ns/iter (± 277228) 1.01
es/minify/libraries/react 16825234 ns/iter (± 82179) 16762444 ns/iter (± 121301) 1.00
es/minify/libraries/terser 204180504 ns/iter (± 2101476) 203124503 ns/iter (± 1054133) 1.01
es/minify/libraries/three 375455647 ns/iter (± 9434251) 350784224 ns/iter (± 4993770) 1.07
es/minify/libraries/typescript 2816548672 ns/iter (± 23128409) 2567842448 ns/iter (± 14397880) 1.10
es/minify/libraries/victory 565465516 ns/iter (± 15025404) 539959178 ns/iter (± 8700728) 1.05
es/minify/libraries/vue 113878452 ns/iter (± 666734) 114942402 ns/iter (± 1202365) 0.99
es/visitor/compare/clone 2295122 ns/iter (± 8106) 2318843 ns/iter (± 11051) 0.99
es/visitor/compare/visit_mut_span 2646212 ns/iter (± 10706) 2685863 ns/iter (± 5645) 0.99
es/visitor/compare/visit_mut_span_panic 2696219 ns/iter (± 14251) 2723606 ns/iter (± 6092) 0.99
es/visitor/compare/fold_span 3756038 ns/iter (± 24749) 3790974 ns/iter (± 17814) 0.99
es/visitor/compare/fold_span_panic 3898212 ns/iter (± 25037) 3906969 ns/iter (± 7713) 1.00
es/lexer/colors 12931 ns/iter (± 86) 13093 ns/iter (± 16) 0.99
es/lexer/angular 6394735 ns/iter (± 45567) 6410982 ns/iter (± 22651) 1.00
es/lexer/backbone 764621 ns/iter (± 4721) 771368 ns/iter (± 1033) 0.99
es/lexer/jquery 4310435 ns/iter (± 34527) 4309836 ns/iter (± 4172) 1.00
es/lexer/jquery mobile 6707527 ns/iter (± 64580) 6751319 ns/iter (± 8616) 0.99
es/lexer/mootools 3441910 ns/iter (± 11289) 3413553 ns/iter (± 1852) 1.01
es/lexer/underscore 634200 ns/iter (± 3806) 638557 ns/iter (± 1088) 0.99
es/lexer/three 20484672 ns/iter (± 97566) 20539928 ns/iter (± 36511) 1.00
es/lexer/yui 3846027 ns/iter (± 16970) 3842670 ns/iter (± 3790) 1.00
es/parser/colors 28335 ns/iter (± 236) 28493 ns/iter (± 47) 0.99
es/parser/angular 14705215 ns/iter (± 156563) 14883859 ns/iter (± 117139) 0.99
es/parser/backbone 2123300 ns/iter (± 20521) 2168091 ns/iter (± 17447) 0.98
es/parser/jquery 11554338 ns/iter (± 110826) 11651413 ns/iter (± 88081) 0.99
es/parser/jquery mobile 17884937 ns/iter (± 122906) 18132936 ns/iter (± 133153) 0.99
es/parser/mootools 8751185 ns/iter (± 41808) 8883718 ns/iter (± 24381) 0.99
es/parser/underscore 1798703 ns/iter (± 19220) 1830362 ns/iter (± 12136) 0.98
es/parser/three 52841678 ns/iter (± 1136259) 52874614 ns/iter (± 1019183) 1.00
es/parser/yui 8919548 ns/iter (± 67888) 8992259 ns/iter (± 44476) 0.99
es/preset-env/usage/builtin_type 142291 ns/iter (± 32797) 143628 ns/iter (± 34222) 0.99
es/preset-env/usage/property 20997 ns/iter (± 82) 21146 ns/iter (± 99) 0.99
es/resolver/typescript 126209432 ns/iter (± 1780861) 119533842 ns/iter (± 3332860) 1.06
es/fixer/typescript 94129574 ns/iter (± 2148587) 85264274 ns/iter (± 701438) 1.10
es/hygiene/typescript 200045013 ns/iter (± 2899682) 182772962 ns/iter (± 3867393) 1.09
es/resolver_with_hygiene/typescript 347620806 ns/iter (± 4531391) 320864731 ns/iter (± 2613472) 1.08
es/visitor/base-perf/module_clone 80318 ns/iter (± 428) 80608 ns/iter (± 243) 1.00
es/visitor/base-perf/fold_empty 89877 ns/iter (± 567) 90569 ns/iter (± 341) 0.99
es/visitor/base-perf/fold_noop_impl_all 90086 ns/iter (± 553) 90905 ns/iter (± 431) 0.99
es/visitor/base-perf/fold_noop_impl_vec 90192 ns/iter (± 705) 91353 ns/iter (± 315) 0.99
es/visitor/base-perf/boxing_boxed_clone 55 ns/iter (± 0) 56 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_unboxed_clone 41 ns/iter (± 0) 42 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_boxed 103 ns/iter (± 0) 102 ns/iter (± 0) 1.01
es/visitor/base-perf/boxing_unboxed 77 ns/iter (± 0) 78 ns/iter (± 0) 0.99
es/visitor/base-perf/visit_contains_this 3610 ns/iter (± 74) 3513 ns/iter (± 85) 1.03
es/base/parallel/resolver/typescript 6564634539 ns/iter (± 590313023) 6144535654 ns/iter (± 459052033) 1.07
es/base/parallel/hygiene/typescript 2493008979 ns/iter (± 17972183) 2192710717 ns/iter (± 19045147) 1.14
misc/visitors/time-complexity/time 5 103 ns/iter (± 0) 104 ns/iter (± 0) 0.99
misc/visitors/time-complexity/time 10 321 ns/iter (± 0) 326 ns/iter (± 2) 0.98
misc/visitors/time-complexity/time 15 651 ns/iter (± 4) 661 ns/iter (± 0) 0.98
misc/visitors/time-complexity/time 20 1209 ns/iter (± 6) 1253 ns/iter (± 12) 0.96
misc/visitors/time-complexity/time 40 6114 ns/iter (± 27) 6199 ns/iter (± 7) 0.99
misc/visitors/time-complexity/time 60 15448 ns/iter (± 114) 15575 ns/iter (± 34) 0.99
es/full-target/es2016 249394 ns/iter (± 1713) 253012 ns/iter (± 478) 0.99
es/full-target/es2017 243742 ns/iter (± 1039) 246955 ns/iter (± 437) 0.99
es/full-target/es2018 232330 ns/iter (± 861) 235430 ns/iter (± 255) 0.99
es2020_nullish_coalescing 91683 ns/iter (± 512) 93167 ns/iter (± 392) 0.98
es2020_optional_chaining 122181 ns/iter (± 309) 123787 ns/iter (± 194) 0.99
es2022_class_properties 146605 ns/iter (± 648) 148662 ns/iter (± 388) 0.99
es2018_object_rest_spread 94939 ns/iter (± 534) 96249 ns/iter (± 165) 0.99
es2019_optional_catch_binding 84207 ns/iter (± 493) 85251 ns/iter (± 263) 0.99
es2017_async_to_generator 84712 ns/iter (± 436) 86109 ns/iter (± 204) 0.98
es2016_exponentiation 87524 ns/iter (± 625) 90371 ns/iter (± 235) 0.97
es2015_arrow 90211 ns/iter (± 475) 93896 ns/iter (± 217) 0.96
es2015_block_scoped_fn 89415 ns/iter (± 344) 91800 ns/iter (± 208) 0.97
es2015_block_scoping 164447 ns/iter (± 882) 169353 ns/iter (± 175) 0.97

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

Please sign in to comment.