-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
99 lines (89 loc) · 2.53 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
/**
* 1. 如何在vite.config.ts 中使用环境变量,defineConfig() 参数配置成一个函数
* 2.
*
*/
import { defineConfig, loadEnv } from 'vite'
import type { UserConfig, ConfigEnv } from 'vite'
import pkg from './package.json'
// type checking for browser
import checker from 'vite-plugin-checker'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx' // jsx,tsx 语法插件
import UnoCSS from 'unocss/vite'
import dayjs from 'dayjs'
import { configSvgIconsPlugin, createSvgIconsByUnplugin } from './vite-config/plugins/svgSprite'
import path from 'path' //这个path用到了上面安装的@types/node
// https://vitejs.dev/config/
const { name, version } = pkg
const __APP_INFO__ = {
pkg: { name, version },
lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
}
export default defineConfig(({ command, mode, ssrBuild }: ConfigEnv): UserConfig => {
console.log('获取 mode--', { command, mode, ssrBuild })
const root = process.cwd()
const env = loadEnv(mode, root) // root 和 envDir 选项会影响加载行为
console.log('获取 env--', { root, env }) // 获取环境变量
return {
root,
define: {
__APP_ENV__: JSON.stringify(env.APP_ENV),
__APP_INFO__: JSON.stringify(__APP_INFO__),
},
plugins: [
configSvgIconsPlugin({ isBuild: command === 'build' }),
createSvgIconsByUnplugin(),
// type checking
checker({
typescript: true,
vueTsc: true,
}),
vue(),
vueJsx({
// options are passed on to @vue/babel-plugin-jsx
}),
UnoCSS(),
],
//这里进行配置别名
resolve: {
alias: {
'@': path.resolve('./src'), // @代替src
'#': path.resolve('./types'), // #代替types
},
},
// https://cn.vitejs.dev/config/shared-options.html#css-preprocessoroptions
css: {
preprocessorOptions: {
scss: {
// 注入额外的 data,建议以变量为主
additionalData: '@import "@/assets/styles/index.scss";',
},
},
},
server: {
// hmr: {
// overlay: false,
// },
proxy: {
'/dev': {
// Note: https:
target: 'http://127.0.0.1:3000/',
changeOrigin: true,
secure: false,
rewrite: (path) => {
console.log({ path })
return path.replace(/^\/dev/, '')
},
},
},
},
build: {
rollupOptions: {
output: {
// entryFileNames: 'assets/[name].js',
},
},
},
}
})