vite.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { defineConfig, type Plugin } from 'vite'
  2. import uni from '@dcloudio/vite-plugin-uni'
  3. import { existsSync, readFileSync } from 'node:fs'
  4. import { resolve } from 'node:path'
  5. import { parse as parseJsonc } from 'jsonc-parser'
  6. /**
  7. * 在 uni 内置逻辑读 pages.json 之前做一次校验。
  8. * 若文件缺失、为空或解析失败,@dcloudio 内部会对 undefined 访问 .subPackages,报
  9. * 「Cannot read property 'subPackages' of undefined」,此处抛出更可读的错误。
  10. */
  11. function assertPagesJsonParsable(): Plugin {
  12. return {
  13. name: 'assert-pages-json-parsable',
  14. enforce: 'pre',
  15. configResolved() {
  16. const inputDir = process.env.UNI_INPUT_DIR || resolve(process.cwd(), 'src')
  17. const pagesPath = resolve(inputDir, 'pages.json')
  18. if (!existsSync(pagesPath)) {
  19. throw new Error(`[ErpMini] 未找到 pages.json:${pagesPath}`)
  20. }
  21. const raw = readFileSync(pagesPath, 'utf8')
  22. if (!raw.trim()) {
  23. throw new Error(`[ErpMini] pages.json 为空:${pagesPath}`)
  24. }
  25. const parsed = parseJsonc(raw)
  26. if (parsed === undefined || parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
  27. throw new Error(`[ErpMini] pages.json 解析失败或根节点非法:${pagesPath}`)
  28. }
  29. }
  30. }
  31. }
  32. export default defineConfig({
  33. plugins: [assertPagesJsonParsable(), uni()],
  34. transpileDependencies: ['uview-plus'],
  35. css: {
  36. preprocessorOptions: {
  37. scss: {
  38. silenceDeprecations: ['legacy-js-api', 'color-functions', 'import']
  39. }
  40. }
  41. },
  42. resolve: {
  43. /** 避免主包/分包各打一份 vue、pinia,导致 defineStore 产物异常(如 useXxxStore is not a function) */
  44. dedupe: ['vue', 'pinia'],
  45. alias: {
  46. '@': resolve(__dirname, 'src')
  47. }
  48. },
  49. server: {
  50. port: 9011,
  51. host: '0.0.0.0',
  52. /**
  53. * H5 调试:后端 jeesharp.web.adminPath=/admin,登录等白名单为 /admin/login。
  54. * 使用方式:`.env.development` 中设置 `VITE_API_BASE_URL=http://127.0.0.1:9011/admin`,
  55. * 可选 `VITE_DEV_API_TARGET=http://127.0.0.1:34560`(默认即本机 34560 后端)。
  56. */
  57. proxy: {
  58. '/admin': {
  59. target: process.env.VITE_DEV_API_TARGET || 'http://127.0.0.1:34560',
  60. changeOrigin: true,
  61. secure: false
  62. }
  63. }
  64. }
  65. })