Skip to content

WASM+Vite via SvelteKit: TypeError: Failed to construct 'URL': Invalid URL on client #5075

@stanf0rd

Description

@stanf0rd

Describe the bug

Hi! I am trying to setup Vite with WASM via SvelteKit and rollup-plugin-assemblyscript. My wasm-module is ignored on SSR and loads only on client:

onMount(async () => {
  // this runs only on client
  const wasmUrl = await import("asc:@/core/add.as");
  WebAssembly.instantiateStreaming(fetch(wasmUrl)).then(({ instance }) =>
    console.log(instance.exports.add(40, 2))
  );
});

After successful build I receive such error on client:

TypeError: Failed to construct 'URL': Invalid URL
    at add-ff7f1bae.js:1

Inside generated add-*.js file:

const e = new URL("../assets/add-5e595341.wasm",{}.url).href;
export {e as default, e as wasmUrl};

Looks like missing import.meta.url, but I'm not sure.

I opened a similar issue on SvelteKit, and received a recommendation to refer here.

Reproduction

You can reproduce described error here:
https://github.com/stanf0rd/rasseki-game/tree/wasm-setup

Just

npm install
npm run build
npm preview

and look into browser console on localhost:3000

System Info

System:
    OS: macOS 11.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 35.07 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.9.1 - /usr/local/bin/node
    npm: 7.21.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 93.0.4577.82
    Safari: 15.0
  npmPackages:
    @sveltejs/adapter-node: ^1.0.0-next.46 => 1.0.0-next.49 
    @sveltejs/kit: next => 1.0.0-next.171 
    svelte: ^3.34.0 => 3.43.0

# vite is not in list because it is dependency of @sveltejs/kit
    vite: ^2.5.7 => 2.5.10

Used Package Manager

npm

Logs

❯ npx svelte-kit build --verbose
vite v2.5.10 building for production...
✓ 18 modules transformed.
.svelte-kit/output/client/_app/assets/add-5e595341.wasm                    0.09 KiB
.svelte-kit/output/client/_app/manifest.json                               1.73 KiB
.svelte-kit/output/client/_app/pages/__layout.svelte-b6ef297e.js           0.54 KiB / brotli: 0.31 KiB
.svelte-kit/output/client/_app/error.svelte-641ac797.js                    1.56 KiB / brotli: 0.64 KiB
.svelte-kit/output/client/_app/pages/index.svelte-893d5051.js              1.64 KiB / brotli: 0.71 KiB
.svelte-kit/output/client/_app/chunks/add-ff7f1bae.js                      0.09 KiB / brotli: 0.09 KiB
.svelte-kit/output/client/_app/chunks/preload-helper-ec9aa979.js           0.50 KiB / brotli: 0.28 KiB
.svelte-kit/output/client/_app/assets/pages/__layout.svelte-dd72df29.css   0.16 KiB / brotli: 0.10 KiB
.svelte-kit/output/client/_app/assets/start-61d1577b.css                   0.16 KiB / brotli: 0.11 KiB
.svelte-kit/output/client/_app/assets/pages/index.svelte-1ecc56f5.css      0.96 KiB / brotli: 0.30 KiB
.svelte-kit/output/client/_app/chunks/vendor-af0f2ef7.js                   6.98 KiB / brotli: 2.60 KiB
.svelte-kit/output/client/_app/start-6f9adcce.js                           17.30 KiB / brotli: 5.51 KiB
vite v2.5.10 building SSR bundle for production...
✓ 18 modules transformed.
.svelte-kit/output/server/app.js   58.62 KiB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-node
  Copying assets
  Compressing assets
  Building SvelteKit middleware
  Building SvelteKit server
  Prerendering static pages
  ✔ done

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions