80 lines
2.8 KiB
JavaScript
80 lines
2.8 KiB
JavaScript
import { fileURLToPath, URL } from 'node:url'
|
|
|
|
import { defineConfig } from 'vite'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import vueJsx from '@vitejs/plugin-vue-jsx'
|
|
import vueDevTools from 'vite-plugin-vue-devtools'
|
|
import viteCompression from 'vite-plugin-compression'
|
|
import scriptInjectPlugin from './vite-plugin-script-inject'
|
|
|
|
// https://vite.dev/config/
|
|
export default defineConfig(({mode = 'production', command = "server"}) => {
|
|
console.log(mode, command)
|
|
return {
|
|
plugins: [
|
|
vue(),
|
|
vueJsx(),
|
|
vueDevTools(),
|
|
scriptInjectPlugin(),
|
|
viteCompression({
|
|
algorithm: 'gzip', // 压缩算法,可选 ['gzip', 'brotliCompress', 'deflate', 'deflateRaw']
|
|
ext: '.gz', // 生成的压缩包后缀
|
|
threshold: 10240, // 大于 10KB 的文件才压缩(默认值)
|
|
deleteOriginFile: false, // 压缩后是否删除源文件
|
|
verbose: true, // 是否在控制台显示压缩结果
|
|
disable: false, // 是否禁用
|
|
}),
|
|
],
|
|
resolve: {
|
|
alias: {
|
|
'@': fileURLToPath(new URL('./src', import.meta.url)),
|
|
},
|
|
},
|
|
build: {
|
|
target: 'esnext',
|
|
// 启用 CSS 代码分割,每个异步 chunk 会生成对应的 CSS 文件
|
|
// 这样可以实现按需加载,提高首屏加载性能
|
|
cssCodeSplit: true,
|
|
cssMinify: true, // 压缩 CSS
|
|
|
|
// Rollup 构建配置选项
|
|
rollupOptions: {
|
|
output: {
|
|
// 分包策略
|
|
manualChunks(id) {
|
|
// 将 node_modules 中的代码单独打包
|
|
if (id.includes('node_modules')) {
|
|
// 将 Vue 相关库打包到一起 vue: ['vue', 'vue-router', 'pinia'],
|
|
if (id.includes('vue') || id.includes('pinia')|| id.includes('vue-router')) {
|
|
return 'vue-vendor';
|
|
}
|
|
console.log('ACK', id)
|
|
// 其他第三方库打包到 vendor
|
|
return 'vendor';
|
|
}
|
|
},
|
|
// 自定义构建后的文件名
|
|
// [name] 为文件名
|
|
// [hash] 为文件内容的 hash 值,用于缓存控制
|
|
// [ext] 为文件扩展名
|
|
entryFileNames: 'assets/[name].[hash].js', // 入口文件命名规则
|
|
chunkFileNames: 'assets/[name].[hash].js', // 代码块命名规则
|
|
assetFileNames: 'assets/[name].[hash].[ext]' // 静态资源命名规则
|
|
}
|
|
},
|
|
|
|
// 代码压缩配置
|
|
minify: 'terser', // 使用 terser 进行代码压缩
|
|
terserOptions: {
|
|
compress: {
|
|
// drop_console: true, // 删除所有 console.* 调用,减小生产环境代码体积
|
|
drop_debugger: true, // 删除所有 debugger 语句,移除调试代码
|
|
}
|
|
},
|
|
|
|
// 代码分割配置
|
|
chunkSizeWarningLimit: 2000, // 块大小警告的限制(以 kbs 为单位)
|
|
},
|
|
}
|
|
})
|