diff --git a/src/helper.ts b/src/helper.ts index b50fc18..b49e337 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -17,7 +17,7 @@ buf = Buffer.from(src, 'base64') ` const browserFilePath = ` -return instantiate(fetch(filepath), imports, true); +return instantiate(fetch(new URL(filepath, import.meta.url)), imports, true); ` const browserDecode = ` diff --git a/tests/__snapshots__/init/browser.snap b/tests/__snapshots__/init/browser.snap index 1ffe116..26b53a4 100644 --- a/tests/__snapshots__/init/browser.snap +++ b/tests/__snapshots__/init/browser.snap @@ -9,7 +9,7 @@ function loadWasmModule(sync, filepath, src, imports) { } let buf = null; if (filepath) { - return instantiate(fetch(filepath), imports, true); + return instantiate(fetch(new URL(filepath, import.meta.url)), imports, true); } const raw = globalThis.atob(src); const len = raw.length; diff --git a/tests/__snapshots__/init/neutral.snap b/tests/__snapshots__/init/neutral.snap index 7756a2e..d1062d2 100644 --- a/tests/__snapshots__/init/neutral.snap +++ b/tests/__snapshots__/init/neutral.snap @@ -14,7 +14,7 @@ function loadWasmModule(sync, filepath, src, imports) { const path = process.getBuiltinModule("path"); return readFile(path.resolve(import.meta.dirname, filepath)).then((buffer) => instantiate(buffer, imports)); } else if (filepath) { - return instantiate(fetch(filepath), imports, true); + return instantiate(fetch(new URL(filepath, import.meta.url)), imports, true); } if (isNode) { const { Buffer } = process.getBuiltinModule("buffer"); diff --git a/tests/e2e.test.ts b/tests/e2e.test.ts index e73c510..b63d861 100644 --- a/tests/e2e.test.ts +++ b/tests/e2e.test.ts @@ -4,6 +4,7 @@ import { Buffer } from 'node:buffer' import { readFile } from 'node:fs/promises' import path from 'node:path' import process from 'node:process' +import { pathToFileURL } from 'node:url' import { createContext, SourceTextModule } from 'node:vm' import { rolldownBuild, testFixtures } from '@sxzz/test-utils' import { describe, expect, test } from 'vitest' @@ -63,6 +64,7 @@ async function e2e( const code = chunks[0].code const mod = new SourceTextModule(code, { context: createContext({ + URL, atob: platform === 'browser' ? atob : undefined, fetch: platform === 'browser' && maxFileSize === 0 @@ -90,6 +92,7 @@ async function e2e( }), initializeImportMeta: (meta) => { meta.dirname = process.cwd() + meta.url = pathToFileURL(`${process.cwd()}/`).href }, }) await mod.link((spec) => {