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
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions e2e/src/test-wasm.ts
@@ -1,5 +1,6 @@
// @syntect/wasm requires WASM to ES module transform
import { highlight } from "@syntect/wasm";
import { parse } from "@jsona/openapi";

const TEST_CODE = "#include <cstdio>";
const TEST_LANG = "cpp";
Expand All @@ -10,3 +11,8 @@ const result = div.innerText.trim();
if (result !== TEST_CODE) {
console.error(`Expected ${JSON.stringify(TEST_CODE)} but got ${JSON.stringify(result)}.`);
}

// https://github.com/Menci/vite-plugin-wasm/pull/11
if (parse("{}") == null) {
console.error(`@jsona/openapi returned null`);
}
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -44,7 +44,9 @@
"vite": "3",
"vite-plugin-top-level-await": "^1.1.0"
},
"dependencies": {},
"dependencies": {
"@jsona/openapi": "^0.2.5"
},
"peerDependencies": {
"vite": "^3"
},
Expand Down
34 changes: 32 additions & 2 deletions src/index.ts
@@ -1,10 +1,12 @@
import type { Plugin, ResolvedConfig } from "vite";
import path from "path";

import { parseWasm } from "./parse-wasm";
import * as wasmHelper from "./wasm-helper";

export default function wasm(): Plugin {
let resolvedConfig: ResolvedConfig;
let moduleIds: string[] = [];

return {
name: "vite-plugin-wasm",
Expand All @@ -18,6 +20,7 @@ export default function wasm(): Plugin {
}
},
async load(id) {
moduleIds.push(id);
if (id === wasmHelper.id) {
return `export default ${wasmHelper.code}`;
}
Expand All @@ -30,15 +33,16 @@ export default function wasm(): Plugin {

// Get WASM's download URL by Vite's ?url import
const wasmUrlUrl = id + "?url";
const importUrls = await Promise.all(imports.map(async ({ from }) => getImportUrl(id, from, moduleIds)));

return `
import __vite__wasmUrl from ${JSON.stringify(wasmUrlUrl)};
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 All @@ -55,3 +59,29 @@ ${exports
}
};
}

async function getImportUrl(id: string, from: string, moduleIds: string[]) {
const importerPath = path.resolve(id, "../" + from);
if (importerPath.indexOf("node_modules") === -1) {
// Local js won't have versionHash, so the importerPath is importerId
return importerPath;
}
// The module may be pre-bundled, pre-bundling js file has higher priority than original file.
const preBundlingPath = getPreBundlePath(importerPath);
return (
moduleIds.find(v => v.startsWith(preBundlingPath)) ||
moduleIds.find(v => v.startsWith(importerPath)) ||
importerPath
);
}

function getPreBundlePath(importerPath: string) {
const [prefix, modulePath] = importerPath.split("node_modules/");
const modulePathParts = modulePath.split("/");
let pkgName = modulePathParts[0];
if (pkgName.startsWith("@")) {
// Scope Package
pkgName = `${modulePathParts[0]}_${modulePathParts[1]}`;
}
return `${prefix}node_modules/.vite/deps/${pkgName}.js`;
}
12 changes: 12 additions & 0 deletions yarn.lock
Expand Up @@ -814,6 +814,13 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@jsona/openapi@^0.2.5":
version "0.2.5"
resolved "https://registry.npmjs.org/@jsona/openapi/-/openapi-0.2.5.tgz#346d74d4f92460cb264feb90a5fdb797d9fd2bf2"
integrity sha512-bIw08OAEaMbNHWyDgW6fwiJNnnxcflrKKWGcf9csgphHBArpkBKWRZoAtDt/5bXgAXmyYmaVUYsoUVPGme3KtQ==
dependencies:
openapi-types "^12.0.2"

"@sinclair/typebox@^0.24.1":
version "0.24.19"
resolved "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.24.19.tgz#5297278e0d8a1aea084685a3216074910ac6c113"
Expand Down Expand Up @@ -3055,6 +3062,11 @@ onetime@^5.1.2:
dependencies:
mimic-fn "^2.1.0"

openapi-types@^12.0.2:
version "12.0.2"
resolved "https://registry.npmjs.org/openapi-types/-/openapi-types-12.0.2.tgz#b752ab0a463c594fd7e3142e0e5983a9645503f6"
integrity sha512-GuTo7FyZjOIWVhIhQSWJVaws6A82sWIGyQogxxYBYKZ0NBdyP2CYSIgOwFfSB+UVoPExk/YzFpyYitHS8KVZtA==

os-tmpdir@~1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"
Expand Down