vue.config.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const webpack = require('webpack')
  5. const CompressionWebpackPlugin = require('compression-webpack-plugin')//这个插件可以看官网还是很好用的
  6. const productionGzipExtensions = ['js', 'css']//对什么文件进行压缩
  7. const Timestamp = new Date().getTime();//防止http的缓存每次打包用时间戳进行区分
  8. const {BASE_URL }= require('./src/utils/config.js');
  9. function resolve(dir) {
  10. return path.join(__dirname, dir)
  11. }
  12. const name = defaultSettings.title || '后台' // page title
  13. // If your port is set to 80,
  14. // use administrator privileges to execute the command line.
  15. // For example, Mac: sudo npm run
  16. // You can change the port by the following methods:
  17. // port = 9528 npm run dev OR npm run dev --port = 9528
  18. const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  19. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  20. module.exports = {
  21. /**
  22. * You will need to set publicPath if you plan to deploy your site under a sub path,
  23. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  24. * then publicPath should be set to "/bar/".
  25. * In most cases please use '/' !!!
  26. * Detail: https://cli.vuejs.org/config/#publicpath
  27. */
  28. publicPath: '/',
  29. outputDir: 'dist',
  30. assetsDir: 'static',
  31. lintOnSave: false,
  32. productionSourceMap: false,
  33. devServer: {
  34. port: port,
  35. open: true,
  36. overlay: {
  37. warnings: false,
  38. errors: true
  39. },
  40. // 反向代理
  41. proxy: {
  42. '^/api/baidu': {
  43. target: 'https://api.map.baidu.com/',//代理
  44. changeOrigin: true,//是否跨域
  45. pathRewrite: {
  46. '^/api/baidu': ''
  47. }
  48. },
  49. '^/api': {
  50. // target: 'http://192.168.110.153:9002/',//代理
  51. target: BASE_URL,//代理
  52. changeOrigin: true,//是否跨域
  53. pathRewrite: {
  54. '^/api': ''
  55. }
  56. },
  57. }
  58. // before: require('./mock/mock-server.js')
  59. },
  60. configureWebpack: {
  61. // provide the app's title in webpack's name field, so that
  62. // it can be accessed in index.html to inject the correct title.
  63. name: name,
  64. output: { // 输出重构 打包编译后的 文件名称
  65. filename: `js/[name]${Timestamp}.js`,
  66. chunkFilename: `js/[name]${Timestamp}.js`
  67. },
  68. plugins: [
  69. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),//该插件能够使得指定目录被忽略,从而使得打包变快,文件变小
  70. // 下面是下载的插件的配置
  71. new CompressionWebpackPlugin({
  72. cache:false,
  73. algorithm: 'gzip',//压缩算法
  74. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),//处理所有匹配此 {RegExp} 的资源
  75. threshold: 512,//只处理比这个值大的资源。按字节计算
  76. minRatio: 0.8//只有压缩率比这个值小的资源才会被处理
  77. }),
  78. new webpack.optimize.LimitChunkCountPlugin({
  79. maxChunks: 15, //控制打包生成js的个数
  80. minChunkSize: 100
  81. })
  82. ],
  83. resolve: {
  84. alias: {
  85. '@': resolve('src')
  86. }
  87. }
  88. },
  89. chainWebpack(config) {
  90. // it can improve the speed of the first screen, it is recommended to turn on preload
  91. config.plugin('preload').tap(() => [
  92. {
  93. rel: 'preload',
  94. // to ignore runtime.js
  95. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  96. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  97. include: 'initial'
  98. }
  99. ])
  100. // when there are many pages, it will cause too many meaningless requests
  101. config.plugins.delete('prefetch')
  102. // set svg-sprite-loader
  103. config.module
  104. .rule('svg')
  105. .exclude.add(resolve('src/icons'))
  106. .end()
  107. config.module
  108. .rule('icons')
  109. .test(/\.svg$/)
  110. .include.add(resolve('src/icons'))
  111. .end()
  112. .use('svg-sprite-loader')
  113. .loader('svg-sprite-loader')
  114. .options({
  115. symbolId: 'icon-[name]'
  116. })
  117. .end()
  118. config
  119. .when(process.env.NODE_ENV !== 'development',
  120. config => {
  121. config
  122. .plugin('ScriptExtHtmlWebpackPlugin')
  123. .after('html')
  124. .use('script-ext-html-webpack-plugin', [{
  125. // `runtime` must same as runtimeChunk name. default is `runtime`
  126. inline: /runtime\..*\.js$/
  127. }])
  128. .end()
  129. config
  130. .optimization.splitChunks({
  131. chunks: 'all',
  132. cacheGroups: {
  133. libs: {
  134. name: 'chunk-libs',
  135. test: /[\\/]node_modules[\\/]/,
  136. priority: 10,
  137. chunks: 'initial' // only package third parties that are initially dependent
  138. },
  139. elementUI: {
  140. name: 'chunk-elementUI', // split elementUI into a single package
  141. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  142. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  143. },
  144. commons: {
  145. name: 'chunk-commons',
  146. test: resolve('src/components'), // can customize your rules
  147. minChunks: 3, // minimum common number
  148. priority: 5,
  149. reuseExistingChunk: true
  150. }
  151. }
  152. })
  153. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  154. config.optimization.runtimeChunk('single')
  155. }
  156. )
  157. }
  158. }