Skip to content
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
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ rustup_nightly: &rustup_nightly
name: Install Cargo and Rust compiler
command: |
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> $BASH_ENV
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain 1.28.0
source $BASH_ENV
rustup target add wasm32-unknown-unknown --toolchain nightly
rustup target add wasm32-unknown-unknown
smoke_tests: &smoke_tests
steps:
- checkout
Expand Down
36 changes: 15 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</div>

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

## Requirements

This module requires a minimum of Node v8.9.0, Jest v23.4.2, and Rust in [nightly channel][].
<ul>
<li>Node v8 or later</li>
<li>Jest v23 or later</li>
<li><details>
<summary>Rust v1.28.0 with wasm32-uknown-unknown installed</summary>

```console
rustup default 1.28.0
rustup target add wasm32-unknown-unknown
```

</details></li>
</ul>

## Getting Started

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

- Type: `string`
- Default: `promise`
- Expected value:
- `buffer` will export wasm code as [Buffer][]
- `module` will export wasm code as [WebAssembly.Module][]
- `instance` will export wasm code as [WebAssembly.Instance][]
- `async` will [instantiate][webassembly.instantiate] wasm code asynchronously, return promise of both [WebAssembly.Module][] and [WebAssembly.Instance][]
- `async-module` will [compile][webassembly.compile] wasm code asynchronously, return promise of [WebAssembly.Module][]
- `async-instance` will [instantiate][webassembly.instantiate] wasm code asynchronously, return promise of [WebAssembly.Instance][]

How wasm code would be exported. (see [examples](#examples))
How wasm code would be exported. This options is identical with [option `export` in webassembly-loader][webassembly-loader]. (see [examples](#examples))

```json
{
Expand Down Expand Up @@ -316,17 +317,10 @@ wasmCompile(importObject | undefined).then(module => {
[![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)

[ts-jest]: https://github.com/kulshekhar/ts-jest/
[nightly channel]: https://rustwasm.github.io/book/game-of-life/setup.html#the-wasm32-unknown-unknown-target
[buffer]: https://nodejs.org/api/buffer.html
[webassembly.module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module
[webassembly.instance]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance
[webassembly.instantiate]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate
[webassembly.compile]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/compile
[webassembly-loader]: https://github.com/DrSensor/webassembly-loader#export
[npm]: https://img.shields.io/npm/v/rs-jest.svg
[npm-url]: https://npmjs.com/package/rs-jest
[npm-download]: https://img.shields.io/npm/dm/rs-jest.svg
[node]: https://img.shields.io/node/v/rs-jest.svg
[node-url]: https://nodejs.org
[deps]: https://david-dm.org/DrSensor/rs-jest.svg
[deps-url]: https://david-dm.org/DrSensor/rs-jest
[tests]: https://img.shields.io/circleci/project/github/DrSensor/rs-jest.svg
Expand Down
91 changes: 91 additions & 0 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
"typescript": "^3.0.1"
}
"typescript": "^3.0.1",
"webassembly-loader": "^1.1.0"
},
"dependencies": {}
}
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export default [
exclude: ["test/**"],
tsconfigOverride: {compilerOptions: {module: "esnext"}}
}),
babel(),
commonjs(),
resolve(),
babel(),
autoExternal(),
prettier(prettierrc.files("*.js"))
]
Expand Down
20 changes: 5 additions & 15 deletions src/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cargoCommand, findSrcDir, handleCargo } from './cargo';

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

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

switch (options.export) {
case 'buffer':
return wrap(wasmCode).asBuffer;
case 'instance':
return wrap(wasmCode).asWebAssembly.Instance;
case 'module':
return wrap(wasmCode).asWebAssembly.Module;
case 'async':
return wrap(wasmCode).promiseWebAssembly.Both;
case 'async-instance':
return wrap(wasmCode).promiseWebAssembly.Instance;
case 'async-module':
return wrap(wasmCode).promiseWebAssembly.Module;
}
return wasm2js(wasmCode, {
export: options.export,
module: 'cjs'
});
}
37 changes: 0 additions & 37 deletions src/wrapper.ts

This file was deleted.

9 changes: 6 additions & 3 deletions test/export.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/// https://stackoverflow.com/a/50856790/5221998
import { resolve } from './utils';
import { basename } from 'path';
import jest_please = require('jest');

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

test('globals: {"rs-jest": {export: *}}', async () => {
options.projects = resolve('fixtures/export_*');
const { results } = await jest_please.runCLI(options, options.projects);
const fixture = resolve('fixtures/export_*');
const projects = fixture.map((s, i) => [basename(s).replace('_', ' as '), i]);

test.each(projects)(`%s`, async (s, i) => {
const { results } = await jest_please.runCLI(options, [fixture[i]]);
expect(results.success).toBe(true);
});
});