Skip to content

Commit

Permalink
feat(wasm): Switch to TypeScript & named exports
Browse files Browse the repository at this point in the history
BREAKING CHANGES: Named exports are used for CJS
  • Loading branch information
NotWoods committed May 1, 2020
1 parent 96e0900 commit eb596bf
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 94 deletions.
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
7 changes: 5 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 Down Expand Up @@ -46,6 +46,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 +62,7 @@
"contributors": [
"Jamen Marz <jamenmarz+gh@gmail.com>",
"Colin Eberhardt <colin.eberhardt@gmail.com>"
]
],
"module": "dist/index.es.js",
"types": "types/index.d.ts"
}
16 changes: 9 additions & 7 deletions packages/wasm/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const { builtinModules } = require('module');
import typescript from '@rollup/plugin-typescript';

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

const dependencies = Object.keys(pkg.dependencies || {});
import pkg from './package.json';

export default [
{
input: 'src/index.js',
output: { exports: 'named', file: 'dist/index.js', format: 'cjs' },
external: [...builtinModules, ...dependencies]
input: 'src/index.ts',
plugins: [typescript()],
external: ['fs', 'path'],
output: [
{ format: 'cjs', file: pkg.main, exports: 'named' },
{ format: 'esm', file: pkg.module }
]
}
];
61 changes: 0 additions & 61 deletions packages/wasm/src/index.js

This file was deleted.

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
37 changes: 30 additions & 7 deletions pnpm-lock.yaml

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

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

0 comments on commit eb596bf

Please sign in to comment.