import { defineConfig } from 'vite' import uni from '@dcloudio/vite-plugin-uni' import path from 'node:path' import { fileURLToPath } from 'node:url' // 新版 Vite / uni 编译器会把配置文件预打包成 CJS 加载,此时 import.meta.url 为 undefined, // fileURLToPath(undefined) 会抛 "The path argument must be of type string. Received undefined"。 // 这里做兜底:拿不到模块路径时退回 __dirname / 当前工作目录(构建命令均在 uniapp 目录执行)。 function resolveRootDir() { try { const url = typeof import.meta !== 'undefined' ? import.meta.url : '' if (url) return path.dirname(fileURLToPath(url)) } catch (e) { /* import.meta.url 不可用时继续兜底 */ } try { if (typeof __dirname !== 'undefined') return __dirname } catch (e) { /* 忽略 */ } return process.cwd() } const rootDir = resolveRootDir() export default defineConfig({ base: '/h5/', plugins: [uni()], resolve: { alias: { '@': rootDir, }, }, server: { host: '127.0.0.1', port: 4403, strictPort: true, proxy: { // H5 开发:api/config.js 默认 baseURL 为 /proxy-api,转发到 Go 后端 '/proxy-api': { // 本地联调统一认证(/app/home 等新接口生产未部署时用本地) target: 'http://127.0.0.1:9000', // target: 'https://api.yunzer.cn', changeOrigin: true, rewrite: (path) => path.replace(/^\/proxy-api/, ''), }, }, }, css: { preprocessorOptions: { scss: { // 关闭废弃 API 警告 silenceDeprecations: ['legacy-js-api', 'color-functions', 'import'], }, }, }, })