javascriptvitetaurisurrealdb

vite error when importing surrealdb.wasm into tauri project


I have problems on running surrealdb.wasm on a tauri project. I have installed the library, but when i try to do import { Surreal } from 'surrealdb.wasm';, vite gives me compiling error in development:

Error: Build failed with 6 errors:
node_modules/surrealdb.wasm/dist/full/index.js:8:13: ERROR: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides)
... and more alike errors ...

I have tried to set build.target and esbuild.target to esnext, but it does not seem to change anything. I have also tried to add the top-level-await plugin which changes all await 's to .then(), but this makes the compiler unable to find the library, for some reason:

Error: Not found: /node_modules/.vite/deps/index_bg.wasmat resolve (C:/my/directory/project_name/node_modules/@sveltejs/kit/src/runtime/server/respond.js:480:13)

Solution

  • I cannot remember where i found the answer, but it works to add surrealdb.wasm to optimizeDeps.exclude:

    export default defineConfig({
        plugins: [ sveltekit() ],
        optimizeDeps: {
            exclude: ['surrealdb.wasm'],
        }
    });