build(vite): refine vite.config.ts

This commit is contained in:
Burt 2024-01-03 11:16:57 +08:00
parent a1f2bef5d1
commit e3648ad1a0

View File

@ -13,6 +13,7 @@ import vueSetupExtend from 'vite-plugin-vue-setup-extend'
import UnoCSS from 'unocss/vite' import UnoCSS from 'unocss/vite'
import autoprefixer from 'autoprefixer' import autoprefixer from 'autoprefixer'
/** 这个修改只对web生效小程序没有index.html这个文件 */
const htmlPlugin = (title: string) => { const htmlPlugin = (title: string) => {
return { return {
name: 'html-transform', name: 'html-transform',
@ -25,8 +26,14 @@ const htmlPlugin = (title: string) => {
} }
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default ({ mode }) => { export default ({ command, mode }) => {
// mode: 区分生产环境还是开发环境 // mode: 区分生产环境还是开发环境
console.log(command, mode)
// pnpm dev:h5 时得到 => serve development
// pnpm build:h5 时得到 => build development
// pnpm dev:mp-weixin 时得到 => build development (注意区别command为build)
// pnpm build:mp-weixin 时得到 => build production
// process.cwd(): 获取当前文件的目录跟地址 // process.cwd(): 获取当前文件的目录跟地址
// loadEnv(): 返回当前环境env文件中额外定义的变量 // loadEnv(): 返回当前环境env文件中额外定义的变量
const env = loadEnv(mode, path.resolve(process.cwd(), 'env')) const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
@ -38,12 +45,13 @@ export default ({ mode }) => {
htmlPlugin(env.VITE_APP_TITLE), htmlPlugin(env.VITE_APP_TITLE),
svgLoader(), svgLoader(),
// 打包分析插件 // 打包分析插件
visualizer({ mode === 'production' &&
filename: './node_modules/.cache/visualizer/stats.html', visualizer({
open: true, filename: './node_modules/.cache/visualizer/stats.html',
gzipSize: true, open: true,
brotliSize: true, gzipSize: true,
}), brotliSize: true,
}),
ViteRestart({ ViteRestart({
// 通过这个插件在修改vite.config.js文件则不需要重新运行也生效配置 // 通过这个插件在修改vite.config.js文件则不需要重新运行也生效配置
restart: ['vite.config.js'], restart: ['vite.config.js'],