diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 9be94394bd23..e0054a51645f 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -148,7 +148,7 @@ jobs: run: | echo '[patch.crates-io]' >> bindings/Cargo.toml ./scripts/cargo/patch-section.sh >> bindings/Cargo.toml - cd bindings && cargo update -p swc_core + cd bindings && cargo update -p swc_core -p swc_fast_ts_strip - name: Install wasm-pack run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh @@ -367,7 +367,7 @@ jobs: echo '[patch.crates-io]' >> bindings/Cargo.toml ./scripts/cargo/patch-section.sh ./scripts/cargo/patch-section.sh >> bindings/Cargo.toml - cd bindings && cargo update -p swc_core + cd bindings && cargo update -p swc_core -p swc_fast_ts_strip - name: Set platform name run: | @@ -424,7 +424,7 @@ jobs: run: | echo '[patch.crates-io]' >> bindings/Cargo.toml ./scripts/cargo/patch-section.sh >> bindings/Cargo.toml - cd bindings && cargo update -p swc_core + cd bindings && cargo update -p swc_core -p swc_fast_ts_strip - name: Prepare run: | diff --git a/Cargo.lock b/Cargo.lock index 067d5eb8eace..484f7029585d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5019,6 +5019,7 @@ dependencies = [ "swc_ecma_transforms_typescript", "swc_ecma_visit", "testing", + "wasm-bindgen", ] [[package]] diff --git a/bindings/Cargo.toml b/bindings/Cargo.toml index 4d624ceb05fa..23e95c3894ec 100644 --- a/bindings/Cargo.toml +++ b/bindings/Cargo.toml @@ -35,7 +35,7 @@ resolver = "2" tracing-chrome = "0.5.0" tracing-futures = "0.2.5" tracing-subscriber = "0.3.9" - wasm-bindgen = "0.2.82" + wasm-bindgen = "0.2.91" wasm-bindgen-futures = "0.4.41" [profile.release] diff --git a/bindings/binding_typescript_wasm/Cargo.toml b/bindings/binding_typescript_wasm/Cargo.toml index 2f2a1616d03c..995d06d71019 100644 --- a/bindings/binding_typescript_wasm/Cargo.toml +++ b/bindings/binding_typescript_wasm/Cargo.toml @@ -22,7 +22,7 @@ serde-wasm-bindgen = { workspace = true } serde_json = { workspace = true } swc_common = { workspace = true } swc_error_reporters = { workspace = true } -swc_fast_ts_strip = { workspace = true } +swc_fast_ts_strip = { workspace = true, features = ["wasm-bindgen"] } tracing = { workspace = true, features = ["max_level_off"] } wasm-bindgen = { workspace = true, features = ["enable-interning"] } wasm-bindgen-futures = { workspace = true } diff --git a/bindings/binding_typescript_wasm/src/lib.rs b/bindings/binding_typescript_wasm/src/lib.rs index 32a779213143..680726add384 100644 --- a/bindings/binding_typescript_wasm/src/lib.rs +++ b/bindings/binding_typescript_wasm/src/lib.rs @@ -16,14 +16,18 @@ export function transform(src: string, opts?: Options): Promise export function transformSync(src: string, opts?: Options): TransformOutput; "#; -#[wasm_bindgen] +#[wasm_bindgen(skip_typescript)] pub fn transform(input: JsString, options: JsValue) -> Promise { future_to_promise(async move { transform_sync(input, options) }) } -#[wasm_bindgen(js_name = "transformSync")] +#[wasm_bindgen(js_name = "transformSync", skip_typescript)] pub fn transform_sync(input: JsString, options: JsValue) -> Result { - let options: Options = serde_wasm_bindgen::from_value(options)?; + let options: Options = if options.is_falsy() { + Default::default() + } else { + serde_wasm_bindgen::from_value(options)? + }; let input = input.as_string().unwrap(); diff --git a/crates/swc_fast_ts_strip/Cargo.toml b/crates/swc_fast_ts_strip/Cargo.toml index 4cff8c46ad43..2e6e586379a5 100644 --- a/crates/swc_fast_ts_strip/Cargo.toml +++ b/crates/swc_fast_ts_strip/Cargo.toml @@ -11,8 +11,10 @@ version = "0.4.1" [dependencies] -anyhow = { workspace = true } -serde = { workspace = true, features = ["derive"] } +anyhow = { workspace = true } +serde = { workspace = true, features = ["derive"] } +wasm-bindgen = { workspace = true, optional = true } + swc_allocator = { version = "0.1.7", path = "../swc_allocator", default-features = false } swc_common = { version = "0.36.0", path = "../swc_common", features = [ diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 887a22cc184f..8dcddb5e6b48 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -31,6 +31,8 @@ use swc_ecma_transforms_base::{ }; use swc_ecma_transforms_typescript::typescript; use swc_ecma_visit::{Visit, VisitMutWith, VisitWith}; +#[cfg(feature = "wasm-bindgen")] +use wasm_bindgen::prelude::*; #[derive(Default, Deserialize)] #[serde(rename_all = "camelCase")] @@ -53,6 +55,17 @@ pub struct Options { pub source_map: bool, } +#[cfg(feature = "wasm-bindgen")] +#[wasm_bindgen(typescript_custom_section)] +const Type_Options: &'static str = r#" +interface Options { + module?: boolean; + filename?: string; + mode?: Mode; + sourceMap?: boolean; +} +"#; + #[derive(Debug, Default, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Mode { @@ -61,6 +74,12 @@ pub enum Mode { Transform, } +#[cfg(feature = "wasm-bindgen")] +#[wasm_bindgen(typescript_custom_section)] +const Type_Mode: &'static str = r#" +type Mode = "strip-only" | "transform"; +"#; + fn default_ts_syntax() -> TsSyntax { TsSyntax { decorators: true, @@ -74,6 +93,15 @@ pub struct TransformOutput { pub map: Option, } +#[cfg(feature = "wasm-bindgen")] +#[wasm_bindgen(typescript_custom_section)] +const Type_TransformOutput: &'static str = r#" +interface TransformOutput { + code: string; + map?: string; +} +"#; + pub fn operate( cm: &Lrc, handler: &Handler,