vite
vite copied to clipboard
`@vitejs/plugin-legacy` does not play nicely with web workers
Describe the bug
Applying the @vitejs/plugin-legacy
plugin causes runtime failures in web workers that reference import.meta.env
ReferenceError: Can't find variable: __VITE_IS_LEGACY__
It looks like the legacy env var marker is being defined in workers, but never populated because renderChunk()
is not called for worker output.
Reproduction
https://github.com/walkerburgin/vite-plugin-legacy-worker-repro
Steps to reproduce
- Clone https://github.com/walkerburgin/vite-plugin-legacy-worker-repro
- Run
pnpm install
to install dependencies - Run
pnpm build && pnpm preview
- Observe the runtime error when previewing the app in the browser:
ReferenceError: Can't find variable: __VITE_IS_LEGACY__
Alternately:
- Clone https://github.com/walkerburgin/vite-plugin-legacy-worker-repro
- Run
pnpm install
to install dependencies - Run
pnpm build
- Observe that the worker output file (e.g.
dist/assets/worker-NmsWRLpo.js
) contains a reference to__VITE_IS_LEGACY__
:(function() { "use strict"; console.log("[worker] import.meta.env", { BASE_URL: "/", MODE: "production", DEV: !1, PROD: !0, SSR: !1, LEGACY: __VITE_IS_LEGACY__ }) })();
System Info
System:
OS: macOS 14.5
CPU: (12) arm64 Apple M3 Pro
Memory: 82.25 MB / 18.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.5.1 - ~/.nvm/versions/node/v20.5.1/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v20.5.1/bin/yarn
npm: 9.8.0 - ~/.nvm/versions/node/v20.5.1/bin/npm
pnpm: 8.15.5 - ~/.nvm/versions/node/v20.5.1/bin/pnpm
Browsers:
Chrome: 126.0.6478.127
Safari: 17.5
npmPackages:
@vitejs/plugin-legacy: ^5.4.1 => 5.4.1
vite: ^5.3.3 => 5.3.3
Used Package Manager
pnpm
Logs
No response
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.