webpack.prod.conf.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 MiniCssExtractPlugin = require('mini-css-extract-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. },
  28. plugins: [
  29. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  30. new webpack.DefinePlugin({
  31. 'process.env': env
  32. }),
  33. // extract css into its own file
  34. new MiniCssExtractPlugin({
  35. filename: utils.assetsPath('css/[name].[chunkhash].css'),
  36. }),
  37. // Compress extracted CSS. We are using this plugin so that possible
  38. // duplicated CSS from different components can be deduped.
  39. new OptimizeCSSPlugin({
  40. cssProcessorOptions: config.build.productionSourceMap
  41. ? { safe: true, map: { inline: false } }
  42. : { safe: true }
  43. }),
  44. // generate dist index.html with correct asset hash for caching.
  45. // you can customize output by editing /index.html
  46. // see https://github.com/ampedandwired/html-webpack-plugin
  47. new HtmlWebpackPlugin({
  48. filename: config.build.index,
  49. template: 'index.html',
  50. inject: true,
  51. minify: {
  52. removeComments: true,
  53. collapseWhitespace: true,
  54. removeAttributeQuotes: true
  55. // more options:
  56. // https://github.com/kangax/html-minifier#options-quick-reference
  57. },
  58. // necessary to consistently work with multiple chunks
  59. chunksSortMode: 'dependency'
  60. }),
  61. // keep module.id stable when vendor modules does not change
  62. new webpack.NamedChunksPlugin(),
  63. new webpack.HashedModuleIdsPlugin(),
  64. // copy custom static assets
  65. new CopyWebpackPlugin([
  66. {
  67. from: path.resolve(__dirname, '../static'),
  68. to: config.build.assetsSubDirectory,
  69. ignore: ['.*']
  70. }
  71. ])
  72. ],
  73. optimization: {
  74. concatenateModules: true,
  75. splitChunks: {
  76. chunks: 'all',
  77. cacheGroups: {
  78. vendor: {
  79. name: 'vendor',
  80. test: /[\\/]node_modules[\\/]/,
  81. enforce: true,
  82. },
  83. },
  84. },
  85. runtimeChunk: 'single',
  86. minimizer: [
  87. new UglifyJsPlugin({
  88. uglifyOptions: {
  89. compress: {
  90. warnings: false
  91. }
  92. },
  93. sourceMap: config.build.productionSourceMap,
  94. parallel: true
  95. }),
  96. ],
  97. },
  98. })
  99. if (config.build.productionGzip) {
  100. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  101. webpackConfig.plugins.push(
  102. new CompressionWebpackPlugin({
  103. asset: '[path].gz[query]',
  104. algorithm: 'gzip',
  105. test: new RegExp(
  106. '\\.(' +
  107. config.build.productionGzipExtensions.join('|') +
  108. ')$'
  109. ),
  110. threshold: 10240,
  111. minRatio: 0.8
  112. })
  113. )
  114. }
  115. if (config.build.bundleAnalyzerReport) {
  116. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  117. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  118. }
  119. module.exports = webpackConfig