查看文档发现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

参考博客:https://www.jianshu.com/p/4973bd983e96

Logo

前往低代码交流专区

更多推荐