light-blog-admin/vite.config.js

72 lines
2.0 KiB
JavaScript

import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import process from 'node:process'
import vue from '@vitejs/plugin-vue'
// 打包大小分析
import { visualizer } from 'rollup-plugin-visualizer'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
console.log(loadEnv(mode, process.cwd()).VITE_APP_BASE_URL)
return {
base: loadEnv(mode, process.cwd()).VITE_APP_BASE_URL,
plugins: [
vue(),
visualizer({
open: true,
filename: 'stats.html',
gzipSize: true,
brotliSize: true
})
],
esbuild: {
// 去除项目中的console和debugger
drop: ['console', 'debugger']
},
build: {
rollupOptions: {
// 打包后目录结构
output: {
entryFileNames: 'js/[name]-[hash].js',
chunkFileNames: 'js/[name]-chunk-[hash].js',
assetFileNames: (assetInfo) => {
if (assetInfo.name.endsWith('.css')) {
return 'css/[name]-[hash].css'
}
const imgExts = ['.jpg', '.jpeg', '.png', '.webp', '.gif', '.svg', '.ico']
if (imgExts.some((ext) => assetInfo.name.endsWith(ext))) {
return 'img/[name]-[hash].[ext]'
}
return 'assets/[name]-[hash].[ext]'
}
}
}
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@c': fileURLToPath(new URL('./src/components', import.meta.url)),
'@v': fileURLToPath(new URL('./src/views', import.meta.url))
}
},
server: {
port: '20011',
proxy: {
'/api': {
target: 'http://localhost:8081/',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
},
'/static': {
target: 'http://localhost:8081/static',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/static/, '')
}
}
}
}
})