vite.config.ts
2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import fs from 'node:fs'
import path from 'node:path'
import { defineConfig, loadEnv } from 'vite'
import dayjs from 'dayjs'
import pkg from './package.json'
import createVitePlugins from './vite/plugins'
// https://vitejs.dev/config/
export default ({ mode, command }) => {
const env = loadEnv(mode, process.cwd())
// 全局 scss 资源
const scssResources = []
fs.readdirSync('src/assets/styles/resources').forEach((dirname) => {
if (fs.statSync(`src/assets/styles/resources/${dirname}`).isFile()) {
scssResources.push(`@use "src/assets/styles/resources/${dirname}" as *;`)
}
})
// css 精灵图相关
fs.readdirSync('src/assets/sprites').forEach((dirname) => {
if (fs.statSync(`src/assets/sprites/${dirname}`).isDirectory()) {
// css 精灵图生成的 scss 文件也需要放入全局 scss 资源
scssResources.push(`@use "src/assets/sprites/_${dirname}.scss" as *;`)
}
})
return defineConfig({
base: env.VITE_SERVE_BASE ?? './',
// 开发服务器选项 https://cn.vitejs.dev/config/#server-options
server: {
open: true,
host: '0.0.0.0',
port: 9000,
proxy: {
'/api': {
target: env.VITE_API_BASEURL,
changeOrigin: env.VITE_OPEN_PROXY === 'true',
rewrite: path => path.replace(/\/api/, ''),
},
'/portal':{
target: env.VITE_API_PORTALURL,
changeOrigin: env.VITE_OPEN_PROXY === 'true',
rewrite: path => path.replace(/\/portal/, ''),
},
'/circulation':{
target: env.VITE_APP_CIRCULATION,
changeOrigin: env.VITE_OPEN_PROXY === 'true',
rewrite: path => path.replace(/\/circulation/, ''),
},
'/delivery':{
target: env.VITE_APP_DATA_DELIVERY,
changeOrigin: env.VITE_OPEN_PROXY === 'true',
rewrite: path => path.replace(/\/delivery/, ''),
},
'/obs': {
target: '//csbr-daop.obs.cn-north-1.myhuaweicloud.com:443',
changeOrigin: env.VITE_OPEN_PROXY === 'true',
rewrite: path => path.replace(/\/obs/, ''),
}
},
},
// 构建选项 https://cn.vitejs.dev/config/#server-fsserve-root
build: {
outDir: mode === 'product' ? 'dist' : `dist-${mode}`,
sourcemap: env.VITE_BUILD_SOURCEMAP === 'true',
copyPublicDir: true, // 确保 public 目录下的文件被复制到输出目录的根目录
},
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'),
'#': path.resolve(__dirname, 'src/types'),
},
},
css: {
preprocessorOptions: {
scss: {
additionalData: scssResources.join(''),
},
},
},
})
}