mirror of
https://gitee.com/wonderful-code/buildadmin
synced 2024-11-21 22:55:36 +00:00
refactor:环境变量加载优化
This commit is contained in:
parent
0602a65a2d
commit
65a9c035d1
@ -40,7 +40,6 @@
|
||||
"@typescript-eslint/parser": "5.60.0",
|
||||
"@vitejs/plugin-vue": "4.2.3",
|
||||
"async-validator": "4.2.5",
|
||||
"dotenv": "16.3.1",
|
||||
"eslint": "8.43.0",
|
||||
"eslint-config-prettier": "8.8.0",
|
||||
"eslint-plugin-vue": "9.15.1",
|
||||
|
@ -1,50 +1,13 @@
|
||||
/* vite相关 */
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
export interface ViteEnv {
|
||||
VITE_PORT: number
|
||||
VITE_OPEN: boolean
|
||||
VITE_BASE_PATH: string
|
||||
VITE_OUT_DIR: string
|
||||
VITE_AXIOS_BASE_URL: string
|
||||
VITE_PROXY_URL: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否在开发环境
|
||||
*/
|
||||
export function isDev(mode: string): boolean {
|
||||
return mode === 'development'
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否在生产环境
|
||||
*/
|
||||
export function isProd(mode: string | undefined): boolean {
|
||||
return mode === 'production'
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all environment variable configuration files to process.env
|
||||
*/
|
||||
export function loadEnv(mode: string): ViteEnv {
|
||||
const ret: any = {}
|
||||
const envList = [`.env.${mode}.local`, `.env.${mode}`, '.env.local', '.env']
|
||||
envList.forEach((e) => {
|
||||
dotenv.config({ path: e })
|
||||
})
|
||||
for (const envName of Object.keys(process.env)) {
|
||||
let realName = (process.env as any)[envName].replace(/\\n/g, '\n')
|
||||
realName = realName === 'true' ? true : realName === 'false' ? false : realName
|
||||
if (envName === 'VITE_PORT') realName = Number(realName)
|
||||
if (envName === 'VITE_OPEN') realName = Boolean(realName)
|
||||
if (envName === 'VITE_PROXY') {
|
||||
try {
|
||||
realName = JSON.parse(realName)
|
||||
} catch (error) {
|
||||
realName = ''
|
||||
}
|
||||
}
|
||||
ret[envName] = realName
|
||||
if (typeof realName === 'string') {
|
||||
process.env[envName] = realName
|
||||
} else if (typeof realName === 'object') {
|
||||
process.env[envName] = JSON.stringify(realName)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import { resolve } from 'path'
|
||||
import type { UserConfig, ConfigEnv, ProxyOptions } from 'vite'
|
||||
import { isProd, loadEnv } from '/@/utils/vite'
|
||||
import { UserConfig, ConfigEnv, ProxyOptions, loadEnv } from 'vite'
|
||||
import { isProd } from '/@/utils/vite'
|
||||
import { svgBuilder } from '/@/components/icon/svg/index'
|
||||
|
||||
const pathResolve = (dir: string): any => {
|
||||
@ -10,7 +10,7 @@ const pathResolve = (dir: string): any => {
|
||||
|
||||
// https://vitejs.cn/config/
|
||||
const viteConfig = ({ mode }: ConfigEnv): UserConfig => {
|
||||
const { VITE_PORT, VITE_OPEN, VITE_BASE_PATH, VITE_OUT_DIR, VITE_PROXY_URL } = loadEnv(mode)
|
||||
const { VITE_PORT, VITE_OPEN, VITE_BASE_PATH, VITE_OUT_DIR, VITE_PROXY_URL } = loadEnv(mode, process.cwd())
|
||||
|
||||
const alias: Record<string, string> = {
|
||||
'/@': pathResolve('./src/'),
|
||||
@ -34,9 +34,8 @@ const viteConfig = ({ mode }: ConfigEnv): UserConfig => {
|
||||
resolve: { alias },
|
||||
base: VITE_BASE_PATH,
|
||||
server: {
|
||||
host: '0.0.0.0',
|
||||
port: VITE_PORT,
|
||||
open: VITE_OPEN,
|
||||
port: parseInt(VITE_PORT),
|
||||
open: VITE_OPEN != 'false',
|
||||
proxy: proxy,
|
||||
},
|
||||
build: {
|
||||
|
Loading…
Reference in New Issue
Block a user