terserOptions.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // @ts-check
  2. const TerserPlugin = require('terser-webpack-plugin')
  3. const genTerserOptions = (defaultOptions, options) => {
  4. const userOptions = options.terser && options.terser.terserOptions
  5. // user's config is first
  6. return {
  7. ...defaultOptions,
  8. ...userOptions
  9. }
  10. }
  11. const terserMinify = (options) => ({
  12. terserOptions: genTerserOptions(
  13. {
  14. compress: {
  15. // turn off flags with small gains to speed up minification
  16. arrows: false,
  17. collapse_vars: false, // 0.3kb
  18. comparisons: false,
  19. computed_props: false,
  20. hoist_funs: false,
  21. hoist_props: false,
  22. hoist_vars: false,
  23. inline: false,
  24. loops: false,
  25. negate_iife: false,
  26. properties: false,
  27. reduce_funcs: false,
  28. reduce_vars: false,
  29. switches: false,
  30. toplevel: false,
  31. typeofs: false,
  32. // a few flags with noticeable gains/speed ratio
  33. // numbers based on out of the box vendor bundle
  34. booleans: true, // 0.7kb
  35. if_return: true, // 0.4kb
  36. sequences: true, // 0.7kb
  37. unused: true, // 2.3kb
  38. // required features to drop conditional branches
  39. conditionals: true,
  40. dead_code: true,
  41. evaluate: true
  42. },
  43. mangle: {
  44. safari10: true
  45. }
  46. },
  47. options
  48. ),
  49. parallel: options.parallel,
  50. extractComments: false
  51. })
  52. // `terserOptions` options will be passed to `esbuild`
  53. // Link to options - https://esbuild.github.io/api/#minify
  54. const esbuildMinify = (options) => ({
  55. minify: TerserPlugin.esbuildMinify,
  56. terserOptions: genTerserOptions(
  57. {
  58. minify: false,
  59. minifyWhitespace: true,
  60. minifyIdentifiers: false,
  61. minifySyntax: true
  62. },
  63. options
  64. ),
  65. parallel: options.parallel
  66. })
  67. // `terserOptions` options will be passed to `swc` (`@swc/core`)
  68. // Link to options - https://swc.rs/docs/config-js-minify
  69. const swcMinify = (options) => ({
  70. minify: TerserPlugin.swcMinify,
  71. terserOptions: genTerserOptions(
  72. {
  73. compress: {
  74. unused: true
  75. },
  76. mangle: true
  77. },
  78. options
  79. ),
  80. parallel: options.parallel
  81. })
  82. // `terserOptions` options will be passed to `uglify-js`
  83. // Link to options - https://github.com/mishoo/UglifyJS#minify-options
  84. const uglifyJsMinify = (options) => ({
  85. minify: TerserPlugin.uglifyJsMinify,
  86. terserOptions: genTerserOptions({}, options),
  87. parallel: options.parallel
  88. })
  89. // Currently we do not allow custom minify function
  90. const getMinify = (options) => {
  91. const { minify = 'terser' } = options.terser || {}
  92. const minifyMap = {
  93. terser: terserMinify,
  94. esbuild: esbuildMinify,
  95. swc: swcMinify,
  96. uglifyJs: uglifyJsMinify
  97. }
  98. return minifyMap[minify](options)
  99. }
  100. module.exports = getMinify