rsbuild.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
  2. import { defineConfig, loadEnv } from '@rsbuild/core';
  3. import { pluginBabel } from '@rsbuild/plugin-babel';
  4. import { pluginLess } from '@rsbuild/plugin-less';
  5. import { pluginSass } from '@rsbuild/plugin-sass';
  6. import { pluginVue } from '@rsbuild/plugin-vue';
  7. import pkg from './package.json';
  8. import { generateExposesFromRoutes } from './plugins/generateExposesPlugin';
  9. import { zipDistPlugin } from './plugins/zipDistPlugin';
  10. import { routes } from './src/router/routes';
  11. export default defineConfig((env: any) => {
  12. const nodeEnv = env?.env || 'development';
  13. const envVars = loadEnv(nodeEnv) as any;
  14. const distDir = 'dist';
  15. const isDev = nodeEnv !== 'production';
  16. const { exposes } = generateExposesFromRoutes(routes, {
  17. onlyLeafNodes: true,
  18. });
  19. // 宿主在加载任何页面之前先加载 bootstrap 以完成登录初始化
  20. exposes['./bootstrap'] = './src/bootstrap.ts';
  21. const backendHost =
  22. envVars.parsed?.VITE_API_BASE_HOST || 'http://10.15.0.138:26278';
  23. const plugins: any[] = [
  24. pluginVue({
  25. vueLoaderOptions: {
  26. compilerOptions: {
  27. expressionPlugins: ['typescript'],
  28. },
  29. },
  30. }),
  31. pluginLess({
  32. lessLoaderOptions: {
  33. lessOptions: {
  34. javascriptEnabled: true,
  35. },
  36. },
  37. }),
  38. pluginSass(),
  39. pluginBabel({
  40. include: /\.(?:vue|jsx|tsx)$/,
  41. babelLoaderOptions: {
  42. plugins: ['@vue/babel-plugin-jsx'],
  43. },
  44. }),
  45. ];
  46. if (!isDev) {
  47. plugins.push(
  48. pluginModuleFederation({
  49. name: pkg.name,
  50. 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 + "/"; }`,
  51. exposes: exposes,
  52. dts: false,
  53. shared: {
  54. vue: { singleton: true, requiredVersion: '^3.5.26' },
  55. 'vue-router': { singleton: true, requiredVersion: '^4.6.4' },
  56. pinia: { singleton: true, eager: true, requiredVersion: '^3.0.4' },
  57. },
  58. }),
  59. // routes-info.json 改为构建后由 script/generateRoutesInfo.mjs 生成
  60. // zipDistPlugin is now called separately via npm run zip
  61. );
  62. }
  63. return {
  64. resolve: {
  65. alias: {
  66. '@': './src',
  67. '@widget': './src/widgets',
  68. 'vue-video-player': './src/compat/VideoPlayer.vue',
  69. 'vue-video-player/src/custom-theme.css':
  70. './src/compat/custom-theme.css',
  71. },
  72. },
  73. server: {
  74. port: 3000,
  75. proxy: {
  76. '/pdcApi': {
  77. target: 'http://r5b9cae6.natappfree.cc',
  78. changeOrigin: true,
  79. pathRewrite: {
  80. '^/pdcApi': '',
  81. },
  82. },
  83. },
  84. },
  85. dev: {},
  86. output: {
  87. assetPrefix: 'auto',
  88. distPath: { root: distDir },
  89. },
  90. plugins,
  91. };
  92. });