Skip to content

Commit 281bdd9

Browse files
authored
feat(wasm): Expose async facade interfaces (#5352)
1 parent f88a8c9 commit 281bdd9

File tree

6 files changed

+353
-66
lines changed

6 files changed

+353
-66
lines changed

.rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ imports_granularity = "Crate"
55
reorder_impl_items = true
66
use_field_init_shorthand = true
77
wrap_comments = true
8+
edition = "2018"

Cargo.lock

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

crates/binding_core_wasm/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ plugin = [
2525
"wasmer-wasi",
2626
"wasmer/js-default",
2727
"wasmer-wasi/js-default",
28-
"js-sys",
2928
]
3029

3130
[dependencies]
3231
anyhow = "1.0.58"
3332
console_error_panic_hook = "0.1.7"
34-
js-sys = { version = "0.3.59", optional = true }
33+
js-sys = { version = "0.3.59" }
3534
once_cell = "1.13.0"
3635
parking_lot_core = "0.9.3"
3736
path-clean = "0.1.0"
@@ -49,6 +48,7 @@ wasm-bindgen = { version = "0.2.82", features = [
4948
"serde-serialize",
5049
"enable-interning",
5150
] }
51+
wasm-bindgen-futures = "0.4.32"
5252
wasmer = { version = "2.3.0", optional = true, default-features = false }
5353
wasmer-wasi = { version = "2.3.0", optional = true, default-features = false }
5454

+241-26
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,248 @@
11
const swc = require("../pkg");
22

3-
it("should be loadable", function () {
4-
const output = swc.transformSync("class Foo {}", {});
3+
describe("transform", () => {
4+
it("should work", function () {
5+
const output = swc.transformSync("class Foo {}", {});
6+
7+
expect(output).toMatchInlineSnapshot(`
8+
Object {
9+
"code": "function _classCallCheck(instance, Constructor) {
10+
if (!(instance instanceof Constructor)) {
11+
throw new TypeError(\\"Cannot call a class as a function\\");
12+
}
13+
}
14+
var Foo = function Foo() {
15+
\\"use strict\\";
16+
_classCallCheck(this, Foo);
17+
};
18+
",
19+
}
20+
`);
21+
});
22+
23+
it("should work with async facade", async () => {
24+
const output = await swc.transform("class Foo {}", {});
25+
26+
expect(output).toMatchInlineSnapshot(`
27+
Object {
28+
"code": "function _classCallCheck(instance, Constructor) {
29+
if (!(instance instanceof Constructor)) {
30+
throw new TypeError(\\"Cannot call a class as a function\\");
31+
}
32+
}
33+
var Foo = function Foo() {
34+
\\"use strict\\";
35+
_classCallCheck(this, Foo);
36+
};
37+
",
38+
}
39+
`);
40+
});
41+
42+
it("should work with program object", async () => {
43+
const input = swc.parseSync("class Foo {}", {
44+
syntax: "typescript",
45+
target: "es2021",
46+
});
47+
48+
const output = await swc.transform(input, {});
49+
expect(output).toMatchInlineSnapshot(`
50+
Object {
51+
"code": "function _classCallCheck(instance, Constructor) {
52+
if (!(instance instanceof Constructor)) {
53+
throw new TypeError(\\"Cannot call a class as a function\\");
54+
}
55+
}
56+
var Foo = function Foo() {
57+
\\"use strict\\";
58+
_classCallCheck(this, Foo);
59+
};
60+
",
61+
}
62+
`);
63+
});
64+
65+
it("should support 'paths' and 'baseUrl'", () => {
66+
const { code } = swc.transformSync(
67+
`
68+
import foo from '@src/app';
69+
console.log(foo)
70+
`,
71+
{
72+
filename: "main.js",
73+
jsc: {
74+
parser: {
75+
syntax: "typescript",
76+
},
77+
target: "es2021",
78+
transform: {},
79+
baseUrl: __dirname,
80+
paths: {
81+
"@src/*": ["bar/*"],
82+
},
83+
},
84+
module: {
85+
type: "commonjs",
86+
},
87+
}
88+
);
89+
90+
expect(code).toContain(`bar/app`);
91+
});
592
});
693

7-
it("should support 'paths' and 'baseUrl'", async () => {
8-
const { code } = await swc.transformSync(
9-
`
10-
import foo from '@src/app';
11-
console.log(foo)
12-
`,
13-
{
14-
filename: "main.js",
15-
jsc: {
16-
parser: {
17-
syntax: "typescript",
94+
describe("parse", () => {
95+
it("should work", () => {
96+
const output = swc.parseSync("class Foo {}", {
97+
syntax: "typescript",
98+
target: "es2021",
99+
});
100+
101+
expect(output).toMatchInlineSnapshot(`
102+
Object {
103+
"body": Array [
104+
Object {
105+
"body": Array [],
106+
"declare": false,
107+
"decorators": Array [],
108+
"identifier": Object {
109+
"optional": false,
110+
"span": Object {
111+
"ctxt": 0,
112+
"end": 394,
113+
"start": 391,
114+
},
115+
"type": "Identifier",
116+
"value": "Foo",
117+
},
118+
"implements": Array [],
119+
"isAbstract": false,
120+
"span": Object {
121+
"ctxt": 0,
122+
"end": 397,
123+
"start": 385,
124+
},
125+
"superClass": null,
126+
"superTypeParams": null,
127+
"type": "ClassDeclaration",
128+
"typeParams": null,
18129
},
19-
target: "es2021",
20-
transform: {},
21-
baseUrl: __dirname,
22-
paths: {
23-
"@src/*": ["bar/*"],
130+
],
131+
"interpreter": null,
132+
"span": Object {
133+
"ctxt": 0,
134+
"end": 397,
135+
"start": 385,
136+
},
137+
"type": "Module",
138+
}
139+
`);
140+
});
141+
142+
it("should work with async facade", async () => {
143+
const output = await swc.parse("class Foo {}", {
144+
syntax: "typescript",
145+
target: "es2021",
146+
});
147+
148+
expect(output).toMatchInlineSnapshot(`
149+
Object {
150+
"body": Array [
151+
Object {
152+
"body": Array [],
153+
"declare": false,
154+
"decorators": Array [],
155+
"identifier": Object {
156+
"optional": false,
157+
"span": Object {
158+
"ctxt": 0,
159+
"end": 407,
160+
"start": 404,
161+
},
162+
"type": "Identifier",
163+
"value": "Foo",
164+
},
165+
"implements": Array [],
166+
"isAbstract": false,
167+
"span": Object {
168+
"ctxt": 0,
169+
"end": 410,
170+
"start": 398,
171+
},
172+
"superClass": null,
173+
"superTypeParams": null,
174+
"type": "ClassDeclaration",
175+
"typeParams": null,
24176
},
25-
},
26-
module: {
27-
type: "commonjs",
28-
},
29-
}
30-
);
31-
32-
expect(code).toContain(`bar/app`);
177+
],
178+
"interpreter": null,
179+
"span": Object {
180+
"ctxt": 0,
181+
"end": 410,
182+
"start": 398,
183+
},
184+
"type": "Module",
185+
}
186+
`);
187+
});
188+
});
189+
190+
describe("minify", () => {
191+
it("should work", () => {
192+
const output = swc.minifySync(
193+
"const somename = 1; console.log(somename);"
194+
);
195+
196+
expect(output).toMatchInlineSnapshot(`
197+
Object {
198+
"code": "const a=1;console.log(1)",
199+
}
200+
`);
201+
});
202+
203+
it("should work with async facade", async () => {
204+
const output = await swc.minify(
205+
"const somename = 1; console.log(somename);"
206+
);
207+
208+
expect(output).toMatchInlineSnapshot(`
209+
Object {
210+
"code": "const a=1;console.log(1)",
211+
}
212+
`);
213+
});
214+
});
215+
216+
describe("print", () => {
217+
it("should work", () => {
218+
const input = swc.parseSync("class Foo {}", {
219+
syntax: "typescript",
220+
target: "es2021",
221+
});
222+
223+
const output = swc.printSync(input);
224+
expect(output).toMatchInlineSnapshot(`
225+
Object {
226+
"code": "class Foo {
227+
}
228+
",
229+
}
230+
`);
231+
});
232+
233+
it("should work with async facade", async () => {
234+
const input = swc.parseSync("class Foo {}", {
235+
syntax: "typescript",
236+
target: "es2021",
237+
});
238+
239+
const output = await swc.print(input);
240+
expect(output).toMatchInlineSnapshot(`
241+
Object {
242+
"code": "class Foo {
243+
}
244+
",
245+
}
246+
`);
247+
});
33248
});

0 commit comments

Comments
 (0)