Skip to content

Commit

Permalink
feat(wasm): Expose async facade interfaces (#5352)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj authored Aug 1, 2022
1 parent f88a8c9 commit 281bdd9
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 66 deletions.
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ imports_granularity = "Crate"
reorder_impl_items = true
use_field_init_shorthand = true
wrap_comments = true
edition = "2018"
5 changes: 3 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/binding_core_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ plugin = [
"wasmer-wasi",
"wasmer/js-default",
"wasmer-wasi/js-default",
"js-sys",
]

[dependencies]
anyhow = "1.0.58"
console_error_panic_hook = "0.1.7"
js-sys = { version = "0.3.59", optional = true }
js-sys = { version = "0.3.59" }
once_cell = "1.13.0"
parking_lot_core = "0.9.3"
path-clean = "0.1.0"
Expand All @@ -49,6 +48,7 @@ wasm-bindgen = { version = "0.2.82", features = [
"serde-serialize",
"enable-interning",
] }
wasm-bindgen-futures = "0.4.32"
wasmer = { version = "2.3.0", optional = true, default-features = false }
wasmer-wasi = { version = "2.3.0", optional = true, default-features = false }

Expand Down
267 changes: 241 additions & 26 deletions crates/binding_core_wasm/__tests__/simple.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,248 @@
const swc = require("../pkg");

it("should be loadable", function () {
const output = swc.transformSync("class Foo {}", {});
describe("transform", () => {
it("should work", function () {
const output = swc.transformSync("class Foo {}", {});

expect(output).toMatchInlineSnapshot(`
Object {
"code": "function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError(\\"Cannot call a class as a function\\");
}
}
var Foo = function Foo() {
\\"use strict\\";
_classCallCheck(this, Foo);
};
",
}
`);
});

it("should work with async facade", async () => {
const output = await swc.transform("class Foo {}", {});

expect(output).toMatchInlineSnapshot(`
Object {
"code": "function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError(\\"Cannot call a class as a function\\");
}
}
var Foo = function Foo() {
\\"use strict\\";
_classCallCheck(this, Foo);
};
",
}
`);
});

it("should work with program object", async () => {
const input = swc.parseSync("class Foo {}", {
syntax: "typescript",
target: "es2021",
});

const output = await swc.transform(input, {});
expect(output).toMatchInlineSnapshot(`
Object {
"code": "function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError(\\"Cannot call a class as a function\\");
}
}
var Foo = function Foo() {
\\"use strict\\";
_classCallCheck(this, Foo);
};
",
}
`);
});

it("should support 'paths' and 'baseUrl'", () => {
const { code } = swc.transformSync(
`
import foo from '@src/app';
console.log(foo)
`,
{
filename: "main.js",
jsc: {
parser: {
syntax: "typescript",
},
target: "es2021",
transform: {},
baseUrl: __dirname,
paths: {
"@src/*": ["bar/*"],
},
},
module: {
type: "commonjs",
},
}
);

expect(code).toContain(`bar/app`);
});
});

it("should support 'paths' and 'baseUrl'", async () => {
const { code } = await swc.transformSync(
`
import foo from '@src/app';
console.log(foo)
`,
{
filename: "main.js",
jsc: {
parser: {
syntax: "typescript",
describe("parse", () => {
it("should work", () => {
const output = swc.parseSync("class Foo {}", {
syntax: "typescript",
target: "es2021",
});

expect(output).toMatchInlineSnapshot(`
Object {
"body": Array [
Object {
"body": Array [],
"declare": false,
"decorators": Array [],
"identifier": Object {
"optional": false,
"span": Object {
"ctxt": 0,
"end": 394,
"start": 391,
},
"type": "Identifier",
"value": "Foo",
},
"implements": Array [],
"isAbstract": false,
"span": Object {
"ctxt": 0,
"end": 397,
"start": 385,
},
"superClass": null,
"superTypeParams": null,
"type": "ClassDeclaration",
"typeParams": null,
},
target: "es2021",
transform: {},
baseUrl: __dirname,
paths: {
"@src/*": ["bar/*"],
],
"interpreter": null,
"span": Object {
"ctxt": 0,
"end": 397,
"start": 385,
},
"type": "Module",
}
`);
});

it("should work with async facade", async () => {
const output = await swc.parse("class Foo {}", {
syntax: "typescript",
target: "es2021",
});

expect(output).toMatchInlineSnapshot(`
Object {
"body": Array [
Object {
"body": Array [],
"declare": false,
"decorators": Array [],
"identifier": Object {
"optional": false,
"span": Object {
"ctxt": 0,
"end": 407,
"start": 404,
},
"type": "Identifier",
"value": "Foo",
},
"implements": Array [],
"isAbstract": false,
"span": Object {
"ctxt": 0,
"end": 410,
"start": 398,
},
"superClass": null,
"superTypeParams": null,
"type": "ClassDeclaration",
"typeParams": null,
},
},
module: {
type: "commonjs",
},
}
);

expect(code).toContain(`bar/app`);
],
"interpreter": null,
"span": Object {
"ctxt": 0,
"end": 410,
"start": 398,
},
"type": "Module",
}
`);
});
});

describe("minify", () => {
it("should work", () => {
const output = swc.minifySync(
"const somename = 1; console.log(somename);"
);

expect(output).toMatchInlineSnapshot(`
Object {
"code": "const a=1;console.log(1)",
}
`);
});

it("should work with async facade", async () => {
const output = await swc.minify(
"const somename = 1; console.log(somename);"
);

expect(output).toMatchInlineSnapshot(`
Object {
"code": "const a=1;console.log(1)",
}
`);
});
});

describe("print", () => {
it("should work", () => {
const input = swc.parseSync("class Foo {}", {
syntax: "typescript",
target: "es2021",
});

const output = swc.printSync(input);
expect(output).toMatchInlineSnapshot(`
Object {
"code": "class Foo {
}
",
}
`);
});

it("should work with async facade", async () => {
const input = swc.parseSync("class Foo {}", {
syntax: "typescript",
target: "es2021",
});

const output = await swc.print(input);
expect(output).toMatchInlineSnapshot(`
Object {
"code": "class Foo {
}
",
}
`);
});
});
Loading

1 comment on commit 281bdd9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 281bdd9 Previous: b0cb35a Ratio
es/full/minify/libraries/antd 1731486185 ns/iter (± 75071336) 1730150475 ns/iter (± 40319214) 1.00
es/full/minify/libraries/d3 393948950 ns/iter (± 28709028) 402969266 ns/iter (± 7432410) 0.98
es/full/minify/libraries/echarts 1509150776 ns/iter (± 144793807) 1503733288 ns/iter (± 69848188) 1.00
es/full/minify/libraries/jquery 93984041 ns/iter (± 2606859) 104830868 ns/iter (± 9174835) 0.90
es/full/minify/libraries/lodash 139937913 ns/iter (± 2481002) 128829984 ns/iter (± 4475215) 1.09
es/full/minify/libraries/moment 83068090 ns/iter (± 35065053) 57130061 ns/iter (± 2173952) 1.45
es/full/minify/libraries/react 20249021 ns/iter (± 2462054) 18388191 ns/iter (± 429848) 1.10
es/full/minify/libraries/terser 321804629 ns/iter (± 30271583) 314908962 ns/iter (± 5448801) 1.02
es/full/minify/libraries/three 581494021 ns/iter (± 45568394) 556065626 ns/iter (± 8683884) 1.05
es/full/minify/libraries/typescript 3580987472 ns/iter (± 193327691) 3583439274 ns/iter (± 123316778) 1.00
es/full/minify/libraries/victory 778032112 ns/iter (± 35779447) 773788359 ns/iter (± 46676026) 1.01
es/full/minify/libraries/vue 140085049 ns/iter (± 19692060) 144341234 ns/iter (± 16854658) 0.97
es/full/codegen/es3 33917 ns/iter (± 1658) 33661 ns/iter (± 1604) 1.01
es/full/codegen/es5 34092 ns/iter (± 3771) 33725 ns/iter (± 1038) 1.01
es/full/codegen/es2015 33890 ns/iter (± 3527) 33800 ns/iter (± 863) 1.00
es/full/codegen/es2016 34217 ns/iter (± 3327) 33505 ns/iter (± 824) 1.02
es/full/codegen/es2017 34376 ns/iter (± 3081) 33669 ns/iter (± 2297) 1.02
es/full/codegen/es2018 33819 ns/iter (± 3455) 33660 ns/iter (± 1344) 1.00
es/full/codegen/es2019 33817 ns/iter (± 2965) 33816 ns/iter (± 1627) 1.00
es/full/codegen/es2020 33487 ns/iter (± 2339) 33963 ns/iter (± 845) 0.99
es/full/all/es3 191731278 ns/iter (± 14203121) 197882005 ns/iter (± 11741551) 0.97
es/full/all/es5 188471112 ns/iter (± 18861830) 186007358 ns/iter (± 11826776) 1.01
es/full/all/es2015 163489873 ns/iter (± 16050753) 150926673 ns/iter (± 18465357) 1.08
es/full/all/es2016 166234071 ns/iter (± 13991307) 149529222 ns/iter (± 15425543) 1.11
es/full/all/es2017 157357512 ns/iter (± 11924167) 148648827 ns/iter (± 10599900) 1.06
es/full/all/es2018 158878524 ns/iter (± 22529313) 147588332 ns/iter (± 16908808) 1.08
es/full/all/es2019 164263099 ns/iter (± 21517484) 147429980 ns/iter (± 13939758) 1.11
es/full/all/es2020 157638820 ns/iter (± 21898124) 142627649 ns/iter (± 9460782) 1.11
es/full/parser 781238 ns/iter (± 60164) 773218 ns/iter (± 38949) 1.01
es/full/base/fixer 29625 ns/iter (± 2547) 28995 ns/iter (± 730) 1.02
es/full/base/resolver_and_hygiene 89990 ns/iter (± 9896) 89752 ns/iter (± 8093) 1.00
serialization of ast node 217 ns/iter (± 24) 215 ns/iter (± 5) 1.01
serialization of serde 239 ns/iter (± 20) 238 ns/iter (± 7) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.