rollup-plugin-rust icon indicating copy to clipboard operation
rollup-plugin-rust copied to clipboard

Bundled `.js` accesses `.wasm` at the wrong URL

Open shniubobo opened this issue 9 months ago • 1 comments

I am using vite v6.3.2 with this plugin at v3.0.4.

When in dev mode, the wasm file can be correctly loaded. However, in the bundled js file, an additional "assets" segment is added to the URL. For example, foo.wasm is placed in assets/foo.wasm and should be accessed at that address, but the bundled js file tries to access it at assets/assets/foo.wasm instead.

I have tried the workaround in #43, but the serverPath option is deprecated and has no effect.

This is my vite.config.ts:

import { fileURLToPath, URL } from "node:url";

import tailwindcss from "@tailwindcss/vite";
import vue from "@vitejs/plugin-vue";
// @ts-expect-error No `.d.ts` available
import rust from "@wasm-tool/rollup-plugin-rust";
import { defineConfig } from "vite";
import vueDevTools from "vite-plugin-vue-devtools";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    rust({
      experimental: {
        typescriptDeclarationDir: "../target/rollup-plugin-rust/",
      },
    }),
    tailwindcss(),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  esbuild: {
    supported: {
      "top-level-await": true,
    },
  },
});

shniubobo avatar Apr 25 '25 01:04 shniubobo

Using this patch as a workaround:

--- a/src/index.js
+++ b/src/index.js
@@ -781,7 +781,7 @@ export default function rust(options = {}) {
 
         resolveFileUrl(info) {
             if (state.fileIds.has(info.referenceId)) {
-                return `new URL(${JSON.stringify(info.fileName)}, import.meta.url)`;
+                return `new URL(${JSON.stringify(info.fileName).replace("assets/", "")}, import.meta.url)`;
 
             } else {
                 return null;

shniubobo avatar Apr 25 '25 01:04 shniubobo

I run into the same issue using svelte kit with vite, for me the following patch worked. I'm not sure whether it also works with vue.

--- a/node_modules/@wasm-tool/rollup-plugin-rust/src/index.js
+++ b/node_modules/@wasm-tool/rollup-plugin-rust/src/index.js
@@ -794,7 +794,7 @@ export default function rust(options = {}) {

         resolveFileUrl(info) {
             if (state.fileIds.has(info.referenceId)) {
-                return `new URL(${JSON.stringify(info.fileName)}, import.meta.url)`;
+                return `new URL(${JSON.stringify(info.relativePath)}, import.meta.url)`;

             } else {
                 return null;

ginnyTheCat avatar Sep 19 '25 23:09 ginnyTheCat