generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
wasmPlugin.mjs
38 lines (33 loc) · 1 KB
/
wasmPlugin.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import * as fs from "fs/promises";
import * as path from "path";
const wasmPlugin = {
name: "wasm",
setup(build) {
build.onResolve({ filter: /\.wasm$/ }, (args) => {
if (args.namespace === "wasm-stub") {
return {
path: args.path,
namespace: "wasm-binary",
};
}
if (args.resolveDir === "") {
return;
}
return {
path: path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path),
namespace: "wasm-stub",
};
});
build.onLoad({ filter: /.*/, namespace: "wasm-stub" }, async (args) => ({
contents: `import wasm from ${JSON.stringify(args.path)}
export default (imports) =>
WebAssembly.instantiate(wasm, imports).then(
result => result.instance.exports)`,
}));
build.onLoad({ filter: /.*/, namespace: "wasm-binary" }, async (args) => ({
contents: await fs.readFile(args.path),
loader: "binary",
}));
},
};
export default wasmPlugin;