diff options
| author | shu <nvcsadjkojf@outlook.com> | 2026-09-07 16:00:42 +0800 |
|---|---|---|
| committer | shu <nvcsadjkojf@outlook.com> | 2026-09-07 16:00:42 +0800 |
| commit | be5b47eee4d7305e54677de45860d8b085f3cfec (patch) | |
| tree | f91db47aa2e2cf36788b9e715158a80a979749c9 /vite.config.ts | |
| parent | 481402e09fe760634b826b09e302e2e1bfec88b1 (diff) | |
Diffstat (limited to 'vite.config.ts')
| -rw-r--r-- | vite.config.ts | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/vite.config.ts b/vite.config.ts index c012b56..6771fca 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,30 +1,44 @@ import react from '@vitejs/plugin-react' -import { defineConfig } from 'vite' +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({ - 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'; +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'; + } } } } |
