Skip to content

Commit 27ca712

Browse files
authoredJul 20, 2024··
fix(es/typescript): Fix typings (#9301)
1 parent 51d3f8d commit 27ca712

File tree

7 files changed

+45
-10
lines changed

7 files changed

+45
-10
lines changed
 

‎.github/workflows/CI.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ jobs:
148148
run: |
149149
echo '[patch.crates-io]' >> bindings/Cargo.toml
150150
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
151-
cd bindings && cargo update -p swc_core
151+
cd bindings && cargo update -p swc_core -p swc_fast_ts_strip
152152
153153
- name: Install wasm-pack
154154
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
@@ -367,7 +367,7 @@ jobs:
367367
echo '[patch.crates-io]' >> bindings/Cargo.toml
368368
./scripts/cargo/patch-section.sh
369369
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
370-
cd bindings && cargo update -p swc_core
370+
cd bindings && cargo update -p swc_core -p swc_fast_ts_strip
371371
372372
- name: Set platform name
373373
run: |
@@ -424,7 +424,7 @@ jobs:
424424
run: |
425425
echo '[patch.crates-io]' >> bindings/Cargo.toml
426426
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
427-
cd bindings && cargo update -p swc_core
427+
cd bindings && cargo update -p swc_core -p swc_fast_ts_strip
428428
429429
- name: Prepare
430430
run: |

‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindings/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ resolver = "2"
3535
tracing-chrome = "0.5.0"
3636
tracing-futures = "0.2.5"
3737
tracing-subscriber = "0.3.9"
38-
wasm-bindgen = "0.2.82"
38+
wasm-bindgen = "0.2.91"
3939
wasm-bindgen-futures = "0.4.41"
4040

4141
[profile.release]

‎bindings/binding_typescript_wasm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ serde-wasm-bindgen = { workspace = true }
2222
serde_json = { workspace = true }
2323
swc_common = { workspace = true }
2424
swc_error_reporters = { workspace = true }
25-
swc_fast_ts_strip = { workspace = true }
25+
swc_fast_ts_strip = { workspace = true, features = ["wasm-bindgen"] }
2626
tracing = { workspace = true, features = ["max_level_off"] }
2727
wasm-bindgen = { workspace = true, features = ["enable-interning"] }
2828
wasm-bindgen-futures = { workspace = true }

‎bindings/binding_typescript_wasm/src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ export function transform(src: string, opts?: Options): Promise<TransformOutput>
1616
export function transformSync(src: string, opts?: Options): TransformOutput;
1717
"#;
1818

19-
#[wasm_bindgen]
19+
#[wasm_bindgen(skip_typescript)]
2020
pub fn transform(input: JsString, options: JsValue) -> Promise {
2121
future_to_promise(async move { transform_sync(input, options) })
2222
}
2323

24-
#[wasm_bindgen(js_name = "transformSync")]
24+
#[wasm_bindgen(js_name = "transformSync", skip_typescript)]
2525
pub fn transform_sync(input: JsString, options: JsValue) -> Result<JsValue, JsValue> {
26-
let options: Options = serde_wasm_bindgen::from_value(options)?;
26+
let options: Options = if options.is_falsy() {
27+
Default::default()
28+
} else {
29+
serde_wasm_bindgen::from_value(options)?
30+
};
2731

2832
let input = input.as_string().unwrap();
2933

‎crates/swc_fast_ts_strip/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ version = "0.4.1"
1111

1212

1313
[dependencies]
14-
anyhow = { workspace = true }
15-
serde = { workspace = true, features = ["derive"] }
14+
anyhow = { workspace = true }
15+
serde = { workspace = true, features = ["derive"] }
16+
wasm-bindgen = { workspace = true, optional = true }
17+
1618
swc_allocator = { version = "0.1.7", path = "../swc_allocator", default-features = false }
1719

1820
swc_common = { version = "0.36.0", path = "../swc_common", features = [

‎crates/swc_fast_ts_strip/src/lib.rs

+28
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use swc_ecma_transforms_base::{
3131
};
3232
use swc_ecma_transforms_typescript::typescript;
3333
use swc_ecma_visit::{Visit, VisitMutWith, VisitWith};
34+
#[cfg(feature = "wasm-bindgen")]
35+
use wasm_bindgen::prelude::*;
3436

3537
#[derive(Default, Deserialize)]
3638
#[serde(rename_all = "camelCase")]
@@ -53,6 +55,17 @@ pub struct Options {
5355
pub source_map: bool,
5456
}
5557

58+
#[cfg(feature = "wasm-bindgen")]
59+
#[wasm_bindgen(typescript_custom_section)]
60+
const Type_Options: &'static str = r#"
61+
interface Options {
62+
module?: boolean;
63+
filename?: string;
64+
mode?: Mode;
65+
sourceMap?: boolean;
66+
}
67+
"#;
68+
5669
#[derive(Debug, Default, Deserialize)]
5770
#[serde(rename_all = "kebab-case")]
5871
pub enum Mode {
@@ -61,6 +74,12 @@ pub enum Mode {
6174
Transform,
6275
}
6376

77+
#[cfg(feature = "wasm-bindgen")]
78+
#[wasm_bindgen(typescript_custom_section)]
79+
const Type_Mode: &'static str = r#"
80+
type Mode = "strip-only" | "transform";
81+
"#;
82+
6483
fn default_ts_syntax() -> TsSyntax {
6584
TsSyntax {
6685
decorators: true,
@@ -74,6 +93,15 @@ pub struct TransformOutput {
7493
pub map: Option<String>,
7594
}
7695

96+
#[cfg(feature = "wasm-bindgen")]
97+
#[wasm_bindgen(typescript_custom_section)]
98+
const Type_TransformOutput: &'static str = r#"
99+
interface TransformOutput {
100+
code: string;
101+
map?: string;
102+
}
103+
"#;
104+
77105
pub fn operate(
78106
cm: &Lrc<SourceMap>,
79107
handler: &Handler,

0 commit comments

Comments
 (0)
Please sign in to comment.