diff --git a/bindings/Cargo.lock b/bindings/Cargo.lock index a6d06d600c0f..50b0a9cc145d 100644 --- a/bindings/Cargo.lock +++ b/bindings/Cargo.lock @@ -387,6 +387,7 @@ version = "1.10.7" dependencies = [ "anyhow", "getrandom", + "js-sys", "serde", "serde-wasm-bindgen 0.6.5", "serde_json", @@ -1036,7 +1037,7 @@ dependencies = [ "cssparser-macros", "dtoa-short", "itoa", - "phf 0.10.1", + "phf 0.11.2", "smallvec", ] @@ -2156,9 +2157,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.76" +version = "0.3.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" dependencies = [ "once_cell", "wasm-bindgen", @@ -2944,9 +2945,7 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259" dependencies = [ - "phf_macros 0.10.0", "phf_shared 0.10.0", - "proc-macro-hack", ] [[package]] @@ -2955,7 +2954,7 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" dependencies = [ - "phf_macros 0.11.2", + "phf_macros", "phf_shared 0.11.2", ] @@ -2989,20 +2988,6 @@ dependencies = [ "rand", ] -[[package]] -name = "phf_macros" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58fdf3184dd560f160dd73922bea2d5cd6e8f064bf4b13110abd81b03697b4e0" -dependencies = [ - "phf_generator 0.10.0", - "phf_shared 0.10.0", - "proc-macro-hack", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "phf_macros" version = "0.11.2" @@ -3177,12 +3162,6 @@ dependencies = [ "syn 2.0.90", ] -[[package]] -name = "proc-macro-hack" -version = "0.5.20+deprecated" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" - [[package]] name = "proc-macro2" version = "1.0.92" @@ -6318,20 +6297,21 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" dependencies = [ "cfg-if", "once_cell", + "rustversion", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" dependencies = [ "bumpalo", "log", @@ -6343,9 +6323,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.49" +version = "0.4.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2" +checksum = "555d470ec0bc3bb57890405e5d4322cc9ea83cebb085523ced7be4144dac1e61" dependencies = [ "cfg-if", "js-sys", @@ -6356,9 +6336,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -6366,9 +6346,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", @@ -6379,9 +6359,12 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] [[package]] name = "wasm-encoder" @@ -6799,9 +6782,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.76" +version = "0.3.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc" +checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/bindings/Cargo.toml b/bindings/Cargo.toml index 5ded416d5782..e5d6472d7db0 100644 --- a/bindings/Cargo.toml +++ b/bindings/Cargo.toml @@ -20,6 +20,7 @@ resolver = "2" anyhow = "1.0.86" backtrace = "0.3" getrandom = "0.2.15" + js-sys = "0.3.77" lightningcss = "1.0.0-alpha.58" napi = { version = "2", default-features = false } napi-build = "2" diff --git a/bindings/binding_typescript_wasm/Cargo.toml b/bindings/binding_typescript_wasm/Cargo.toml index 69b4087b460d..a8a8216d0ec9 100644 --- a/bindings/binding_typescript_wasm/Cargo.toml +++ b/bindings/binding_typescript_wasm/Cargo.toml @@ -17,6 +17,7 @@ crate-type = ["cdylib"] [dependencies] anyhow = { workspace = true } getrandom = { workspace = true, features = ["js"] } +js-sys = { workspace = true } serde = { workspace = true, features = ["derive"] } serde-wasm-bindgen = { workspace = true } serde_json = { workspace = true } diff --git a/bindings/binding_typescript_wasm/src/lib.rs b/bindings/binding_typescript_wasm/src/lib.rs index 3eb1be2e1033..88aff86920b6 100644 --- a/bindings/binding_typescript_wasm/src/lib.rs +++ b/bindings/binding_typescript_wasm/src/lib.rs @@ -1,37 +1,48 @@ use anyhow::Error; +use js_sys::Uint8Array; use serde::Serialize; use swc_common::{errors::ColorConfig, sync::Lrc, SourceMap, GLOBALS}; use swc_error_reporters::handler::{try_with_handler, HandlerOpts}; use swc_fast_ts_strip::{ErrorCode, Options, TransformOutput, TsError}; use wasm_bindgen::prelude::*; -use wasm_bindgen_futures::{ - future_to_promise, - js_sys::{JsString, Promise}, -}; +use wasm_bindgen_futures::{future_to_promise, js_sys::Promise}; /// Custom interface definitions for the @swc/wasm's public interface instead of /// auto generated one, which is not reflecting most of types in detail. #[wasm_bindgen(typescript_custom_section)] const INTERFACE_DEFINITIONS: &'static str = r#" -export declare function transform(src: string, opts?: Options): Promise; -export declare function transformSync(src: string, opts?: Options): TransformOutput; +export declare function transform(src: string | Uint8Array, opts?: Options): Promise; +export declare function transformSync(src: string | Uint8Array, opts?: Options): TransformOutput; export type { Options, TransformOutput }; "#; #[wasm_bindgen(skip_typescript)] -pub fn transform(input: JsString, options: JsValue) -> Promise { +pub fn transform(input: JsValue, options: JsValue) -> Promise { future_to_promise(async move { transform_sync(input, options) }) } #[wasm_bindgen(js_name = "transformSync", skip_typescript)] -pub fn transform_sync(input: JsString, options: JsValue) -> Result { +pub fn transform_sync(input: JsValue, options: JsValue) -> Result { let options: Options = if options.is_falsy() { Default::default() } else { serde_wasm_bindgen::from_value(options)? }; - let input = input.as_string().unwrap(); + let input = match input.as_string() { + Some(input) => input, + None => { + if input.is_instance_of::() { + let input = input.unchecked_into::(); + match input.to_string().as_string() { + Some(input) => input, + None => return Err(JsValue::from_str("Input Uint8Array is not valid utf-8")), + } + } else { + return Err(JsValue::from_str("Input is not a string or Uint8Array")); + } + } + }; let result = GLOBALS .set(&Default::default(), || operate(input, options))