Skip to content

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

@Yehya22

Description

@Yehya22

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions