Files
Yi.Admin/Yi.Ai.Vue3/vite.config.ts

175 lines
5.4 KiB
TypeScript
Raw Normal View History

2025-06-17 22:37:37 +08:00
import { defineConfig, loadEnv } from "vite";
import path from "path";
import plugins from "./.build/plugins";
2026-02-01 00:30:44 +08:00
import { APP_VERSION, APP_NAME } from "./src/config/version";
2025-07-03 17:13:21 +08:00
2025-06-17 22:37:37 +08:00
// https://vite.dev/config/
export default defineConfig((cnf) => {
2025-07-03 17:13:21 +08:00
2025-06-17 22:37:37 +08:00
const { mode } = cnf;
const env = loadEnv(mode, process.cwd());
const { VITE_APP_ENV } = env;
return {
2026-02-01 00:30:44 +08:00
// 注入全局常量,供 index.html 和项目代码使用
define: {
__APP_VERSION__: JSON.stringify(APP_VERSION),
__APP_NAME__: JSON.stringify(APP_NAME),
},
2025-06-17 22:37:37 +08:00
base: VITE_APP_ENV === "production" ? "/" : "/",
plugins: plugins(cnf),
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
2025-12-17 12:10:24 +08:00
"@components": path.resolve(__dirname, "./src/vue-element-plus-y/components"),
2025-06-17 22:37:37 +08:00
},
},
css: {
// css全局变量使用@/styles/variable.scss文件
preprocessorOptions: {
scss: {
additionalData: '@use "@/styles/var.scss" as *;',
},
},
},
2025-06-19 23:45:22 +08:00
2026-02-01 00:30:44 +08:00
// 构建优化配置
build: {
target: 'es2015',
cssTarget: 'chrome80',
// 代码分割策略
rollupOptions: {
output: {
// 分包策略 - 更精细的分割以提高加载速度
manualChunks: (id) => {
// Vue 核心库
if (id.includes('node_modules/vue/') || id.includes('node_modules/@vue/') || id.includes('node_modules/vue-router/')) {
return 'vue-vendor';
}
// Pinia 状态管理
if (id.includes('node_modules/pinia/')) {
return 'pinia';
}
// Element Plus UI 库
if (id.includes('node_modules/element-plus/') || id.includes('node_modules/@element-plus/')) {
return 'element-plus';
}
// Markdown 相关
if (id.includes('node_modules/unified/') || id.includes('node_modules/remark-') || id.includes('node_modules/rehype-') || id.includes('node_modules/marked/')) {
return 'markdown';
}
// 工具库
if (id.includes('node_modules/lodash-es/') || id.includes('node_modules/@vueuse/')) {
return 'utils';
}
// 代码高亮
if (id.includes('node_modules/highlight.js/') || id.includes('node_modules/shiki/')) {
return 'highlight';
}
// 图表库
if (id.includes('node_modules/echarts/')) {
return 'echarts';
}
// PDF 处理
if (id.includes('node_modules/pdfjs-dist/')) {
return 'pdf';
}
// 其他第三方库
if (id.includes('node_modules/')) {
return 'vendor';
}
},
// 文件命名
chunkFileNames: 'js/[name]-[hash].js',
entryFileNames: 'js/[name]-[hash].js',
assetFileNames: (assetInfo) => {
const name = assetInfo.name || '';
if (name.endsWith('.css')) {
return 'css/[name]-[hash][extname]';
}
if (/\.(png|jpe?g|gif|svg|webp|ico)$/.test(name)) {
return 'images/[name]-[hash][extname]';
}
if (/\.(woff2?|eot|ttf|otf)$/.test(name)) {
return 'fonts/[name]-[hash][extname]';
}
return '[ext]/[name]-[hash][extname]';
},
},
},
// 压缩配置
minify: 'terser',
terserOptions: {
compress: {
drop_console: VITE_APP_ENV === 'production',
drop_debugger: VITE_APP_ENV === 'production',
pure_funcs: VITE_APP_ENV === 'production' ? ['console.log', 'console.info'] : [],
},
},
// chunk 大小警告限制
chunkSizeWarningLimit: 1000,
// 启用 CSS 代码分割
cssCodeSplit: true,
// 构建后是否生成 source map
sourcemap: VITE_APP_ENV !== 'production',
},
// 性能优化
optimizeDeps: {
include: [
'vue',
'vue-router',
'pinia',
'element-plus',
'@element-plus/icons-vue',
'lodash-es',
'@vueuse/core',
'@fortawesome/vue-fontawesome',
'@fortawesome/fontawesome-svg-core',
'@fortawesome/free-solid-svg-icons',
],
// 强制预构建依赖
force: false,
// 排除不需要优化的依赖
exclude: [],
},
// 预加载设置
preview: {
// 预览服务配置
},
// 实验性功能
experimental: {
// 启用渲染内联 CSS提高首次加载速度
renderBuiltUrl(filename, { hostType }) {
// 生产环境使用相对路径
if (hostType === 'js') {
return { runtime: `window.__assetsPath(${JSON.stringify(filename)})` };
}
return { relative: true };
},
},
2025-06-19 23:45:22 +08:00
server: {
2025-06-22 19:09:13 +08:00
port: 17001,
open: true,
2025-06-19 23:45:22 +08:00
proxy: {
2025-06-22 19:09:13 +08:00
[env.VITE_WEB_BASE_API]: {
target: env.VITE_API_URL,
changeOrigin: true,
rewrite: (path) => path.replace(`${[env.VITE_WEB_BASE_API]}`, ""),
//查看真实代理url
bypass(req, res, options) {
//@ts-ignore
const proxyUrl = new URL(options.rewrite(req.url) || '',(options.target)as string)?.href || '';
req.headers['x-req-proxyUrl'] = proxyUrl
res.setHeader('x-res-proxyUrl',proxyUrl)
},
}
2025-06-19 23:45:22 +08:00
},
},
2025-06-17 22:37:37 +08:00
};
});