Skip to content

Commit

Permalink
feat(bindings/cli): Append sourceMappingURL= to the output (#6873)
Browse files Browse the repository at this point in the history
  • Loading branch information
realtimetodie authored Jan 29, 2023
1 parent cb3901c commit c4db8e4
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions bindings/swc_cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn resolve_output_file_path(
}

fn emit_output(
output: &TransformOutput,
mut output: TransformOutput,
out_dir: &Option<PathBuf>,
file_path: &Path,
file_extension: PathBuf,
Expand All @@ -219,12 +219,18 @@ fn emit_output(
fs::create_dir_all(output_dir)?;
}

fs::write(&output_file_path, &output.code)?;

if let Some(source_map) = &output.map {
let source_map_path = output_file_path.with_extension("js.map");

output.code.push_str("\n//# sourceMappingURL=");
output
.code
.push_str(&source_map_path.file_name().unwrap().to_string_lossy());

fs::write(source_map_path, source_map)?;
}

fs::write(output_file_path, &output.code)?;
} else {
println!(
"{}\n{}\n{}",
Expand Down Expand Up @@ -400,27 +406,38 @@ impl CompileOptions {
)?;
let mut buf = File::create(single_out_file)?;
let mut buf_srcmap = None;
let mut source_map_path = None;

// write all transformed files to single output buf
result?.iter().try_for_each(|r| {
if let Some(src_map) = r.map.as_ref() {
if buf_srcmap.is_none() {
// we'll init buf lazily as we don't read ./.swcrc directly to determine if
// sourcemap would be generated or not
let srcmap_buf_name =
if let Some(source_map_target) = &self.source_map_target {
File::create(source_map_target)?
} else {
File::create(single_out_file.with_extension(format!(
"{}map",
if let Some(ext) = single_out_file.extension() {
format!("{}.", ext.to_string_lossy())
} else {
"".to_string()
}
)))?
};
buf_srcmap = Some(srcmap_buf_name);
let map_out_file = if let Some(source_map_target) = &self.source_map_target
{
source_map_path = Some(source_map_target.clone());
source_map_target.into()
} else {
let map_out_file = single_out_file.with_extension(format!(
"{}map",
if let Some(ext) = single_out_file.extension() {
format!("{}.", ext.to_string_lossy())
} else {
"".to_string()
}
));

// Get the filename of the source map, as the source map will
// be created in the same directory next to the output.
source_map_path = Some(
map_out_file
.file_name()
.unwrap()
.to_string_lossy()
.to_string(),
);
map_out_file
};
buf_srcmap = Some(File::create(map_out_file)?);
}

buf_srcmap
Expand All @@ -433,6 +450,11 @@ impl CompileOptions {
buf.write(r.code.as_bytes()).and(Ok(()))
})?;

if let Some(source_map_path) = source_map_path {
buf.write_all(b"\n//# sourceMappingURL=")?;
buf.write_all(source_map_path.as_bytes())?;
}

buf.flush()
.context("Failed to write output into single file")
} else {
Expand All @@ -448,7 +470,7 @@ impl CompileOptions {

match result {
Ok(output) => {
emit_output(&output, &self.out_dir, &file_path, file_extension)
emit_output(output, &self.out_dir, &file_path, file_extension)
}
Err(e) => Err(e),
}
Expand Down

1 comment on commit c4db8e4

@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: c4db8e4 Previous: c0e72ef Ratio
es/full/bugs-1 301820 ns/iter (± 15748) 302076 ns/iter (± 10229) 1.00
es/full/minify/libraries/antd 1611872239 ns/iter (± 41318970) 1722388271 ns/iter (± 31178604) 0.94
es/full/minify/libraries/d3 306449043 ns/iter (± 6417618) 309423598 ns/iter (± 8151676) 0.99
es/full/minify/libraries/echarts 1269031000 ns/iter (± 15156228) 1309745468 ns/iter (± 11903421) 0.97
es/full/minify/libraries/jquery 91739512 ns/iter (± 1435857) 91024682 ns/iter (± 1195309) 1.01
es/full/minify/libraries/lodash 108169293 ns/iter (± 994631) 107232339 ns/iter (± 797941) 1.01
es/full/minify/libraries/moment 53547460 ns/iter (± 570355) 53281175 ns/iter (± 325284) 1.00
es/full/minify/libraries/react 19336719 ns/iter (± 264144) 19198990 ns/iter (± 94892) 1.01
es/full/minify/libraries/terser 249905264 ns/iter (± 4592383) 255196660 ns/iter (± 2421711) 0.98
es/full/minify/libraries/three 440422572 ns/iter (± 4164483) 473706043 ns/iter (± 5279168) 0.93
es/full/minify/libraries/typescript 3075861195 ns/iter (± 22339092) 3262471679 ns/iter (± 22087194) 0.94
es/full/minify/libraries/victory 666549206 ns/iter (± 11717490) 706385120 ns/iter (± 12320628) 0.94
es/full/minify/libraries/vue 132373155 ns/iter (± 2511831) 133447180 ns/iter (± 853284) 0.99
es/full/codegen/es3 25992 ns/iter (± 59) 26598 ns/iter (± 74) 0.98
es/full/codegen/es5 26051 ns/iter (± 50) 26532 ns/iter (± 51) 0.98
es/full/codegen/es2015 25998 ns/iter (± 33) 26556 ns/iter (± 46) 0.98
es/full/codegen/es2016 26011 ns/iter (± 61) 26532 ns/iter (± 45) 0.98
es/full/codegen/es2017 25993 ns/iter (± 43) 26529 ns/iter (± 47) 0.98
es/full/codegen/es2018 25994 ns/iter (± 34) 26481 ns/iter (± 56) 0.98
es/full/codegen/es2019 26008 ns/iter (± 46) 26518 ns/iter (± 49) 0.98
es/full/codegen/es2020 26014 ns/iter (± 69) 26513 ns/iter (± 30) 0.98
es/full/all/es3 175832561 ns/iter (± 2197057) 167714400 ns/iter (± 3267148) 1.05
es/full/all/es5 167628109 ns/iter (± 2707148) 164662224 ns/iter (± 2233133) 1.02
es/full/all/es2015 129030006 ns/iter (± 2685271) 125454902 ns/iter (± 1433243) 1.03
es/full/all/es2016 126203142 ns/iter (± 1800250) 123729275 ns/iter (± 1277655) 1.02
es/full/all/es2017 124903185 ns/iter (± 1900687) 122806882 ns/iter (± 1211959) 1.02
es/full/all/es2018 123269641 ns/iter (± 2574496) 119959563 ns/iter (± 856615) 1.03
es/full/all/es2019 123524299 ns/iter (± 2529363) 118687782 ns/iter (± 1213558) 1.04
es/full/all/es2020 116663164 ns/iter (± 1078424) 114835423 ns/iter (± 1259851) 1.02
es/full/parser 547757 ns/iter (± 8640) 549119 ns/iter (± 9905) 1.00
es/full/base/fixer 21729 ns/iter (± 38) 21843 ns/iter (± 31) 0.99
es/full/base/resolver_and_hygiene 80136 ns/iter (± 70) 79397 ns/iter (± 124) 1.01
serialization of ast node 126 ns/iter (± 0) 127 ns/iter (± 0) 0.99
serialization of serde 129 ns/iter (± 0) 129 ns/iter (± 0) 1
css/minify/libraries/bootstrap 28049011 ns/iter (± 49142) 28240882 ns/iter (± 115187) 0.99
css/visitor/compare/clone 2059820 ns/iter (± 16576) 2067904 ns/iter (± 18036) 1.00
css/visitor/compare/visit_mut_span 2257473 ns/iter (± 7475) 2288804 ns/iter (± 9904) 0.99
css/visitor/compare/visit_mut_span_panic 2301248 ns/iter (± 8878) 2289724 ns/iter (± 6036) 1.01
css/visitor/compare/fold_span 3019876 ns/iter (± 19435) 3045976 ns/iter (± 42372) 0.99
css/visitor/compare/fold_span_panic 3217520 ns/iter (± 24062) 3150347 ns/iter (± 21672) 1.02
css/lexer/bootstrap_5_1_3 5215111 ns/iter (± 59232) 5210526 ns/iter (± 1576) 1.00
css/lexer/foundation_6_7_4 4393683 ns/iter (± 1101) 4382758 ns/iter (± 1272) 1.00
css/lexer/tailwind_3_1_1 834389 ns/iter (± 263) 833770 ns/iter (± 651) 1.00
css/parser/bootstrap_5_1_3 21652132 ns/iter (± 33684) 21791011 ns/iter (± 102804) 0.99
css/parser/foundation_6_7_4 17331484 ns/iter (± 51791) 17339568 ns/iter (± 14015) 1.00
css/parser/tailwind_3_1_1 3312993 ns/iter (± 13198) 3328877 ns/iter (± 4049) 1.00
es/codegen/colors 329401 ns/iter (± 184400) 325370 ns/iter (± 183787) 1.01
es/codegen/large 1188917 ns/iter (± 615528) 1192829 ns/iter (± 615582) 1.00
es/codegen/with-parser/colors 47972 ns/iter (± 342) 48406 ns/iter (± 327) 0.99
es/codegen/with-parser/large 536005 ns/iter (± 1435) 533576 ns/iter (± 1363) 1.00
es/minify/libraries/antd 1420368446 ns/iter (± 9501654) 1514933054 ns/iter (± 11380506) 0.94
es/minify/libraries/d3 258241457 ns/iter (± 4467393) 257197899 ns/iter (± 4098326) 1.00
es/minify/libraries/echarts 1068298569 ns/iter (± 20074028) 1136625178 ns/iter (± 6313896) 0.94
es/minify/libraries/jquery 79756626 ns/iter (± 793832) 79257372 ns/iter (± 479930) 1.01
es/minify/libraries/lodash 97127096 ns/iter (± 2198589) 95613083 ns/iter (± 835255) 1.02
es/minify/libraries/moment 46905379 ns/iter (± 924465) 46489847 ns/iter (± 245428) 1.01
es/minify/libraries/react 17133167 ns/iter (± 198262) 17165437 ns/iter (± 75233) 1.00
es/minify/libraries/terser 212794290 ns/iter (± 2094322) 215542897 ns/iter (± 2038921) 0.99
es/minify/libraries/three 364540467 ns/iter (± 9972967) 376351262 ns/iter (± 8310638) 0.97
es/minify/libraries/typescript 2623585197 ns/iter (± 34602244) 2771893705 ns/iter (± 15291733) 0.95
es/minify/libraries/victory 557323125 ns/iter (± 13145662) 614558064 ns/iter (± 9577509) 0.91
es/minify/libraries/vue 118552349 ns/iter (± 2781540) 118075731 ns/iter (± 2228309) 1.00
es/visitor/compare/clone 2422567 ns/iter (± 26540) 2416068 ns/iter (± 17803) 1.00
es/visitor/compare/visit_mut_span 2764764 ns/iter (± 8174) 2812076 ns/iter (± 13078) 0.98
es/visitor/compare/visit_mut_span_panic 2835624 ns/iter (± 7058) 2865700 ns/iter (± 5415) 0.99
es/visitor/compare/fold_span 3939901 ns/iter (± 8303) 3933286 ns/iter (± 14282) 1.00
es/visitor/compare/fold_span_panic 4055809 ns/iter (± 10396) 4126796 ns/iter (± 15171) 0.98
es/lexer/colors 17412 ns/iter (± 10) 17293 ns/iter (± 66) 1.01
es/lexer/angular 8322838 ns/iter (± 7123) 8256280 ns/iter (± 11234) 1.01
es/lexer/backbone 1095310 ns/iter (± 516) 1079470 ns/iter (± 809) 1.01
es/lexer/jquery 6041600 ns/iter (± 8856) 5972745 ns/iter (± 3376) 1.01
es/lexer/jquery mobile 9311849 ns/iter (± 3542) 9211005 ns/iter (± 2934) 1.01
es/lexer/mootools 4742753 ns/iter (± 8586) 4692775 ns/iter (± 7844) 1.01
es/lexer/underscore 911987 ns/iter (± 403) 899754 ns/iter (± 852) 1.01
es/lexer/three 28238267 ns/iter (± 14228) 27991433 ns/iter (± 25552) 1.01
es/lexer/yui 5107605 ns/iter (± 2350) 5043579 ns/iter (± 3161) 1.01
es/parser/colors 31128 ns/iter (± 163) 31297 ns/iter (± 192) 0.99
es/parser/angular 15880015 ns/iter (± 138146) 16025678 ns/iter (± 152205) 0.99
es/parser/backbone 2336073 ns/iter (± 13132) 2361050 ns/iter (± 12182) 0.99
es/parser/jquery 12608432 ns/iter (± 102183) 12568887 ns/iter (± 73513) 1.00
es/parser/jquery mobile 20011752 ns/iter (± 173838) 19967483 ns/iter (± 127258) 1.00
es/parser/mootools 9595575 ns/iter (± 16102) 9617936 ns/iter (± 22837) 1.00
es/parser/underscore 1985724 ns/iter (± 9791) 1995173 ns/iter (± 12983) 1.00
es/parser/three 59314753 ns/iter (± 578265) 59587628 ns/iter (± 390216) 1.00
es/parser/yui 9671059 ns/iter (± 42865) 9758665 ns/iter (± 93110) 0.99
es/preset-env/usage/builtin_type 140469 ns/iter (± 31625) 138332 ns/iter (± 30629) 1.02
es/preset-env/usage/property 20948 ns/iter (± 94) 20952 ns/iter (± 77) 1.00
es/resolver/typescript 109514812 ns/iter (± 1953435) 122400536 ns/iter (± 2291631) 0.89
es/fixer/typescript 83564490 ns/iter (± 305166) 93941561 ns/iter (± 6210838) 0.89
es/hygiene/typescript 178249061 ns/iter (± 1439738) 196747618 ns/iter (± 1145771) 0.91
es/resolver_with_hygiene/typescript 310191757 ns/iter (± 3400136) 333915061 ns/iter (± 2315215) 0.93
es/visitor/base-perf/module_clone 74712 ns/iter (± 1108) 75088 ns/iter (± 1291) 0.99
es/visitor/base-perf/fold_empty 84923 ns/iter (± 1513) 86182 ns/iter (± 1294) 0.99
es/visitor/base-perf/fold_noop_impl_all 86223 ns/iter (± 1984) 86345 ns/iter (± 1603) 1.00
es/visitor/base-perf/fold_noop_impl_vec 85681 ns/iter (± 1728) 85850 ns/iter (± 2193) 1.00
es/visitor/base-perf/boxing_boxed_clone 56 ns/iter (± 0) 56 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed_clone 62 ns/iter (± 0) 59 ns/iter (± 0) 1.05
es/visitor/base-perf/boxing_boxed 104 ns/iter (± 0) 104 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed 103 ns/iter (± 0) 102 ns/iter (± 0) 1.01
es/visitor/base-perf/visit_contains_this 3525 ns/iter (± 67) 3410 ns/iter (± 36) 1.03
es/base/parallel/resolver/typescript 5358892896 ns/iter (± 364180794) 5411652517 ns/iter (± 528794129) 0.99
es/base/parallel/hygiene/typescript 2135308043 ns/iter (± 20173579) 2369685568 ns/iter (± 26290610) 0.90
misc/visitors/time-complexity/time 5 93 ns/iter (± 0) 93 ns/iter (± 0) 1
misc/visitors/time-complexity/time 10 308 ns/iter (± 0) 309 ns/iter (± 4) 1.00
misc/visitors/time-complexity/time 15 663 ns/iter (± 0) 667 ns/iter (± 0) 0.99
misc/visitors/time-complexity/time 20 1225 ns/iter (± 0) 1208 ns/iter (± 0) 1.01
misc/visitors/time-complexity/time 40 6286 ns/iter (± 10) 6194 ns/iter (± 7) 1.01
misc/visitors/time-complexity/time 60 15581 ns/iter (± 1) 15594 ns/iter (± 42) 1.00
es/full-target/es2016 188506 ns/iter (± 318) 184510 ns/iter (± 1635) 1.02
es/full-target/es2017 183337 ns/iter (± 187) 178615 ns/iter (± 229) 1.03
es/full-target/es2018 172724 ns/iter (± 643) 167918 ns/iter (± 231) 1.03
es2020_nullish_coalescing 67114 ns/iter (± 140) 65438 ns/iter (± 355) 1.03
es2020_optional_chaining 95792 ns/iter (± 8656) 93297 ns/iter (± 4021) 1.03
es2022_class_properties 94138 ns/iter (± 117) 91800 ns/iter (± 142) 1.03
es2018_object_rest_spread 71417 ns/iter (± 88) 69982 ns/iter (± 77) 1.02
es2019_optional_catch_binding 61404 ns/iter (± 120) 60137 ns/iter (± 68) 1.02
es2017_async_to_generator 61680 ns/iter (± 51) 60600 ns/iter (± 57) 1.02
es2016_exponentiation 65277 ns/iter (± 78) 63885 ns/iter (± 54) 1.02
es2015_arrow 70307 ns/iter (± 248) 68391 ns/iter (± 102) 1.03
es2015_block_scoped_fn 66349 ns/iter (± 136) 65018 ns/iter (± 100) 1.02
es2015_block_scoping 139092 ns/iter (± 5442) 133673 ns/iter (± 9230) 1.04
es2015_classes 117446 ns/iter (± 389) 113515 ns/iter (± 365) 1.03
es2015_computed_props 61475 ns/iter (± 69) 59834 ns/iter (± 89) 1.03
es2015_destructuring 115819 ns/iter (± 297) 112581 ns/iter (± 179) 1.03
es2015_duplicate_keys 63977 ns/iter (± 113) 62968 ns/iter (± 183) 1.02
es2015_parameters 80118 ns/iter (± 315) 78339 ns/iter (± 246) 1.02
es2015_fn_name 66706 ns/iter (± 416) 65945 ns/iter (± 579) 1.01
es2015_for_of 64677 ns/iter (± 120) 63308 ns/iter (± 194) 1.02
es2015_instanceof 63159 ns/iter (± 109) 61974 ns/iter (± 124) 1.02
es2015_shorthand_property 61692 ns/iter (± 70) 60166 ns/iter (± 174) 1.03
es2015_spread 61447 ns/iter (± 72) 60172 ns/iter (± 93) 1.02
es2015_sticky_regex 62639 ns/iter (± 142) 61052 ns/iter (± 96) 1.03
es2015_typeof_symbol 62867 ns/iter (± 140) 61058 ns/iter (± 128) 1.03
es/transform/baseline/base 52271 ns/iter (± 88) 50766 ns/iter (± 88) 1.03
es/transform/baseline/common_reserved_word 63131 ns/iter (± 87) 61703 ns/iter (± 64) 1.02
es/transform/baseline/common_typescript 168980 ns/iter (± 330) 166148 ns/iter (± 223) 1.02
es/target/es3 170722 ns/iter (± 384) 167291 ns/iter (± 351) 1.02
es/target/es2015 627658 ns/iter (± 1286) 602550 ns/iter (± 912) 1.04
es/target/es2016 65400 ns/iter (± 106) 63889 ns/iter (± 94) 1.02
es/target/es2017 61796 ns/iter (± 69) 60592 ns/iter (± 58) 1.02
es/target/es2018 81781 ns/iter (± 85) 79683 ns/iter (± 71) 1.03
es/target/es2020 133005 ns/iter (± 183) 129783 ns/iter (± 132) 1.02
babelify-only 672804 ns/iter (± 1200) 667897 ns/iter (± 1454) 1.01
parse_and_babelify_angular 42636690 ns/iter (± 626830) 46130260 ns/iter (± 909596) 0.92
parse_and_babelify_backbone 5494447 ns/iter (± 55011) 5733306 ns/iter (± 188325) 0.96
parse_and_babelify_jquery 31472504 ns/iter (± 529645) 32826122 ns/iter (± 268578) 0.96
parse_and_babelify_jquery_mobile 53970528 ns/iter (± 773893) 55911361 ns/iter (± 741657) 0.97
parse_and_babelify_mootools 32684172 ns/iter (± 403975) 33769533 ns/iter (± 394488) 0.97
parse_and_babelify_underscore 4422403 ns/iter (± 39903) 4386620 ns/iter (± 21827) 1.01
parse_and_babelify_yui 32240530 ns/iter (± 472995) 33669347 ns/iter (± 719574) 0.96
html/minify/document/css_spec 41760680 ns/iter (± 204782) 42198141 ns/iter (± 309689) 0.99
html/minify/document/github 17521734 ns/iter (± 13975) 17486717 ns/iter (± 43933) 1.00
html/minify/document/stackoverflow 15724259 ns/iter (± 22886) 15630895 ns/iter (± 54924) 1.01
html/minify/document_fragment/css_spec 39871656 ns/iter (± 167934) 40454448 ns/iter (± 257771) 0.99
html/minify/document_fragment/github 16886068 ns/iter (± 42458) 16788888 ns/iter (± 26880) 1.01
html/minify/document_fragment/stackoverflow 15253013 ns/iter (± 20459) 15234606 ns/iter (± 19997) 1.00
html/document/visitor/compare/clone 342655 ns/iter (± 2339) 348127 ns/iter (± 2044) 0.98
html/document/visitor/compare/visit_mut_span 370011 ns/iter (± 1737) 369194 ns/iter (± 2903) 1.00
html/document/visitor/compare/visit_mut_span_panic 384194 ns/iter (± 2100) 377578 ns/iter (± 1649) 1.02
html/document/visitor/compare/fold_span 407466 ns/iter (± 2198) 407734 ns/iter (± 2591) 1.00
html/document/visitor/compare/fold_span_panic 459815 ns/iter (± 1728) 459448 ns/iter (± 2582) 1.00
html/document_fragment/visitor/compare/clone 345976 ns/iter (± 1923) 339197 ns/iter (± 1504) 1.02
html/document_fragment/visitor/compare/visit_mut_span 372183 ns/iter (± 2236) 365694 ns/iter (± 1655) 1.02
html/document_fragment/visitor/compare/visit_mut_span_panic 371714 ns/iter (± 1501) 381684 ns/iter (± 2346) 0.97
html/document_fragment/visitor/compare/fold_span 405285 ns/iter (± 2028) 402857 ns/iter (± 2185) 1.01
html/document_fragment/visitor/compare/fold_span_panic 460107 ns/iter (± 2647) 460486 ns/iter (± 2189) 1.00
html/lexer/css_2021_spec 15488857 ns/iter (± 14654) 15514664 ns/iter (± 167197) 1.00
html/lexer/github_com_17_05_2022 6022693 ns/iter (± 4850) 6020018 ns/iter (± 4547) 1.00
html/lexer/stackoverflow_com_17_05_2022 5621161 ns/iter (± 4711) 5624463 ns/iter (± 6752) 1.00
html/parser/parser_document/css_2021_spec 25280596 ns/iter (± 134199) 25435484 ns/iter (± 142057) 0.99
html/parser/parser_document/github_com_17_05_2022 8739137 ns/iter (± 10386) 8734552 ns/iter (± 12749) 1.00
html/parser/parser_document/stackoverflow_com_17_05_2022 7707045 ns/iter (± 3078) 7727198 ns/iter (± 4694) 1.00
html/parser/parser_document_fragment/css_2021_spec 25526475 ns/iter (± 411692) 25564352 ns/iter (± 93133) 1.00
html/parser/parser_document_fragment/github_com_17_05_2022 8742374 ns/iter (± 26506) 8736505 ns/iter (± 6356) 1.00
html/parser/parser_document_fragment/stackoverflow_com_17_05_2022 7715383 ns/iter (± 5001) 7753669 ns/iter (± 3907) 1.00

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

Please sign in to comment.