webpack.prod.conf.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. module: {
  16. rules: utils.styleLoaders({
  17. sourceMap: config.build.productionSourceMap,
  18. extract: true,
  19. usePostCSS: true
  20. })
  21. },
  22. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  23. output: {
  24. path: config.build.assetsRoot,
  25. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  26. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  27. },
  28. optimization: {
  29. splitChunks: {
  30. chunks: 'all',
  31. cacheGroups: {
  32. vendors: {
  33. name: 'vendor',
  34. test: /[\\/]node_modules[\\/]/,
  35. priority: -10,
  36. chunks: 'initial'
  37. },
  38. app: {
  39. name: 'app',
  40. test: /[\\/]src[\\/]/,
  41. priority: -20,
  42. chunks: 'initial'
  43. }
  44. }
  45. },
  46. runtimeChunk: {
  47. name: 'manifest'
  48. }
  49. },
  50. plugins: [
  51. new webpack.DefinePlugin({
  52. 'process.env': env
  53. }),
  54. new UglifyJsPlugin({
  55. uglifyOptions: {
  56. compress: {
  57. warnings: false
  58. }
  59. },
  60. sourceMap: config.build.productionSourceMap,
  61. parallel: true
  62. }),
  63. new ExtractTextPlugin({
  64. filename: utils.assetsPath('css/[name].[hash].css'),
  65. allChunks: true,
  66. }),
  67. new OptimizeCSSPlugin({
  68. cssProcessorOptions: config.build.productionSourceMap
  69. ? { safe: true, map: { inline: false } }
  70. : { safe: true }
  71. }),
  72. new HtmlWebpackPlugin({
  73. filename: config.build.index,
  74. template: 'index.html',
  75. inject: true,
  76. minify: {
  77. removeComments: true,
  78. collapseWhitespace: true,
  79. removeAttributeQuotes: true
  80. },
  81. chunksSortMode: 'auto'
  82. }),
  83. new webpack.HashedModuleIdsPlugin(),
  84. new CopyWebpackPlugin([
  85. {
  86. from: path.resolve(__dirname, '../static'),
  87. to: config.build.assetsSubDirectory,
  88. ignore: ['.*']
  89. }
  90. ])
  91. ]
  92. })
  93. if (config.build.productionGzip) {
  94. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  95. webpackConfig.plugins.push(
  96. new CompressionWebpackPlugin({
  97. asset: '[path].gz[query]',
  98. algorithm: 'gzip',
  99. test: new RegExp(
  100. '\\.(' +
  101. config.build.productionGzipExtensions.join('|') +
  102. ')$'
  103. ),
  104. threshold: 10240,
  105. minRatio: 0.8
  106. })
  107. )
  108. }
  109. if (config.build.bundleAnalyzerReport) {
  110. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  111. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  112. }
  113. module.exports = webpackConfig