vite icon indicating copy to clipboard operation
vite copied to clipboard

`@vitejs/plugin-legacy` does not play nicely with web workers

Open walkerburgin opened this issue 7 months ago • 0 comments

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

walkerburgin avatar Jul 12 '24 00:07 walkerburgin