optimization.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* eslint-disable camelcase */
  2. const {ESBuildMinifyPlugin} = require('esbuild-loader');
  3. const TerserPlugin = require('terser-webpack-plugin');
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5. const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
  6. const isProduction = process.env.NODE_ENV === 'production';
  7. const optimization = {
  8. minimize: isProduction,
  9. minimizer: [
  10. new TerserPlugin({
  11. terserOptions: {
  12. parse: {
  13. // We want terser to parse ecma 8 code. However, we don't want it
  14. // to apply any minification steps that turns valid ecma 5 code
  15. // into invalid ecma 5 code. This is why the 'compress' and 'output'
  16. // sections only apply transformations that are ecma 5 safe
  17. // https://github.com/facebook/create-react-app/pull/4234
  18. ecma: 8,
  19. },
  20. compress: {
  21. ecma: 5,
  22. warnings: false,
  23. // Disabled because of an issue with Uglify breaking seemingly valid code:
  24. // https://github.com/facebook/create-react-app/issues/2376
  25. // Pending further investigation:
  26. // https://github.com/mishoo/UglifyJS2/issues/2011
  27. comparisons: false,
  28. // Disabled because of an issue with Terser breaking valid code:
  29. // https://github.com/facebook/create-react-app/issues/5250
  30. // Pending further investigation:
  31. // https://github.com/terser-js/terser/issues/120
  32. inline: 2,
  33. },
  34. mangle: {
  35. safari10: true,
  36. },
  37. // Added for profiling in devtools
  38. keep_classnames: isProduction,
  39. keep_fnames: isProduction,
  40. output: {
  41. ecma: 5,
  42. comments: false,
  43. // Turned on because emoji and regex is not minified properly using default
  44. // https://github.com/facebook/create-react-app/issues/2488
  45. ascii_only: true,
  46. },
  47. },
  48. }),
  49. // This is only used in production mode
  50. new CssMinimizerPlugin(),
  51. ],
  52. runtimeChunk: {
  53. name: entrypoint => `runtime~${entrypoint.name}`,
  54. },
  55. splitChunks: {
  56. chunks: 'async',
  57. minSize: 20000,
  58. minRemainingSize: 0,
  59. minChunks: 1,
  60. maxAsyncRequests: 30,
  61. maxInitialRequests: 30,
  62. enforceSizeThreshold: 50000,
  63. cacheGroups: {
  64. defaultVendors: {
  65. test: /[\\/]node_modules[\\/]/,
  66. priority: -10,
  67. reuseExistingChunk: true,
  68. },
  69. default: {
  70. minChunks: 2,
  71. priority: -20,
  72. reuseExistingChunk: true,
  73. },
  74. reactVendors: {
  75. name: 'react-vendors',
  76. chunks: 'all',
  77. test: /[\\/]node_modules[\\/](react|react-dom|react-dom-router|@remix-run|scheduler)/,
  78. priority: 10,
  79. reuseExistingChunk: true,
  80. },
  81. lottieVendors: {
  82. name: 'lottie',
  83. chunks: 'all',
  84. test: /[\\/]node_modules[\\/](lottie)/,
  85. priority: 10,
  86. reuseExistingChunk: true,
  87. },
  88. },
  89. },
  90. };
  91. module.exports = optimization;