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