rsbuild.config.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import path from 'path'
  2. import { createRequire } from 'module'
  3. import { fileURLToPath } from 'url'
  4. import { defineConfig, loadEnv } from '@rsbuild/core'
  5. import { pluginReact } from '@rsbuild/plugin-react'
  6. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  7. const require = createRequire(import.meta.url)
  8. const semiUiDir = path.resolve(
  9. path.dirname(require.resolve('@douyinfe/semi-ui')),
  10. '../..',
  11. )
  12. export default defineConfig(({ envMode }) => {
  13. const env = loadEnv({ mode: envMode, prefixes: ['VITE_'] })
  14. const clientServerUrl =
  15. process.env.VITE_REACT_APP_SERVER_URL ||
  16. env.rawPublicVars.VITE_REACT_APP_SERVER_URL ||
  17. ''
  18. const proxyServerUrl =
  19. clientServerUrl ||
  20. 'http://localhost:3000'
  21. const isProd = envMode === 'production'
  22. const devProxy = Object.fromEntries(
  23. (['/api', '/mj', '/pg'] as const).map((key) => [
  24. key,
  25. { target: proxyServerUrl, changeOrigin: true },
  26. ]),
  27. ) as Record<string, { target: string; changeOrigin: boolean }>
  28. return {
  29. plugins: [pluginReact()],
  30. source: {
  31. entry: {
  32. index: './src/index.jsx',
  33. },
  34. define: {
  35. 'import.meta.env.VITE_REACT_APP_SERVER_URL': JSON.stringify(
  36. clientServerUrl,
  37. ),
  38. },
  39. },
  40. resolve: {
  41. alias: {
  42. '@': path.resolve(__dirname, './src'),
  43. '@douyinfe/semi-ui/dist/css/semi.css': path.resolve(
  44. semiUiDir,
  45. 'dist/css/semi.css',
  46. ),
  47. },
  48. },
  49. html: {
  50. template: './index.html',
  51. },
  52. server: {
  53. host: '0.0.0.0',
  54. strictPort: false,
  55. proxy: devProxy,
  56. },
  57. output: {
  58. minify: isProd,
  59. target: 'web',
  60. distPath: {
  61. root: 'dist',
  62. },
  63. },
  64. performance: {
  65. removeConsole: isProd ? ['log'] : false,
  66. buildCache: {
  67. cacheDigest: [process.env.VITE_REACT_APP_VERSION],
  68. },
  69. },
  70. tools: {
  71. rspack: {
  72. module: {
  73. rules: [
  74. {
  75. test: /src[\\/].*\.js$/,
  76. type: 'javascript/auto',
  77. use: [
  78. {
  79. loader: 'builtin:swc-loader',
  80. options: {
  81. jsc: {
  82. parser: {
  83. syntax: 'ecmascript',
  84. jsx: true,
  85. },
  86. transform: {
  87. react: {
  88. runtime: 'automatic',
  89. development: !isProd,
  90. refresh: !isProd,
  91. },
  92. },
  93. },
  94. },
  95. },
  96. ],
  97. },
  98. ],
  99. },
  100. },
  101. },
  102. }
  103. })