-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
151 lines (146 loc) · 4.44 KB
/
vite.config.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import path from 'node:path'
import useUni from '@dcloudio/vite-plugin-uni'
import useEslint from 'vite-plugin-eslint'
import useUnoCSS from 'unocss/vite'
import useUniPages from '@uni-helper/vite-plugin-uni-pages'
import AutoImport from 'unplugin-auto-import/vite'
import { defineConfig, loadEnv } from 'vite'
import postcssConfig from './postcss.config.js'
import { useProxy } from './src/configs/server'
const isDevelopment = process.env.NODE_ENV === 'development'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
const {
VITE_APP_PROXY_PATH,
VITE_APP_API_URL,
VITE_APP_FILE_PATH,
VITE_GLOB_HOME_PAGE,
VITE_APP_PROXY_PORT,
} = env
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
}, {}),
}
const p = {
...viteEnvKeys.reduce((config, variable) => {
config[`${variable}`] = JSON.stringify(env[variable])
return config
}, {}),
}
return {
server: {
cors: true,
host: true,
port: VITE_APP_PROXY_PORT,
https: false,
proxy: {
...(useProxy && VITE_APP_API_URL
? {
[`^${VITE_APP_PROXY_PATH}`]: {
target: `${VITE_APP_API_URL}`,
changeOrigin: true,
rewrite: path =>
path.replace(new RegExp(`^${VITE_APP_API_URL}`), ''),
secure: false,
bypass(req, res, options: any) {
const proxyURLTruth
= options.target + options.rewrite(req.url)
console.log('proxyURL', proxyURLTruth)
req.headers['x-req-proxyURL'] = proxyURLTruth // 设置未生效
res.setHeader('x-req-proxyURL', proxyURLTruth) // 设置响应头可以看到
},
},
// 解决开发环境上传图片无法直接显示的问题
[`^${VITE_APP_FILE_PATH}`]: {
target: `${VITE_APP_API_URL}${VITE_APP_FILE_PATH}`,
changeOrigin: true,
rewrite: path =>
path.replace(new RegExp(`^${VITE_APP_FILE_PATH}`), ''),
},
}
: {}),
},
},
resolve: {
alias: {
'^@': path.resolve(__dirname, './src/'),
'@': path.resolve(__dirname, './src/'),
'$uni-router': path.resolve(__dirname, './src/utils/uni-router/'),
},
},
css: {
// 修复外部 postcss.config.js 不被解析的问题
postcss: postcssConfig,
},
build: {
// minify: false,
// lib: {
// formats: ['es'],
// },
// TODO 解决 Windows 下开发模式控制台提示崩溃的问题
...(isDevelopment
? {
watch: {
exclude: ['node_modules/**', '/__uno.css'],
},
}
: {}),
},
plugins: [
useEslint(),
useUnoCSS(),
useUniPages({
mergePages: false,
homePage: VITE_GLOB_HOME_PAGE,
}),
useUni(),
AutoImport({
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/, // .vue
/\.md$/, // .md
],
imports: [
'vue',
'vue-router',
{
'@vueuse/core': [
'useMouse', // import { useMouse } from '@vueuse/core',
['useFetch', 'useMyFetch'], // import { useFetch as useMyFetch } from '@vueuse/core',
],
'axios': [
['default', 'axios'], // import { default as axios } from 'axios',
],
},
{
from: 'vue-router',
imports: ['RouteLocationRaw'],
type: true,
},
],
defaultExportByFilename: false,
dts: './auto-imports.d.ts',
vueTemplate: false,
resolvers: [],
injectAtEnd: true,
eslintrc: {
enabled: false, // Default `false`
filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
},
}),
],
define: {
...define,
'process.env': {
...process.env,
...p,
},
},
}
})