webpack.config.devserver.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. const _ = require('lodash');
  2. const errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware');
  3. const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware');
  4. const paths = require('./paths');
  5. const path = require('path');
  6. const express = require('express');
  7. const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
  8. const hostt = process.env.HOST || '0.0.0.0';
  9. let apiRootPath = process.env._SETTING_API_BASE_PATH || '';
  10. if (!_.startsWith(apiRootPath, '/')) {
  11. apiRootPath = '/' + apiRootPath;
  12. }
  13. const contextPath = process.env._SETTING_CONTEXT_PATH;
  14. /* eslint-disable func-names */
  15. module.exports = function(proxy, allowedHost) {
  16. return {
  17. port: 3000,
  18. // 禁止主机检测,不检查主机的应用程序容易受到DNS重新绑定攻击
  19. disableHostCheck: true,
  20. // 启用gzip 压缩
  21. compress: true,
  22. clientLogLevel: 'none',
  23. // 提供静态内容的服务目录
  24. contentBase: paths.appPublic,
  25. // 静态内容服务目录文件变更时,触发页面重新加载
  26. watchContentBase: true,
  27. // 启用 webpack 的模块热替换特性
  28. hot: true,
  29. publicPath: _.isEmpty(contextPath) ? '' : `/${contextPath}/`,
  30. // 除了初始启动信息之外的任何内容都不会被打印到控制台。 webpack 的错误或警告在控制台不可见
  31. quiet: true,
  32. progress: true,
  33. watchOptions: {
  34. ignored: /node_modules/,
  35. },
  36. // 在响应中添加Header内容:
  37. headers: {
  38. 'Access-Control-Allow-Origin': '*',
  39. 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
  40. 'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization',
  41. },
  42. https: protocol === 'https',
  43. host: hostt,
  44. overlay: false,
  45. historyApiFallback: {
  46. verbose: false,
  47. disableDotRule: true,
  48. index: _.isEmpty(contextPath) ? '/' : `/${contextPath}/`,
  49. },
  50. public: allowedHost,
  51. proxy: process.env._SETTING_ENABLE_PROXY_API
  52. ? {
  53. [`${apiRootPath}bc/**`]: {
  54. target: process.env._SETTING_SERVER_PROXY_BC,
  55. changeOrigin: true,
  56. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_BC && {
  57. pathRewrite: {
  58. [`^${apiRootPath}bc/`]: '/',
  59. },
  60. }),
  61. secure: false,
  62. // logLevel: 'debug',
  63. },
  64. [`${apiRootPath}uc/**`]: {
  65. target: process.env._SETTING_SERVER_PROXY_UC,
  66. changeOrigin: true,
  67. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_UC && {
  68. pathRewrite: {
  69. [`^${apiRootPath}uc/`]: `/`,
  70. },
  71. }),
  72. secure: false,
  73. // logLevel: 'debug',
  74. },
  75. [`${apiRootPath}uc/oauth/zj4a/login`]: {
  76. target: process.env._SETTING_LOGIN_URL,
  77. changeOrigin: true,
  78. secure: false,
  79. },
  80. [`${apiRootPath}dsem/**`]: {
  81. target: process.env._SETTING_SERVER_PROXY_DSEM,
  82. changeOrigin: true,
  83. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_DSEM &&
  84. {
  85. // pathRewrite: {
  86. // [`^${apiRootPath}dsem/`]: '/',
  87. // },
  88. }),
  89. secure: false,
  90. // logLevel: 'debug',
  91. },
  92. [`${apiRootPath}login/**`]: {
  93. target: process.env._SETTING_SERVER_PROXY,
  94. changeOrigin: true,
  95. secure: false,
  96. // logLevel: 'debug',
  97. },
  98. [`${apiRootPath}ocr/**`]: {
  99. target: process.env._SETTING_SERVER_PROXY_OCR,
  100. changeOrigin: true,
  101. secure: false,
  102. // logLevel: 'debug',
  103. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_OCR && {
  104. pathRewrite: {
  105. [`^${apiRootPath}ocr/`]: '/',
  106. },
  107. }),
  108. },
  109. [`${apiRootPath}appscene/**`]: {
  110. target: process.env._SETTING_SERVER_PROXY_APPSCENE,
  111. changeOrigin: true,
  112. secure: false,
  113. // logLevel: 'debug',
  114. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_APPSCENE && {
  115. pathRewrite: {
  116. [`^${apiRootPath}appscene/`]: '/',
  117. },
  118. }),
  119. },
  120. [`${apiRootPath}ammc/**`]: {
  121. target: process.env._SETTING_SERVER_PROXY_REPO,
  122. changeOrigin: true,
  123. secure: false,
  124. // logLevel: 'debug',
  125. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_REPO && {
  126. pathRewrite: {
  127. [`^${apiRootPath}ammc/`]: '/',
  128. },
  129. }),
  130. },
  131. [`${apiRootPath}soc/**`]: {
  132. target: process.env._SETTING_SERVER_PROXY_SC,
  133. changeOrigin: true,
  134. secure: false,
  135. // logLevel: 'debug',
  136. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_SC && {
  137. pathRewrite: {
  138. [`^${apiRootPath}soc/`]: '/',
  139. },
  140. }),
  141. },
  142. [`${apiRootPath}sic/**`]: {
  143. target: process.env._SETTING_SERVER_PROXY_SIC,
  144. changeOrigin: true,
  145. secure: false,
  146. // logLevel: 'debug',
  147. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_SIC && {
  148. pathRewrite: {
  149. [`^${apiRootPath}sic/`]: '/',
  150. },
  151. }),
  152. },
  153. [`${apiRootPath}biz/**`]: {
  154. target: process.env._SETTING_SERVER_PROXY_OPT,
  155. changeOrigin: true,
  156. secure: false,
  157. // logLevel: 'debug',
  158. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_OPT && {
  159. pathRewrite: {
  160. [`^${apiRootPath}biz/`]: '/',
  161. },
  162. }),
  163. },
  164. [`${apiRootPath}rcc/**`]: {
  165. target: process.env._SETTING_SERVER_PROXY_RES,
  166. changeOrigin: true,
  167. secure: false,
  168. // logLevel: 'debug',
  169. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_RES && {
  170. pathRewrite: {
  171. [`^${apiRootPath}rcc/`]: '/',
  172. },
  173. }),
  174. },
  175. [`${apiRootPath}anb/**`]: {
  176. target: process.env._SETTING_SERVER_PROXY_ANB,
  177. changeOrigin: true,
  178. secure: false,
  179. // logLevel: 'debug',
  180. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_ANB && {
  181. pathRewrite: {
  182. [`^${apiRootPath}anb/`]: '/',
  183. },
  184. }),
  185. },
  186. [`${apiRootPath}scc/**`]: {
  187. target: process.env._SETTING_SERVER_PROXY_APIS,
  188. changeOrigin: true,
  189. secure: false,
  190. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_APIS && {
  191. pathRewrite: {
  192. [`^${apiRootPath}scc/`]: '/',
  193. },
  194. }),
  195. },
  196. [`${apiRootPath}mc/**`]: {
  197. target: process.env._SETTING_SERVER_PROXY_MC,
  198. changeOrigin: true,
  199. secure: false,
  200. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_MC && {
  201. pathRewrite: {
  202. [`^${apiRootPath}mc/`]: '/',
  203. },
  204. }),
  205. },
  206. [`${apiRootPath}project/**`]: {
  207. target: process.env._SETTING_SERVER_PROXY_PROJECT,
  208. changeOrigin: true,
  209. secure: false,
  210. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_PROJECT && {
  211. pathRewrite: {
  212. [`^${apiRootPath}project/`]: '/',
  213. },
  214. }),
  215. },
  216. [`${apiRootPath}route/**`]: {
  217. target: process.env._SETTING_SERVER_PROXY_ROUTE,
  218. changeOrigin: true,
  219. secure: false,
  220. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_ROUTE && {
  221. pathRewrite: {
  222. [`^${apiRootPath}route/`]: '/',
  223. },
  224. }),
  225. },
  226. [`${apiRootPath}upload/**`]: {
  227. target: process.env._SETTING_SERVER_PROXY_UPLOAD,
  228. changeOrigin: true,
  229. secure: false,
  230. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_UPLOAD && {
  231. pathRewrite: {
  232. [`^${apiRootPath}upload/`]: '/',
  233. },
  234. }),
  235. },
  236. [`${apiRootPath}codemodel/**`]: {
  237. target: process.env._SETTING_SERVER_PROXY_CODE_MODEL,
  238. changeOrigin: true,
  239. secure: false,
  240. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_CODE_MODEL && {
  241. pathRewrite: {
  242. [`^${apiRootPath}codemodel/`]: '/',
  243. },
  244. }),
  245. },
  246. [`${apiRootPath}learning/**`]: {
  247. target: process.env._SETTING_SERVER_PROXY_LEARN,
  248. changeOrigin: true,
  249. secure: false,
  250. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_LEARN && {
  251. pathRewrite: {
  252. [`^${apiRootPath}learning/`]: '/',
  253. },
  254. }),
  255. },
  256. [`${apiRootPath}job-manage/**`]: {
  257. target: process.env._SETTING_SERVER_PROXY_OR,
  258. changeOrigin: true,
  259. secure: false,
  260. // logLevel: 'debug',
  261. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_OR && {
  262. pathRewrite: {
  263. [`^${apiRootPath}job-manage/`]: '/',
  264. },
  265. }),
  266. },
  267. [`${apiRootPath}job-serve/**`]: {
  268. target: process.env._SETTING_SERVER_PROXY_JOBS_SERVE,
  269. changeOrigin: true,
  270. secure: false,
  271. // logLevel: 'debug',
  272. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_JOBS_SERVE && {
  273. pathRewrite: {
  274. [`^${apiRootPath}job-serve/`]: '/',
  275. },
  276. }),
  277. },
  278. [`${apiRootPath}gitlab/**`]: {
  279. target: process.env._SETTING_SERVER_PROXY_NB_VIEW,
  280. changeOrigin: true,
  281. secure: false,
  282. // logLevel: 'debug',
  283. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_NB_VIEW && {
  284. pathRewrite: {
  285. [`^${apiRootPath}gitlab/`]: '/',
  286. },
  287. }),
  288. },
  289. [`${apiRootPath}guide-train/**`]: {
  290. target: process.env._SETTING_SERVER_PROXY_GUIDE_TRAIN,
  291. changeOrigin: true,
  292. secure: false,
  293. // logLevel: 'debug',
  294. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_GUIDE_TRAIN && {
  295. pathRewrite: {
  296. [`^${apiRootPath}guide-train/`]: '/',
  297. },
  298. }),
  299. },
  300. [`${apiRootPath}data-pre/**`]: {
  301. target: process.env._SETTING_SERVER_PROXY_DATA_PRE,
  302. changeOrigin: true,
  303. secure: false,
  304. // logLevel: 'debug',
  305. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_DATA_PRE && {
  306. pathRewrite: {
  307. [`^${apiRootPath}data-pre/api/`]: '/',
  308. },
  309. }),
  310. },
  311. [`${apiRootPath}streaming-reasoning/**`]: {
  312. target: process.env._SETTING_SERVER_PROXY_STREAMING_REASONING,
  313. changeOrigin: true,
  314. secure: false,
  315. // logLevel: 'debug',
  316. ...(process.env._SETTING_SERVER_PROXY !== process.env._SETTING_SERVER_PROXY_STREAMING_REASONING && {
  317. pathRewrite: {
  318. [`^${apiRootPath}streaming-reasoning/`]: '/',
  319. },
  320. }),
  321. },
  322. }
  323. : {},
  324. before(app) {
  325. if (!_.isEmpty(contextPath)) {
  326. app.use(`/${contextPath}/images`, express.static(path.join(paths.appPublic, 'images')));
  327. app.use(`/${contextPath}/docs`, express.static(path.join(paths.appPublic, 'docs')));
  328. }
  329. app.use(errorOverlayMiddleware());
  330. app.use(noopServiceWorkerMiddleware());
  331. },
  332. };
  333. };