vue.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: process.env.VUE_APP_PATH_ALIAS,
  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. // https: true,
  50. overlay: {
  51. warnings: false,
  52. errors: true
  53. },
  54. proxy: {
  55. // 配置跨域
  56. '/api': {
  57. target: 'https://jiasm.zfire.top/zfdapi/', // 测试服接口地址
  58. ws: true,
  59. changeOrigin: true,
  60. pathRewrite: {
  61. '^/api': ''
  62. }
  63. }
  64. },
  65. before: require('./mock/mock-server.js')
  66. },
  67. configureWebpack: {
  68. // provide the app's title in webpack's name field, so that
  69. // it can be accessed in index.html to inject the correct title.
  70. name: name,
  71. plugins:
  72. process.env.NODE_ENV === 'development'
  73. ? [new webpack.ProvidePlugin({}), chunkCount]
  74. : [new webpack.ProvidePlugin({}), compress, chunkCount],
  75. resolve: {
  76. alias: {
  77. '@': resolve('src'),
  78. 'element-ui': resolve('node_modules/@zjlib/element-ui2')
  79. // '@packages': resolve('../element-plugins/packages')
  80. },
  81. extensions: ['*', '.js', '.vue', '.json']
  82. },
  83. optimization: {
  84. minimizer: [
  85. new UglifyJsPlugin({
  86. uglifyOptions: {
  87. compress: {
  88. // warnings:false
  89. drop_debugger: true,
  90. drop_console: true,
  91. pure_funcs: ['console.log']
  92. }
  93. },
  94. sourceMap: false,
  95. parallel: true
  96. })
  97. ]
  98. }
  99. },
  100. chainWebpack(config) {
  101. // it can improve the speed of the first screen, it is recommended to turn on preload
  102. config.plugin('preload').tap(() => [
  103. {
  104. rel: 'preload',
  105. // to ignore runtime.js
  106. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  107. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  108. include: 'initial'
  109. }
  110. ])
  111. // when there are many pages, it will cause too many meaningless requests
  112. config.plugins.delete('prefetch')
  113. // set svg-sprite-loader
  114. config.module.rule('svg').exclude.add(resolve('src/icons')).end()
  115. config.module
  116. .rule('icons')
  117. .test(/\.svg$/)
  118. .include.add(resolve('src/icons'))
  119. .end()
  120. .use('svg-sprite-loader')
  121. .loader('svg-sprite-loader')
  122. .options({
  123. symbolId: 'icon-[name]'
  124. })
  125. .end()
  126. config.when(process.env.NODE_ENV !== 'development', config => {
  127. config
  128. .plugin('ScriptExtHtmlWebpackPlugin')
  129. .after('html')
  130. .use('script-ext-html-webpack-plugin', [
  131. {
  132. // `runtime` must same as runtimeChunk name. default is `runtime`
  133. inline: /runtime\..*\.js$/
  134. }
  135. ])
  136. .end()
  137. config.optimization.splitChunks({
  138. chunks: 'all',
  139. cacheGroups: {
  140. libs: {
  141. name: 'chunk-libs',
  142. test: /[\\/]node_modules[\\/]/,
  143. priority: 10,
  144. chunks: 'initial' // only package third parties that are initially dependent
  145. },
  146. elementUI: {
  147. name: 'chunk-elementUI', // split elementUI into a single package
  148. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  149. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  150. },
  151. commons: {
  152. name: 'chunk-commons',
  153. test: resolve('src/components'), // can customize your rules
  154. minChunks: 3, // minimum common number
  155. priority: 5,
  156. reuseExistingChunk: true
  157. }
  158. }
  159. })
  160. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  161. config.optimization.runtimeChunk('single')
  162. })
  163. }
  164. }