vite.config.ts
中的server配置部分示例
| import type { UserConfig, ConfigEnv } from 'vite'; |
| import { loadEnv } from 'vite'; |
| |
| import { createProxy } from './build/vite/proxy'; |
| |
| export default ({ command, mode }: ConfigEnv): UserConfig => { |
| const root = process.cwd(); |
| const env = loadEnv(mode, root); |
| |
| return { |
| ... |
| server: { |
| port: env.VITE_PORT, |
| |
| proxy: createProxy(VITE_PROXY), |
| }, |
| ... |
| }; |
| VITE_PROXY = [["/api/v1","http://localhost:3000"],["/upload","http://localhost:3001/upload"]] |
| import type { ProxyOptions } from 'vite'; |
| type ProxyItem = [string, string]; |
| type ProxyList = ProxyItem[]; |
| type ProxyTargetList = Record<string, ProxyOptions & { rewrite: (path: string) => string }>; |
| |
| |
| |
| |
| import type { ProxyOptions } from 'vite'; |
| |
| type ProxyItem = [string, string]; |
| |
| type ProxyList = ProxyItem[]; |
| |
| type ProxyTargetList = Record<string, ProxyOptions & { rewrite: (path: string) => string }>; |
| |
| const httpsRE = /^https:\/\//; |
| |
| |
| |
| |
| |
| export function createProxy(list: ProxyList = []) { |
| const ret: ProxyTargetList = {}; |
| for (const [prefix, target] of list) { |
| const isHttps = httpsRE.test(target); |
| |
| |
| ret[prefix] = { |
| target: target, |
| changeOrigin: true, |
| ws: true, |
| rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''), |
| |
| ...(isHttps ? { secure: false } : {}), |
| }; |
| } |
| return ret; |
| } |
| import { OUTPUT_DIR } from './build/constant'; |
| |
| ... |
| build: { |
| target: 'es2015', |
| outDir: OUTPUT_DIR, |
| terserOptions: { |
| compress: { |
| keep_infinity: true, |
| // Used to delete console in production environment |
| drop_console: Boolean(env.VITE_DROP_CONSOLE), |
| }, |
| }, |
| // Turning off brotliSize display can slightly reduce packaging time |
| brotliSize: false, |
| chunkSizeWarningLimit: 1500, |
| }, |
https://cn.vitejs.dev/config/#build-terseroptions
| css: { |
| preprocessorOptions: { |
| less: { |
| modifyVars: generateModifyVars(), |
| javascriptEnabled: true, |
| }, |
| }, |
| }, |
所谓的“依赖预构建”。这个过程有两个目的:
- CommonJS 和 UMD 兼容性: 开发阶段中,Vite 的开发服务器将所有代码视为原生 ES 模块。因此,Vite 必须先将作为 CommonJS 或 UMD 发布的依赖项转换为 ESM。当转换 CommonJS 依赖时,Vite 会执行智能导入分析,这样即使导出是动态分配的(如 React),按名导入也会符合预期效果
- 性能: Vite 将有许多内部模块的 ESM 依赖关系转换为单个模块,以提高后续页面加载性能。一些包将它们的 ES 模块构建作为许多单独的文件相互导入。例如,lodash-es 有超过 600 个内置模块!当我们执行 import { debounce } from ‘lodash-es’ 时,浏览器同时发出 600 多个 HTTP 请求!尽管服务器在处理这些请求时没有问题,但大量的请求会在浏览器端造成网络拥塞,导致页面的加载速度相当慢。通过预构建 lodash-es 成为一个模块,我们就只需要一个 HTTP 请求了!
| optimizeDeps: { |
| |
| include: [ |
| '@iconify/iconify', |
| 'ant-design-vue/es/locale/zh_CN', |
| 'moment/dist/locale/zh-cn', |
| 'ant-design-vue/es/locale/en_US', |
| 'moment/dist/locale/eu', |
| ], |
| exclude: ['vue-demi'], |
| }, |
| export function createVitePlugins(viteEnv: ImportMetaEnv, isBuild: boolean) { |
| const { |
| VITE_USE_IMAGEMIN, |
| VITE_USE_MOCK, |
| VITE_LEGACY, |
| VITE_BUILD_COMPRESS, |
| VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE, |
| } = viteEnv; |
| |
| const vitePlugins: (Plugin|Plugin[])[] = [ |
| |
| vue(), |
| ]; |
| |
| |
| VITE_LEGACY && isBuild && vitePlugins.push(legacy()); |
| |
| |
| vitePlugins.push(configHtmlPlugin(viteEnv, isBuild)); |