Skip to content

Commit 279d296

Browse files
committed
Add vite tests
1 parent e85565a commit 279d296

File tree

6 files changed

+457
-235
lines changed

6 files changed

+457
-235
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,5 @@ dist
106106
# Rust:
107107
/target
108108
/pkg
109-
/pkg.*/
109+
/pkg.*/
110+
.idea/

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,64 @@ You'll need a [browser Buffer polyfill](https://www.npmjs.com/package/browserify
5353

5454
If you want to support node & browsers with the same code, you can use the latter `await` form here everywhere (since awaiting the fixed value in node just returns the value as-is).
5555

56+
57+
## Vite and Rollup
58+
59+
For usage in Vite, you have to use the `vite-plugin-wasm` and `static-files` plugins. The config looks like this:
60+
61+
```javascript
62+
import { defineConfig } from "vite";
63+
import wasm from "vite-plugin-wasm";
64+
import { wasm as rollupWasm } from "@rollup/plugin-wasm";
65+
import { viteStaticCopy } from "vite-plugin-static-copy";
66+
67+
// https://vitejs.dev/config/
68+
export default defineConfig({
69+
plugins: [
70+
viteStaticCopy({
71+
targets: [
72+
{
73+
src: require.resolve("brotli-wasm/pkg.web/brotli_wasm_bg.wasm"),
74+
dest: "."
75+
}
76+
]
77+
}),
78+
wasm(),
79+
topLevelAwait()
80+
],
81+
build: {
82+
minify: false,
83+
target: ["esnext"],
84+
sourcemap: true,
85+
rollupOptions: {
86+
treeshake: false,
87+
plugins: [rollupWasm()]
88+
},
89+
ssr: false
90+
}
91+
});
92+
```
93+
94+
and you can call it in your code like this:
95+
96+
```typescript
97+
import init, { decompress } from "brotli-wasm/pkg.web/brotli_wasm";
98+
99+
const initPromise = init("brotli_wasm_bg.wasm");
100+
export const brotliDecompress = zlib.brotliDecompress
101+
? promisify(zlib.brotliDecompress)
102+
: async (buffer: Uint8Array): Promise<Uint8Array | undefined> => {
103+
try {
104+
await initPromise;
105+
const output = decompress(buffer);
106+
return output;
107+
} catch (e) {
108+
console.error(e);
109+
return;
110+
}
111+
};
112+
```
113+
56114
## Alternatives
57115

58116
There's a few other packages that do similar things, but I found they were all unusable and/or unmaintained:

build.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ shell.rm('pkg.bundler/{LICENSE,package.json,README.md,.gitignore}');
1919
shell.rm('-rf', 'pkg');
2020
shell.exec('wasm-pack build --target nodejs');
2121
shell.mv('pkg', 'pkg.node');
22-
shell.rm('pkg.node/{LICENSE,package.json,README.md,.gitignore}');
22+
shell.rm('pkg.node/{LICENSE,package.json,README.md,.gitignore}');
23+
24+
// Create the web output
25+
shell.rm('-rf', 'pkg');
26+
shell.exec('wasm-pack build --target web');
27+
shell.mv('pkg', 'pkg.web');
28+
shell.rm('pkg.web/{LICENSE,package.json,README.md,.gitignore}');

karma.conf.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@ module.exports = function(config) {
3838
resolve: {
3939
extensions: ['.ts', '.js'],
4040
fallback: {
41-
buffer: require.resolve('buffer/')
41+
buffer: require.resolve('buffer/'),
42+
fs: require.resolve('graceful-fs/graceful-fs'),
43+
path: require.resolve('path-browserify/'),
44+
constants: require.resolve('constants-browserify/'),
45+
stream: require.resolve('stream-browserify/'),
4246
}
4347
},
4448
experiments: {
4549
asyncWebAssembly: true
4650
},
4751
plugins: [
4852
new webpack.ProvidePlugin({
49-
Buffer: ['buffer', 'Buffer'],
53+
Buffer: ['buffer', 'Buffer'],process: 'process/browser',
5054
})
5155
],
5256
output: {

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
"files": [
1010
"pkg.node",
1111
"pkg.bundler",
12+
"pkg.web",
1213
"index.browser.js"
1314
],
1415
"scripts": {
1516
"build": "node ./build.js",
1617
"pretest": "npm run build",
1718
"test": "npm run test:node && npm run test:browser",
18-
"test:node": "TS_NODE_FILES=true mocha -r ts-node/register 'test/**/*.spec.ts'",
19+
"test:node": "ts-mocha -r esm -p test/tsconfig.json 'test/**/*.spec.ts'",
1920
"test:browser": "karma start",
2021
"test:browser:debug": "npm run test:browser -- --single-run=false --browsers Chrome"
2122
},
@@ -39,6 +40,7 @@
3940
"@types/chai": "^4.2.18",
4041
"@types/mocha": "^8.2.2",
4142
"@types/node": "^15.6.0",
43+
"@types/text-encoding": "0.0.36",
4244
"buffer": "^6.0.3",
4345
"chai": "^4.3.4",
4446
"karma": "^6.3.2",
@@ -54,7 +56,10 @@
5456
"ts-loader": "^9.2.1",
5557
"ts-node": "^9.1.1",
5658
"typescript": "^4.2.4",
57-
"wasm-pack": "^0.9.1",
58-
"webpack": "^5.37.1"
59+
"wasm-pack": "^0.10.3",
60+
"webpack": "^5.37.1",
61+
"esm": "^3.2.25",
62+
"text-encoding": "^0.7.0",
63+
"ts-mocha": "^10.0.0"
5964
}
6065
}

0 commit comments

Comments
 (0)