2022-09-26 00:21:50 +08:00
|
|
|
import fs from 'node:fs'
|
|
|
|
import path from 'node:path'
|
2023-08-26 12:18:40 +08:00
|
|
|
import process from 'node:process'
|
2022-09-26 00:21:50 +08:00
|
|
|
import { defineConfig, loadEnv } from 'vite'
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
import pkg from './package.json'
|
|
|
|
import createVitePlugins from './vite/plugins'
|
|
|
|
|
|
|
|
// https://vitejs.dev/config/
|
2023-10-29 23:17:31 +08:00
|
|
|
export default async ({ mode, command }) => {
|
2022-09-26 00:21:50 +08:00
|
|
|
const env = loadEnv(mode, process.cwd())
|
|
|
|
// 全局 scss 资源
|
|
|
|
const scssResources = []
|
|
|
|
fs.readdirSync('src/assets/styles/resources').forEach((dirname) => {
|
2023-08-26 12:18:40 +08:00
|
|
|
if (fs.statSync(`src/assets/styles/resources/${dirname}`).isFile()) {
|
|
|
|
scssResources.push(`@use "src/assets/styles/resources/${dirname}" as *;`)
|
|
|
|
}
|
2022-09-26 00:21:50 +08:00
|
|
|
})
|
|
|
|
return defineConfig({
|
|
|
|
base: './',
|
|
|
|
// 开发服务器选项 https://cn.vitejs.dev/config/#server-options
|
|
|
|
server: {
|
|
|
|
open: true,
|
|
|
|
port: 9000,
|
|
|
|
proxy: {
|
|
|
|
'/proxy': {
|
|
|
|
target: env.VITE_APP_API_BASEURL,
|
|
|
|
changeOrigin: command === 'serve' && env.VITE_OPEN_PROXY === 'true',
|
|
|
|
rewrite: path => path.replace(/\/proxy/, ''),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// 构建选项 https://cn.vitejs.dev/config/#server-fsserve-root
|
|
|
|
build: {
|
|
|
|
outDir: mode === 'production' ? 'dist' : `dist-${mode}`,
|
|
|
|
sourcemap: env.VITE_BUILD_SOURCEMAP === 'true',
|
|
|
|
},
|
|
|
|
define: {
|
|
|
|
__SYSTEM_INFO__: JSON.stringify({
|
|
|
|
pkg: {
|
|
|
|
dependencies: pkg.dependencies,
|
|
|
|
devDependencies: pkg.devDependencies,
|
|
|
|
},
|
|
|
|
lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
plugins: createVitePlugins(env, command === 'build'),
|
|
|
|
resolve: {
|
|
|
|
alias: {
|
|
|
|
'@': path.resolve(__dirname, 'src'),
|
2022-12-09 01:01:19 +08:00
|
|
|
'#': path.resolve(__dirname, 'src/types'),
|
2022-09-26 00:21:50 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
css: {
|
|
|
|
preprocessorOptions: {
|
|
|
|
scss: {
|
|
|
|
additionalData: scssResources.join(''),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|