vite.config.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import path from 'path';
  2. import commonjs from 'vite-plugin-commonjs'
  3. import {
  4. defineConfig, loadEnv
  5. } from 'vite'
  6. import uni from '@dcloudio/vite-plugin-uni'
  7. import pkg from './package.json'
  8. // https://vitejs.dev/config/
  9. // FRP 内网穿透绕过中间件
  10. // FRP 默认拦截 URL 中包含 /node_modules/ 的请求返回 404
  11. // 此中间件:
  12. // 1. 将响应内容中的 /node_modules/ 替换为 /__deps__/(浏览器请求时避开 FRP 拦截)
  13. // 2. 将传入请求中的 /__deps__/ 还原为 /node_modules/(Vite 才能正确处理)
  14. function frpBypassMiddleware() {
  15. return {
  16. name: 'frp-bypass',
  17. configureServer(server) {
  18. server.middlewares.use((req, res, next) => {
  19. // 重写传入请求:/__deps__/ → /node_modules/
  20. if (req.url && req.url.includes('/__deps__/')) {
  21. req.url = req.url.replace(/\/__deps__\//g, '/node_modules/')
  22. }
  23. // 拦截传出响应,将 /node_modules/ 替换为 /__deps__/
  24. const origEnd = res.end
  25. const chunks = []
  26. res.write = function (chunk, ...args) {
  27. if (chunk != null) {
  28. chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)
  29. }
  30. return true
  31. }
  32. res.end = function (chunk, ...args) {
  33. if (chunk != null) {
  34. chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)
  35. }
  36. if (chunks.length === 0) {
  37. return origEnd.apply(res, arguments)
  38. }
  39. const fullBody = Buffer.concat(chunks)
  40. const ct = String(res.getHeader('content-type') || '')
  41. // 仅对文本类型响应进行替换
  42. if (/text|javascript|json|xml|css/.test(ct)) {
  43. const bodyStr = fullBody.toString('utf-8')
  44. if (bodyStr.includes('/node_modules/')) {
  45. const replaced = bodyStr.replace(/\/node_modules\//g, '/__deps__/')
  46. res.setHeader('content-length', String(Buffer.byteLength(replaced, 'utf-8')))
  47. return origEnd.call(res, replaced, ...args)
  48. }
  49. return origEnd.call(res, bodyStr, ...args)
  50. }
  51. // 非文本类型直接透传
  52. return origEnd.call(res, fullBody, ...args)
  53. }
  54. next()
  55. })
  56. },
  57. }
  58. }
  59. export default defineConfig(({ mode }) => {
  60. // 根据当前 mode 加载对应 .env 文件中的环境变量
  61. const env = loadEnv(mode, process.cwd(), 'VITE_')
  62. const apiTarget = env.VITE_APP_HOST || ''
  63. // 自定义 mode 列表,构建时追加到 outDir 末尾
  64. const customModes = ['', 'csxg', 'xnet', 'qyyst', 'qycrg']
  65. if (customModes.includes(mode)) {
  66. // 必须在 uni() 插件之前设置,这样 static/uni_modules 复制才会进子目录
  67. process.env.UNI_OUTPUT_DIR = `dist/build/h5/${mode}/ABC_${mode}_patientapp_${pkg.version}`
  68. } else {
  69. process.env.UNI_OUTPUT_DIR = `dist/build/h5/ABC_patientapp_${pkg.version}`
  70. }
  71. // 从环境变量获取输出目录(uni-app CLI 已经设置好)
  72. const outDir = process.env.UNI_OUTPUT_DIR
  73. return {
  74. build: {
  75. outDir,
  76. },
  77. plugins: [
  78. uni(),
  79. commonjs(),
  80. // frpBypassMiddleware(), // FRP内网穿透绕过,非FRP环境可注释
  81. // 强制覆盖 uni-app 设置的 build.outDir,确保 assets/index.html 也进 patientapp
  82. {
  83. name: 'force-outdir',
  84. enforce: 'post',
  85. config(config) {
  86. config.build = config.build || {};
  87. config.build.outDir = outDir;
  88. }
  89. },
  90. ],
  91. css: {
  92. preprocessorOptions: {
  93. scss: {
  94. api: 'modern-compiler' // 使用新的现代编译器API,避免旧版JS API弃用警告
  95. }
  96. }
  97. },
  98. resolve: {
  99. alias: {
  100. '@vue/shared': '@vue/shared/dist/shared.esm-bundler.js',
  101. '@': 'src',
  102. 'jweixin-module': path.resolve(__dirname, 'src/js_sdk/jweixin-module/index.js')
  103. }
  104. },
  105. server: {
  106. host: '0.0.0.0',
  107. allowedHosts: [
  108. 'frp.12123wzcx.com', // 允许所有 frp.12123wzcx.com 的子域名
  109. ],
  110. proxy: {
  111. '/wechatbdhealth': {
  112. target: apiTarget,
  113. changeOrigin: true,
  114. rewrite: (path) => path.replace('/wechatbdhealth', '/wechatbdhealth')
  115. },
  116. '/patientapp/images': {
  117. target: apiTarget,
  118. changeOrigin: true,
  119. rewrite: (path) => path.replace('/patientapp/images', '/images')
  120. },
  121. '/uploadFile': {
  122. target: apiTarget,
  123. changeOrigin: true,
  124. rewrite: (path) => path.replace('/uploadFile', '/uploadFile')
  125. },
  126. '/images': {
  127. target: apiTarget,
  128. changeOrigin: true,
  129. rewrite: (path) => path.replace('/images', '/images')
  130. },
  131. // '/MP_verify_mRdA5h8p5Phywrus.txt': {
  132. // target: 'http://localhost:81',
  133. // changeOrigin: true,
  134. // rewrite: (path) => path.replace('/MP_verify_mRdA5h8p5Phywrus.txt', '/MP_verify_mRdA5h8p5Phywrus.txt')
  135. // },
  136. '/ws/': {
  137. target: 'https://apis.map.qq.com',
  138. changeOrigin: true,
  139. rewrite: (path) => path.replace('/ws/', '/ws/')
  140. },
  141. '/fileUp/': {
  142. target: 'https://np.h03.p0551.com',
  143. changeOrigin: true,
  144. rewrite: (path) => path.replace('/fileUp/', '')
  145. },
  146. '/pingd': {
  147. target: 'https://pingtas.qq.com',
  148. changeOrigin: true,
  149. },
  150. }
  151. },
  152. }
  153. })