vue.config.js 5.4 KB

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