-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
145 lines (138 loc) · 5.31 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
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import ElementPlus from 'unplugin-element-plus/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
const TRUE = 'true'
export default ({ mode, command }: { mode: string, command: string }) => {
const ENV = loadEnv(mode, process.cwd()) // load (dev || prod) .env
// vite config
return defineConfig({
base: '/',
define: {
'process.env': ENV,
},
css: {
preprocessorOptions: {
scss: {
additionalData: '@use "@/styles/scss/element/index.scss" as *;',
},
},
},
plugins: [
vue(),
AutoImport({
// targets to transform
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/, /\.vue\?vue/, // .vue
/\.md$/, // .md
],
// global imports to register
imports: [
// presets
'vue',
'vue-router',
// custom
{
'@vueuse/core': [
// named imports
'useMouse', // import { useMouse } from '@vueuse/core',
'useWindowSize', // import { useWindowSize } from '@vueuse/core',
],
axios: [
['default', 'axios'], // import { default as axios } from 'axios',
],
},
],
// Generate corresponding .eslintrc-auto-import.json file.
// eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
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')
},
// custom resolvers
// see https://github.com/antfu/unplugin-auto-import/pull/23/
resolvers: [
ElementPlusResolver({
// for import styles/scss/element/index.scss to change system color
importStyle: 'sass',
}),
],
}),
Components({
resolvers: [
ElementPlusResolver({
importStyle: 'sass',
}),
],
}),
createSvgIconsPlugin({
// Specify the icon folder to be cached
iconDirs: [resolve(process.cwd(), 'src/assets/icon')],
// Specify symbolId format
symbolId: 'icon-[dir]-[name]',
}),
ElementPlus({
useSource: true,
}),
],
resolve: {
alias: {
mqtt: 'mqtt/dist/mqtt.js',
'@': resolve(__dirname, resolve('src')),
},
},
server: {
open: true,
proxy: {
'/proxy': {
target: ENV.VITE_BASE_URL,
changeOrigin: command === 'serve' && ENV.VITE_OPEN_PROXY === TRUE, // is cross proxy,
rewrite: path => path.replace(/\/proxy/, ''),
},
},
// reload mqtt-vue-hook
watch: {
usePolling: true, // For Docker.
ignored: ['!**/node_modules/mqtt-vue-hook/**'],
},
// reload mqtt-vue-hook
},
// reload mqtt-vue-hook
optimizeDeps: {
exclude: ['mqtt-vue-hook'],
},
// reload mqtt-vue-hook
build: {
outDir: mode === 'production' ? 'dist' : `dist-${mode}`,
sourcemap: ENV.VITE_BUILD_SOURCEMAP === TRUE,
minify: 'terser',
terserOptions: {
compress: {
drop_console: ENV.VITE_BUILD_DROP_CONSOLE === TRUE, // disable console in prod.env
drop_debugger: ENV.VITE_BUILD_DROP_DEBUGGER === TRUE, // disable debug in prod.env
},
},
rollupOptions: {
output: {
assetFileNames: ({ name }) => {
if (/\.(gif|jpe?g|png|svg|webp)$/.test(name ?? '')) {
return 'assets/images/[name]-[hash][extname]'
}
if (/\.css$/.test(name ?? '')) {
return 'assets/css/[name]-[hash][extname]'
}
// default value
// ref: https://rollupjs.org/guide/en/#outputassetfilenames
return 'assets/[name]-[hash][extname]'
},
},
},
},
})
}