Skip to content

Commit 995ced6

Browse files
authored
feat: use webassembly-loader internally (#8)
* feat: use webassembly-loader internally * test: utilize jest-each for better error reporting * docs: update README due to loader integration * ci: lock Rust version to 1.28.0
1 parent 150e6af commit 995ced6

File tree

8 files changed

+124
-81
lines changed

8 files changed

+124
-81
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ rustup_nightly: &rustup_nightly
66
name: Install Cargo and Rust compiler
77
command: |
88
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> $BASH_ENV
9-
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly
9+
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.28.0
1010
source $BASH_ENV
11-
rustup target add wasm32-unknown-unknown --toolchain nightly
11+
rustup target add wasm32-unknown-unknown
1212
smoke_tests: &smoke_tests
1313
steps:
1414
- checkout

README.md

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
</div>
99

1010
[![npm][npm]][npm-url]
11-
[![node][node]][node-url]
1211
[![size][size]][size-url]
1312
[![npm][npm-download]][npm-url]
1413
[![deps][deps]][deps-url]
@@ -27,7 +26,19 @@ This is a jest transformer that loads Rust code so it can be interop with Javasc
2726

2827
## Requirements
2928

30-
This module requires a minimum of Node v8.9.0, Jest v23.4.2, and Rust in [nightly channel][].
29+
<ul>
30+
<li>Node v8 or later</li>
31+
<li>Jest v23 or later</li>
32+
<li><details>
33+
<summary>Rust v1.28.0 with wasm32-uknown-unknown installed</summary>
34+
35+
```console
36+
rustup default 1.28.0
37+
rustup target add wasm32-unknown-unknown
38+
```
39+
40+
</details></li>
41+
</ul>
3142

3243
## Getting Started
3344

@@ -94,17 +105,7 @@ Pretty much like [ts-jest][], you can configure `rs-jest` by using global variab
94105
<details>
95106
<summary><b><code>export</code></b></summary>
96107

97-
- Type: `string`
98-
- Default: `promise`
99-
- Expected value:
100-
- `buffer` will export wasm code as [Buffer][]
101-
- `module` will export wasm code as [WebAssembly.Module][]
102-
- `instance` will export wasm code as [WebAssembly.Instance][]
103-
- `async` will [instantiate][webassembly.instantiate] wasm code asynchronously, return promise of both [WebAssembly.Module][] and [WebAssembly.Instance][]
104-
- `async-module` will [compile][webassembly.compile] wasm code asynchronously, return promise of [WebAssembly.Module][]
105-
- `async-instance` will [instantiate][webassembly.instantiate] wasm code asynchronously, return promise of [WebAssembly.Instance][]
106-
107-
How wasm code would be exported. (see [examples](#examples))
108+
How wasm code would be exported. This options is identical with [option `export` in webassembly-loader][webassembly-loader]. (see [examples](#examples))
108109

109110
```json
110111
{
@@ -316,17 +317,10 @@ wasmCompile(importObject | undefined).then(module => {
316317
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FDrSensor%2Frs-jest.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FDrSensor%2Frs-jest?ref=badge_large)
317318

318319
[ts-jest]: https://github.com/kulshekhar/ts-jest/
319-
[nightly channel]: https://rustwasm.github.io/book/game-of-life/setup.html#the-wasm32-unknown-unknown-target
320-
[buffer]: https://nodejs.org/api/buffer.html
321-
[webassembly.module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module
322-
[webassembly.instance]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance
323-
[webassembly.instantiate]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate
324-
[webassembly.compile]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/compile
320+
[webassembly-loader]: https://github.com/DrSensor/webassembly-loader#export
325321
[npm]: https://img.shields.io/npm/v/rs-jest.svg
326322
[npm-url]: https://npmjs.com/package/rs-jest
327323
[npm-download]: https://img.shields.io/npm/dm/rs-jest.svg
328-
[node]: https://img.shields.io/node/v/rs-jest.svg
329-
[node-url]: https://nodejs.org
330324
[deps]: https://david-dm.org/DrSensor/rs-jest.svg
331325
[deps-url]: https://david-dm.org/DrSensor/rs-jest
332326
[tests]: https://img.shields.io/circleci/project/github/DrSensor/rs-jest.svg

package-lock.json

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
"ts-node": "^7.0.1",
9191
"tslint": "^5.11.0",
9292
"tslint-config-prettier": "^1.15.0",
93-
"typescript": "^3.0.1"
94-
}
93+
"typescript": "^3.0.1",
94+
"webassembly-loader": "^1.1.0"
95+
},
96+
"dependencies": {}
9597
}

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export default [
3636
exclude: ["test/**"],
3737
tsconfigOverride: {compilerOptions: {module: "esnext"}}
3838
}),
39+
babel(),
3940
commonjs(),
4041
resolve(),
41-
babel(),
4242
autoExternal(),
4343
prettier(prettierrc.files("*.js"))
4444
]

src/preprocess.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { cargoCommand, findSrcDir, handleCargo } from './cargo';
33

44
import { flushLogs, logOnce, execPermissive } from './utils';
55
import { defaultConfig, getConfigFrom } from './options';
6-
import wrap from './wrapper';
6+
import wasm2js from 'webassembly-loader';
77

88
//#region helper
99
const is = (path: string) => ({ rustFile: /\.rs$/.test(path) });
@@ -70,18 +70,8 @@ export default function preprocess(
7070
const wasmCode = readFileSync(wasmFile);
7171
flushLogs();
7272

73-
switch (options.export) {
74-
case 'buffer':
75-
return wrap(wasmCode).asBuffer;
76-
case 'instance':
77-
return wrap(wasmCode).asWebAssembly.Instance;
78-
case 'module':
79-
return wrap(wasmCode).asWebAssembly.Module;
80-
case 'async':
81-
return wrap(wasmCode).promiseWebAssembly.Both;
82-
case 'async-instance':
83-
return wrap(wasmCode).promiseWebAssembly.Instance;
84-
case 'async-module':
85-
return wrap(wasmCode).promiseWebAssembly.Module;
86-
}
73+
return wasm2js(wasmCode, {
74+
export: options.export,
75+
module: 'cjs'
76+
});
8777
}

src/wrapper.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/export.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/// https://stackoverflow.com/a/50856790/5221998
22
import { resolve } from './utils';
3+
import { basename } from 'path';
34
import jest_please = require('jest');
45

56
describe('test export mode', () => {
67
const options = {
78
reporters: [['jest-silent-reporter', { useDots: true }]]
89
} as jestCLI.Options;
910

10-
test('globals: {"rs-jest": {export: *}}', async () => {
11-
options.projects = resolve('fixtures/export_*');
12-
const { results } = await jest_please.runCLI(options, options.projects);
11+
const fixture = resolve('fixtures/export_*');
12+
const projects = fixture.map((s, i) => [basename(s).replace('_', ' as '), i]);
13+
14+
test.each(projects)(`%s`, async (s, i) => {
15+
const { results } = await jest_please.runCLI(options, [fixture[i]]);
1316
expect(results.success).toBe(true);
1417
});
1518
});

0 commit comments

Comments
 (0)