-
I'm using TypeScript and am confused about type interface ImportMetaEnv {
[key: string]: string | boolean | undefined
BASE_URL: string
MODE: string
DEV: boolean
PROD: boolean
SSR: boolean
} I've tried setting a variable If env vars are always |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
For whomever is running into this, I fixed it with this little plugin: function parseEnvironmentVariables() {
let config;
return {
name: 'parse-environment-variables',
configResolved(resolvedConfig) {
config = resolvedConfig
const env = config.env;
config.env = Object.keys(env).reduce((a, b) => {
let parsed = config.env[b];
try {
parsed = JSON.parse(config.env[b])
} catch {}
return ({
...a,
[b]: parsed,
});
}, {});
return config;
},
}
} It runs over the variables in the config and tries to parse them as JSON. If that does not work, we revert back to the stringified value. |
Beta Was this translation helpful? Give feedback.
-
vite/packages/vite/src/node/config.ts Lines 1088 to 1105 in 09c4fc0
There's no casting to booleans or other types: motdotla/dotenv#544 So seems like userEnv values should be typed as |
Beta Was this translation helpful? Give feedback.
-
This is still a problem ... |
Beta Was this translation helpful? Give feedback.
-
My first foray into Vite and just ran into this, not a great look that you can type the env vars but they aren't actually handled. |
Beta Was this translation helpful? Give feedback.
I just wrote a plugin workaround this issue. You can try it out.
https://www.npmjs.com/package/@niku/vite-env-caster