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

Document not defined in worker import #17570

Open
7 tasks done
ldavidpace opened this issue Jun 26, 2024 · 5 comments · May be fixed by #18202
Open
7 tasks done

Document not defined in worker import #17570

ldavidpace opened this issue Jun 26, 2024 · 5 comments · May be fixed by #18202
Labels
feat: css feat: web workers p2-nice-to-have Not breaking anything but nice to have (priority)

Comments

@ldavidpace
Copy link

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

Copy link

stackblitz bot commented Jun 26, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@ldavidpace
Copy link
Author

This minimal reproduction included React and that is not necessary. https://stackblitz.com/edit/vitejs-vite-lblszh

@siefkenj
Copy link

siefkenj commented Jul 5, 2024

I am running into a similar issue after upgrading to vite 5. It could be a similar error to one that was fixed in vite 4 #12611

@sapphi-red
Copy link
Member

Importing CSS files without ?url or ?raw in workers doesn't make sense as it's not possible to modify/inject CSS from workers directly. What is the expected behavior?

@siefkenj
Copy link

Importing CSS files without ?url or ?raw in workers doesn't make sense as it's not possible to modify/inject CSS from workers directly. What is the expected behavior?

I would expect the CSS to be omitted with a warning during build time.

@bluwy bluwy added feat: css p2-nice-to-have Not breaking anything but nice to have (priority) feat: web workers and removed pending triage labels Sep 1, 2024
schontz added a commit to schontz/vite that referenced this issue Sep 26, 2024
Only run DOM operations in the client when there is a document (vitejs#17570)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: css feat: web workers p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants