Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fast-ts): Support Uint8Array Input #9879

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 23 additions & 40 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 @@ -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"
Expand Down
1 change: 1 addition & 0 deletions bindings/binding_typescript_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
29 changes: 20 additions & 9 deletions bindings/binding_typescript_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<TransformOutput>;
export declare function transformSync(src: string, opts?: Options): TransformOutput;
export declare function transform(src: string | Uint8Array, opts?: Options): Promise<TransformOutput>;
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<JsValue, JsValue> {
pub fn transform_sync(input: JsValue, options: JsValue) -> Result<JsValue, JsValue> {
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::<Uint8Array>() {
let input = input.unchecked_into::<Uint8Array>();
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))
Expand Down
Loading