webpack.prod.config.js 2.2 KB

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