-
Notifications
You must be signed in to change notification settings - Fork 68
/
vite.config.js
94 lines (86 loc) · 2.5 KB
/
vite.config.js
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
91
92
93
94
import path from 'node:path'
import { defineConfig, loadEnv } from 'vite'
import useUni from '@dcloudio/vite-plugin-uni'
import useUnoCSS from 'unocss/vite'
import useUniPages from '@uni-helper/vite-plugin-uni-pages'
import useRemoveConsole from 'vite-plugin-remove-console'
import postcssConfig from './postcss.config.js'
import {
proxyPath,
proxyPort,
requestFilePath,
requestPath,
useProxy,
} from './src/configs/server.js'
import { homePage } from './src/configs/index.js'
const isDevelopment = process.env.NODE_ENV === 'development'
const isProduction = process.env.NODE_ENV === 'production'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
const proxyURL = env.VITE_APP_API_URL
const viteEnvKeys = Object.keys(env).filter(key => key.startsWith('VITE_'))
const define = {
...viteEnvKeys.reduce((config, variable) => {
config[`process.env.${variable}`] = JSON.stringify(env[variable])
return config
}, {}),
}
return {
plugins: [
useUnoCSS(),
useUniPages({
mergePages: false,
homePage,
}),
useUni(),
...(isProduction ? [useRemoveConsole()] : []),
],
server: {
cors: true,
host: true,
port: proxyPort,
proxy: {
...(useProxy && proxyURL
? {
[`^${proxyPath}`]: {
target: `${proxyURL}${requestPath}`,
changeOrigin: true,
rewrite: path =>
path.replace(new RegExp(`^${proxyPath}`), ''),
},
// 解决开发环境上传图片无法直接显示的问题
[`^${requestFilePath}`]: {
target: `${proxyURL}${requestFilePath}`,
changeOrigin: true,
rewrite: path =>
path.replace(new RegExp(`^${requestFilePath}`), ''),
},
}
: {}),
},
},
resolve: {
alias: {
'^@': path.resolve(__dirname, './src/'),
'$uni-router': path.resolve(__dirname, './src/utils/uni-router/'),
},
},
css: {
/** 解决外部 postcss.config.js 不被解析的问题 */
postcss: postcssConfig,
},
build: {
/** 解决 Windows 下开发模式控制台提示崩溃的问题 */
...(isDevelopment
? {
watch: {
exclude: ['node_modules/**', '/__uno.css'],
},
}
: {}),
// minify: false,
},
define,
}
})