vue.config.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * @Date: 2023-06-18 10:09:15
  3. * @LastEditors: zhubj
  4. * @LastEditTime: 2023-06-19 10:49:41
  5. * @Description: 头部注释
  6. * @FilePath: \own-vue3-vuecli-template\vue.config.js
  7. */
  8. 'use strict'
  9. const path = require('path')
  10. function resolve(dir) {
  11. return path.join(__dirname, dir)
  12. }
  13. const { defineConfig } = require('@vue/cli-service')
  14. const name = process.env.VUE_APP_TITLE || '网页标题' // 网页标题
  15. const port = process.env.port || process.env.npm_config_port || 8011 // 端口
  16. // element-plus 按需导入自动导入的插件
  17. const AutoImport = require('unplugin-auto-import/webpack')
  18. const Components = require('unplugin-vue-components/webpack')
  19. const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
  20. // 实现 gzip 压缩打包
  21. const CompressionPlugin = require('compression-webpack-plugin')
  22. // 添加less继承
  23. function addStyleResource(rule) {
  24. rule
  25. .use("style-resource")
  26. .loader("style-resources-loader")
  27. .options({
  28. patterns: [resolve("src/assets/styles/common/common.less")],
  29. });
  30. }
  31. function extendDefaultPlugins(arr) {
  32. let plug = [
  33. "removeDoctype",
  34. "removeXMLProcInst",
  35. "removeComments",
  36. "removeMetadata",
  37. "removeEditorsNSData",
  38. "cleanupAttrs",
  39. "mergeStyles",
  40. "inlineStyles",
  41. "minifyStyles",
  42. "cleanupIDs",
  43. "removeUselessDefs",
  44. "cleanupNumericValues",
  45. "convertColors",
  46. "removeUnknownsAndDefaults",
  47. "removeNonInheritableGroupAttrs",
  48. "removeUselessStrokeAndFill",
  49. "removeViewBox",
  50. "cleanupEnableBackground",
  51. "removeHiddenElems",
  52. "removeEmptyText",
  53. "convertShapeToPath",
  54. "convertEllipseToCircle",
  55. "moveElemsAttrsToGroup",
  56. "moveGroupAttrsToElems",
  57. "collapseGroups",
  58. "convertPathData",
  59. "convertTransform",
  60. "removeEmptyAttrs",
  61. "removeEmptyContainers",
  62. "mergePaths",
  63. "removeUnusedNS",
  64. "sortDefsChildren",
  65. "removeTitle",
  66. "removeDesc",
  67. ];
  68. return plug.concat(arr);
  69. }
  70. module.exports = defineConfig({
  71. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  72. // 例如 https://www.my-app.com/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径
  73. // 例如,如果你的应用被部署在 https://www.my-app.com/my-app/,则设置 publicPath 为 /my-app/
  74. publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  75. // 当运行 vue-cli-service build 时生成的生产环境构建文件的目录。注意目标目录的内容在构建之前会被清除 (构建时传入 --no-clean 可关闭该行为)。
  76. outputDir: 'dist',
  77. // 放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录
  78. assetsDir: 'static',
  79. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  80. productionSourceMap: false,
  81. // 默认情况下 babel-loader 会忽略所有 node_modules 中的文件。你可以启用本选项,以避免构建后的代码中出现未转译的第三方依赖(对所有的依赖都进行转译可能会降低构建速度)
  82. transpileDependencies: false,
  83. // webpack-dev-server 相关配置
  84. devServer: {
  85. client: {
  86. progress: true,
  87. overlay: false
  88. },
  89. host: '0.0.0.0',
  90. port: port,
  91. open: false,
  92. proxy: {
  93. "/rib": {
  94. target: (process.env.NODE_ENV === "10" ? "https://10.127.7.241:9001" : "https://172.16.12.101:9001"), // 请求地址
  95. changeOrigin: true, // 在vue-cli3中,默认changeOrigin的值是true,意味着服务器host设置成target,这与vue-cli2不一致,vue-cli2这个默认值是false
  96. // changeOrigin的值是true,target是host, request URL是http://baidu.com
  97. // 如果设置changeOrigin: false,host就是浏览器发送过来的host,也就是localhost:8082。
  98. pathRewrite: {
  99. // 路径重写,eg:把api接口替换为''
  100. "^/api48080": "",
  101. },
  102. },
  103. "/api48080": {
  104. target: (process.env.NODE_ENV === "10" ? "https://10.127.7.241:48080" : "https://172.16.12.101:48080"), // 请求地址
  105. changeOrigin: true, // 在vue-cli3中,默认changeOrigin的值是true,意味着服务器host设置成target,这与vue-cli2不一致,vue-cli2这个默认值是false
  106. // changeOrigin的值是true,target是host, request URL是http://baidu.com
  107. // 如果设置changeOrigin: false,host就是浏览器发送过来的host,也就是localhost:8082。
  108. pathRewrite: {
  109. // 路径重写,eg:把api接口替换为''
  110. "^/api48080": "",
  111. },
  112. },
  113. "/api9002": {
  114. target: (process.env.NODE_ENV === "10" ? "https://10.127.7.241:9002" : "https://172.16.12.101:9002"), // 请求地址
  115. changeOrigin: true, // 在vue-cli3中,默认changeOrigin的值是true,意味着服务器host设置成target,这与vue-cli2不一致,vue-cli2这个默认值是false
  116. // changeOrigin的值是true,target是host, request URL是http://baidu.com
  117. // 如果设置changeOrigin: false,host就是浏览器发送过来的host,也就是localhost:8082。
  118. pathRewrite: {
  119. // 路径重写,eg:把api接口替换为''
  120. "^/api9002": "",
  121. },
  122. },
  123. "/api8086": {
  124. target: (process.env.NODE_ENV === "10" ? "https://10.127.7.241:8086" : "https://172.16.12.101:8086"), // 请求地址
  125. changeOrigin: true, // 在vue-cli3中,默认changeOrigin的值是true,意味着服务器host设置成target,这与vue-cli2不一致,vue-cli2这个默认值是false
  126. // changeOrigin的值是true,target是host, request URL是http://baidu.com
  127. // 如果设置changeOrigin: false,host就是浏览器发送过来的host,也就是localhost:8082。
  128. pathRewrite: {
  129. // 路径重写,eg:把api接口替换为''
  130. "^/api8086": "",
  131. },
  132. },
  133. "/api7095": {
  134. target: (process.env.NODE_ENV === "10" ? "https://10.127.7.241:7095" : "https://172.16.12.101:7095"), // 请求地址
  135. changeOrigin: true, // 在vue-cli3中,默认changeOrigin的值是true,意味着服务器host设置成target,这与vue-cli2不一致,vue-cli2这个默认值是false
  136. // changeOrigin的值是true,target是host, request URL是http://baidu.com
  137. // 如果设置changeOrigin: false,host就是浏览器发送过来的host,也就是localhost:8082。
  138. pathRewrite: {
  139. // 路径重写,eg:把api接口替换为''
  140. "^/api7095": "",
  141. },
  142. },
  143. }
  144. },
  145. // 和webpapck属性完全一致,最后会进行合并
  146. configureWebpack: {
  147. name: name,
  148. resolve: {
  149. alias: {
  150. '@': resolve('src'),
  151. '@tools': resolve('./src/tools'),
  152. '@api': resolve('./src/api'),
  153. '@com': resolve('./src/components'),
  154. '@public': resolve('public'),
  155. '@node': resolve('node_modules'),
  156. '@assets': resolve('src/assets'),
  157. '@store': resolve('src/store'),
  158. '@modeConfig': resolve('public/static/config'),
  159. // 'cesium': resolve('node_modules/cesium/Source'),
  160. },
  161. fallback: {
  162. "https": false,
  163. "zlib": false,
  164. "http": false,
  165. "url": false
  166. },
  167. mainFiles: ['index', 'Cesium']
  168. },
  169. //配置webpack自动按需引入element-plus
  170. plugins: [
  171. AutoImport({
  172. resolvers: [ElementPlusResolver()],
  173. }),
  174. Components({
  175. resolvers: [ElementPlusResolver()],
  176. }),
  177. // http://doc.ruoyi.vip/ruoyi-vue/other/faq.html#使用gzip解压缩静态文件
  178. new CompressionPlugin({
  179. test: /\.(js|css|html)?$/i, // 压缩文件格式
  180. filename: '[path][base].gz', // 压缩后的文件名
  181. algorithm: 'gzip', // 使用gzip压缩
  182. threshold: 10240, // 最小文件开启压缩
  183. minRatio: 0.8 // 压缩率小于1才会压缩
  184. })
  185. ],
  186. },
  187. chainWebpack(config) {
  188. // less 继承
  189. const types = ["vue-modules", "vue", "normal-modules", "normal"];
  190. types.forEach((type) =>
  191. addStyleResource(config.module.rule("less").oneOf(type))
  192. );
  193. const metaLoaderRule = config.module.rule("meta-loader");
  194. metaLoaderRule
  195. .test(/\.js$/)
  196. .use("@open-wc/webpack-import-meta-loader")
  197. .loader("@open-wc/webpack-import-meta-loader");
  198. // svg 雪碧图
  199. config.module // 排除其他svg-loader
  200. .rule("svg")
  201. .exclude.add(resolve("src/assets/icon/svg"))
  202. .end()
  203. .exclude.add(resolve("src/assets/icon/svg_fill"))
  204. .end();
  205. // svg雪碧图
  206. const svgRule = config.module.rule("icons");
  207. svgRule
  208. .test(/\.svg$/)
  209. .include.add(resolve("src/assets/icon/svg"))
  210. .end()
  211. .use("svg-sprite-loader")
  212. .loader("svg-sprite-loader")
  213. .options({
  214. symbolId: "svg-[name]",
  215. });
  216. // svgo 去除svg中无用元素
  217. svgRule
  218. .use("svgo-loader")
  219. .loader("svgo-loader")
  220. .options({
  221. plugins: extendDefaultPlugins([
  222. {
  223. name: "removeAttrs",
  224. params: {
  225. attrs: "fill",
  226. },
  227. },
  228. ]),
  229. });
  230. }
  231. })