vue.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. },
  75. extensions: ['*', '.js', '.vue', '.json']
  76. },
  77. optimization: {
  78. minimizer: [
  79. new UglifyJsPlugin({
  80. uglifyOptions: {
  81. compress: {
  82. // warnings:false
  83. drop_debugger: true,
  84. drop_console: true,
  85. pure_funcs: ['console.log']
  86. }
  87. },
  88. sourceMap: false,
  89. parallel: true
  90. })
  91. ]
  92. }
  93. },
  94. chainWebpack(config) {
  95. // it can improve the speed of the first screen, it is recommended to turn on preload
  96. config.plugin('preload').tap(() => [
  97. {
  98. rel: 'preload',
  99. // to ignore runtime.js
  100. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  101. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  102. include: 'initial'
  103. }
  104. ])
  105. // when there are many pages, it will cause too many meaningless requests
  106. config.plugins.delete('prefetch')
  107. // set svg-sprite-loader
  108. config.module.rule('svg').exclude.add(resolve('src/icons')).end()
  109. config.module
  110. .rule('icons')
  111. .test(/\.svg$/)
  112. .include.add(resolve('src/icons'))
  113. .end()
  114. .use('svg-sprite-loader')
  115. .loader('svg-sprite-loader')
  116. .options({
  117. symbolId: 'icon-[name]'
  118. })
  119. .end()
  120. config.when(process.env.NODE_ENV !== 'development', config => {
  121. config
  122. .plugin('ScriptExtHtmlWebpackPlugin')
  123. .after('html')
  124. .use('script-ext-html-webpack-plugin', [
  125. {
  126. // `runtime` must same as runtimeChunk name. default is `runtime`
  127. inline: /runtime\..*\.js$/
  128. }
  129. ])
  130. .end()
  131. config.optimization.splitChunks({
  132. chunks: 'all',
  133. cacheGroups: {
  134. libs: {
  135. name: 'chunk-libs',
  136. test: /[\\/]node_modules[\\/]/,
  137. priority: 10,
  138. chunks: 'initial' // only package third parties that are initially dependent
  139. },
  140. elementUI: {
  141. name: 'chunk-elementUI', // split elementUI into a single package
  142. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  143. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  144. },
  145. commons: {
  146. name: 'chunk-commons',
  147. test: resolve('src/components'), // can customize your rules
  148. minChunks: 3, // minimum common number
  149. priority: 5,
  150. reuseExistingChunk: true
  151. }
  152. }
  153. })
  154. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  155. config.optimization.runtimeChunk('single')
  156. })
  157. }
  158. }