vite build --mode=production rollup syntax error when undefined import.meta.env variable #9369
Closed
7 tasks done
Labels
p2-edge-case
Bug, but has workaround or limited in scope (priority)
Describe the bug
Running
vite build --mode=production
on a TypeScript file that includesimport.meta.env
with an unset variable errors withError: Unexpected token
onimport.meta.env
.I hit this bug attempting to upgrade Vite from 2 to 3.
TypeScript file
Error
Workaround
Ensure that every import.meta.env variable is defined, like so:
Related issues:
Project build failed with "import.meta.env" #3304 This issue deals with
import.meta.env
in a string literal. That's not the case in this bug.Incorrect define replacement in build #8663 Looks like the same root cause but hard to tell. This bug uses the recommended
VITE_
prefix. I was able to work around by definingVITE_FOO=enabled
Support missing import.meta.env storybookjs/builder-vite#413 Looks like the same root cause which is that rollup is doing an inline replacement of
import.meta.env
with an object literal which leads to invalid syntax of{MODE: "production}.VITE_FOO
when starting fromimport.meta.env.VITE_FOO
.Reproduction
https://stackblitz.com/edit/vitejs-vite-rw3kdr?file=src/main.ts
System Info
System: OS: Linux 5.0 undefined CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 16.14.2 - /usr/local/bin/node Yarn: 1.22.10 - /bin/yarn npm: 7.17.0 - /bin/npm npmPackages: vite: ^3.0.2 => 3.0.3
Used Package Manager
npm
Logs
Command:
Log output (click to expand)
Validations
The text was updated successfully, but these errors were encountered: