cubing / cubing.js

🛠 A library for displaying and working with twisty puzzles. Also currently home to the code for Twizzle.
https://js.cubing.net/cubing/
GNU General Public License v3.0
232 stars 42 forks source link

Svelte / SvelteKit Module Worker Errors #327

Open LiamKrenn opened 3 months ago

LiamKrenn commented 3 months ago

Steps to reproduce the issue

  1. Create brand new Svelte or SvelteKit project
  2. Install cubing npm package
  3. Basic code for scramble generation:

    <script lang="ts">
    import { randomScrambleForEvent } from "cubing/scramble";
    import { onMount } from "svelte";
    
    onMount(async () => {
        console.log(await randomScrambleForEvent("333"))
    });
    </script>

Observed behaviour

Server side error:

The file does not exist at "C:/Users/Liam/Documents/Coden/ScrambleGen/node_modules/.vite/deps/search-worker-entry.js" which is in the optimize deps directory. The dependency might be incompatible with the dep optimizer. Try adding it to `optimizeDeps.exclude`.
The file does not exist at "C:/Users/Liam/Documents/Coden/ScrambleGen/node_modules/.vite/deps/search-worker-entry.js?worker_file&type=module" which is in the optimize deps directory. The dependency might be incompatible with the dep optimizer. Try adding it to `optimizeDeps.exclude`.
The file does not exist at "C:/Users/Liam/Documents/Coden/ScrambleGen/node_modules/.vite/deps/search-worker-entry.js" which is in the optimize deps directory. The dependency might be incompatible with the dep optimizer. Try adding it to `optimizeDeps.exclude`.

Client side error:

Module worker instantiation using `import.meta.resolve(…) failed, falling back.
Module worker instantiation using inline `new URL(…, import.meta.url)` failed, falling back.
Module worker instantiation using `new URL(…, import.meta.url)` failed, falling back.
Module worker instantiation using the `esbuild` workaround failed, falling back.
Uncaught ReferenceError: document is not defined
Uncaught (in promise) Error: Module worker instantiation failed. There are no more fallbacks available. If you are using Firefox, please update to version 114 or later.

🖼 Screenshots

Error log browser console

Expected behaviour

Scramble logged - no error, since #323 mentions svelte compatibility.

Environment

Using Chrome (since error log mentions firefox)

Additional info

Tried with brand new svelte and sveltekit projects, and after some researching also tried adding following into the vite config:

worker: {
    format: 'es'
}

and

build: { target: 'es2020' },
optimizeDeps: { esbuildOptions: { target: "es2020" } }

also tried vitejs/vite#14499 (comment)

lgarron commented 1 month ago

Apologies for not replying earlier. I spent quite some time looking at this trying to get you a good answer and kind of failed. I think SvelteKit is just broken for our web worker code, despite the extreme time I have invested in making it as compatible as possible. It's probably because they're using item.

I'll comment with more details when I have time to look into it again.