vue.config.js 6.7 KB

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