webpack.prod.config.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * @Author: Johnhong9527
  3. * @Date: 2020-05-12 16:35:36
  4. * @Last Modified by: Johnhong9527
  5. * @Last Modified time: 2020-05-12 16:35:44
  6. */
  7. const merge = require('webpack-merge');
  8. const common = require('./webpack.common.config.js');
  9. const HtmlWebpackPlugin = require('html-webpack-plugin');
  10. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  11. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  12. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  13. const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  14. const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
  15. module.exports = merge(common, {
  16. mode: 'production',
  17. stats: 'detailed',
  18. module: {
  19. rules: [
  20. {
  21. test: /\.css$/,
  22. use: [
  23. MiniCssExtractPlugin.loader,
  24. // 'style-loader',
  25. 'css-loader',
  26. 'postcss-loader',
  27. 'less-loader',
  28. ],
  29. },
  30. {
  31. test: /\.less$/,
  32. use: [
  33. MiniCssExtractPlugin.loader,
  34. // 'style-loader',
  35. 'css-loader',
  36. 'postcss-loader',
  37. 'less-loader',
  38. ],
  39. },
  40. // {
  41. // test: /\.(scss|sass)$/,
  42. // use: [
  43. // MiniCssExtractPlugin.loader,
  44. // 'css-loader',
  45. // 'postcss-loader',
  46. // 'sass-loader'
  47. // ]
  48. // },
  49. ],
  50. },
  51. plugins: [
  52. new HtmlWebpackPlugin({
  53. filename: 'index.html',
  54. template: 'public/index.html',
  55. inject: 'body',
  56. minify: {
  57. removeComments: true,
  58. collapseWhitespace: true,
  59. },
  60. }),
  61. new CleanWebpackPlugin(),
  62. new CleanWebpackPlugin(),
  63. new FriendlyErrorsWebpackPlugin(),
  64. new MiniCssExtractPlugin({
  65. filename: 'css/[name].[hash].css',
  66. chunkFilename: 'css/[id].[hash].css',
  67. }),
  68. ],
  69. optimization: {
  70. minimizer: [
  71. new UglifyJsPlugin(),
  72. new OptimizeCssAssetsPlugin({
  73. assetNameRegExp: /\.css$/g,
  74. cssProcessor: require('cssnano'),
  75. cssProcessorPluginOptions: {
  76. preset: ['default', { discardComments: { removeAll: true } }],
  77. },
  78. canPrint: true,
  79. }),
  80. ],
  81. splitChunks: {
  82. chunks: 'all',
  83. minSize: 30000,
  84. maxSize: 0,
  85. minChunks: 1,
  86. cacheGroups: {
  87. framework: {
  88. test: 'framework',
  89. name: 'framework',
  90. enforce: true,
  91. },
  92. vendors: {
  93. priority: -10,
  94. test: /node_modules/,
  95. name: 'vendor',
  96. enforce: true,
  97. },
  98. },
  99. },
  100. },
  101. });