dai-shi / react-hooks-worker

React custom hooks for web workers
MIT License
709 stars 17 forks source link

Doesn't work with Vite #53

Closed grumd closed 2 years ago

grumd commented 2 years ago

Hi, Daishi! Big fan.

Tried using react-hooks-worker in Vite, and both the result and error are simply undefined. Maybe I'm doing something wrong, or it's just not compatible?

Sandbox: https://codesandbox.io/s/react-vite-forked-w7ftgt?file=/src/App.jsx

dai-shi commented 2 years ago

It shows an error:

Uncaught SyntaxError: Cannot use import statement outside a module

https://vitejs.dev/guide/features.html#import-with-constructors

It looks like we need to specify module type:

const createWorker = () => new Worker(new URL("./fib.worker.js", import.meta.url), {                                                       
  type: 'module'
});

This fixes it, but in DEV, it doesn't seem to work correctly. If you vite build, it works well. I think it's vite issue.