diff --git a/e2e/src/test-wasm.ts b/e2e/src/test-wasm.ts index 2994fdf..96f9cbe 100644 --- a/e2e/src/test-wasm.ts +++ b/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 "; const TEST_LANG = "cpp"; @@ -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`); +} diff --git a/package.json b/package.json index 7632cb3..294f986 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,9 @@ "vite": "3", "vite-plugin-top-level-await": "^1.1.0" }, - "dependencies": {}, + "dependencies": { + "@jsona/openapi": "^0.2.5" + }, "peerDependencies": { "vite": "^3" }, diff --git a/src/index.ts b/src/index.ts index 0d71db2..d02ec7b 100644 --- a/src/index.ts +++ b/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", @@ -18,6 +20,7 @@ export default function wasm(): Plugin { } }, async load(id) { + moduleIds.push(id); if (id === wasmHelper.id) { return `export default ${wasmHelper.code}`; } @@ -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")} @@ -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`; +} diff --git a/yarn.lock b/yarn.lock index 0546c7c..b155d9b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"