vue.config.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  5. const setMemeoy = require('./memoryConfig')
  6. const webpack = require('webpack')
  7. function resolve(dir) {
  8. console.log(__dirname)
  9. return path.join(__dirname, dir)
  10. }
  11. const name = defaultSettings.title || '登录系统' // page title
  12. // const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://192.168.100.204:1111'
  13. // const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://xrty.vipgz4.idcfengye.com'
  14. // const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://192.168.2.231:1111'
  15. const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://192.168.2.238:1111'
  16. // const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://127.0.0.1:1111'
  17. const port = 8031
  18. // const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  19. module.exports = {
  20. /**
  21. * 如果计划在子路径下部署站点,则需要设置publicPath
  22. */
  23. publicPath: './', // 发布路径
  24. // publicPath: '/',//本地路径
  25. outputDir: 'dist',
  26. assetsDir: 'static',
  27. // lintOnSave: process.env.NODE_ENV === 'development',
  28. lintOnSave: false,
  29. productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
  30. devServer: {
  31. port: port, // 端口
  32. open: false, // 自动开启浏览器
  33. compress: false, // 开启压缩
  34. overlay: {
  35. warnings: false,
  36. errors: false
  37. },
  38. proxy: {
  39. '/backstage': {
  40. target: 'http://192.168.2.238:1111', // 修改后台接口地址
  41. // target: 'http://127.0.0.1:1111', // 修改后台接口地址
  42. // target: 'http://192.168.2.231:1111', // 修改后台接口地址
  43. // target: 'http://xrty.vipgz4.idcfengye.com',
  44. // target: 'http://192.168.100.204:1111',
  45. changeOrigin: true,
  46. pathRewrite: {
  47. '^/backstage': ''
  48. }
  49. },
  50. '/api': {
  51. target: 'http://192.168.2.235:1118', // 后台接口地址
  52. changeOrigin: true,
  53. pathRewrite: {
  54. '^/api': ''
  55. }
  56. }
  57. // '/omsweb': { // 自定义 接口前缀
  58. // target: 'http://58.17.241.6:1212', // 这里可以跟随项目实际部署服务器来
  59. // changeOrigin: true,
  60. // pathRewrite: {
  61. // '^/omsweb': ''
  62. // }
  63. // }
  64. }
  65. // before: require('./mock/mock-server.js')
  66. },
  67. css: {
  68. // 是否使用css分离插件 ExtractTextPlugin
  69. extract: true,
  70. // 开启 CSS source maps?
  71. // sourceMap: false,
  72. // css预设器配置项
  73. loaderOptions: {
  74. // pass options to sass-loader
  75. sass: {
  76. // 引入全局变量样式,@使我们设置的别名,执行src目录
  77. data: `@import "@/styles/variables.scss";`
  78. }
  79. },
  80. // 启用 CSS modules for all css / pre-processor files.
  81. modules: false
  82. },
  83. configureWebpack: {
  84. name: name,
  85. resolve: {
  86. alias: {
  87. '@': resolve('src'),
  88. staticPub: resolve('public')
  89. }
  90. },
  91. plugins: [
  92. // 压缩代码
  93. new CompressionWebpackPlugin({
  94. filename: '[path].gz[query]',
  95. algorithm: 'gzip',
  96. test: /\.js$|\.html$|\.json$|\.css/,
  97. threshold: 0, // 只有大小大于该值的资源会被处理
  98. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  99. deleteOriginalAssets: false // 删除原文件
  100. }),
  101. new webpack.ProvidePlugin({
  102. $: 'jquery',
  103. jQuery: 'jquery',
  104. 'windows.jQuery': 'jquery',
  105. 'vue-moment': 'vue-moment',
  106. 'vue-echarts': 'vue-echarts'
  107. })
  108. ]
  109. },
  110. chainWebpack(config) {
  111. // 删除预加载
  112. config.plugins.delete('preload')
  113. config.plugins.delete('prefetch')
  114. // 设置 svg-sprite-loader
  115. config.module
  116. .rule('svg')
  117. .exclude.add(resolve('src/icons'))
  118. .end()
  119. config.module
  120. .rule('icons')
  121. .test(/\.svg$/)
  122. .include.add(resolve('src/icons'))
  123. .end()
  124. .use('svg-sprite-loader')
  125. .loader('svg-sprite-loader')
  126. .options({
  127. symbolId: 'icon-[name]'
  128. })
  129. .end()
  130. // 设置保留空白
  131. config.module
  132. .rule('vue')
  133. .use('vue-loader')
  134. .loader('vue-loader')
  135. .tap((options) => {
  136. options.compilerOptions.preserveWhitespace = true
  137. return options
  138. })
  139. .end()
  140. config.when(process.env.NODE_ENV === 'development', (config) => config.devtool('cheap-source-map'))
  141. config.when(process.env.NODE_ENV !== 'development', (config) => {
  142. config
  143. .plugin('ScriptExtHtmlWebpackPlugin')
  144. .after('html')
  145. .use('script-ext-html-webpack-plugin', [
  146. {
  147. // `runtime`必须与runtimeChunk名称相同。默认值为“runtime”`
  148. inline: /runtime\..*\.js$/
  149. }
  150. ])
  151. .end()
  152. // 分割代码
  153. config.optimization.splitChunks({
  154. chunks: 'all',
  155. cacheGroups: {
  156. libs: {
  157. name: 'chunk-libs',
  158. test: /[\\/]node_modules[\\/]/,
  159. priority: 10,
  160. chunks: 'initial' // 仅打包最初依赖的第三方
  161. },
  162. elementUI: {
  163. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  164. priority: 20, // 重量必须大于libs和app,否则将打包成libs或app
  165. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为了适应cnpm
  166. },
  167. monent: {
  168. name: 'chunk-moment',
  169. priority: 20,
  170. test: /[\\/]node_modules[\\/]_?vue-moment(.*)/
  171. },
  172. echarts: {
  173. name: 'chunk-echarts',
  174. priority: 20,
  175. test: /[\\/]node_modules[\\/]_?vue-echarts(.*)/
  176. },
  177. commons: {
  178. name: 'chunk-commons',
  179. test: resolve('src/components'), // 可以自定义规则
  180. minChunks: 3, // 最小公共数
  181. priority: 5,
  182. reuseExistingChunk: true
  183. }
  184. }
  185. })
  186. config.optimization.runtimeChunk('single')
  187. })
  188. config.externals({ './cptable': 'var cptable' })
  189. setMemeoy(config)
  190. }
  191. }