aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/vite.config.ts
blob: 6771fca4ec7ae683b618a0cccad1811bbabbee33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import react from '@vitejs/plugin-react'
import { defineConfig, loadEnv } from 'vite'
import macros from 'unplugin-parcel-macros';
import optimizeLocales from '@react-aria/optimize-locales-plugin';

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  const routerMode = env.VITE_ROUTER_MODE ?? 'history'
  const base = env.VITE_BASE_URL ?? '/'

  if (routerMode !== 'hash' && routerMode !== 'history') {
    throw new Error('VITE_ROUTER_MODE must be "hash" or "history".')
  }
  if (!base.startsWith('/') || base.startsWith('//') || /[?#\\\s]/.test(base)) {
    throw new Error('VITE_BASE_URL must be an absolute pathname, such as "/" or "/app/".')
  }

  return {
    base: base.endsWith('/') ? base : `${base}/`,
    plugins: [
      macros.vite(), // Must be first!
      react(),
      {
        ...optimizeLocales.vite({ locales: ['en-US', 'zh-CN'] }),
        enforce: 'pre'
      }
    ],
    build: {
      target: ['es2022'],
      // Lightning CSS produces a much smaller CSS bundle than the default minifier.
      cssMinify: 'lightningcss',
      rollupOptions: {
        output: {
          // Bundle all S2 and style-macro generated CSS into a single bundle instead of code splitting.
          // Because atomic CSS has so much overlap between components, loading all CSS up front results in
          // smaller bundles instead of producing duplication between pages.
          manualChunks(id) {
            if (/macro-(.*)\.css$/.test(id) || /@react-spectrum\/s2\/.*\.css$/.test(id)) {
              return 's2-styles';
            }
          }
        }
      }
    }
  }
})