You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.0 KiB

2 months ago
  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 name = process.env.VUE_APP_TITLE || '"与牧同行"' // 网页标题
  8. const baseUrl = // 后端接口
  9. 'http://localhost:8081'
  10. // 'http://192.168.101.105:8082'
  11. // 'http://192.168.101.109:8080'
  12. // 'http://192.168.101.111:8081'
  13. const port = process.env.port || process.env.npm_config_port || 81 // 端口
  14. // vue.config.js 配置说明
  15. //官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  16. // 这里只列一部分,具体配置参考文档
  17. module.exports = {
  18. // 部署生产环境和开发环境下的URL。
  19. // 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  20. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  21. publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  22. // 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)(默认dist)
  23. outputDir: 'dist',
  24. // 用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
  25. assetsDir: 'static',
  26. // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  27. productionSourceMap: false,
  28. transpileDependencies: ['quill'],
  29. // webpack-dev-server 相关配置
  30. devServer: {
  31. host: '0.0.0.0',
  32. port: port,
  33. open: true,
  34. proxy: {
  35. // detail: https://cli.vuejs.org/config/#devserver-proxy
  36. [process.env.VUE_APP_BASE_API]: {
  37. target: baseUrl,
  38. changeOrigin: true,
  39. pathRewrite: {
  40. ['^' + process.env.VUE_APP_BASE_API]: ''
  41. }
  42. },
  43. // 专门处理 WebSocket 请求
  44. '/ws': {
  45. target: baseUrl,
  46. ws: true, // 关键!启用 WebSocket 代理
  47. changeOrigin: true,
  48. pathRewrite: {
  49. '^/ws': '/ws'
  50. }
  51. },
  52. // springdoc proxy
  53. '^/v3/api-docs/(.*)': {
  54. target: baseUrl,
  55. changeOrigin: true
  56. }
  57. },
  58. disableHostCheck: true
  59. },
  60. css: {
  61. loaderOptions: {
  62. sass: {
  63. sassOptions: { outputStyle: "expanded" }
  64. }
  65. }
  66. },
  67. configureWebpack: {
  68. name: name,
  69. resolve: {
  70. alias: {
  71. '@': resolve('src')
  72. }
  73. },
  74. plugins: [
  75. // http://doc.ruoyi.vip/ruoyi-vue/other/faq.html#使用gzip解压缩静态文件
  76. new CompressionPlugin({
  77. cache: false, // 不启用文件缓存
  78. test: /\.(js|css|html|jpe?g|png|gif|svg)?$/i, // 压缩文件格式
  79. filename: '[path][base].gz[query]', // 压缩后的文件名
  80. algorithm: 'gzip', // 使用gzip压缩
  81. minRatio: 0.8, // 压缩比例,小于 80% 的文件不会被压缩
  82. deleteOriginalAssets: false // 压缩后删除原文件
  83. })
  84. ],
  85. },
  86. chainWebpack(config) {
  87. config.plugins.delete('preload') // TODO: need test
  88. config.plugins.delete('prefetch') // TODO: need test
  89. // set svg-sprite-loader
  90. config.module
  91. .rule('svg')
  92. .exclude.add(resolve('src/assets/icons'))
  93. .end()
  94. config.module
  95. .rule('icons')
  96. .test(/\.svg$/)
  97. .include.add(resolve('src/assets/icons'))
  98. .end()
  99. .use('svg-sprite-loader')
  100. .loader('svg-sprite-loader')
  101. .options({
  102. symbolId: 'icon-[name]'
  103. })
  104. .end()
  105. config.when(process.env.NODE_ENV !== 'development', config => {
  106. config
  107. .plugin('ScriptExtHtmlWebpackPlugin')
  108. .after('html')
  109. .use('script-ext-html-webpack-plugin', [{
  110. // `runtime` must same as runtimeChunk name. default is `runtime`
  111. inline: /runtime\..*\.js$/
  112. }])
  113. .end()
  114. config.optimization.splitChunks({
  115. chunks: 'all',
  116. cacheGroups: {
  117. libs: {
  118. name: 'chunk-libs',
  119. test: /[\\/]node_modules[\\/]/,
  120. priority: 10,
  121. chunks: 'initial' // only package third parties that are initially dependent
  122. },
  123. elementUI: {
  124. name: 'chunk-elementUI', // split elementUI into a single package
  125. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  126. priority: 20 // the weight needs to be larger than libs and app or it will be packaged into libs or app
  127. },
  128. commons: {
  129. name: 'chunk-commons',
  130. test: resolve('src/components'), // can customize your rules
  131. minChunks: 3, // minimum common number
  132. priority: 5,
  133. reuseExistingChunk: true
  134. }
  135. }
  136. })
  137. config.optimization.runtimeChunk('single')
  138. })
  139. }
  140. }