vue.config.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. const path = require('path');
  3. function resolve(dir) {
  4. return path.join(__dirname, dir);
  5. }
  6. const CompressionPlugin = require('compression-webpack-plugin');
  7. const TerserPlugin = require('terser-webpack-plugin');
  8. const name = process.env.VUE_APP_TITLE || '水价改革'; // 网页标题
  9. const port = process.env.port || process.env.npm_config_port || 8021; // 端口
  10. // const proxyPath = 'http://192.168.3.40:7000';
  11. const proxyPath = 'https://dev.a.yunfeiyun.com';
  12. // vue.config.js 配置说明
  13. // 官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  14. // 这里只列一部分,具体配置参考文档
  15. module.exports = {
  16. // 部署生产环境和开发环境下的URL。
  17. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  18. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  19. publicPath:
  20. process.env.NODE_ENV === 'production'
  21. ? process.env.VUE_APP_PUBLIC_PATH
  22. : process.env.VUE_APP_PUBLIC_PATH,
  23. // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  24. outputDir: 'dist',
  25. // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  26. assetsDir: 'static',
  27. // 是否开启eslint保存检测,有效值:ture | false | 'error'
  28. lintOnSave: process.env.NODE_ENV === 'development',
  29. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  30. productionSourceMap: false,
  31. // webpack-dev-server 相关配置
  32. devServer: {
  33. host: '0.0.0.0',
  34. port: port,
  35. open: true,
  36. proxy: {
  37. // detail: https://cli.vuejs.org/config/#devserver-proxy
  38. ['/iotmprod-api']: {
  39. // target: `http://192.168.1.57:8027`,
  40. target: proxyPath,
  41. changeOrigin: true,
  42. pathRewrite: {
  43. ['^' + process.env.VUE_APP_BASE_API]: ''
  44. }
  45. },
  46. ['/icsprod-api']: {
  47. // target: `http://192.168.1.57:8027`,
  48. target: proxyPath,
  49. changeOrigin: true,
  50. pathRewrite: {
  51. ['^' + process.env.VUE_APP_BASE_API]: ''
  52. }
  53. },
  54. [process.env.VUE_APP_BASE_API]: {
  55. // target: `http://192.168.1.57:8027`,
  56. // target: `http://192.168.95:8021`,
  57. target: proxyPath,
  58. changeOrigin: true,
  59. pathRewrite: {
  60. // ['^' + process.env.VUE_APP_BASE_API]: ''
  61. }
  62. },
  63. ['/portalprod-api']: {
  64. // target: `http://192.168.1.95:7000`,
  65. target: proxyPath,
  66. // target: `http://114.55.0.7:7000`,
  67. changeOrigin: true,
  68. pathRewrite: {
  69. // ['^' + process.env.VUE_APP_BASE_API]: ''
  70. }
  71. }
  72. },
  73. disableHostCheck: true
  74. },
  75. css: {
  76. loaderOptions: {
  77. sass: {
  78. sassOptions: { outputStyle: 'expanded' }
  79. }
  80. }
  81. },
  82. configureWebpack: {
  83. name: name,
  84. resolve: {
  85. alias: {
  86. '@': resolve('src')
  87. }
  88. },
  89. plugins: [
  90. // http://doc.ruoyi.vip/ruoyi-vue/other/faq.html#使用gzip解压缩静态文件
  91. new CompressionPlugin({
  92. cache: false, // 不启用文件缓存
  93. test: /\.(js|css|html)?$/i, // 压缩文件格式
  94. filename: '[path].gz[query]', // 压缩后的文件名
  95. algorithm: 'gzip', // 使用gzip压缩
  96. minRatio: 0.8 // 压缩率小于1才会压缩
  97. })
  98. ],
  99. optimization: {
  100. minimizer: [
  101. new TerserPlugin({
  102. terserOptions: {
  103. compress: {
  104. warnings: false,
  105. drop_console: true,
  106. drop_debugger: true,
  107. pure_funcs: ['console.log']
  108. }
  109. }
  110. })
  111. ]
  112. }
  113. },
  114. chainWebpack(config) {
  115. config.plugins.delete('preload'); // TODO: need test
  116. config.plugins.delete('prefetch'); // TODO: need test
  117. // set svg-sprite-loader
  118. config.module.rule('svg').exclude.add(resolve('src/assets/icons')).end();
  119. config.module
  120. .rule('icons')
  121. .test(/\.svg$/)
  122. .include.add(resolve('src/assets/icons'))
  123. .end()
  124. .use('svg-sprite-loader')
  125. .loader('svg-sprite-loader')
  126. .options({
  127. symbolId: 'icon-[name]'
  128. })
  129. .end();
  130. config.when(process.env.NODE_ENV !== 'development', (config) => {
  131. config
  132. .plugin('ScriptExtHtmlWebpackPlugin')
  133. .after('html')
  134. .use('script-ext-html-webpack-plugin', [
  135. {
  136. // `runtime` must same as runtimeChunk name. default is `runtime`
  137. inline: /runtime\..*\.js$/
  138. }
  139. ])
  140. .end();
  141. config.optimization.splitChunks({
  142. chunks: 'all',
  143. cacheGroups: {
  144. libs: {
  145. name: 'chunk-libs',
  146. test: /[\\/]node_modules[\\/]/,
  147. priority: 10,
  148. chunks: 'initial' // only package third parties that are initially dependent
  149. },
  150. elementUI: {
  151. name: 'chunk-elementUI', // split elementUI into a single package
  152. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  153. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  154. },
  155. commons: {
  156. name: 'chunk-commons',
  157. test: resolve('src/components'), // can customize your rules
  158. minChunks: 3, // minimum common number
  159. priority: 5,
  160. reuseExistingChunk: true
  161. }
  162. }
  163. });
  164. config.optimization.runtimeChunk('single'),
  165. {
  166. from: path.resolve(__dirname, './public/robots.txt'), // 防爬虫文件
  167. to: './' // 到根目录下
  168. };
  169. });
  170. }
  171. };