Skip to content

Commit

Permalink
feat(binding): Create Wasm package for stripping only TypeScript (#9124)
Browse files Browse the repository at this point in the history
**Description:**

This PR adds a Wasm binding which is only capable of stripping TypeScript types.


**Related issue:**

 - marco-ippolito/node#2
  • Loading branch information
kdy1 authored Jul 3, 2024
1 parent 1597c5d commit 6b3c0da
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 47 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/publish-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,14 @@ jobs:
matrix:
settings:
- crate: "binding_core_wasm"
npm: "@swc/wasm"
npm: "@swc\\/wasm"
target: nodejs
- crate: "binding_core_wasm"
npm: "@swc/wasm-web"
npm: "@swc\\/wasm-web"
target: web
- crate: "binding_typescript_wasm"
npm: "@swc\\/wasm-typescript"
target: no-modules
steps:
- uses: actions/checkout@v4

Expand Down
22 changes: 20 additions & 2 deletions bindings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"binding_core_wasm",
"binding_minifier_node",
"binding_minifier_wasm",
"binding_typescript_wasm",
"swc_cli",
]
resolver = "2"
Expand Down
2 changes: 1 addition & 1 deletion bindings/binding_core_node/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
let out_dir = env::var("OUT_DIR").expect("Outdir should exist");
let dest_path = Path::new(&out_dir).join("triple.txt");
let mut f =
BufWriter::new(File::create(&dest_path).expect("Failed to create target triple text"));
BufWriter::new(File::create(dest_path).expect("Failed to create target triple text"));
write!(
f,
"{}",
Expand Down
51 changes: 25 additions & 26 deletions bindings/binding_minifier_node/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use napi::{
};
use serde::Deserialize;
use swc_compiler_base::{
minify_file_comments, parse_js, IdentCollector, IsModule, PrintArgs, SourceMapsConfig,
TransformOutput,
minify_file_comments, parse_js, IdentCollector, PrintArgs, SourceMapsConfig, TransformOutput,
};
use swc_config::config_types::BoolOr;
use swc_core::{
Expand All @@ -23,7 +22,7 @@ use swc_core::{
js::{JsMinifyCommentOption, JsMinifyOptions},
option::{MinifyOptions, TopLevelOptions},
},
parser::{EsConfig, Syntax},
parser::{EsSyntax, Syntax},
transforms::base::{fixer::fixer, hygiene::hygiene, resolver},
visit::{FoldWith, VisitMutWith, VisitWith},
},
Expand Down Expand Up @@ -109,11 +108,30 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
..Default::default()
};

let comments = SingleThreadedComments::default();

let module = parse_js(
cm.clone(),
fm.clone(),
handler,
target,
Syntax::Es(EsSyntax {
jsx: true,
decorators: true,
decorators_before_export: true,
import_attributes: true,
..Default::default()
}),
options.module,
Some(&comments),
)
.context("failed to parse input file")?;

// top_level defaults to true if module is true

// https://github.com/swc-project/swc/issues/2254

