ufutx-pc-website/vite.config.ts

123 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-06-09 10:23:45 +08:00
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
2025-07-11 14:51:12 +08:00
import path from 'path'
2025-06-09 10:23:45 +08:00
2025-07-11 14:51:12 +08:00
import viteImagemin from 'vite-plugin-imagemin'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
2025-07-11 14:51:12 +08:00
import { visualizer } from 'rollup-plugin-visualizer'
import legacy from '@vitejs/plugin-legacy'
2025-07-11 14:51:12 +08:00
export default defineConfig(({ mode }) => ({
// 新增:指定打包基础路径为 /web/
// base: '/web/', // 所有资源引用会以 /web/ 为前缀(如静态资源路径变为 /web/static/js/xxx.js
base: mode === 'production' ? '/web/' : '/',
plugins: [
2025-07-11 14:51:12 +08:00
visualizer({
filename: './node_modules/.cache/visualizer/stats.html',
open: true,
gzipSize: true,
brotliSize: true
}),
vue(),
2025-06-20 18:44:15 +08:00
...(process.env.NODE_ENV === 'production'
? [
legacy({
targets: ['ie >= 11'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
modernPolyfills: true
})
]
: []),
AutoImport({
2025-07-11 14:51:12 +08:00
imports: ['vue', 'vue-router'],
resolvers: [
ElementPlusResolver({
2025-07-11 14:51:12 +08:00
importStyle: false,
directives: true
})
2025-07-04 11:29:23 +08:00
],
2025-07-11 14:51:12 +08:00
dts: true
}),
Components({
resolvers: [
ElementPlusResolver({
2025-07-11 14:51:12 +08:00
importStyle: false
})
2025-07-11 14:51:12 +08:00
],
dts: true,
include: [/\.vue$/, /\.vue\?vue/, /\.tsx$/],
dirs: ['src/components']
}),
2025-06-20 18:44:15 +08:00
...(process.env.NODE_ENV !== 'development'
? [
viteImagemin({
gifsicle: { optimizationLevel: 7 },
optipng: { optimizationLevel: 7 },
mozjpeg: { quality: 80 },
pngquant: { quality: [0.8, 0.9] },
svgo: { plugins: [{ name: 'removeViewBox' }] }
})
]
: [])
],
server: {
host: '0.0.0.0', // 允许外部访问
port: 3000, // 指定端口
open: true, // 自动打开浏览器
fs: {
strict: false // 允许访问项目根目录外的文件
},
// 代理配置(如有跨域问题)
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, '')
}
}
2025-07-11 14:51:12 +08:00
// proxy: {
// '^/dev-api': {
// target: ''
// }
// }
},
optimizeDeps: {
2025-07-11 14:51:12 +08:00
// include: ['element-plus']
},
build: {
2025-07-11 14:51:12 +08:00
chunkSizeWarningLimit: 1000,
rollupOptions: {
output: {
2025-07-04 11:29:23 +08:00
manualChunks(id) {
if (id.includes('node_modules')) {
// 将第三方库单独分包(如 axios、vue 等)
return id.split('node_modules/')[1].split('/')[0]
}
}
2025-07-11 14:51:12 +08:00
// chunkFileNames: 'static/js/[name]-[hash].js',
// entryFileNames: 'static/js/[name]-[hash].js',
// assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
}
}
},
css: {
preprocessorOptions: {
scss: {
api: 'modern-compiler'
},
less: {
additionalData: `@import "@/styles/global.less";`,
javascriptEnabled: true
}
}
},
2025-06-09 10:23:45 +08:00
resolve: {
alias: {
2025-07-11 14:51:12 +08:00
'@': path.resolve(__dirname, 'src')
2025-06-09 10:23:45 +08:00
}
}
2025-07-11 14:51:12 +08:00
}))