vue.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  5. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  6. const webpack = require('webpack')
  7. function resolve(dir) {
  8. return path.join(__dirname, dir)
  9. }
  10. const name = defaultSettings.title || 'vue Admin Template' // page title
  11. const compress = new CompressionWebpackPlugin({
  12. filename: info => {
  13. return `${info.path}.gz${info.query}`
  14. },
  15. algorithm: 'gzip',
  16. threshold: 10240,
  17. test: new RegExp('\\.(' + ['js'].join('|') + ')$'),
  18. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  19. deleteOriginalAssets: false // 删除原文件
  20. })
  21. const chunkCount = new webpack.optimize.LimitChunkCountPlugin({
  22. maxChunks: 5,
  23. minChunkSize: 100
  24. })
  25. // If your port is set to 80,
  26. // use administrator privileges to execute the command line.
  27. // For example, Mac: sudo npm run
  28. // You can change the port by the following methods:
  29. // port = 9528 npm run dev OR npm run dev --port = 9528
  30. const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  31. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  32. module.exports = {
  33. /**
  34. * You will need to set publicPath if you plan to deploy your site under a sub path,
  35. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  36. * then publicPath should be set to "/bar/".
  37. * In most cases please use '/' !!!
  38. * Detail: https://cli.vuejs.org/config/#publicpath
  39. */
  40. publicPath: '/',
  41. outputDir: 'dist',
  42. assetsDir: 'static',
  43. // lintOnSave: process.env.NODE_ENV === 'development',
  44. lintOnSave: false, // 关闭eslint
  45. productionSourceMap: false,
  46. devServer: {
  47. port: port,
  48. open: true,
  49. overlay: {
  50. warnings: false,
  51. errors: true
  52. },
  53. proxy: {
  54. // 配置跨域
  55. '/api': {
  56. target: 'https://gmintertest.zfire.top/', // 测试服接口地址
  57. ws: true,
  58. changeOrigin: true,
  59. pathRewrite: {
  60. '^/api': ''
  61. }
  62. }
  63. },
  64. before: require('./mock/mock-server.js')
  65. },
  66. configureWebpack: {
  67. // provide the app's title in webpack's name field, so that
  68. // it can be accessed in index.html to inject the correct title.
  69. name: name,
  70. plugins: [new webpack.ProvidePlugin({}), compress, chunkCount],
  71. resolve: {
  72. alias: {
  73. '@': resolve('src'),
  74. 'element-ui': resolve('node_modules/@zjlib/element-ui2')
  75. // '@packages': resolve('../element-plugins/packages')
  76. },
  77. extensions: ['*', '.js', '.vue', '.json']
  78. },
  79. optimization: {
  80. minimizer: [
  81. new UglifyJsPlugin({
  82. uglifyOptions: {
  83. compress: {
  84. // warnings:false
  85. drop_debugger: true,
  86. drop_console: true,
  87. pure_funcs: ['console.log']
  88. }
  89. },
  90. sourceMap: false,
  91. parallel: true
  92. })
  93. ]
  94. }
  95. },
  96. chainWebpack(config) {
  97. // it can improve the speed of the first screen, it is recommended to turn on preload
  98. config.plugin('preload').tap(() => [
  99. {
  100. rel: 'preload',
  101. // to ignore runtime.js
  102. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  103. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  104. include: 'initial'
  105. }
  106. ])
  107. // when there are many pages, it will cause too many meaningless requests
  108. config.plugins.delete('prefetch')
  109. // set svg-sprite-loader
  110. config.module.rule('svg').exclude.add(resolve('src/icons')).end()
  111. config.module
  112. .rule('icons')
  113. .test(/\.svg$/)
  114. .include.add(resolve('src/icons'))
  115. .end()
  116. .use('svg-sprite-loader')
  117. .loader('svg-sprite-loader')
  118. .options({
  119. symbolId: 'icon-[name]'
  120. })
  121. .end()
  122. config.when(process.env.NODE_ENV !== 'development', config => {
  123. config
  124. .plugin('ScriptExtHtmlWebpackPlugin')
  125. .after('html')
  126. .use('script-ext-html-webpack-plugin', [
  127. {
  128. // `runtime` must same as runtimeChunk name. default is `runtime`
  129. inline: /runtime\..*\.js$/
  130. }
  131. ])
  132. .end()
  133. config.optimization.splitChunks({
  134. chunks: 'all',
  135. cacheGroups: {
  136. libs: {
  137. name: 'chunk-libs',
  138. test: /[\\/]node_modules[\\/]/,
  139. priority: 10,
  140. chunks: 'initial' // only package third parties that are initially dependent
  141. },
  142. elementUI: {
  143. name: 'chunk-elementUI', // split elementUI into a single package
  144. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  145. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  146. },
  147. commons: {
  148. name: 'chunk-commons',
  149. test: resolve('src/components'), // can customize your rules
  150. minChunks: 3, // minimum common number
  151. priority: 5,
  152. reuseExistingChunk: true
  153. }
  154. }
  155. })
  156. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  157. config.optimization.runtimeChunk('single')
  158. })
  159. }
  160. }