if options.module {
if module.is_module() {
if let Some(opts) = &mut min_opts.compress {
if opts.top_level.is_none() {
opts.top_level = Some(TopLevelOptions { functions: true });
Expand All @@ -136,25 +154,6 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
}
}

let comments = SingleThreadedComments::default();

let module = parse_js(
cm.clone(),
fm.clone(),
handler,
target,
Syntax::Es(EsConfig {
jsx: true,
decorators: true,
decorators_before_export: true,
import_attributes: true,
..Default::default()
}),
IsModule::Bool(options.module),
Some(&comments),
)
.context("failed to parse input file")?;

let source_map_names = if source_map.enabled() {
let mut v = IdentCollector {
names: Default::default(),
Expand All @@ -172,7 +171,7 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf

let is_mangler_enabled = min_opts.mangle.is_some();

let module = (|| {
let module = {
let module = module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false));

let mut module = swc_core::ecma::minifier::optimize(
Expand All @@ -192,7 +191,7 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
}
module.visit_mut_with(&mut fixer(Some(&comments as &dyn Comments)));
module
})();
};

let preserve_comments = options
.format
Expand All @@ -211,7 +210,7 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
inline_sources_content: options.inline_sources_content,
source_map,
source_map_names: &source_map_names,
orig: orig.as_ref(),
orig,
comments: Some(&comments),
emit_source_map_columns: options.emit_source_map_columns,
preamble: &options.format.preamble,
Expand Down
1 change: 0 additions & 1 deletion bindings/binding_minifier_wasm/scripts/build.sh

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions bindings/binding_minifier_wasm/scripts/build_web_release.sh

This file was deleted.

6 changes: 0 additions & 6 deletions bindings/binding_minifier_wasm/scripts/test.sh

This file was deleted.

40 changes: 40 additions & 0 deletions bindings/binding_typescript_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
description = "wasm module for swc"
edition = "2021"
license = "Apache-2.0"
name = "binding_typescript_wasm"
publish = false
repository = "https://github.com/swc-project/swc.git"
version = "1.6.6"

[lib]
bench = false
crate-type = ["cdylib"]

[features]
default = ["swc_v1"]
swc_v1 = []
swc_v2 = []

[dependencies]
anyhow = "1.0.66"
getrandom = { version = "0.2.10", features = ["js"] }
serde = { version = "1", features = ["derive"] }
serde-wasm-bindgen = "0.4.5"
serde_json = "1.0.120"
swc_core = { version = "0.95.10", features = [
"ecma_ast_serde",
"ecma_codegen",
"ecma_transforms",
"ecma_transforms_typescript",
"ecma_visit",
] }
swc_ecma_codegen = { version = "0.151.1", features = ["serde-impl"] }
swc_error_reporters = "0.18.0"
tracing = { version = "0.1.37", features = ["max_level_off"] }
wasm-bindgen = { version = "0.2.82", features = ["enable-interning"] }
wasm-bindgen-futures = { version = "0.4.41" }

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
5 changes: 5 additions & 0 deletions bindings/binding_typescript_wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"jest": "^25.1.0"
}
}
Loading

1 comment on commit 6b3c0da

