// vite.config.ts import { defineConfig, loadEnv, type PluginOption } from 'vite' import path from 'path' import createVitePlugins from './vite/plugins' // 环境变量类型定义(可根据实际变量扩展) interface ViteEnv { VITE_APP_ENV?: 'production' | 'development' VITE_BUILD_COMPRESS?: string // ... 其他变量 } const baseUrl = 'http://localhost:8080' // 后端接口地址 // https://vitejs.dev/config/ export default defineConfig(({ mode, command }) => { // 加载环境变量并断言为自定义类型 const env = loadEnv(mode, process.cwd()) as unknown as ViteEnv const { VITE_APP_ENV } = env // 是否为生产构建 const isBuild = command === 'build' return { // 基础路径(可根据实际需要调整) base: VITE_APP_ENV === 'production' ? '/' : '/', plugins: [...(createVitePlugins(env, isBuild) as PluginOption[])], resolve: { tsconfigPaths: true, alias: { '~': path.resolve(__dirname, './') }, extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'] }, // Vite 8 构建配置(使用 rolldownOptions 替代 rollupOptions) build: { sourcemap: isBuild ? false : 'inline', outDir: 'dist', assetsDir: 'assets', chunkSizeWarningLimit: 2000, // ✅ 使用 rolldownOptions(兼容 Rollup 选项,但推荐使用新命名) rolldownOptions: { output: { // 命名规则与原来相同,Rolldown 支持这些配置 chunkFileNames: 'static/js/[name]-[hash].js', entryFileNames: 'static/js/[name]-[hash].js', assetFileNames: 'static/[ext]/[name]-[hash].[ext]' } } }, server: { port: 80, host: true, open: true, proxy: { '/dev-api': { target: baseUrl, changeOrigin: true, rewrite: p => p.replace(/^\/dev-api/, '') } } }, // CSS 配置(保持不变) css: { postcss: { plugins: [ { postcssPlugin: 'internal:charset-removal', AtRule: { charset: atRule => { if (atRule.name === 'charset') { atRule.remove() } } } } ] } } } })