60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
import { defineConfig, loadEnv } from 'vite'
|
||
|
||
import { convertEnv, getSrcPath, getRootPath } from './build/utils'
|
||
import { createVitePlugins } from './build/plugin'
|
||
import { OUTPUT_DIR, PROXY_CONFIG } from './build/constant'
|
||
|
||
const INVALID_CHAR_REGEX = /[\x00-\x1F\x7F<>*#"{}|^[\]`;?:&=+$,]/g;
|
||
const DRIVE_LETTER_REGEX = /^[a-z]:/i;
|
||
|
||
export default defineConfig(({ command, mode }) => {
|
||
const srcPath = getSrcPath()
|
||
const rootPath = getRootPath()
|
||
const isBuild = command === 'build'
|
||
|
||
const env = loadEnv(mode, process.cwd())
|
||
const viteEnv = convertEnv(env)
|
||
const { VITE_PORT, VITE_PUBLIC_PATH, VITE_USE_PROXY, VITE_BASE_API } = viteEnv
|
||
|
||
return {
|
||
base: VITE_PUBLIC_PATH || '/',
|
||
resolve: {
|
||
alias: {
|
||
'~': rootPath,
|
||
'@': srcPath,
|
||
},
|
||
},
|
||
plugins: createVitePlugins(viteEnv, isBuild),
|
||
server: {
|
||
host: '0.0.0.0',
|
||
port: VITE_PORT,
|
||
open: false,
|
||
proxy: VITE_USE_PROXY
|
||
? {
|
||
[VITE_BASE_API]: PROXY_CONFIG[VITE_BASE_API],
|
||
'/api/v2': PROXY_CONFIG['/api/v2'],
|
||
}
|
||
: undefined,
|
||
},
|
||
build: {
|
||
target: 'es2015',
|
||
outDir: OUTPUT_DIR || 'dist',
|
||
reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告
|
||
chunkSizeWarningLimit: 1024, // chunk 大小警告的限制(单位kb)
|
||
rollupOptions: {
|
||
output: {
|
||
sanitizeFileName(name) {
|
||
const match = DRIVE_LETTER_REGEX.exec(name);
|
||
const driveLetter = match ? match[0] : '';
|
||
// substr 是被淘汰語法,因此要改 slice
|
||
return (
|
||
driveLetter +
|
||
name.slice(driveLetter.length).replace(INVALID_CHAR_REGEX, "")
|
||
);
|
||
}
|
||
}
|
||
}
|
||
},
|
||
}
|
||
})
|