Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wasm functions return undefined #11

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/index.ts
Expand Up @@ -6,13 +6,15 @@ import * as wasmHelper from "./wasm-helper";
export default function wasm(): Plugin {
let resolvedConfig: ResolvedConfig;
let originalWasmPlugin: Plugin;
let resolvePlugin: Plugin;

return {
name: "vite-plugin-wasm",
enforce: "pre",
configResolved(config) {
resolvedConfig = config;
originalWasmPlugin = resolvedConfig.plugins.find(plugin => plugin.name === "vite:wasm-helper");
resolvePlugin = resolvedConfig.plugins.find(plugin => plugin.name === "vite:resolve");
},
resolveId(id) {
if (id === wasmHelper.id) {
Expand All @@ -33,14 +35,20 @@ export default function wasm(): Plugin {
// Make a call to Vite's internal `fileToUrl` function by calling Vite's original WASM plugin's load()
const originalLoadResult = (await originalWasmPlugin.load.call(this, id + "?init")) as string;
const url = JSON.parse(/".+"/g.exec(originalLoadResult.trim().split("\n")[1])[0]) as string;
const importUrls = await Promise.all(
imports.map(async ({ from }) => {
const importerPath = id.slice(0, id.lastIndexOf("/")) + from.slice(from.lastIndexOf("/"));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand this path concatenation for importerPath, what would happen if the WASM file imports a JS file that are not in the same directory? e.g. when from is ../../../a/b/c/xxx.js.

Copy link
Author

@sigoden sigoden Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just replace file name:

id: 'C:/t/test-wasm2/node_modules/@jsona/openapi/index_bg.wasm'
from: "./index_bg.js"
importerPath: "C:/t/test-wasm2/node_modules/@jsona/openapi/index_bg.js"

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if

id: 'C:/t/test-wasm2/node_modules/@jsona/openapi/a/b/c/d/e/index_bg.wasm'
from: "../../f/g/index_bg.js"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The index_bg.wasm always point to index_bg.js. in same directory.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only appies to wasm-bindgen, you can't assume all WASM files are generated by wasm-bindgen. I don't see in the WASM ESM spec it has any restrictions on the imported files' directory.

return resolvePlugin.resolveId.call(this, importerPath, null, null);
})
);

return `
import __vite__initWasm from "${wasmHelper.id}"
${imports
.map(
({ from, names }, i) =>
({ names }, i) =>
`import { ${names.map((name, j) => `${name} as __vite__wasmImport_${i}_${j}`).join(", ")} } from ${JSON.stringify(
from
importUrls[i]
)};`
)
.join("\n")}
Expand Down