Menci/vite-plugin-wasm

rollup fails with `id.endsWith is not a function`

diamondburned opened this issue · 2 comments

I'm trying to vite build with an import to v86-module's v86.wasm blob. However, this seems to cause rollup to crash:

vite build --outDir $PROJECT_ROOT/build/site
vite v4.1.4 building for production...
✓ 34 modules transformed.
9:05:50 PM [vite-plugin-svelte] dom compile done.
package 	files	  time	   avg
libdb.so	    2	97.1ms	48.5ms
[commonjs--resolver] id.endsWith is not a function
error during build:
TypeError: id.endsWith is not a function
    at isWrappedId (file:///home/diamond/Scripts/libdb.so/node_modules/vite/dist/node/chunks/dep-ca21228b.js:7713:40)
    at Object.resolveId (file:///home/diamond/Scripts/libdb.so/node_modules/vite/dist/node/chunks/dep-ca21228b.js:7922:11)
    at file:///home/diamond/Scripts/libdb.so/node_modules/rollup/dist/es/shared/node-entry.js:24343:40
    at async PluginDriver.hookFirstAndGetPlugin (file:///home/diamond/Scripts/libdb.so/node_modules/rollup/dist/es/shared/node-entry.js:24243:28)
    at async resolveId (file:///home/diamond/Scripts/libdb.so/node_modules/rollup/dist/es/shared/node-entry.js:23187:26)
    at async ModuleLoader.loadEntryModule (file:///home/diamond/Scripts/libdb.so/node_modules/rollup/dist/es/shared/node-entry.js:23796:33)
    at async Promise.all (index 1)
    at async Promise.all (index 0)
make: *** [Makefile:20: build/site] Error 1

Here are a few relevant files:

vite.config.js
import { defineConfig, loadEnv } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import wasm from "vite-plugin-wasm";
import type * as vite from "vite";
import * as path from "path";
import sveltePreprocess from "svelte-preprocess";

const root = path.resolve(__dirname);

export default defineConfig({
  plugins: [
    svelte({
      preprocess: sveltePreprocess(),
    }),
    wasm(),
  ],
  root: path.join(root, "site"),
  envPrefix: ["BUILD_"],
  publicDir: path.join(root, "site", "public"),
  server: {
    port: 5000,
  },
  build: {
    emptyOutDir: true,
    rollupOptions: {
      output: {
        format: "esm",
        manualChunks: {
          vm: ["v86"],
          vmmisc: [],
          terminal: ["xterm", /xterm-addon-.*/],
        },
      },
      external: ["node_modules/v86/build/v86.wasm"],
    },
    target: "esnext",
  },
  // https://github.com/vitejs/vite/issues/7385#issuecomment-1286606298
  resolve: {
    alias: {
      "#/libdb.so": root,
    },
  },
});
site/lib/vm.ts (which imports the wasm blob)
const RAMSize = 128 * 1024 * 1024; // 128 MB
const VGASize = 8 * 1024 * 1024; // 8 MB

export async function spawn() {
  // @ts-ignore
  const v86 = await import("v86");
  // @ts-ignore
  const v86wasm = await import("v86/build/v86.wasm");
  const v86bios = await import("v86/bios/seabios.bin?url");

  const vm = v86.V86Starter({
    // TODO: swap this out for a wasm loader
    wasm_fn: v86wasm,
    memory_size: RAMSize,
    vga_memory_size: VGASize,
    autostart: true,
  });
}

The experimental repository is over at diamondburned/libdb.so. Build with either make or vite build.

Sorry, I misconfigured something else in vite.config.js. It was probably the manualChunks.

I ran into the same error when my config structure in rollupOptions was incorrect.