rsbuild.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { defineConfig,loadEnv } from '@rsbuild/core';
  2. import { pluginVue } from '@rsbuild/plugin-vue';
  3. import { pluginSass } from '@rsbuild/plugin-sass';
  4. import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
  5. import { generateRoutesInfoPlugin } from './plugins/generateRoutesInfoPlugin';
  6. import { generateExposesFromRoutes } from './plugins/generateExposesPlugin';
  7. import { zipDistPlugin } from './plugins/zipDistPlugin';
  8. import { routes } from './src/router/routes';
  9. import pkg from './package.json';
  10. // Docs: https://rsbuild.rs/config/
  11. export default defineConfig((env:any) => {
  12. // 生成 Module Federation exposes 配置(只展示叶子节点)
  13. const { exposes } = generateExposesFromRoutes(routes, { onlyLeafNodes: true });
  14. const nodeEnv = env?.env || 'development';
  15. // 加载环境变量
  16. const envVars = loadEnv(nodeEnv) as any;
  17. const distDir = 'dist';
  18. return {
  19. source: {
  20. // 配置路径别名
  21. alias: {
  22. '@': './src',
  23. },
  24. },
  25. server: {
  26. port: 3000,
  27. proxy: {
  28. '/api': {
  29. target: 'https://qysgl.mwr.cn/wr_web_manager/api',
  30. changeOrigin: true,
  31. pathRewrite: { '^/api': '' },
  32. },
  33. },
  34. },
  35. dev: {
  36. // writeToDisk 仅 MF 宿主加载远程入口时需要,独立 dev 关闭可避免 JS 请求被 SPA fallback 拦截
  37. writeToDisk: false,
  38. },
  39. html: {
  40. template: './index.html',
  41. mountId: 'root',
  42. },
  43. output: {
  44. assetPrefix: 'auto',
  45. injectStyles: true, // ← 新增:将 CSS 内联到 JS 中
  46. // 🚀 使用 VITE_API_BASE_NAME 作为输出目录名称
  47. distPath: {
  48. root: distDir,
  49. },
  50. },
  51. plugins: [
  52. pluginVue(),
  53. pluginSass(),
  54. pluginModuleFederation({
  55. name: pkg.name,
  56. getPublicPath: `function() { try { if (window.__MF_REMOTE_BASE_URL__ && window.__MF_REMOTE_BASE_URL__["${pkg.name}"]) { return window.__MF_REMOTE_BASE_URL__["${pkg.name}"]; } console.warn("__MF_REMOTE_BASE_URL__ not found, using fallback"); } catch (e) { console.warn("Failed to get publicPath from global variable:", e); } return window.location.origin + "/"; }`,
  57. // 🚀 根据路由自动生成 exposes 配置(只展示叶子节点)
  58. exposes: exposes,
  59. // ⭐ 完全禁用 DTS 插件
  60. dts: false,
  61. shared: {
  62. vue: {
  63. singleton: true,
  64. requiredVersion: '^3.5.26',
  65. },
  66. 'vue-router': {
  67. singleton: true,
  68. requiredVersion: '^4.6.4',
  69. },
  70. pinia: {
  71. singleton: true,
  72. requiredVersion: '^3.0.4',
  73. },
  74. 'ant-design-vue': {
  75. singleton: true,
  76. requiredVersion: '^4.2.6',
  77. },
  78. },
  79. }),
  80. // 自动生成 routes-info.json 插件
  81. generateRoutesInfoPlugin(),
  82. // 📦 最后执行:打包成 zip 并删除原始目录
  83. zipDistPlugin(distDir),
  84. ],
  85. };
  86. });