Skip to content

Commit 53cd883

Browse files
committed
add other files (2AM commit haha)
1 parent 423d6ce commit 53cd883

File tree

6 files changed

+60
-15
lines changed

6 files changed

+60
-15
lines changed

src/plugin/runtime.ts

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readPackageJSON } from "pkg-types";
12
import {
23
UMWASM_HELPERS_ID,
34
UNWASM_EXTERNAL_PREFIX,
@@ -8,19 +9,33 @@ import {
89
// https://marketplace.visualstudio.com/items?itemName=Tobermory.es6-string-html
910
const js = String.raw;
1011

11-
export function getWasmBinding(asset: WasmAsset, opts: UnwasmPluginOptions) {
12+
export async function getWasmBinding(
13+
asset: WasmAsset,
14+
opts: UnwasmPluginOptions,
15+
) {
16+
// -- Auto load imports --
17+
const autoImports = await getWasmImports(asset, opts);
18+
1219
// --- Environment dependent code to initialize the wasm module using inlined base 64 or dynamic import ---
1320
const envCode: string = opts.esmImport
1421
? js`
15-
async function _instantiate(imports) {
22+
${autoImports};
23+
24+
async function _instantiate(imports = _imports) {
1625
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
17-
return WebAssembly.instantiate(_mod, imports)
26+
try {
27+
return await WebAssembly.instantiate(_mod, imports)
28+
} catch (error) {
29+
console.error('[wasm] [error]', error);
30+
throw error;
31+
}
1832
}
1933
`
2034
: js`
2135
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
36+
${autoImports};
2237
23-
function _instantiate(imports) {
38+
function _instantiate(imports = _imports) {
2439
const _data = base64ToUint8Array("${asset.source.toString("base64")}")
2540
return WebAssembly.instantiate(_data, imports)
2641
}
@@ -136,3 +151,38 @@ export function createLazyWasmModule(_instantiator) {
136151
}
137152
`;
138153
}
154+
155+
export async function getWasmImports(
156+
asset: WasmAsset,
157+
opts: UnwasmPluginOptions,
158+
) {
159+
const importNames = Object.keys(asset.imports || {});
160+
if (importNames.length === 0) {
161+
return "const _imports = { /* no imports */ }";
162+
}
163+
// Try to resolve from nearest package.json
164+
const pkgJSON = await readPackageJSON(asset.id);
165+
166+
let code = "const _imports = {";
167+
168+
for (const moduleName of importNames) {
169+
const importNames = asset.imports[moduleName];
170+
const pkgImport =
171+
pkgJSON.imports?.[moduleName] || pkgJSON.imports?.[`#${moduleName}`];
172+
173+
if (pkgImport) {
174+
code = `import * as _imports_${moduleName} from "${pkgImport}";\n${code}`;
175+
}
176+
code += `\n ${moduleName}: {`;
177+
for (const name of importNames) {
178+
code += pkgImport
179+
? `\n ${name}: _imports_${moduleName}.${name},\n`
180+
: `\n ${name}: () => { throw new Error("\`${moduleName}.${name}\` is not provided!")},\n`;
181+
}
182+
code += " },\n";
183+
}
184+
185+
code += "};\n";
186+
187+
return code;
188+
}

test/fixture/_shared.mjs

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

test/fixture/dynamic-import.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const { sum } = await import("@fixture/wasm/sum.wasm");
2-
3-
const { imports } = await import("./_shared.mjs");
42
const { rand } = await import("@fixture/wasm/rand.wasm").then((r) =>
5-
r.default(imports),
3+
r.default(),
64
);
75

86
export function test() {

test/fixture/static-import.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { imports } from "./_shared.mjs";
21
import { sum } from "@fixture/wasm/sum.wasm";
32
import initRand, { rand } from "@fixture/wasm/rand.wasm";
43

5-
await initRand(imports);
4+
await initRand();
65

76
export function test() {
87
if (sum(1, 2) !== 3) {

test/node_modules/@fixture/wasm/env.mjs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/@fixture/wasm/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)