import { fileURLToPath, URL } from 'node:url' import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { nodePolyfills } from 'vite-plugin-node-polyfills' /** 移除 crossorigin 属性,避免部分手机浏览器加载资源异常 */ function removeCrossorigin() { return { name: 'remove-crossorigin', transformIndexHtml(html: string) { return html.replace(/\s*crossorigin\s*/g, ' ') }, } } export default defineConfig({ resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)), }, }, plugins: [ vue(), nodePolyfills({ protocolImports: true, }), removeCrossorigin(), ], define: { 'process.env': {}, }, build: { target: 'es2020', cssTarget: 'chrome64', }, server: { host: true, proxy: { '/api': { target: 'http://localhost:8888', // 测试服务器地址,可根据实际后端地址修改 changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ''), // 如果后端接口本身没有 /api 前缀,需要将其重写掉 }, }, }, })