vue.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. const path = require("path");
  2. const TerserPlugin = require("terser-webpack-plugin");
  3. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
  4. const WorkboxPlugin = require("workbox-webpack-plugin");
  5. const HappyPack = require("happypack");
  6. const webpack = require("webpack");
  7. // const HtmlWebpackPlugin = require("html-webpack-plugin");
  8. function resolve(dir) {
  9. return path.join(__dirname, dir);
  10. }
  11. /** 仅注册已配置 target 的代理,避免 undefined 导致 devServer 启动失败 */
  12. function buildDevProxy(routes) {
  13. return routes.reduce((proxy, route) => {
  14. if (!route.target) return proxy;
  15. const { context, target, pathRewrite, onProxyReq } = route;
  16. proxy[context] = {
  17. target,
  18. changeOrigin: true,
  19. ...(pathRewrite ? { pathRewrite } : {}),
  20. ...(onProxyReq ? { onProxyReq } : {}),
  21. };
  22. return proxy;
  23. }, {});
  24. }
  25. module.exports = {
  26. chainWebpack(config) {
  27. config.module.rule("svg").exclude.add(resolve("src/icons")).end();
  28. config.module
  29. .rule("icons")
  30. .test(/\.svg$/)
  31. .include.add(resolve("src/icons"))
  32. .end()
  33. .use("svg-sprite-loader")
  34. .loader("svg-sprite-loader")
  35. .options({
  36. symbolId: "icon-[name]",
  37. })
  38. .end();
  39. config.plugin("happypack").use(HappyPack, [
  40. {
  41. loaders: ["babel-loader"],
  42. },
  43. ]);
  44. // config.plugin('dll').use(webpack.DllPlugin, [
  45. // {
  46. // name: '[name]_library',
  47. // path: path.join(__dirname, 'dll', '[name]-manifest.json'),
  48. // },
  49. // ]);
  50. // 仅生产构建注册 Service Worker:dev server(--watch)下 GenerateSW 重复调用
  51. // 存在已知问题——首次生成的 precache 版本号后续不会正确更新,会导致浏览器
  52. // 一直用旧缓存响应请求,改代码、重启 dev server、强制刷新都不生效。
  53. if (process.env.NODE_ENV === "production") {
  54. config.plugin("workbox").use(WorkboxPlugin.GenerateSW, [
  55. {
  56. clientsClaim: true,
  57. skipWaiting: true,
  58. },
  59. ]);
  60. }
  61. },
  62. css: {
  63. loaderOptions: {
  64. postcss: {
  65. postcssOptions: {
  66. plugins: [require("tailwindcss"), require("autoprefixer")],
  67. },
  68. },
  69. sass: {
  70. additionalData: `
  71. @import "@/styles/global.scss";
  72. `,
  73. },
  74. },
  75. },
  76. devServer: {
  77. // contentBase: path.join(__dirname, "public"),
  78. // 页面 IP/端口:在各 .env.* 中配置 VUE_APP_DEV_HOST、VUE_APP_DEV_PORT
  79. host: process.env.VUE_APP_DEV_HOST || "localhost",
  80. port: process.env.VUE_APP_DEV_PORT
  81. ? Number(process.env.VUE_APP_DEV_PORT)
  82. : 8080,
  83. proxy: buildDevProxy([
  84. {
  85. context: "/api",
  86. target: process.env.VUE_APP_APIPROXY,
  87. pathRewrite: { "^/api": "" },
  88. },
  89. {
  90. context: "/DatangEnosData",
  91. target: process.env.VUE_APP_DatangEnosData_APIPROXY,
  92. pathRewrite: { "^/DatangEnosData": "" },
  93. },
  94. // 健康分析接口(勿用 /health,避免与路由 /home/health 及 history 回退冲突)
  95. {
  96. context: "/healthApi",
  97. target: process.env.VUE_APP_HEALTH_APIPROXY,
  98. pathRewrite: { "^/healthApi": "" },
  99. },
  100. // 振动、激光测距仪
  101. {
  102. context: "/WZLapi",
  103. target: process.env.VUE_APP_WZLAPIPROXY,
  104. pathRewrite: { "^/WZLapi": "" },
  105. },
  106. {
  107. context: "/ETLapi",
  108. target: process.env.VUE_APP_ETLAPIPROXY,
  109. pathRewrite: { "^/ETLapi": "" },
  110. onProxyReq(proxyReq) {
  111. console.log("Proxying /ETLapi request to:", proxyReq.path);
  112. },
  113. },
  114. {
  115. context: "/tiles",
  116. target: process.env.VUE_APP_MAP,
  117. pathRewrite: { "^/tiles": "/tiles" },
  118. },
  119. {
  120. context: "/AnalysisMulti",
  121. target: process.env.VUE_APP_AnalysisMultiAPIPROXY,
  122. pathRewrite: { "^/AnalysisMulti": "" },
  123. },
  124. {
  125. context: "/transDataWeb",
  126. target: process.env.VUE_APP_WZLAPIPROXY,
  127. pathRewrite: { "^/transDataWeb": "" },
  128. },
  129. {
  130. context: "/sAlgorithm",
  131. target: process.env.VUE_APP_sAlgorithmAPIPROXY,
  132. pathRewrite: { "^/sAlgorithm": "" },
  133. },
  134. {
  135. context: "/databaseApi",
  136. target: process.env.VUE_APP_databaseApiAPIPROXY,
  137. pathRewrite: { "^/databaseApi": "" },
  138. },
  139. {
  140. context: "/downLoadChart",
  141. target: process.env.VUE_APP_downLoadChartAPIPROXY,
  142. pathRewrite: { "^/downLoadChart": "" },
  143. },
  144. ]),
  145. },
  146. configureWebpack: {
  147. resolve: {
  148. alias: {
  149. "@": resolve("src"),
  150. },
  151. },
  152. optimization: {
  153. usedExports: true,
  154. splitChunks: {
  155. chunks: "all",
  156. },
  157. minimize: true,
  158. minimizer: [new TerserPlugin(), new CssMinimizerPlugin()],
  159. },
  160. // plugins: [
  161. // new HtmlWebpackPlugin({
  162. // filename: "custom-filename.html", // 自定义输出的 HTML 文件名
  163. // title: "风机运行管理系统", // 设置页面的标题
  164. // // 其他配置...
  165. // }),
  166. // // 其他插件...
  167. // ],
  168. },
  169. pluginOptions: {
  170. "style-resources-loader": {
  171. preProcessor: "sass",
  172. patterns: [],
  173. },
  174. },
  175. configureWebpack: {
  176. devtool: "source-map",
  177. },
  178. };