|
| 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