-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathvite.config.ts
112 lines (109 loc) · 4.64 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
import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { createHtmlPlugin } from 'vite-plugin-html';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import AutoImport from 'unplugin-auto-import/vite';
import { viteVConsole } from 'vite-plugin-vconsole';
import { babel } from '@rollup/plugin-babel';
const pathResolve = (dir: string) => resolve(__dirname, dir);
// https://vitejs.dev/config/
export default ({ command, mode }) => {
// 获取环境变量
const env: Partial<ImportMetaEnv> = loadEnv(mode, process.cwd());
return defineConfig({
define: {
'process.env': env,
},
resolve: {
// 这里的alias是路径别名,是运行阶段的替换路径,而tsconfig.json中的paths是编码阶段的提示,
alias: {
'@': pathResolve('src'), // path.resolve中,'./src' 等于 'src'
// '@router': pathResolve('src/router'),
},
},
plugins: [
vue(),
// 默认会向 index.html 注入 .env 文件的内容,类似 vite 的 loadEnv函数
// 还可配置entry入口文件, inject自定义注入数据等
createHtmlPlugin(),
// 自动导入src/compoents下的组件和配置的ui库组件
// 只能在template中使用,js中需要手动导入
// vant组件需要带上van前缀
// 自定义组件没有类型提示问题:在tsconfig的include中加入"./components.d.ts"即可解决
// Vant 中有个别组件是以函数的形式提供的,包括 Toast,Dialog,Notify 和 ImagePreview 组件。
// 在使用函数组件时,unplugin-vue-components 无法自动引入对应的样式,因此需要手动引入样式。
// importStyle: false,关闭自动导入样式
// 目前无法指定"src/compoents"下部分组件生产类型声明,可能需要自己实现一个resolvers
Components({
resolvers: [VantResolver({ importStyle: false })],
// globs: ['src/components/**/index.vue'], // 会导致index.vue生成的类型声明为Undefined
}),
// 自动导入api
AutoImport({
imports: ['vue', 'vue-router'],
// 设置为在'src/'目录下生成解决ts报错,默认是当前目录('./',即根目录)
dts: 'src/auto-import.d.ts',
// 自动生成'eslintrc-auto-import.json'文件,在'.eslintrc.cjs'的'extends'中引入解决报错
// 'vue-global-api'这个插件仅仅解决vue3 hook报错
eslintrc: {
enabled: true,
},
}),
viteVConsole({
entry: pathResolve('src/main.ts'),
localEnabled: true,
enabled: env.VITE_BUILD_VCONSOLE === 'true',
config: {
maxLogNumber: 1000,
theme: 'dark',
},
}),
// 在 serve 环境时,如果需要解决低版本chrome可选链报错问题,就打开 babel 配置;如果需要 debug ,则注释掉 babel 配置
// build 时 vite 会对文件进行转译以支持低版本浏览器,不影响
/* babel({
babelHelpers: 'bundled',
plugins: ['@babel/plugin-proposal-optional-chaining'],
include: [/\.vue$/, /\.ts$/],
extensions: ['.vue', '.ts'],
}), */
],
server: {
// port: 3000, // 默认 // vite3已改为默认5173
host: true, // 支持从ip启动
/* open: true,
proxy: {
'/api': {
target: 'http://127.0.0.1:8000', // 后台服务地址
changeOrigin: true, // 是否允许不同源
secure: false, // 支持https
rewrite: path => path.replace(/^\/api/, ''),
},
}, */
},
build: {
outDir: 'dist', // 指定打包路径,默认为项目根目录下的 dist 目录
sourcemap: env.VITE_BUILD_SOURCEMAP === 'true',
// minify默认esbuild,esbuild模式下terserOptions将失效
// vite3变化:Terser 现在是一个可选依赖,如果你使用的是 build.minify: 'terser',你需要手动安装它 `npm add -D terser`
minify: 'terser',
terserOptions: {
compress: {
keep_infinity: true, // 防止 Infinity 被压缩成 1/0,这可能会导致 Chrome 上的性能问题
drop_console: env.VITE_BUILD_DROP_CONSOLE === 'true', // 去除 console
drop_debugger: true, // 去除 debugger
},
},
chunkSizeWarningLimit: 1500, // chunk 大小警告的限制(以 kbs 为单位)
},
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
additionalData: `@import "${pathResolve('src/styles/index.less')}";`,
},
},
},
});
};