import { defineConfig, type Plugin } from 'vite' import uni from '@dcloudio/vite-plugin-uni' import { existsSync, readFileSync } from 'node:fs' import { resolve } from 'node:path' import { parse as parseJsonc } from 'jsonc-parser' /** * 在 uni 内置逻辑读 pages.json 之前做一次校验。 * 若文件缺失、为空或解析失败,@dcloudio 内部会对 undefined 访问 .subPackages,报 * 「Cannot read property 'subPackages' of undefined」,此处抛出更可读的错误。 */ function assertPagesJsonParsable(): Plugin { return { name: 'assert-pages-json-parsable', enforce: 'pre', configResolved() { const inputDir = process.env.UNI_INPUT_DIR || resolve(process.cwd(), 'src') const pagesPath = resolve(inputDir, 'pages.json') if (!existsSync(pagesPath)) { throw new Error(`[ErpMini] 未找到 pages.json:${pagesPath}`) } const raw = readFileSync(pagesPath, 'utf8') if (!raw.trim()) { throw new Error(`[ErpMini] pages.json 为空:${pagesPath}`) } const parsed = parseJsonc(raw) if (parsed === undefined || parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) { throw new Error(`[ErpMini] pages.json 解析失败或根节点非法:${pagesPath}`) } } } } export default defineConfig({ plugins: [assertPagesJsonParsable(), uni()], transpileDependencies: ['uview-plus'], css: { preprocessorOptions: { scss: { silenceDeprecations: ['legacy-js-api', 'color-functions', 'import'] } } }, resolve: { /** 避免主包/分包各打一份 vue、pinia,导致 defineStore 产物异常(如 useXxxStore is not a function) */ dedupe: ['vue', 'pinia'], alias: { '@': resolve(__dirname, 'src') } }, server: { port: 9011, host: '0.0.0.0', /** * H5 调试:后端 jeesharp.web.adminPath=/admin,登录等白名单为 /admin/login。 * 使用方式:`.env.development` 中设置 `VITE_API_BASE_URL=http://127.0.0.1:9011/admin`, * 可选 `VITE_DEV_API_TARGET=http://127.0.0.1:34560`(默认即本机 34560 后端)。 */ proxy: { '/admin': { target: process.env.VITE_DEV_API_TARGET || 'http://127.0.0.1:34560', changeOrigin: true, secure: false } } } })