Skip to content

Commit

Permalink
feat(cli): Support generating .d.ts files (#9097)
Browse files Browse the repository at this point in the history
**Description:**

I verified that `swc_cli` (Rust) works with 

```json
{
    "jsc": {
        "parser": {
            "syntax": "typescript"
        },
        "experimental": {
            "emitIsolatedDts": true
        }
    }
}
```
  • Loading branch information
kdy1 authored Jun 22, 2024
1 parent 7c8397d commit e71b6d3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
7 changes: 7 additions & 0 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,13 @@ impl Compiler {
) -> Result<TransformOutput, Error> {
self.run(|| {
let program = config.program;

if config.emit_isolated_dts && !config.syntax.typescript() {
handler.warn(
"jsc.experimental.emitIsolatedDts is enabled but the syntax is not TypeScript",
);
}

let emit_dts = config.syntax.typescript() && config.emit_isolated_dts;
let source_map_names = if config.source_maps.enabled() {
let mut v = swc_compiler_base::IdentCollector {
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_cli_impl/examples/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() -> anyhow::Result<()> {
swc_cli_impl::run()
}
12 changes: 11 additions & 1 deletion crates/swc_cli_impl/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,17 @@ fn emit_output(
fs::write(source_map_path, source_map)?;
}

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

if let Some(extra) = &output.output {
let mut extra: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(extra).context("failed to parse extra output")?;

if let Some(dts_code) = extra.remove("__swc_isolated_declarations__") {
let dts_file_path = output_file_path.with_extension("d.ts");
fs::write(dts_file_path, dts_code.as_str().unwrap())?;
}
}
} else {
let source_map = if let Some(ref source_map) = output.map {
&**source_map
Expand Down
5 changes: 5 additions & 0 deletions packages/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,11 @@ export interface JscConfig {
* Disable builtin transforms. If enabled, only Wasm plugins are used.
*/
disableBuiltinTransformsForInternalTesting?: boolean;

/**
* Emit isolated dts files for each module.
*/
emitIsolatedDts?: boolean;
};

baseUrl?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@swc/types",
"packageManager": "[email protected]",
"version": "0.1.8",
"version": "0.1.9",
"description": "Typings for the swc project.",
"sideEffects": false,
"scripts": {
Expand Down

1 comment on commit e71b6d3

@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: e71b6d3 Previous: 51a2702 Ratio
es/full/bugs-1 280128 ns/iter (± 11312) 276275 ns/iter (± 4694) 1.01
es/full/minify/libraries/antd 1814605979 ns/iter (± 91259401) 1878560215 ns/iter (± 37707991) 0.97
es/full/minify/libraries/d3 356978574 ns/iter (± 5390144) 355110737 ns/iter (± 5616769) 1.01
es/full/minify/libraries/echarts 1621180355 ns/iter (± 34712322) 1664808888 ns/iter (± 39313845) 0.97
es/full/minify/libraries/jquery 104958804 ns/iter (± 687791) 105016115 ns/iter (± 561034) 1.00
es/full/minify/libraries/lodash 115575586 ns/iter (± 1120053) 115875457 ns/iter (± 809445) 1.00
es/full/minify/libraries/moment 60142456 ns/iter (± 772938) 60128345 ns/iter (± 204996) 1.00
es/full/minify/libraries/react 18557644 ns/iter (± 628806) 18441993 ns/iter (± 82292) 1.01
es/full/minify/libraries/terser 288157598 ns/iter (± 7362672) 285704283 ns/iter (± 4367328) 1.01
es/full/minify/libraries/three 562632840 ns/iter (± 13391285) 515987609 ns/iter (± 17664949) 1.09
es/full/minify/libraries/typescript 3452066840 ns/iter (± 101954286) 3353416706 ns/iter (± 38727253) 1.03
es/full/minify/libraries/victory 948083999 ns/iter (± 67850762) 841258193 ns/iter (± 22948816) 1.13
es/full/minify/libraries/vue 152253009 ns/iter (± 1746183) 149521355 ns/iter (± 1501984) 1.02
es/full/codegen/es3 59214 ns/iter (± 468) 59915 ns/iter (± 164) 0.99
es/full/codegen/es5 59041 ns/iter (± 191) 59471 ns/iter (± 273) 0.99
es/full/codegen/es2015 58683 ns/iter (± 358) 59890 ns/iter (± 280) 0.98
es/full/codegen/es2016 58583 ns/iter (± 328) 59250 ns/iter (± 135) 0.99
es/full/codegen/es2017 58501 ns/iter (± 345) 59395 ns/iter (± 242) 0.98
es/full/codegen/es2018 58790 ns/iter (± 339) 59463 ns/iter (± 650) 0.99
es/full/codegen/es2019 58943 ns/iter (± 504) 59404 ns/iter (± 332) 0.99
es/full/codegen/es2020 59525 ns/iter (± 298) 59114 ns/iter (± 168) 1.01
es/full/all/es3 161217561 ns/iter (± 1188802) 162324163 ns/iter (± 2105707) 0.99
es/full/all/es5 155956771 ns/iter (± 625402) 156565382 ns/iter (± 1069271) 1.00
es/full/all/es2015 115404889 ns/iter (± 1421571) 116600883 ns/iter (± 846966) 0.99
es/full/all/es2016 114565701 ns/iter (± 879051) 115567638 ns/iter (± 890101) 0.99
es/full/all/es2017 113843984 ns/iter (± 846711) 114738425 ns/iter (± 820953) 0.99
es/full/all/es2018 112483797 ns/iter (± 383430) 112779370 ns/iter (± 716094) 1.00
es/full/all/es2019 111920543 ns/iter (± 1379397) 112449363 ns/iter (± 755949) 1.00
es/full/all/es2020 107585315 ns/iter (± 856909) 107767387 ns/iter (± 1028603) 1.00
es/full/parser 456634 ns/iter (± 4945) 461233 ns/iter (± 1288) 0.99
es/full/base/fixer 15607 ns/iter (± 175) 15784 ns/iter (± 73) 0.99
es/full/base/resolver_and_hygiene 80867 ns/iter (± 199) 80923 ns/iter (± 185) 1.00
serialization of serde 253 ns/iter (± 14) 271 ns/iter (± 1) 0.93
css/minify/libraries/bootstrap 22484391 ns/iter (± 24441) 22203162 ns/iter (± 140296) 1.01
css/visitor/compare/clone 1385986 ns/iter (± 7719) 1382524 ns/iter (± 4861) 1.00
css/visitor/compare/visit_mut_span 1523383 ns/iter (± 5463) 1514242 ns/iter (± 8332) 1.01
css/visitor/compare/visit_mut_span_panic 1519056 ns/iter (± 49528) 1528993 ns/iter (± 31776) 0.99
css/visitor/compare/fold_span 2013294 ns/iter (± 10270) 2014020 ns/iter (± 4512) 1.00
css/visitor/compare/fold_span_panic 2143962 ns/iter (± 8486) 2153922 ns/iter (± 4538) 1.00
css/lexer/bootstrap_5_1_3 3417361 ns/iter (± 17814) 3425883 ns/iter (± 5886) 1.00
css/lexer/foundation_6_7_4 2784920 ns/iter (± 5235) 2790593 ns/iter (± 2255) 1.00
css/lexer/tailwind_3_1_1 534914 ns/iter (± 456) 534516 ns/iter (± 4655) 1.00
css/parser/bootstrap_5_1_3 16050841 ns/iter (± 44592) 16256498 ns/iter (± 29125) 0.99
css/parser/foundation_6_7_4 12929625 ns/iter (± 71231) 13002598 ns/iter (± 15516) 0.99
css/parser/tailwind_3_1_1 2530535 ns/iter (± 12199) 2590278 ns/iter (± 9477) 0.98
es/codegen/colors 719597 ns/iter (± 393002) 730887 ns/iter (± 405157) 0.98
es/codegen/large 2998485 ns/iter (± 1589675) 2993707 ns/iter (± 1606867) 1.00
es/codegen/with-parser/colors 40928 ns/iter (± 325) 41330 ns/iter (± 227) 0.99
es/codegen/with-parser/large 446289 ns/iter (± 1689) 447949 ns/iter (± 3115) 1.00
es/minify/libraries/antd 1587222150 ns/iter (± 28547102) 1462991622 ns/iter (± 25635645) 1.08
es/minify/libraries/d3 313521662 ns/iter (± 13624773) 283275075 ns/iter (± 2033915) 1.11
es/minify/libraries/echarts 1307224586 ns/iter (± 11015485) 1186631117 ns/iter (± 33817043) 1.10
es/minify/libraries/jquery 84000654 ns/iter (± 542522) 83339600 ns/iter (± 145420) 1.01
es/minify/libraries/lodash 106180351 ns/iter (± 572025) 104767076 ns/iter (± 213013) 1.01
es/minify/libraries/moment 48737943 ns/iter (± 138540) 48587462 ns/iter (± 141234) 1.00
es/minify/libraries/react 16270350 ns/iter (± 37891) 16385421 ns/iter (± 38400) 0.99
es/minify/libraries/terser 228630901 ns/iter (± 4013670) 219175052 ns/iter (± 599858) 1.04
es/minify/libraries/three 416203469 ns/iter (± 8111425) 367463949 ns/iter (± 10302320) 1.13
es/minify/libraries/typescript 2836908740 ns/iter (± 30077103) 2810813090 ns/iter (± 39583545) 1.01
es/minify/libraries/victory 721691731 ns/iter (± 11374711) 571444675 ns/iter (± 8251950) 1.26
es/minify/libraries/vue 125155645 ns/iter (± 1364135) 121906703 ns/iter (± 179389) 1.03
es/visitor/compare/clone 1738382 ns/iter (± 5624) 1741618 ns/iter (± 5329) 1.00
es/visitor/compare/visit_mut_span 2091807 ns/iter (± 13332) 2095847 ns/iter (± 13012) 1.00
es/visitor/compare/visit_mut_span_panic 2106764 ns/iter (± 51276) 2088723 ns/iter (± 35194) 1.01
es/visitor/compare/fold_span 2823525 ns/iter (± 5562) 2793819 ns/iter (± 7738) 1.01
es/visitor/compare/fold_span_panic 2825188 ns/iter (± 3424) 2890592 ns/iter (± 23124) 0.98
es/lexer/colors 9384 ns/iter (± 60) 9014 ns/iter (± 21) 1.04
es/lexer/angular 4782205 ns/iter (± 15025) 4794067 ns/iter (± 23713) 1.00
es/lexer/backbone 625010 ns/iter (± 1126) 620558 ns/iter (± 1124) 1.01
es/lexer/jquery 3494356 ns/iter (± 1578) 3498801 ns/iter (± 6860) 1.00
es/lexer/jquery mobile 5222975 ns/iter (± 91661) 5175893 ns/iter (± 18030) 1.01
es/lexer/mootools 2770033 ns/iter (± 3254) 2731825 ns/iter (± 3217) 1.01
es/lexer/underscore 523583 ns/iter (± 1414) 515221 ns/iter (± 891) 1.02
es/lexer/three 15592132 ns/iter (± 24383) 15627421 ns/iter (± 43173) 1.00
es/lexer/yui 2883268 ns/iter (± 14659) 2835979 ns/iter (± 9917) 1.02
es/lexer/cal-com 12936074 ns/iter (± 65786) 12845477 ns/iter (± 197377) 1.01
es/lexer/typescript 89088456 ns/iter (± 597979) 89888808 ns/iter (± 824945) 0.99
es/parser/colors 24480 ns/iter (± 191) 24457 ns/iter (± 163) 1.00
es/parser/angular 12597855 ns/iter (± 77634) 12594701 ns/iter (± 139328) 1.00
es/parser/backbone 1844179 ns/iter (± 11801) 1820266 ns/iter (± 5723) 1.01
es/parser/jquery 10123807 ns/iter (± 137773) 10053687 ns/iter (± 63071) 1.01
es/parser/jquery mobile 15286534 ns/iter (± 204237) 15288021 ns/iter (± 304664) 1.00
es/parser/mootools 7901402 ns/iter (± 23680) 7811135 ns/iter (± 20678) 1.01
es/parser/underscore 1605341 ns/iter (± 7807) 1595661 ns/iter (± 8005) 1.01
es/parser/three 44337019 ns/iter (± 363388) 44955600 ns/iter (± 462593) 0.99
es/parser/yui 7506875 ns/iter (± 29955) 7485315 ns/iter (± 48110) 1.00
es/parser/cal-com 41139392 ns/iter (± 566479) 41817608 ns/iter (± 613936) 0.98
es/parser/typescript 273433005 ns/iter (± 1159632) 273663248 ns/iter (± 1212034) 1.00
es/preset-env/usage/builtin_type 137525 ns/iter (± 33401) 141480 ns/iter (± 33542) 0.97
es/preset-env/usage/property 16720 ns/iter (± 67) 17434 ns/iter (± 128) 0.96
es/resolver/typescript 102299082 ns/iter (± 2422811) 96192939 ns/iter (± 2006189) 1.06
es/fixer/typescript 83074792 ns/iter (± 2252623) 79191389 ns/iter (± 2380087) 1.05
es/hygiene/typescript 187852266 ns/iter (± 3757205) 169446711 ns/iter (± 1651781) 1.11
es/resolver_with_hygiene/typescript 305400494 ns/iter (± 3136043) 297557947 ns/iter (± 4861431) 1.03
es/visitor/base-perf/module_clone 57196 ns/iter (± 2051) 57128 ns/iter (± 758) 1.00
es/visitor/base-perf/fold_empty 60091 ns/iter (± 284) 60160 ns/iter (± 1113) 1.00
es/visitor/base-perf/fold_noop_impl_all 60406 ns/iter (± 781) 60997 ns/iter (± 1888) 0.99
es/visitor/base-perf/fold_noop_impl_vec 60565 ns/iter (± 322) 60074 ns/iter (± 1133) 1.01
es/visitor/base-perf/boxing_boxed_clone 61 ns/iter (± 0) 60 ns/iter (± 0) 1.02
es/visitor/base-perf/boxing_unboxed_clone 48 ns/iter (± 0) 47 ns/iter (± 0) 1.02
es/visitor/base-perf/boxing_boxed 106 ns/iter (± 0) 106 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed 80 ns/iter (± 0) 80 ns/iter (± 0) 1
es/visitor/base-perf/visit_contains_this 2632 ns/iter (± 7) 2640 ns/iter (± 16) 1.00
es/base/parallel/resolver/typescript 2439055544 ns/iter (± 101297118) 2342885886 ns/iter (± 106143516) 1.04
es/base/parallel/hygiene/typescript 3167905463 ns/iter (± 39682484) 3119897002 ns/iter (± 41619945) 1.02
babelify-only 491506 ns/iter (± 6006) 490346 ns/iter (± 9291) 1.00
parse_and_babelify_angular 37585933 ns/iter (± 642079) 45326021 ns/iter (± 2098478) 0.83
parse_and_babelify_backbone 4368395 ns/iter (± 167897) 4561697 ns/iter (± 149849) 0.96
parse_and_babelify_jquery 28213743 ns/iter (± 243608) 30479680 ns/iter (± 278502) 0.93
parse_and_babelify_jquery_mobile 47806341 ns/iter (± 352167) 53698854 ns/iter (± 1049052) 0.89
parse_and_babelify_mootools 25653770 ns/iter (± 360516) 27681142 ns/iter (± 160910) 0.93
parse_and_babelify_underscore 3688556 ns/iter (± 50573) 3714557 ns/iter (± 25059) 0.99
parse_and_babelify_yui 23187746 ns/iter (± 242594) 25894731 ns/iter (± 204849) 0.90
html/minify/document/css_spec 45585278 ns/iter (± 822413) 45793361 ns/iter (± 294584) 1.00
html/minify/document/github 18039504 ns/iter (± 68072) 18186043 ns/iter (± 40206) 0.99
html/minify/document/stackoverflow 16574552 ns/iter (± 198827) 16641889 ns/iter (± 708866) 1.00
html/minify/document_fragment/css_spec 46377499 ns/iter (± 89836) 45833234 ns/iter (± 336098) 1.01
html/minify/document_fragment/github 18193101 ns/iter (± 37972) 18096997 ns/iter (± 104124) 1.01
html/minify/document_fragment/stackoverflow 16807836 ns/iter (± 59904) 16556514 ns/iter (± 80629) 1.02
html/document/visitor/compare/clone 165358 ns/iter (± 1701) 165226 ns/iter (± 1806) 1.00
html/document/visitor/compare/visit_mut_span 183819 ns/iter (± 3131) 181410 ns/iter (± 2469) 1.01
html/document/visitor/compare/visit_mut_span_panic 187502 ns/iter (± 4477) 187651 ns/iter (± 7278) 1.00
html/document/visitor/compare/fold_span 232326 ns/iter (± 1361) 226090 ns/iter (± 712) 1.03
html/document/visitor/compare/fold_span_panic 277525 ns/iter (± 1160) 281098 ns/iter (± 1044) 0.99
html/document_fragment/visitor/compare/clone 167786 ns/iter (± 1740) 168260 ns/iter (± 1537) 1.00
html/document_fragment/visitor/compare/visit_mut_span 184275 ns/iter (± 1190) 182245 ns/iter (± 1166) 1.01
html/document_fragment/visitor/compare/visit_mut_span_panic 188111 ns/iter (± 1882) 187079 ns/iter (± 1083) 1.01
html/document_fragment/visitor/compare/fold_span 230997 ns/iter (± 747) 228467 ns/iter (± 1298) 1.01
html/document_fragment/visitor/compare/fold_span_panic 278838 ns/iter (± 1152) 279301 ns/iter (± 675) 1.00
html/lexer/css_2021_spec 12987880 ns/iter (± 227254) 12971035 ns/iter (± 15087) 1.00
html/lexer/github_com_17_05_2022 4972229 ns/iter (± 10277) 4977989 ns/iter (± 9997) 1.00
html/lexer/stackoverflow_com_17_05_2022 4742602 ns/iter (± 16796) 4742002 ns/iter (± 25102) 1.00
html/parser/parser_document/css_2021_spec 25671949 ns/iter (± 42757) 25940034 ns/iter (± 67706) 0.99
html/parser/parser_document/github_com_17_05_2022 8557577 ns/iter (± 15365) 8625199 ns/iter (± 40443) 0.99
html/parser/parser_document/stackoverflow_com_17_05_2022 7421931 ns/iter (± 12056) 7540315 ns/iter (± 26473) 0.98
html/parser/parser_document_fragment/css_2021_spec 25278327 ns/iter (± 40852) 25735709 ns/iter (± 201776) 0.98
html/parser/parser_document_fragment/github_com_17_05_2022 8485562 ns/iter (± 16759) 8693111 ns/iter (± 39959) 0.98
html/parser/parser_document_fragment/stackoverflow_com_17_05_2022 7388785 ns/iter (± 16347) 7511781 ns/iter (± 15499) 0.98

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

Please sign in to comment.