Skip to content

Commit 44fd280

Browse files
committed
refactor: split runtime logic
1 parent adabaa8 commit 44fd280

File tree

5 files changed

+230
-231
lines changed

5 files changed

+230
-231
lines changed

src/plugin/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import MagicString from "magic-string";
44
import type { RenderedChunk, Plugin as RollupPlugin } from "rollup";
55
import { createUnplugin } from "unplugin";
66
import { parseWasm } from "../tools";
7+
import { getWasmESMBinding, getWasmModuleBinding } from "./runtime/binding";
8+
import { getPluginUtils } from "./runtime/utils";
79
import {
810
sha1,
911
UMWASM_HELPERS_ID,
@@ -12,11 +14,6 @@ import {
1214
UnwasmPluginOptions,
1315
WasmAsset,
1416
} from "./shared";
15-
import {
16-
getPluginUtils,
17-
getWasmESMBinding,
18-
getWasmModuleBinding,
19-
} from "./runtime";
2017

2118
export type { UnwasmPluginOptions } from "./shared";
2219

src/plugin/runtime.ts

Lines changed: 0 additions & 226 deletions
This file was deleted.

src/plugin/runtime/binding.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {
2+
UMWASM_HELPERS_ID,
3+
UNWASM_EXTERNAL_PREFIX,
4+
WasmAsset,
5+
UnwasmPluginOptions,
6+
} from "../shared";
7+
import { getWasmImports } from "./imports";
8+
9+
/**
10+
* Returns ESM compatible exports binding
11+
*/
12+
export async function getWasmESMBinding(
13+
asset: WasmAsset,
14+
opts: UnwasmPluginOptions,
15+
) {
16+
// -- Auto load imports --
17+
const autoImports = await getWasmImports(asset, opts);
18+
19+
// --- Environment dependent code to initialize the wasm module using inlined base64 or dynamic import ---
20+
const envCode: string = opts.esmImport
21+
? /* js */ `
22+
${autoImports.code};
23+
24+
async function _instantiate(imports = _imports) {
25+
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
26+
return WebAssembly.instantiate(_mod, imports)
27+
}
28+
`
29+
: /* js */ `
30+
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
31+
${autoImports.code};
32+
33+
function _instantiate(imports = _imports) {
34+
const _data = base64ToUint8Array("${asset.source.toString("base64")}")
35+
return WebAssembly.instantiate(_data, imports)
36+
}
37+
`;
38+
39+
// --- Binding code to export the wasm module exports ---
40+
const canTopAwait = opts.lazy !== true && autoImports.resolved;
41+
42+
// eslint-disable-next-line unicorn/prefer-ternary
43+
if (canTopAwait) {
44+
// -- Non proxied exports when no imports are needed and we can have top-level await ---
45+
return /* js */ `
46+
import { getExports } from "${UMWASM_HELPERS_ID}";
47+
${envCode}
48+
49+
const $exports = getExports(await _instantiate());
50+
51+
${asset.exports
52+
.map((name) => `export const ${name} = $exports.${name};`)
53+
.join("\n")}
54+
55+
const defaultExport = () => $exports;
56+
${asset.exports.map((name) => `defaultExport["${name}"] = $exports.${name};`).join("\n")}
57+
export default defaultExport;
58+
`;
59+
} else {
60+
// --- Proxied exports when imports are needed or we can't have top-level await ---
61+
return /* js */ `
62+
import { createLazyWasmModule } from "${UMWASM_HELPERS_ID}";
63+
${envCode}
64+
65+
const _mod = createLazyWasmModule(_instantiate);
66+
67+
${asset.exports
68+
.map((name) => `export const ${name} = _mod.${name};`)
69+
.join("\n")}
70+
71+
export default _mod;
72+
`;
73+
}
74+
}
75+
76+
/**
77+
* Returns WebAssembly.Module binding for compatibility
78+
*/
79+
export function getWasmModuleBinding(
80+
asset: WasmAsset,
81+
opts: UnwasmPluginOptions,
82+
) {
83+
return opts.esmImport
84+
? /* js */ `
85+
const _mod = ${opts.lazy === true ? "" : `await`} import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
86+
export default _mod;
87+
`
88+
: /* js */ `
89+
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
90+
const _data = base64ToUint8Array("${asset.source.toString("base64")}");
91+
const _mod = new WebAssembly.Module(_data);
92+
export default _mod;
93+
`;
94+
}

0 commit comments

Comments
 (0)