vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const CompressionWebpackPlugin = require('compression-webpack-plugin');
  5. const productionGzipExtensions = ['js', 'css']
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || 'vue Element Admin' // page title
  10. // const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  11. // If your port is set to 80,
  12. // use administrator privileges to execute the command line.
  13. // For example, Mac: sudo npm run
  14. // You can change the port by the following method:
  15. // port = 9527 npm run dev OR npm run dev --port = 9527
  16. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  17. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  18. module.exports = {
  19. /**
  20. * You will need to set publicPath if you plan to deploy your site under a sub path,
  21. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  22. * then publicPath should be set to "/bar/".
  23. * In most cases please use '/' !!!
  24. * Detail: https://cli.vuejs.org/config/#publicpath
  25. */
  26. publicPath: '/',
  27. outputDir: 'dist',
  28. assetsDir: 'static',
  29. lintOnSave: process.env.NODE_ENV === 'development',
  30. productionSourceMap: false,
  31. devServer: {
  32. port: port,
  33. open: true,
  34. overlay: {
  35. warnings: false,
  36. errors: true
  37. },
  38. before: require('./mock/mock-server.js')
  39. },
  40. configureWebpack: {
  41. // provide the app's title in webpack's name field, so that
  42. // it can be accessed in index.html to inject the correct title.
  43. name: name,
  44. resolve: {
  45. alias: {
  46. '@': resolve('src')
  47. }
  48. },
  49. plugins: [
  50. // new BundleAnalyzerPlugin(),//打包模块分析图
  51. new CompressionWebpackPlugin({//开启gzip打包
  52. filename:'[path].gz[query]',
  53. algorithm: 'gzip',
  54. test: new RegExp(
  55. '\\.(' +
  56. productionGzipExtensions.join('|') +
  57. ')$'
  58. ),
  59. threshold: 10240,
  60. // deleteOriginalAssets:true, //删除源文件,不建议
  61. minRatio: 0.8
  62. })
  63. ]
  64. },
  65. chainWebpack(config) {
  66. // it can improve the speed of the first screen, it is recommended to turn on preload
  67. // it can improve the speed of the first screen, it is recommended to turn on preload
  68. config.plugin('preload').tap(() => [
  69. {
  70. rel: 'preload',
  71. // to ignore runtime.js
  72. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  73. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  74. include: 'initial'
  75. }
  76. ])
  77. // when there are many pages, it will cause too many meaningless requests
  78. config.plugins.delete('prefetch')
  79. // set svg-sprite-loader
  80. config.module
  81. .rule('svg')
  82. .exclude.add(resolve('src/icons'))
  83. .end()
  84. config.module
  85. .rule('icons')
  86. .test(/\.svg$/)
  87. .include.add(resolve('src/icons'))
  88. .end()
  89. .use('svg-sprite-loader')
  90. .loader('svg-sprite-loader')
  91. .options({
  92. symbolId: 'icon-[name]'
  93. })
  94. .end()
  95. config
  96. .when(process.env.NODE_ENV !== 'development',
  97. config => {
  98. config
  99. .plugin('ScriptExtHtmlWebpackPlugin')
  100. .after('html')
  101. .use('script-ext-html-webpack-plugin', [{
  102. // `runtime` must same as runtimeChunk name. default is `runtime`
  103. inline: /runtime\..*\.js$/
  104. }])
  105. .end()
  106. config
  107. .optimization.splitChunks({
  108. chunks: 'all',
  109. automaticNameDelimiter:"-",
  110. maxInitialRequests: 3,
  111. maxAsyncRequests: 3,
  112. cacheGroups: {
  113. elementUI: {
  114. name: 'chunk-elementUI', // split elementUI into a single package
  115. priority: 40, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  116. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  117. },
  118. libs: {
  119. name: 'chunk-libs',
  120. test: /[\\/]node_modules[\\/]/,
  121. priority: 10,
  122. chunks: 'initial' // only package third parties that are initially dependent
  123. },
  124. components: {
  125. name: "chunk-components",
  126. test: resolve('src/components'),
  127. minChunks: 2,
  128. priority: 5,
  129. reuseExistingChunk: true
  130. }
  131. }
  132. })
  133. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  134. config.optimization.runtimeChunk('single')
  135. }
  136. )
  137. }
  138. }