2024-07-09 23:43:08 +08:00
|
|
|
import type { ApplicationPluginOptions } from '../typing';
|
|
|
|
|
2024-05-19 21:20:42 +08:00
|
|
|
import { join } from 'node:path';
|
|
|
|
|
|
|
|
import { fs } from '@vben/node-utils';
|
2024-06-08 19:49:06 +08:00
|
|
|
|
2024-08-03 10:12:45 +08:00
|
|
|
import dotenv from 'dotenv';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取当前环境下生效的配置文件名
|
|
|
|
*/
|
|
|
|
function getConfFiles() {
|
|
|
|
const script = process.env.npm_lifecycle_script as string;
|
|
|
|
const reg = /--mode ([\d_a-z]+)/;
|
|
|
|
const result = reg.exec(script);
|
2024-07-20 08:31:05 +08:00
|
|
|
|
2024-05-19 21:20:42 +08:00
|
|
|
if (result) {
|
|
|
|
const mode = result[1];
|
|
|
|
return ['.env', `.env.${mode}`];
|
|
|
|
}
|
|
|
|
return ['.env', '.env.production'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the environment variables starting with the specified prefix
|
|
|
|
* @param match prefix
|
|
|
|
* @param confFiles ext
|
|
|
|
*/
|
2024-07-09 23:43:08 +08:00
|
|
|
async function loadEnv<T = Record<string, string>>(
|
2024-05-19 21:20:42 +08:00
|
|
|
match = 'VITE_GLOB_',
|
|
|
|
confFiles = getConfFiles(),
|
|
|
|
) {
|
|
|
|
let envConfig = {};
|
|
|
|
|
|
|
|
for (const confFile of confFiles) {
|
|
|
|
try {
|
|
|
|
const envPath = await fs.readFile(join(process.cwd(), confFile), {
|
|
|
|
encoding: 'utf8',
|
|
|
|
});
|
|
|
|
const env = dotenv.parse(envPath);
|
|
|
|
envConfig = { ...envConfig, ...env };
|
|
|
|
} catch (error) {
|
2024-06-02 22:13:15 +08:00
|
|
|
console.error(`Error while parsing ${confFile}`, error);
|
2024-05-19 21:20:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const reg = new RegExp(`^(${match})`);
|
|
|
|
Object.keys(envConfig).forEach((key) => {
|
|
|
|
if (!reg.test(key)) {
|
|
|
|
Reflect.deleteProperty(envConfig, key);
|
|
|
|
}
|
|
|
|
});
|
2024-07-09 23:43:08 +08:00
|
|
|
return envConfig as T;
|
2024-05-19 21:20:42 +08:00
|
|
|
}
|
2024-07-09 23:43:08 +08:00
|
|
|
|
|
|
|
async function loadAndConvertEnv(
|
|
|
|
match = 'VITE_',
|
|
|
|
confFiles = getConfFiles(),
|
2024-07-13 21:00:31 +08:00
|
|
|
): Promise<
|
2024-07-28 14:29:05 +08:00
|
|
|
{
|
|
|
|
appTitle: string;
|
|
|
|
base: string;
|
|
|
|
port: number;
|
|
|
|
} & Partial<ApplicationPluginOptions>
|
2024-07-13 21:00:31 +08:00
|
|
|
> {
|
2024-07-09 23:43:08 +08:00
|
|
|
const envConfig = await loadEnv(match, confFiles);
|
2024-07-28 14:29:05 +08:00
|
|
|
const {
|
2024-07-29 00:19:26 +08:00
|
|
|
VITE_APP_TITLE,
|
2024-07-28 14:29:05 +08:00
|
|
|
VITE_BASE,
|
|
|
|
VITE_COMPRESS,
|
|
|
|
VITE_DEVTOOLS,
|
|
|
|
VITE_INJECT_APP_LOADING,
|
|
|
|
VITE_NITRO_MOCK,
|
|
|
|
VITE_PORT,
|
|
|
|
VITE_PWA,
|
|
|
|
VITE_VISUALIZER,
|
|
|
|
} = envConfig;
|
|
|
|
const compress = VITE_COMPRESS || '';
|
2024-07-09 23:43:08 +08:00
|
|
|
const compressTypes = compress
|
|
|
|
.split(',')
|
2024-07-16 22:07:28 +08:00
|
|
|
.filter((item) => item === 'brotli' || item === 'gzip');
|
2024-07-20 08:31:05 +08:00
|
|
|
|
2024-07-09 23:43:08 +08:00
|
|
|
return {
|
2024-07-29 00:19:26 +08:00
|
|
|
appTitle: VITE_APP_TITLE ?? 'Vben Admin',
|
2024-07-28 14:29:05 +08:00
|
|
|
base: VITE_BASE || '/',
|
2024-07-09 23:43:08 +08:00
|
|
|
compress: !!compress,
|
|
|
|
compressTypes: compressTypes as ('brotli' | 'gzip')[],
|
2024-07-28 14:29:05 +08:00
|
|
|
devtools: VITE_DEVTOOLS === 'true',
|
|
|
|
injectAppLoading: VITE_INJECT_APP_LOADING === 'true',
|
|
|
|
nitroMock: VITE_NITRO_MOCK === 'true',
|
|
|
|
port: Number(VITE_PORT) || 5173,
|
|
|
|
pwa: VITE_PWA === 'true',
|
|
|
|
visualizer: VITE_VISUALIZER === 'true',
|
2024-07-09 23:43:08 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { loadAndConvertEnv, loadEnv };
|