98 lines
3.1 KiB
TypeScript
98 lines
3.1 KiB
TypeScript
import { defineConfig, loadEnv, type ConfigEnv } from 'vite'
|
|
import type { UserConfig } from 'vite'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
|
|
import path from 'path'
|
|
import type { MinifyOptions } from 'terser'
|
|
import { fileURLToPath } from 'url'
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
// https://vite.dev/config/
|
|
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
|
|
// 加载环境变量
|
|
const env = loadEnv(mode, process.cwd(), '')
|
|
|
|
return {
|
|
base: './', // 部署目录,默认根目录
|
|
define: {
|
|
'process.env': {}
|
|
},
|
|
resolve: {
|
|
alias: {
|
|
'@': path.resolve(__dirname, 'src'),
|
|
'jquery': path.resolve(__dirname, 'node_modules/jquery/dist/jquery.min.js'),
|
|
'bootstrap': path.resolve(__dirname, 'node_modules/bootstrap/dist/js/bootstrap.bundle.min.js'),
|
|
'jquery.nicescroll': path.resolve(__dirname, 'node_modules/jquery.nicescroll/jquery.nicescroll.min.js'),
|
|
'*': path.resolve('')
|
|
},
|
|
// 导入时想要省略的扩展名列表
|
|
extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
|
|
},
|
|
plugins: [
|
|
vue(),
|
|
// SVG 图标插件
|
|
createSvgIconsPlugin({
|
|
iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
|
|
symbolId: 'icon-[dir]-[name]',
|
|
})
|
|
],
|
|
css: {
|
|
preprocessorOptions: {
|
|
scss: {
|
|
additionalData: `@use "@/styles/variable.scss" as *;`,
|
|
},
|
|
},
|
|
},
|
|
// 开发服务器配置
|
|
server: {
|
|
host: '0.0.0.0', // 监听所有地址
|
|
port: 5173, // 默认端口
|
|
open: true, // 自动打开浏览器
|
|
cors: true, // 启用 CORS
|
|
strictPort: false, // 端口被占用时尝试其他端口
|
|
// 代理配置
|
|
proxy: {
|
|
// 开发环境 API 代理
|
|
[env.VITE_APP_BASE_API]: {
|
|
target: env.VITE_APP_BASE_URL,
|
|
changeOrigin: true,
|
|
rewrite: (path) => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), '')
|
|
},
|
|
// 本地开发 API 代理
|
|
'/api': {
|
|
target: 'http://localhost:3000', // 本地开发服务器地址
|
|
changeOrigin: true,
|
|
rewrite: (path) => path.replace(/^\/api/, '')
|
|
}
|
|
},
|
|
// 配置静态资源服务
|
|
fs: {
|
|
strict: true,
|
|
allow: ['..']
|
|
}
|
|
},
|
|
// 构建配置
|
|
build: {
|
|
target: 'es2015',
|
|
outDir: 'dist',
|
|
assetsDir: 'assets',
|
|
assetsInlineLimit: 4096, // 4kb 以下的资源内联为 base64
|
|
sourcemap: mode !== 'production', // 非生产环境生成 sourcemap
|
|
minify: 'terser',
|
|
terserOptions: {
|
|
compress: {
|
|
drop_console: mode === 'production', // 生产环境移除 console
|
|
drop_debugger: true, // 移除 debugger
|
|
},
|
|
} as MinifyOptions,
|
|
chunkSizeWarningLimit: 1000, // chunk 大小警告限制
|
|
},
|
|
// 预构建配置
|
|
optimizeDeps: {
|
|
include: ['vue', 'vue-router', 'pinia', 'axios', 'element-plus'],
|
|
exclude: []
|
|
}
|
|
}
|
|
}) |