webpack.prod.config.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. const TerserPlugin = require('terser-webpack-plugin');
  9. module.exports = merge(common, {
  10. mode: 'production',
  11. output: {
  12. filename: 'js/[name].[chunkhash:8].js',
  13. },
  14. module: {
  15. rules: [
  16. {
  17. test: /\.css$/,
  18. use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
  19. },
  20. {
  21. test: /\.less$/,
  22. use: [
  23. MiniCssExtractPlugin.loader,
  24. 'css-loader',
  25. 'less-loader',
  26. 'postcss-loader',
  27. ],
  28. },
  29. // {
  30. // test: /\.(scss|sass)$/,
  31. // use: [
  32. // MiniCssExtractPlugin.loader,
  33. // 'css-loader',
  34. // 'postcss-loader',
  35. // 'sass-loader'
  36. // ]
  37. // },
  38. ],
  39. },
  40. plugins: [
  41. new HtmlWebpackPlugin({
  42. filename: 'index.html',
  43. template: 'public/index.html',
  44. inject: 'body',
  45. minify: {
  46. removeComments: true,
  47. collapseWhitespace: true,
  48. },
  49. }),
  50. new CleanWebpackPlugin(),
  51. new MiniCssExtractPlugin({
  52. filename: 'css/[name].[hash].css',
  53. chunkFilename: 'css/[id].[hash].css',
  54. }),
  55. ],
  56. optimization: {
  57. minimizer: [
  58. new UglifyJsPlugin(),
  59. new OptimizeCssAssetsPlugin({
  60. assetNameRegExp: /\.css$/g,
  61. cssProcessor: require('cssnano'),
  62. cssProcessorPluginOptions: {
  63. preset: ['default', { discardComments: { removeAll: true } }],
  64. },
  65. canPrint: true,
  66. }),
  67. // This is only used in production mode
  68. new TerserPlugin({
  69. terserOptions: {
  70. parse: {
  71. // We want terser to parse ecma 8 code. However, we don't want it
  72. // to apply any minification steps that turns valid ecma 5 code
  73. // into invalid ecma 5 code. This is why the 'compress' and 'output'
  74. // sections only apply transformations that are ecma 5 safe
  75. // https://github.com/facebook/create-react-app/pull/4234
  76. ecma: 8,
  77. },
  78. compress: {
  79. ecma: 5,
  80. warnings: false,
  81. // Disabled because of an issue with Uglify breaking seemingly valid code:
  82. // https://github.com/facebook/create-react-app/issues/2376
  83. // Pending further investigation:
  84. // https://github.com/mishoo/UglifyJS2/issues/2011
  85. comparisons: false,
  86. // Disabled because of an issue with Terser breaking valid code:
  87. // https://github.com/facebook/create-react-app/issues/5250
  88. // Pending further investigation:
  89. // https://github.com/terser-js/terser/issues/120
  90. inline: 2,
  91. },
  92. mangle: {
  93. safari10: true,
  94. },
  95. // Added for profiling in devtools
  96. keep_classnames: true,
  97. keep_fnames: true,
  98. output: {
  99. ecma: 5,
  100. comments: false,
  101. // Turned on because emoji and regex is not minified properly using default
  102. // https://github.com/facebook/create-react-app/issues/2488
  103. ascii_only: true,
  104. },
  105. },
  106. sourceMap: true,
  107. }),
  108. ],
  109. splitChunks: {
  110. chunks: 'all',
  111. minSize: 30000,
  112. maxSize: 0,
  113. minChunks: 1,
  114. cacheGroups: {
  115. framework: {
  116. test: 'framework',
  117. name: 'framework',
  118. enforce: true,
  119. },
  120. vendors: {
  121. priority: -10,
  122. test: /node_modules/,
  123. name: 'vendor',
  124. enforce: true,
  125. },
  126. },
  127. },
  128. },
  129. });