vite2+vue3在vite.config下使用env环境变量的方法
There's a chicken-egg problem here: Vite expects to resolve .env files from project root, but project root can be made different by the config file.So if we resolve .env before resolving the config fi
·
查看文档发现Vite 环境变量是通过import.meta.env
对象暴露的,但在vite.config
中行不通,经过一通乱找,发现原来是这样用的:
const envResolve = (mode: string, env: string) => loadEnv(mode, process.cwd())[env];
export default ({ command, mode }: ConfigEnv): UserConfigExport =>{
return {
base: envResolve(mode, 'VITE_PUBLIC'),
plugins: [vue()]
}
}
具体原因:
There’s a chicken-egg problem here: Vite expects to resolve .env files from project root, but project root can be made different by the config file.
So if we resolve .env before resolving the config file, we can only resolve it from CWD, which would then break the case where the user puts .env files in a nested root specified via config.
——Evan You
更多推荐
已为社区贡献2条内容
所有评论(0)