2025-06-09 10:23:45 +08:00
|
|
|
|
import { defineConfig } from 'vite'
|
|
|
|
|
|
import vue from '@vitejs/plugin-vue'
|
|
|
|
|
|
import path from 'path' // 引入 path 模块
|
|
|
|
|
|
|
2025-06-10 17:15:13 +08:00
|
|
|
|
import viteImagemin from 'vite-plugin-imagemin' // 新插件
|
2025-06-11 17:09:33 +08:00
|
|
|
|
import AutoImport from 'unplugin-auto-import/vite'
|
|
|
|
|
|
import Components from 'unplugin-vue-components/vite'
|
2025-06-12 19:01:03 +08:00
|
|
|
|
// 引入 ElementPlus 相关解析器
|
|
|
|
|
|
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
2025-06-11 17:53:50 +08:00
|
|
|
|
|
|
|
|
|
|
import legacy from '@vitejs/plugin-legacy'
|
2025-06-09 10:23:45 +08:00
|
|
|
|
export default defineConfig({
|
2025-06-10 17:15:13 +08:00
|
|
|
|
plugins: [
|
|
|
|
|
|
vue(),
|
2025-06-12 19:01:03 +08:00
|
|
|
|
|
2025-06-11 17:53:50 +08:00
|
|
|
|
legacy({
|
|
|
|
|
|
targets: ['ie >= 11'],
|
|
|
|
|
|
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
|
|
|
|
|
|
modernPolyfills: true // 启用现代浏览器的 polyfill
|
|
|
|
|
|
}),
|
2025-06-11 17:09:33 +08:00
|
|
|
|
AutoImport({
|
2025-06-12 19:01:03 +08:00
|
|
|
|
imports: ['vue', 'vue-router'], // 自动导入 Vue、Vue Router API
|
|
|
|
|
|
// 自动导入 Vue 相关函数,以及 ElementPlus 的相关函数等
|
|
|
|
|
|
resolvers: [
|
|
|
|
|
|
ElementPlusResolver({
|
|
|
|
|
|
importStyle: 'sass', // 可选:按需导入样式
|
|
|
|
|
|
directives: true, // 导入指令
|
|
|
|
|
|
version: '2.3.6' // 版本号
|
|
|
|
|
|
})
|
|
|
|
|
|
]
|
2025-06-11 17:09:33 +08:00
|
|
|
|
}),
|
|
|
|
|
|
Components({
|
2025-06-12 19:01:03 +08:00
|
|
|
|
// 自动导入 ElementPlus 的组件
|
|
|
|
|
|
resolvers: [
|
|
|
|
|
|
ElementPlusResolver({
|
|
|
|
|
|
importStyle: 'sass',
|
|
|
|
|
|
directives: true
|
|
|
|
|
|
})
|
|
|
|
|
|
],
|
2025-06-11 17:09:33 +08:00
|
|
|
|
dirs: ['src/components'] // 自动扫描组件目录
|
|
|
|
|
|
}),
|
2025-06-10 17:15:13 +08:00
|
|
|
|
viteImagemin({
|
|
|
|
|
|
// 基础压缩配置(可根据需求扩展)
|
|
|
|
|
|
gifsicle: { optimizationLevel: 7 },
|
|
|
|
|
|
optipng: { optimizationLevel: 7 },
|
|
|
|
|
|
mozjpeg: { quality: 80 },
|
|
|
|
|
|
pngquant: { quality: [0.8, 0.9] },
|
|
|
|
|
|
svgo: { plugins: [{ name: 'removeViewBox' }] }
|
|
|
|
|
|
})
|
|
|
|
|
|
],
|
2025-06-10 17:18:33 +08:00
|
|
|
|
build: {
|
|
|
|
|
|
rollupOptions: {
|
2025-06-16 18:34:43 +08:00
|
|
|
|
// external: ['element-plus', 'element-plus/dist/index.css'], // 排除 Element Plus
|
|
|
|
|
|
// external: ['element-plus'], // 排除 Element Plus 从打包中
|
2025-06-10 17:18:33 +08:00
|
|
|
|
output: {
|
|
|
|
|
|
manualChunks(id) {
|
2025-06-16 18:34:43 +08:00
|
|
|
|
// 只打包实际使用的 Element Plus 组件
|
|
|
|
|
|
// if (id.includes('element-plus/es/components')) {
|
|
|
|
|
|
// const componentName = id.split('element-plus/es/components/')[1].split('/')[0]
|
|
|
|
|
|
// return `element-${componentName}`
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if (id.includes('node_modules')) {
|
|
|
|
|
|
// // 将第三方库单独分包(如 axios、vue 等)
|
|
|
|
|
|
// return id.split('node_modules/')[1].split('/')[0]
|
|
|
|
|
|
// }
|
|
|
|
|
|
// 1. 将 Element Plus 整体打包为一个 chunk
|
|
|
|
|
|
// 只分割实际使用的库
|
|
|
|
|
|
if (id.includes('vue') || id.includes('vue-router') || id.includes('pinia')) {
|
|
|
|
|
|
return 'vue-vendor'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (id.includes('axios') || id.includes('nprogress')) {
|
|
|
|
|
|
return 'utils'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 不强制分割第三方依赖,让 Vite 自动处理
|
|
|
|
|
|
if (id.includes('node_modules') && !id.includes('@babel') && !id.includes('@types')) {
|
|
|
|
|
|
return undefined // 取消手动分割
|
2025-06-10 17:18:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-06-12 19:01:03 +08:00
|
|
|
|
css: {
|
|
|
|
|
|
preprocessorOptions: {
|
2025-06-16 18:34:43 +08:00
|
|
|
|
scss: {
|
|
|
|
|
|
// 关键配置:强制 Sass 使用现代 API(二选一,推荐 modern-compiler)
|
|
|
|
|
|
api: 'modern-compiler'
|
|
|
|
|
|
// api: 'modern' // 轻量版,适合简单场景
|
|
|
|
|
|
},
|
2025-06-12 19:01:03 +08:00
|
|
|
|
less: {
|
|
|
|
|
|
// 关键配置:自动注入 global.less
|
|
|
|
|
|
additionalData: `@import "@/styles/global.less";`,
|
|
|
|
|
|
|
|
|
|
|
|
// 可选:如果需要自定义 Less 选项
|
|
|
|
|
|
javascriptEnabled: true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-06-09 10:23:45 +08:00
|
|
|
|
resolve: {
|
|
|
|
|
|
alias: {
|
|
|
|
|
|
'@': path.resolve(__dirname, 'src') // 添加路径别名
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|