@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: 6b3c0da Previous: 026ff7e Ratio
es/full/bugs-1 275032 ns/iter (± 2223) 289886 ns/iter (± 12294) 0.95
es/full/minify/libraries/antd 1811866763 ns/iter (± 67401519) 1859560991 ns/iter (± 112973168) 0.97
es/full/minify/libraries/d3 369383182 ns/iter (± 15280593) 342723411 ns/iter (± 2675950) 1.08
es/full/minify/libraries/echarts 1601704378 ns/iter (± 40307517) 1533113557 ns/iter (± 44210078) 1.04
es/full/minify/libraries/jquery 103988813 ns/iter (± 2833003) 103038525 ns/iter (± 366458) 1.01
es/full/minify/libraries/lodash 115983672 ns/iter (± 723237) 115235517 ns/iter (± 748888) 1.01
es/full/minify/libraries/moment 60231629 ns/iter (± 461813) 59702838 ns/iter (± 134184) 1.01
es/full/minify/libraries/react 18402396 ns/iter (± 54574) 18410272 ns/iter (± 518890) 1.00
es/full/minify/libraries/terser 280293611 ns/iter (± 5272147) 288539211 ns/iter (± 7905308) 0.97
es/full/minify/libraries/three 482861024 ns/iter (± 16010857) 509879882 ns/iter (± 17601076) 0.95
es/full/minify/libraries/typescript 3280340971 ns/iter (± 41775178) 3279551572 ns/iter (± 45393622) 1.00
es/full/minify/libraries/victory 829863380 ns/iter (± 32870982) 744273120 ns/iter (± 16120565) 1.11
es/full/minify/libraries/vue 146326196 ns/iter (± 979999) 145748656 ns/iter (± 450268) 1.00
es/full/codegen/es3 59523 ns/iter (± 589) 58498 ns/iter (± 208) 1.02
es/full/codegen/es5 59292 ns/iter (± 250) 58842 ns/iter (± 371) 1.01
es/full/codegen/es2015 59284 ns/iter (± 160) 58583 ns/iter (± 557) 1.01
es/full/codegen/es2016 59203 ns/iter (± 235) 58497 ns/iter (± 96) 1.01
es/full/codegen/es2017 59220 ns/iter (± 223) 58609 ns/iter (± 154) 1.01
es/full/codegen/es2018 59246 ns/iter (± 251) 58542 ns/iter (± 284) 1.01
es/full/codegen/es2019 59313 ns/iter (± 337) 58568 ns/iter (± 99) 1.01
es/full/codegen/es2020 59393 ns/iter (± 277) 58721 ns/iter (± 158) 1.01
es/full/all/es3 162890821 ns/iter (± 533522) 162922997 ns/iter (± 1254414) 1.00
es/full/all/es5 157716173 ns/iter (± 926047) 158096046 ns/iter (± 3796798) 1.00
es/full/all/es2015 116971013 ns/iter (± 1404493) 116915695 ns/iter (± 1027195) 1.00
es/full/all/es2016 116163333 ns/iter (± 881327) 116397150 ns/iter (± 432473) 1.00
es/full/all/es2017 115041876 ns/iter (± 1226725) 115261922 ns/iter (± 848059) 1.00
es/full/all/es2018 113537491 ns/iter (± 837830) 113255917 ns/iter (± 629242) 1.00
es/full/all/es2019 113090057 ns/iter (± 2496180) 113079870 ns/iter (± 910388) 1.00
es/full/all/es2020 109134308 ns/iter (± 952149) 108291781 ns/iter (± 720034) 1.01
es/full/parser 478254 ns/iter (± 4483) 475759 ns/iter (± 2482) 1.01
es/full/base/fixer 16797 ns/iter (± 260) 16365 ns/iter (± 55) 1.03
es/full/base/resolver_and_hygiene 79662 ns/iter (± 200) 80734 ns/iter (± 550) 0.99
serialization of serde 277 ns/iter (± 5) 276 ns/iter (± 2) 1.00
css/minify/libraries/bootstrap 21919352 ns/iter (± 211209) 22001257 ns/iter (± 31249) 1.00
css/visitor/compare/clone 1353881 ns/iter (± 20146) 1371317 ns/iter (± 14753) 0.99
css/visitor/compare/visit_mut_span 1484530 ns/iter (± 7646) 1482298 ns/iter (± 5926) 1.00
css/visitor/compare/visit_mut_span_panic 1515848 ns/iter (± 59639) 1504031 ns/iter (± 35796) 1.01
css/visitor/compare/fold_span 1986963 ns/iter (± 4731) 1995948 ns/iter (± 8099) 1.00
css/visitor/compare/fold_span_panic 2108204 ns/iter (± 2602) 2129802 ns/iter (± 5748) 0.99
css/lexer/bootstrap_5_1_3 3494263 ns/iter (± 1057) 3455302 ns/iter (± 85447) 1.01
css/lexer/foundation_6_7_4 2813257 ns/iter (± 3559) 2781711 ns/iter (± 3014) 1.01
css/lexer/tailwind_3_1_1 539153 ns/iter (± 129) 535454 ns/iter (± 190) 1.01
css/parser/bootstrap_5_1_3 16007183 ns/iter (± 18958) 16325385 ns/iter (± 45918) 0.98
css/parser/foundation_6_7_4 12803811 ns/iter (± 34115) 13104512 ns/iter (± 61348) 0.98
css/parser/tailwind_3_1_1 2530747 ns/iter (± 2479) 2589993 ns/iter (± 17947) 0.98
es/codegen/colors 727678 ns/iter (± 400497) 729250 ns/iter (± 398410) 1.00
es/codegen/large 3013102 ns/iter (± 1596871) 3029891 ns/iter (± 1603238) 0.99
es/codegen/with-parser/colors 40424 ns/iter (± 418) 40196 ns/iter (± 156) 1.01
es/codegen/with-parser/large 450806 ns/iter (± 8832) 450082 ns/iter (± 1828) 1.00
es/minify/libraries/antd 1510104554 ns/iter (± 33285115) 1560140206 ns/iter (± 26728486) 0.97
es/minify/libraries/d3 286176009 ns/iter (± 12772893) 301983350 ns/iter (± 5858312) 0.95
es/minify/libraries/echarts 1243859535 ns/iter (± 44584736) 1295038227 ns/iter (± 13470316) 0.96
es/minify/libraries/jquery 82887252 ns/iter (± 782802) 84231286 ns/iter (± 492836) 0.98
es/minify/libraries/lodash 104503489 ns/iter (± 513308) 105537249 ns/iter (± 455399) 0.99
es/minify/libraries/moment 48442281 ns/iter (± 129840) 49067010 ns/iter (± 296606) 0.99
es/minify/libraries/react 16261389 ns/iter (± 41234) 16372134 ns/iter (± 44897) 0.99
es/minify/libraries/terser 222326050 ns/iter (± 4740407) 227480694 ns/iter (± 2087222) 0.98
es/minify/libraries/three 387297937 ns/iter (± 18490903) 404841473 ns/iter (± 6755582) 0.96
es/minify/libraries/typescript 2806027602 ns/iter (± 28943831) 2861201047 ns/iter (± 28786694) 0.98
es/minify/libraries/victory 612576410 ns/iter (± 29805730) 676733147 ns/iter (± 11694955) 0.91
es/minify/libraries/vue 121789874 ns/iter (± 914255) 124005943 ns/iter (± 736756) 0.98
es/visitor/compare/clone 1712096 ns/iter (± 4255) 1718442 ns/iter (± 12248) 1.00
es/visitor/compare/visit_mut_span 2059906 ns/iter (± 9136) 2055571 ns/iter (± 6667) 1.00
es/visitor/compare/visit_mut_span_panic 2056428 ns/iter (± 10596) 2076730 ns/iter (± 29859) 0.99
es/visitor/compare/fold_span 2783268 ns/iter (± 4214) 2806083 ns/iter (± 11414) 0.99
es/visitor/compare/fold_span_panic 2819426 ns/iter (± 5551) 2837815 ns/iter (± 9490) 0.99
es/lexer/colors 9437 ns/iter (± 88) 9964 ns/iter (± 71) 0.95
es/lexer/angular 5116749 ns/iter (± 25345) 5114218 ns/iter (± 50193) 1.00
es/lexer/backbone 659645 ns/iter (± 8460) 669912 ns/iter (± 849) 0.98
es/lexer/jquery 3644491 ns/iter (± 10264) 3829004 ns/iter (± 5321) 0.95
es/lexer/jquery mobile 5468772 ns/iter (± 34263) 5657141 ns/iter (± 13073) 0.97
es/lexer/mootools 2909139 ns/iter (± 21785) 2932071 ns/iter (± 3826) 0.99
es/lexer/underscore 539605 ns/iter (± 2509) 589167 ns/iter (± 2270) 0.92
es/lexer/three 16434830 ns/iter (± 74050) 17393757 ns/iter (± 119864) 0.94
es/lexer/yui 2882127 ns/iter (± 30924) 3044091 ns/iter (± 6723) 0.95
es/lexer/cal-com 13321421 ns/iter (± 77940) 13593923 ns/iter (± 92937) 0.98
es/lexer/typescript 93038819 ns/iter (± 430638) 95785997 ns/iter (± 456665) 0.97
es/parser/colors 23821 ns/iter (± 57) 23877 ns/iter (± 161) 1.00
es/parser/angular 12657830 ns/iter (± 137891) 12689114 ns/iter (± 143296) 1.00
es/parser/backbone 1843644 ns/iter (± 8282) 1842336 ns/iter (± 4416) 1.00
es/parser/jquery 10367822 ns/iter (± 68835) 10381838 ns/iter (± 149545) 1.00
es/parser/jquery mobile 15447456 ns/iter (± 110555) 15584720 ns/iter (± 206433) 0.99
es/parser/mootools 8031705 ns/iter (± 36525) 7973991 ns/iter (± 44350) 1.01
es/parser/underscore 1631969 ns/iter (± 7648) 1614765 ns/iter (± 4823) 1.01
es/parser/three 44682682 ns/iter (± 459280) 45968967 ns/iter (± 723350) 0.97
es/parser/yui 7511590 ns/iter (± 42707) 7567655 ns/iter (± 41126) 0.99
es/parser/cal-com 42652886 ns/iter (± 582888) 42610258 ns/iter (± 541838) 1.00
es/parser/typescript 275328989 ns/iter (± 1178923) 278753391 ns/iter (± 3853739) 0.99
es/preset-env/usage/builtin_type 135625 ns/iter (± 33539) 135964 ns/iter (± 34001) 1.00
es/preset-env/usage/property 16270 ns/iter (± 210) 15719 ns/iter (± 93) 1.04
es/resolver/typescript 99528628 ns/iter (± 2182235) 100578247 ns/iter (± 3147014) 0.99
es/fixer/typescript 81536542 ns/iter (± 3514439) 82242350 ns/iter (± 2170640) 0.99
es/hygiene/typescript 181858324 ns/iter (± 3727808) 174580922 ns/iter (± 2900992) 1.04
es/resolver_with_hygiene/typescript 315159181 ns/iter (± 4308516) 307567120 ns/iter (± 7043212) 1.02
es/visitor/base-perf/module_clone 56688 ns/iter (± 1544) 57874 ns/iter (± 3626) 0.98
es/visitor/base-perf/fold_empty 59996 ns/iter (± 890) 60548 ns/iter (± 487) 0.99
es/visitor/base-perf/fold_noop_impl_all 60096 ns/iter (± 717) 60706 ns/iter (± 1115) 0.99
es/visitor/base-perf/fold_noop_impl_vec 59636 ns/iter (± 307) 60672 ns/iter (± 796) 0.98
es/visitor/base-perf/boxing_boxed_clone 59 ns/iter (± 0) 61 ns/iter (± 0) 0.97
es/visitor/base-perf/boxing_unboxed_clone 47 ns/iter (± 0) 47 ns/iter (± 0) 1
es/visitor/base-perf/boxing_boxed 109 ns/iter (± 0) 106 ns/iter (± 0) 1.03
es/visitor/base-perf/boxing_unboxed 81 ns/iter (± 0) 79 ns/iter (± 0) 1.03
es/visitor/base-perf/visit_contains_this 2565 ns/iter (± 10) 2647 ns/iter (± 25) 0.97
es/base/parallel/resolver/typescript 2436122717 ns/iter (± 104087410) 2483188664 ns/iter (± 120399890) 0.98
es/base/parallel/hygiene/typescript 3202408734 ns/iter (± 53785826) 3249440678 ns/iter (± 91926304) 0.99
babelify-only 497921 ns/iter (± 2463) 489241 ns/iter (± 10572) 1.02
parse_and_babelify_angular 41707222 ns/iter (± 662078) 39841392 ns/iter (± 2516314) 1.05
parse_and_babelify_backbone 4363187 ns/iter (± 77923) 4407504 ns/iter (± 124330) 0.99
parse_and_babelify_jquery 28518612 ns/iter (± 182521) 29276177 ns/iter (± 732037) 0.97
parse_and_babelify_jquery_mobile 49986921 ns/iter (± 932465) 50786452 ns/iter (± 866112) 0.98
parse_and_babelify_mootools 26525056 ns/iter (± 750054) 26695904 ns/iter (± 619230) 0.99
parse_and_babelify_underscore 3694386 ns/iter (± 9614) 3707891 ns/iter (± 18992) 1.00
parse_and_babelify_yui 24189822 ns/iter (± 1177250) 27539288 ns/iter (± 2212349) 0.88
html/minify/document/css_spec 46562394 ns/iter (± 2169388) 46319230 ns/iter (± 1050178) 1.01
html/minify/document/github 18187164 ns/iter (± 56942) 18412353 ns/iter (± 181885) 0.99
html/minify/document/stackoverflow 16709717 ns/iter (± 593363) 16743124 ns/iter (± 566578) 1.00
html/minify/document_fragment/css_spec 45785442 ns/iter (± 313295) 45012627 ns/iter (± 390755) 1.02
html/minify/document_fragment/github 17979810 ns/iter (± 254235) 18137251 ns/iter (± 175072) 0.99
html/minify/document_fragment/stackoverflow 16297047 ns/iter (± 67967) 16386123 ns/iter (± 150628) 0.99
html/document/visitor/compare/clone 166699 ns/iter (± 2270) 168284 ns/iter (± 1360) 0.99
html/document/visitor/compare/visit_mut_span 184474 ns/iter (± 878) 184502 ns/iter (± 1312) 1.00
html/document/visitor/compare/visit_mut_span_panic 193579 ns/iter (± 9127) 190309 ns/iter (± 860) 1.02
html/document/visitor/compare/fold_span 231725 ns/iter (± 1260) 231129 ns/iter (± 1553) 1.00
html/document/visitor/compare/fold_span_panic 279346 ns/iter (± 880) 282290 ns/iter (± 589) 0.99
html/document_fragment/visitor/compare/clone 167530 ns/iter (± 1006) 169056 ns/iter (± 988) 0.99
html/document_fragment/visitor/compare/visit_mut_span 183778 ns/iter (± 1173) 184676 ns/iter (± 791) 1.00
html/document_fragment/visitor/compare/visit_mut_span_panic 189919 ns/iter (± 608) 190255 ns/iter (± 1174) 1.00
html/document_fragment/visitor/compare/fold_span 233322 ns/iter (± 954) 231362 ns/iter (± 1863) 1.01
html/document_fragment/visitor/compare/fold_span_panic 286141 ns/iter (± 1177) 282406 ns/iter (± 12605) 1.01
html/lexer/css_2021_spec 13081368 ns/iter (± 25015) 12879729 ns/iter (± 204373) 1.02
html/lexer/github_com_17_05_2022 5003399 ns/iter (± 20141) 4943951 ns/iter (± 6642) 1.01
html/lexer/stackoverflow_com_17_05_2022 4734435 ns/iter (± 14171) 4679886 ns/iter (± 9224) 1.01
html/parser/parser_document/css_2021_spec 25826317 ns/iter (± 1332461) 25312165 ns/iter (± 174433) 1.02
html/parser/parser_document/github_com_17_05_2022 8473348 ns/iter (± 56281) 8406845 ns/iter (± 53175) 1.01
html/parser/parser_document/stackoverflow_com_17_05_2022 7421874 ns/iter (± 11470) 7318257 ns/iter (± 11762) 1.01
html/parser/parser_document_fragment/css_2021_spec 25715673 ns/iter (± 125714) 25031265 ns/iter (± 89925) 1.03
html/parser/parser_document_fragment/github_com_17_05_2022 8399949 ns/iter (± 19419) 8375192 ns/iter (± 15186) 1.00
html/parser/parser_document_fragment/stackoverflow_com_17_05_2022 7381717 ns/iter (± 15317) 7292847 ns/iter (± 16375) 1.01

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

Please sign in to comment.