mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-01-24 02:00:25 +08:00
fix: support various vite modes of build, not just production (#832)
(cherry picked from commit bcc4773fbfc3dd1b7a24655bc731be830ad6d72d)
This commit is contained in:
parent
cdb10cc4ac
commit
95c16a5d26
@ -39,22 +39,37 @@ export function wrapperEnv(envConf: Recordable): ViteEnv {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前环境下生效的配置文件名
|
||||||
|
*/
|
||||||
|
function getConfFiles() {
|
||||||
|
const script = process.env.npm_lifecycle_script;
|
||||||
|
const reg = new RegExp('--mode ([a-z]+) ');
|
||||||
|
const result = reg.exec(script as string) as any;
|
||||||
|
if (result) {
|
||||||
|
const mode = result[1] as string;
|
||||||
|
return ['.env', `.env.${mode}`];
|
||||||
|
}
|
||||||
|
return ['.env', '.env.production'];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the environment variables starting with the specified prefix
|
* Get the environment variables starting with the specified prefix
|
||||||
* @param match prefix
|
* @param match prefix
|
||||||
* @param confFiles ext
|
* @param confFiles ext
|
||||||
*/
|
*/
|
||||||
export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
|
export function getEnvConfig(match = 'VITE_GLOB_', confFiles = getConfFiles()) {
|
||||||
let envConfig = {};
|
let envConfig = {};
|
||||||
confFiles.forEach((item) => {
|
confFiles.forEach((item) => {
|
||||||
try {
|
try {
|
||||||
const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
|
const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
|
||||||
envConfig = { ...envConfig, ...env };
|
envConfig = { ...envConfig, ...env };
|
||||||
} catch (error) {}
|
} catch (e) {
|
||||||
|
console.error(`Error in parsing ${item}`, e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
const reg = new RegExp(`^(${match})`);
|
||||||
Object.keys(envConfig).forEach((key) => {
|
Object.keys(envConfig).forEach((key) => {
|
||||||
const reg = new RegExp(`^(${match})`);
|
|
||||||
if (!reg.test(key)) {
|
if (!reg.test(key)) {
|
||||||
Reflect.deleteProperty(envConfig, key);
|
Reflect.deleteProperty(envConfig, key);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user