vue.config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // detail: https://cli.vuejs.org/config/#devserver-proxy
  94. // [process.env.VUE_APP_GENERAT_URL]: {
  95. // // target: 'http://123.60.219.66:9002',
  96. // target: 'http://192.168.2.12:9002',
  97. // changeOrigin: true, //开启代理
  98. // pathRewrite: {
  99. // [process.env.VUE_APP_GENERAT_URL]: ''
  100. // }
  101. // },
  102. // [process.env.VUE_APP_BASE_URL]: {
  103. // target: 'http://123.60.219.66:8086',
  104. // changeOrigin: true,
  105. // pathRewrite: {
  106. // [process.env.VUE_APP_BASE_URL]: ''
  107. // }
  108. // }
  109. }
  110. },
  111. // 和webpapck属性完全一致,最后会进行合并
  112. configureWebpack: {
  113. name: name,
  114. resolve: {
  115. alias: {
  116. '@': resolve('src'),
  117. '@tools': resolve('./src/tools'),
  118. '@api': resolve('./src/api'),
  119. '@com': resolve('./src/components'),
  120. '@public': resolve('public'),
  121. '@node': resolve('node_modules'),
  122. '@assets': resolve('src/assets'),
  123. '@store': resolve('src/store'),
  124. '@modeConfig': resolve('public/static/config'),
  125. // 'cesium': resolve('node_modules/cesium/Source'),
  126. },
  127. fallback: {
  128. "https": false,
  129. "zlib": false,
  130. "http": false,
  131. "url": false
  132. },
  133. mainFiles: ['index', 'Cesium']
  134. },
  135. //配置webpack自动按需引入element-plus
  136. plugins: [
  137. AutoImport({
  138. resolvers: [ElementPlusResolver()],
  139. }),
  140. Components({
  141. resolvers: [ElementPlusResolver()],
  142. }),
  143. // http://doc.ruoyi.vip/ruoyi-vue/other/faq.html#使用gzip解压缩静态文件
  144. new CompressionPlugin({
  145. test: /\.(js|css|html)?$/i, // 压缩文件格式
  146. filename: '[path][base].gz', // 压缩后的文件名
  147. algorithm: 'gzip', // 使用gzip压缩
  148. threshold: 10240, // 最小文件开启压缩
  149. minRatio: 0.8 // 压缩率小于1才会压缩
  150. })
  151. ],
  152. },
  153. chainWebpack(config) {
  154. // less 继承
  155. const types = ["vue-modules", "vue", "normal-modules", "normal"];
  156. types.forEach((type) =>
  157. addStyleResource(config.module.rule("less").oneOf(type))
  158. );
  159. const metaLoaderRule = config.module.rule("meta-loader");
  160. metaLoaderRule
  161. .test(/\.js$/)
  162. .use("@open-wc/webpack-import-meta-loader")
  163. .loader("@open-wc/webpack-import-meta-loader");
  164. // svg 雪碧图
  165. config.module // 排除其他svg-loader
  166. .rule("svg")
  167. .exclude.add(resolve("src/assets/icon/svg"))
  168. .end()
  169. .exclude.add(resolve("src/assets/icon/svg_fill"))
  170. .end();
  171. // svg雪碧图
  172. const svgRule = config.module.rule("icons");
  173. svgRule
  174. .test(/\.svg$/)
  175. .include.add(resolve("src/assets/icon/svg"))
  176. .end()
  177. .use("svg-sprite-loader")
  178. .loader("svg-sprite-loader")
  179. .options({
  180. symbolId: "svg-[name]",
  181. });
  182. // svgo 去除svg中无用元素
  183. svgRule
  184. .use("svgo-loader")
  185. .loader("svgo-loader")
  186. .options({
  187. plugins: extendDefaultPlugins([
  188. {
  189. name: "removeAttrs",
  190. params: {
  191. attrs: "fill",
  192. },
  193. },
  194. ]),
  195. });
  196. }
  197. })