在vite中是不像webpack
那样直接可以通过process.env
就能获取到配置的环境的,这里我们要借助loadEnv
//.env.dev
-# 本地环境
ENV = 'development'
VITE_NODE_ENV = 'development'
// package.json
{
"name": "vite-project",
"version": "0.0.0",
"scripts": {
"dev": "vite --mode=dev"
}
}
import { loadEnv } from 'vite'
const envResolve = (mode) => {
if(mode.indexOf('=')!=-1){
mode = mode.split('=')[1]
}
return loadEnv(mode, process.cwd());
};
const getEnv = function (env) {
return envResolve(_.last(process.argv))[env];
}
const modes = getEnv('VITE_NODE_ENV') //development