Last active
August 1, 2025 15:30
-
-
Save TinsFox/95caf459d370b61aabfeb1085f9c4b31 to your computer and use it in GitHub Desktop.
bunder config of vite and rsbuild
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { defineConfig, loadEnv } from '@rsbuild/core'; | |
| import { pluginLess } from '@rsbuild/plugin-less'; | |
| import { pluginReact } from '@rsbuild/plugin-react'; | |
| import { getGitHash } from './scripts/lib'; | |
| const { publicVars } = loadEnv({ prefixes: ['VITE_'] }); | |
| // console.log('env', loadEnv()); | |
| export default defineConfig({ | |
| plugins: [pluginReact(), pluginLess()], | |
| source: { | |
| entry: { | |
| index: './src/main.tsx', | |
| }, | |
| define: { | |
| ...publicVars, | |
| GIT_COMMIT_SHA: JSON.stringify(getGitHash()) || new Date().toISOString(), | |
| }, | |
| }, | |
| html: { | |
| template: './index.html', | |
| }, | |
| server: { | |
| host: '0.0.0.0', | |
| port: 5178, | |
| proxy: { | |
| '/api': { | |
| target: process.env.VITE_API_BASE_URL, | |
| changeOrigin: true, | |
| pathRewrite: { '^/api': '' }, | |
| }, | |
| '/actor-api': { | |
| target: process.env.VITE_ACTOR_API_BASE_URL, | |
| changeOrigin: true, | |
| pathRewrite: { '^/actor-api': '' }, | |
| }, | |
| }, | |
| }, | |
| output: { | |
| distPath: { | |
| root: 'dist', | |
| }, | |
| minify: true, | |
| polyfill: 'entry', | |
| }, | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import react from '@vitejs/plugin-react'; | |
| import { defineConfig, loadEnv } from 'vite'; | |
| import { createHtmlPlugin } from 'vite-plugin-html'; | |
| import tsconfigPaths from 'vite-tsconfig-paths'; | |
| import { getGitHash } from './scripts/lib'; | |
| // https://vitejs.dev/config/ | |
| export default defineConfig(({ mode }) => { | |
| process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }; | |
| // 添加带颜色的环境信息打印 | |
| console.log('\n\x1b[36m=== 环境配置信息 ===\x1b[0m'); | |
| console.log(`\x1b[32m当前环境:\x1b[0m ${mode}`); | |
| console.log(`\x1b[32mAPI 基础地址:\x1b[0m ${process.env.VITE_API_BASE_URL}`); | |
| console.log(`\x1b[32mActor API 基础地址:\x1b[0m ${process.env.VITE_ACTOR_API_BASE_URL}`); | |
| console.log('\x1b[36m================\x1b[0m\n'); | |
| return { | |
| plugins: [ | |
| tsconfigPaths(), | |
| react(), | |
| createHtmlPlugin({ | |
| inject: { | |
| data: { | |
| title: process.env.VITE_APP_TITLE || 'Digi Plus Star', | |
| }, | |
| }, | |
| }), | |
| ], | |
| define: { | |
| GIT_COMMIT_SHA: JSON.stringify(getGitHash()) || new Date().toISOString(), // 如果获取不到 git hash,则使用当前时间戳 | |
| }, | |
| logLevel: 'info', | |
| server: { | |
| host: '0.0.0.0', | |
| port: 5178, | |
| proxy: { | |
| '/api': { | |
| target: process.env.VITE_API_BASE_URL, | |
| changeOrigin: true, | |
| rewrite: (path) => path.replace(/^\/api/, ''), | |
| }, | |
| '/actor-api': { | |
| target: process.env.VITE_ACTOR_API_BASE_URL, | |
| changeOrigin: true, | |
| rewrite: (path) => path.replace(/^\/actor-api/, ''), | |
| }, | |
| }, | |
| }, | |
| // build configure | |
| build: { | |
| outDir: 'dist', | |
| rollupOptions: { | |
| output: { | |
| manualChunks: (id) => { | |
| // 处理 node_modules 中的包 | |
| if (id.includes('node_modules')) { | |
| // React 核心包 | |
| if (id.includes('react') || id.includes('react-dom')) { | |
| return 'core-vendor'; | |
| } | |
| // UI 框架相关 | |
| if (id.includes('antd') || id.includes('@ant-design/cssinjs')) { | |
| return 'antd-basic-vendor'; | |
| } | |
| if (id.includes('@ant-design/icons')) { | |
| return 'antd-icons-vendor'; | |
| } | |
| if (id.includes('@ant-design/pro-components')) { | |
| return 'antd-pro-vendor'; | |
| } | |
| // 数据流相关 | |
| if (id.includes('@tanstack/react-query') || id.includes('jotai')) { | |
| return 'state-management-vendor'; | |
| } | |
| // 图表相关 | |
| if (id.includes('echarts') || id.includes('wordcloud')) { | |
| return 'chart-vendor'; | |
| } | |
| // 工具库 | |
| if ( | |
| id.includes('lodash') || | |
| id.includes('dayjs') || | |
| id.includes('uuid') || | |
| id.includes('classnames') || | |
| id.includes('clsx') || | |
| id.includes('nanoid') | |
| ) { | |
| return 'utils-vendor'; | |
| } | |
| // 数据处理相关 | |
| if (id.includes('draft-js') || id.includes('@draft-js-plugins')) { | |
| return 'editor-vendor'; | |
| } | |
| // 动画相关 | |
| if ( | |
| id.includes('framer-motion') || | |
| id.includes('motion') || | |
| id.includes('rc-queue-anim') || | |
| id.includes('rc-tween-one') | |
| ) { | |
| return 'animation-vendor'; | |
| } | |
| // 样式相关 | |
| if (id.includes('@emotion') || id.includes('tailwind')) { | |
| return 'styles-vendor'; | |
| } | |
| // i18n 相关 | |
| if (id.includes('i18next') || id.includes('react-i18next')) { | |
| return 'i18n-vendor'; | |
| } | |
| // 路由相关 | |
| if (id.includes('react-router')) { | |
| return 'router-vendor'; | |
| } | |
| // React 组件库 | |
| if (id.includes('react-') && !id.includes('react-dom')) { | |
| return 'react-components-vendor'; | |
| } | |
| } | |
| }, | |
| }, | |
| }, | |
| // 适当调整警告阈值 | |
| chunkSizeWarningLimit: 600, | |
| // 构建优化 | |
| minify: 'terser', | |
| terserOptions: { | |
| compress: { | |
| drop_console: true, | |
| drop_debugger: true, | |
| }, | |
| }, | |
| // 启用 gzip 压缩 | |
| reportCompressedSize: true, | |
| }, | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment