Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasm): Switch to TypeScript & named exports #363

Merged
merged 1 commit into from
May 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ npm install @rollup/plugin-wasm --save-dev
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:

```js
import wasm from '@rollup/plugin-wasm';
import { wasm } from '@rollup/plugin-wasm';

export default {
input: 'src/index.js',
Expand Down Expand Up @@ -66,9 +66,9 @@ int main() {
Compile the file using `emscripten`, or the online [WasmFiddle](https://wasdk.github.io/WasmFiddle//) tool. Then import and instantiate the resulting file:

```js
import wasm from './sample.wasm';
import sample from './sample.wasm';

wasm({ ...imports }).then(({ instance }) => {
sample({ ...imports }).then(({ instance }) => {
console.log(instance.exports.main());
});
```
Expand All @@ -90,7 +90,7 @@ wasm({
This means that the exports can be accessed immediately.

```js
import module from './sample.wasm';
import sample from './sample.wasm';

const instance = sample({ ...imports });

Expand Down
8 changes: 6 additions & 2 deletions packages/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Jamen Marz <jamenmarz+gh@gmail.com>",
"homepage": "https://github.com/rollup/plugins/tree/master/packages/wasm/#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index",
"main": "dist/index.js",
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
Expand All @@ -29,6 +29,7 @@
},
"files": [
"dist",
"types",
"README.md",
"LICENSE"
],
Expand All @@ -46,6 +47,7 @@
"rollup": "^1.20.0||^2.0.0"
},
"devDependencies": {
"@rollup/plugin-typescript": "^4.1.1",
"del-cli": "^3.0.0",
"rollup": "^2.0.0",
"source-map": "^0.7.3"
Expand All @@ -61,5 +63,7 @@
"contributors": [
"Jamen Marz <jamenmarz+gh@gmail.com>",
"Colin Eberhardt <colin.eberhardt@gmail.com>"
]
],
"module": "dist/index.es.js",
"types": "types/index.d.ts"
}
18 changes: 7 additions & 11 deletions packages/wasm/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
const { builtinModules } = require('module');
import typescript from '@rollup/plugin-typescript';
import { createConfig } from '../../shared/rollup.config';

const pkg = require('./package.json');
import pkg from './package.json';

const dependencies = Object.keys(pkg.dependencies || {});

export default [
{
input: 'src/index.js',
output: { exports: 'named', file: 'dist/index.js', format: 'cjs' },
external: [...builtinModules, ...dependencies]
}
];
export default {
...createConfig(pkg),
plugins: [typescript()],
}
10 changes: 7 additions & 3 deletions packages/wasm/src/index.js → packages/wasm/src/index.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { readFile } from 'fs';
import { resolve } from 'path';

export default function wasm(options = {}) {
options = Object.assign({}, options); // eslint-disable-line no-param-reassign
import { Plugin } from 'rollup';

import { RollupWasmOptions } from '../types';

export function wasm(options: RollupWasmOptions = {}): Plugin {
const syncFiles = (options.sync || []).map((x) => resolve(x));

return {
Expand Down Expand Up @@ -49,13 +51,15 @@ export default function wasm(options = {}) {
}
`.trim(),

// eslint-disable-next-line consistent-return
transform(code, id) {
if (code && /\.wasm$/.test(id)) {
const src = Buffer.from(code, 'binary').toString('base64');
const sync = syncFiles.indexOf(id) !== -1;
return `export default function(imports){return _loadWasmModule(${+sync}, '${src}', imports)}`;
}
return null;
}
};
}

export default wasm;
15 changes: 5 additions & 10 deletions packages/wasm/test/test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { rollup } from 'rollup';
import test from 'ava';

// eslint-disable-next-line no-unused-vars, import/no-unresolved, import/extensions
import wasm from '../dist/index';
import { getCode } from '../../../util/test';

const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
import wasm from '../';

const generateCode = async (bundle) => {
const { output } = await bundle.generate({ format: 'cjs' });
const [{ code }] = output;
return code;
};
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;

const testBundle = async (t, bundle) => {
const code = await generateCode(bundle);
const code = await getCode(bundle);
const func = new AsyncFunction('t', `let result;\n\n${code}\n\nreturn result;`);
return func(t);
};
Expand Down Expand Up @@ -75,7 +70,7 @@ try {
input: 'test/fixtures/worker.js',
plugins: [wasm()]
});
const code = await generateCode(bundle);
const code = await getCode(bundle);
const executeWorker = () => {
const worker = new Worker(code, { eval: true });
return new Promise((resolve, reject) => {
Expand Down
14 changes: 14 additions & 0 deletions packages/wasm/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Plugin } from 'rollup';

export interface RollupWasmOptions {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this with the latest version of the typescript plugin, or will that do definition file output for us now?

Copy link
Member Author

Choose a reason for hiding this comment

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

At the moment the types folder is a bit cleaner. I'm still refining the declaration output for the typescript plugin.

/**
* Specifies an array of strings that each represent a WebAssembly file to load synchronously.
*/
sync?: readonly string[];
}

/**
* 🍣 A Rollup which allows importing and bundling [WebAssembly modules](http://webassembly.org).
*/
export function wasm(options?: RollupWasmOptions): Plugin;
export default wasm;
29 changes: 29 additions & 0 deletions pnpm-lock.yaml

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

25 changes: 25 additions & 0 deletions shared/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { builtinModules } from 'module';

/**
* Create a base rollup config
* @param {*} pkg Imported package.json
* @returns {import('rollup').RollupOptions}
*/
export function createConfig(pkg) {
return {
input: 'src/index.ts',
external: Object.keys(pkg.dependencies || {}).concat(builtinModules),
output: [
{
format: 'cjs',
file: pkg.main,
exports: 'named',
footer: 'module.exports = Object.assign(exports.default, exports);'
},
{
format: 'esm',
file: pkg.module
}
]
};
}
4 changes: 2 additions & 2 deletions util/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { RollupBuild, OutputOptions, OutputChunk, OutputAsset } from 'rollup';
import { Assertions } from 'ava';

interface GetCode {
(bundle: RollupBuild, outputOptions: OutputOptions, allFiles?: false): Promise<string>;
(bundle: RollupBuild, outputOptions: OutputOptions, allFiles: true): Promise<
(bundle: RollupBuild, outputOptions?: OutputOptions | null, allFiles?: false): Promise<string>;
(bundle: RollupBuild, outputOptions: OutputOptions | null | undefined, allFiles: true): Promise<
Array<{
code: OutputChunk['code'] | undefined;
fileName: OutputChunk['fileName'] | OutputAsset['fileName'];
Expand Down
2 changes: 1 addition & 1 deletion util/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @param {import('rollup').RollupBuild} bundle
* @param {import('rollup').OutputOptions} outputOptions
* @param {import('rollup').OutputOptions} [outputOptions]
*/
const getCode = async (bundle, outputOptions, allFiles = false) => {
const { output } = await bundle.generate(outputOptions || { format: 'cjs' });
Expand Down