Open shellscape opened 9 months ago
Start a new pull request in StackBlitz Codeflow.
This appears to be a conflict/race between define
and when vite's internals replace import.meta.env
with the JSON object with VITE_*
keys.
You can inject the properties in process.env
into import.meta.env
using the following method.
// main.ts
const { DEV } = import.meta.env;
// vite.config.ts
export default defineConfig({
define:
Object
.keys(process.env)
.reduce(
(pre, cur) => (pre[`import.meta.env.${cur}`] = JSON.stringify(process.env[cur]), pre),
{}
),
})
Yes, I'm aware. This issue isn't a request for help.
Describe the bug
When using
define
to replaceprocess.env
withimport.meta.env
, attempting a destructuring ofimport.meta.env
will result in an error at runtime:It is/was quite common and completely valid to destructure
process.env
when writing for the Node platform, as it provided quick and easy means of init. e.g.:In many cases when a Vite app pulls in a module which contains code like this, only a portion of the module is used as pertaining to the frontend app, and this kind of assignment is frequently used as an escape hatch or to block further execution based on context.
I've attempted to use a myriad of keyword combinations to search both the docs and the repo, but haven't found anything addressing this specific issue.
Reproduction
https://stackblitz.com/edit/vitejs-vite-9wbsbd
Steps to reproduce
npm run build
npm run serve
System Info
Used Package Manager
npm
Logs
n/a
Validations