Skip to content

Commit a683315

Browse files
authored
feat(wasm): add include/exclude options for wasm plugin. (#1558)
1 parent ee76a4e commit a683315

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

packages/wasm/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma
4646

4747
## Options
4848

49+
### `exclude`
50+
51+
Type: `String` | `Array[...String]`<br>
52+
Default: `null`
53+
54+
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.
55+
56+
### `include`
57+
58+
Type: `String` | `Array[...String]`<br>
59+
Default: `null`
60+
61+
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.
62+
4963
### `sync`
5064

5165
Type: `Array[...String]`<br>

packages/wasm/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
"optional": true
6363
}
6464
},
65+
"dependencies": {
66+
"@rollup/pluginutils": "^5.0.2"
67+
},
6568
"devDependencies": {
6669
"@rollup/plugin-typescript": "^9.0.1",
6770
"del-cli": "^5.0.0",

packages/wasm/src/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from 'path';
33
import { createHash } from 'crypto';
44

55
import type { Plugin } from 'rollup';
6+
import { createFilter } from '@rollup/pluginutils';
67

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

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

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

2325
return {
2426
name: 'wasm',
@@ -36,6 +38,10 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
3638
return getHelpersModule(targetEnv);
3739
}
3840

41+
if (!filter(id)) {
42+
return null;
43+
}
44+
3945
if (!/\.wasm$/.test(id)) {
4046
return null;
4147
}
@@ -74,6 +80,10 @@ export function wasm(options: RollupWasmOptions = {}): Plugin {
7480
},
7581

7682
transform(code, id) {
83+
if (!filter(id)) {
84+
return null;
85+
}
86+
7787
if (code && /\.wasm$/.test(id)) {
7888
const isSync = syncFiles.indexOf(id) !== -1;
7989
const publicFilepath = copies[id] ? `'${copies[id].publicFilepath}'` : null;

packages/wasm/types/index.d.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Plugin } from 'rollup';
2+
import type { FilterPattern } from '@rollup/pluginutils';
23

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

1112
export interface RollupWasmOptions {
13+
/**
14+
* A picomatch pattern, or array of patterns, which specifies the files in the build the plugin
15+
* should _ignore_.
16+
* By default no files are ignored.
17+
*/
18+
exclude?: FilterPattern;
19+
/**
20+
* A picomatch pattern, or array of patterns, which specifies the files in the build the plugin
21+
* should operate on.
22+
* By default all wasm files are targeted.
23+
*/
24+
include?: FilterPattern;
1225
/**
1326
* Specifies an array of strings that each represent a WebAssembly file to load synchronously.
1427
*/
@@ -18,7 +31,7 @@ export interface RollupWasmOptions {
1831
* If `maxFileSize` is set to `0` all files will be copied.
1932
* Files specified in `sync` to load synchronously are always inlined, regardless of size.
2033
*/
21-
maxFileSize?: Number;
34+
maxFileSize?: number;
2235
/**
2336
* String used to rename the emitted Wasm files.
2437
*/

pnpm-lock.yaml

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)