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

fix(es/typescript): Fix typings #9301

Merged
merged 2 commits into from
Jul 20, 2024
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: |
Expand Down Expand Up @@ -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: |
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion bindings/binding_typescript_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
10 changes: 7 additions & 3 deletions bindings/binding_typescript_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ export function transform(src: string, opts?: Options): Promise<TransformOutput>
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<JsValue, JsValue> {
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();

Expand Down
6 changes: 4 additions & 2 deletions crates/swc_fast_ts_strip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
28 changes: 28 additions & 0 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -74,6 +93,15 @@ pub struct TransformOutput {
pub map: Option<String>,
}

#[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<SourceMap>,
handler: &Handler,
Expand Down
Loading