vitejs / vitejs/vite

Document not defined in worker import

Open
#17,570 9 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feat: css feat: web workers p2-nice-to-have
Dominant language
TypeScript
Stars
82.9k
Forks
8.8k
Avg merge
1d 23h
Merged PRs (30d)
61

Description

Describe the bug

While running in dev mode and importing a worker that has dependency on a css file. The worker fails to start with document not defined

image

I have created a minimal reproduction of the issue on stackblitz

worker.ts

import styles from './worker.module.css';

self.onmessage = () => {
  console.log('hello world', styles.container);
};

imported in
App.tsx

import * as React from 'react';
import reactLogo from './assets/react.svg';
import viteLogo from '/vite.svg';
import './App.css';
import worker from './worker.ts?worker';

function App() {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    const w = new worker();
    w.postMessage('hey');
  }, []);
  return (
    <>
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src={viteLogo} className="logo" alt="Vite logo" />
        </a>
        <a href="https://react.dev" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
        <p>
          Edit <code>src/App.tsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </>
  );
}

export default App;
Reproduction

https://stackblitz.com/edit/vitejs-vite-t1i3lx

Steps to reproduce

Run npm install then run npm run start

Expected:
console log of Hello world hash
Actual:
Console throws errors and webWorker fails to start.

System Info
System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react: ^4.3.1 => 4.3.1 
    vite: ^5.3.1 => 5.3.1
Used Package Manager

npm

Logs

No response

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked StackBlitz reproduction and the worker.ts and App.tsx examples, then trace the dev-mode import path for a worker that depends on worker.module.css. The work is done when the worker starts without a document error and logs the expected message with the generated style value.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, react, typescript
Domain
build-system, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.