rsbuild.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import path from 'path'
  2. import { fileURLToPath } from 'url'
  3. import { defineConfig, loadEnv } from '@rsbuild/core'
  4. import { pluginReact } from '@rsbuild/plugin-react'
  5. import { tanstackRouter } from '@tanstack/router-plugin/rspack'
  6. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  7. export default defineConfig(({ envMode }) => {
  8. const env = loadEnv({ mode: envMode, prefixes: ['VITE_'] })
  9. const serverUrl =
  10. process.env.VITE_REACT_APP_SERVER_URL ||
  11. env.rawPublicVars.VITE_REACT_APP_SERVER_URL ||
  12. 'http://localhost:3000'
  13. const isProd = envMode === 'production'
  14. const devProxy = Object.fromEntries(
  15. (['/api', '/mj', '/pg'] as const).map((key) => [
  16. key,
  17. { target: serverUrl, changeOrigin: true },
  18. ]),
  19. ) as Record<string, { target: string; changeOrigin: boolean }>
  20. return {
  21. plugins: [pluginReact()],
  22. // Rsbuild 2: replaces deprecated `performance.chunkSplit` (RSPack 2 aligned)
  23. splitChunks: {
  24. preset: 'default',
  25. cacheGroups: {
  26. 'vendor-react': {
  27. test: /node_modules[\\/](react|react-dom)[\\/]/,
  28. name: 'vendor-react',
  29. chunks: 'all',
  30. priority: 0,
  31. enforce: true,
  32. },
  33. 'vendor-radix': {
  34. test: /node_modules[\\/]@radix-ui[\\/]/,
  35. name: 'vendor-radix',
  36. chunks: 'all',
  37. priority: 0,
  38. enforce: true,
  39. },
  40. 'vendor-tanstack': {
  41. test: /node_modules[\\/]@tanstack[\\/]/,
  42. name: 'vendor-tanstack',
  43. chunks: 'all',
  44. priority: 0,
  45. enforce: true,
  46. },
  47. },
  48. },
  49. source: {
  50. entry: {
  51. index: './src/main.tsx',
  52. },
  53. },
  54. resolve: {
  55. alias: {
  56. '@': path.resolve(__dirname, './src'),
  57. },
  58. },
  59. html: {
  60. template: './index.html',
  61. },
  62. server: {
  63. host: '0.0.0.0',
  64. proxy: devProxy,
  65. },
  66. output: {
  67. // Production optimizations
  68. minify: isProd,
  69. target: 'web',
  70. distPath: {
  71. root: 'dist',
  72. },
  73. // Rely on Rsbuild default legalComments ("linked" → per-chunk *.LICENSE.txt) in all modes.
  74. // Do not set "none" in production: that strips minifier-preserved third-party notices and
  75. // extracted license files, which some distributions require for open-source compliance.
  76. },
  77. performance: {
  78. // Remove console in production
  79. removeConsole: isProd ? ['log'] : false,
  80. // Speed up repeated `rsbuild build` (local + CI when node_modules/.cache is preserved).
  81. // @see https://v2.rsbuild.dev/config/performance/build-cache
  82. buildCache: {
  83. cacheDigest: [process.env.VITE_REACT_APP_VERSION],
  84. },
  85. },
  86. tools: {
  87. rspack: {
  88. plugins: [
  89. tanstackRouter({
  90. target: 'react',
  91. // Dev: avoid per-route async chunks (reduces white flash on navigation + faster HMR feedback).
  92. // Prod: keep route-based code splitting.
  93. autoCodeSplitting: isProd,
  94. }),
  95. ],
  96. },
  97. },
  98. }
  99. })