webpack.prod.conf.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const webpack = require('webpack')
  5. const config = require('../config')
  6. const { merge } = require('webpack-merge')
  7. const baseWebpackConfig = require('./webpack.base.conf')
  8. const CopyWebpackPlugin = require('copy-webpack-plugin')
  9. const HtmlWebpackPlugin = require('html-webpack-plugin')
  10. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  12. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  13. const env = require('../config/prod.env')
  14. const webpackConfig = merge(baseWebpackConfig, {
  15. mode: 'production',
  16. module: {
  17. rules: utils.styleLoaders({
  18. sourceMap: config.build.productionSourceMap,
  19. extract: true,
  20. usePostCSS: true
  21. })
  22. },
  23. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  24. output: {
  25. path: config.build.assetsRoot,
  26. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  27. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  28. },
  29. optimization: {
  30. splitChunks: {
  31. chunks: 'all',
  32. cacheGroups: {
  33. vendors: {
  34. name: 'vendor',
  35. test: /[\\/]node_modules[\\/]/,
  36. priority: -10,
  37. chunks: 'initial'
  38. },
  39. app: {
  40. name: 'app',
  41. test: /[\\/]src[\\/]/,
  42. priority: -20,
  43. chunks: 'initial'
  44. },
  45. commons: {
  46. name: 'commons',
  47. minChunks: 2,
  48. priority: -30,
  49. chunks: 'initial',
  50. reuseExistingChunk: true
  51. }
  52. }
  53. },
  54. runtimeChunk: {
  55. name: 'manifest'
  56. },
  57. minimizer: [
  58. new UglifyJsPlugin({
  59. uglifyOptions: {
  60. compress: {
  61. warnings: false,
  62. drop_console: true,
  63. drop_debugger: true
  64. }
  65. },
  66. sourceMap: config.build.productionSourceMap,
  67. parallel: true
  68. })
  69. ]
  70. },
  71. performance: {
  72. hints: 'warning',
  73. maxAssetSize: 1024000,
  74. maxEntrypointSize: 2048000,
  75. assetFilter: function (assetFilename) {
  76. return assetFilename.endsWith('.js') || assetFilename.endsWith('.css')
  77. }
  78. },
  79. plugins: [
  80. new webpack.DefinePlugin({
  81. 'process.env': env
  82. }),
  83. new ExtractTextPlugin({
  84. filename: utils.assetsPath('css/[name].[hash].css'),
  85. allChunks: true,
  86. }),
  87. new OptimizeCSSPlugin({
  88. cssProcessorOptions: config.build.productionSourceMap
  89. ? { safe: true, map: { inline: false } }
  90. : { safe: true }
  91. }),
  92. new HtmlWebpackPlugin({
  93. filename: config.build.index,
  94. template: 'index.html',
  95. inject: true,
  96. minify: {
  97. removeComments: true,
  98. collapseWhitespace: true,
  99. removeAttributeQuotes: true,
  100. minifyCSS: true,
  101. minifyJS: true
  102. },
  103. chunksSortMode: 'auto'
  104. }),
  105. new webpack.HashedModuleIdsPlugin(),
  106. new CopyWebpackPlugin([
  107. {
  108. from: path.resolve(__dirname, '../static'),
  109. to: config.build.assetsSubDirectory,
  110. ignore: ['.*']
  111. }
  112. ])
  113. ]
  114. })
  115. if (config.build.productionGzip) {
  116. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  117. webpackConfig.plugins.push(
  118. new CompressionWebpackPlugin({
  119. asset: '[path].gz[query]',
  120. algorithm: 'gzip',
  121. test: new RegExp(
  122. '\\.(' +
  123. config.build.productionGzipExtensions.join('|') +
  124. ')$'
  125. ),
  126. threshold: 10240,
  127. minRatio: 0.8
  128. })
  129. )
  130. }
  131. if (config.build.bundleAnalyzerReport) {
  132. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  133. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  134. }
  135. module.exports = webpackConfig