Skip to content

Commit

Permalink
feat(wasm): add include/exclude options for wasm plugin. (#1558)
Browse files Browse the repository at this point in the history
  • Loading branch information
sky0014 authored Sep 25, 2023
1 parent ee76a4e commit a683315
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma

## Options

### `exclude`

Type: `String` | `Array[...String]`<br>
Default: `null`

A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.

### `include`

Type: `String` | `Array[...String]`<br>
Default: `null`

A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all wasm files are targeted.

### `sync`

Type: `Array[...String]`<br>
Expand Down
3 changes: 3 additions & 0 deletions packages/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
"optional": true
}
},
"dependencies": {
"@rollup/pluginutils": "^5.0.2"
},
"devDependencies": {
"@rollup/plugin-typescript": "^9.0.1",
"del-cli": "^5.0.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/wasm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';
import { createHash } from 'crypto';

import type { Plugin } from 'rollup';
import { createFilter } from '@rollup/pluginutils';

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

Expand All @@ -19,6 +20,7 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {

const syncFiles = sync.map((x) => path.resolve(x));
const copies = Object.create(null);
const filter = createFilter(options.include, options.exclude);

return {
name: 'wasm',
Expand All @@ -36,6 +38,10 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
return getHelpersModule(targetEnv);
}

if (!filter(id)) {
return null;
}

if (!/\.wasm$/.test(id)) {
return null;
}
Expand Down Expand Up @@ -74,6 +80,10 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
},

transform(code, id) {
if (!filter(id)) {
return null;
}

if (code && /\.wasm$/.test(id)) {
const isSync = syncFiles.indexOf(id) !== -1;
const publicFilepath = copies[id] ? `'${copies[id].publicFilepath}'` : null;
Expand Down
15 changes: 14 additions & 1 deletion packages/wasm/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Plugin } from 'rollup';
import type { FilterPattern } from '@rollup/pluginutils';

/**
* - `"auto"` will determine the environment at runtime and invoke the correct methods accordingly
Expand All @@ -9,6 +10,18 @@ import type { Plugin } from 'rollup';
export type TargetEnv = 'auto' | 'auto-inline' | 'browser' | 'node';

export interface RollupWasmOptions {
/**
* A picomatch pattern, or array of patterns, which specifies the files in the build the plugin
* should _ignore_.
* By default no files are ignored.
*/
exclude?: FilterPattern;
/**
* A picomatch pattern, or array of patterns, which specifies the files in the build the plugin
* should operate on.
* By default all wasm files are targeted.
*/
include?: FilterPattern;
/**
* Specifies an array of strings that each represent a WebAssembly file to load synchronously.
*/
Expand All @@ -18,7 +31,7 @@ export interface RollupWasmOptions {
* If `maxFileSize` is set to `0` all files will be copied.
* Files specified in `sync` to load synchronously are always inlined, regardless of size.
*/
maxFileSize?: Number;
maxFileSize?: number;
/**
* String used to rename the emitted Wasm files.
*/
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit a683315

Please sign in to comment.