Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker with import and globally defined window variable causes an error #19307

Open
7 tasks done
Yehya22 opened this issue Jan 28, 2025 · 0 comments
Open
7 tasks done

Comments

@Yehya22
Copy link

Yehya22 commented Jan 28, 2025

Describe the bug

I have encountered a bug in Vite when using a worker that calls import, even if the import isn't directly called, and there is a globally defined window variable.

Expected Behavior

The application should run without any errors.

Actual Behavior

An error occurs when the worker is instantiated, even though the import function is not directly called.

Steps to Reproduce

  1. Create a new Svelte app using Vite:

    pnpm create vite@latest my-app -- --template svelte
    cd my-app
    pnpm install
  2. Create a worker file (worker.js) that defines a function calling import:

    const foo = () => import(url)
  3. In the main application file (App.svelte), import the worker and create a new instance of it:

    <script>
    import Worker from './worker.js?worker&inline'
    new Worker
    </script>
  4. In the Vite configuration file (vite.config.js), define a global window variable:

    export default defineConfig({
      plugins: [svelte()],
      define: {
        'window.__DEBUG__': true,
      },
    })
  5. Run the application using pnpm run dev and observe the error.

Reproduction

You can find a minimal reproduction of the bug in the following repository:
https://github.com/mustafa0x/vite-worker-bug

You can also find a live reproduction on StackBlitz:
https://stackblitz.com/edit/vitejs-vite-zyhbeh

System Info

Vite version: 6.0.11
@sveltejs/vite-plugin-svelte version: 5.0.3
Svelte version: 5.19.3
Browser: Google Chrome (latest version)
Operating System: macOS

Used Package Manager

pnpm

Logs

No response

Validations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant