|
| 1 | +/** |
| 2 | + Get the terser options for the given environment. |
| 3 | +
|
| 4 | + @param NODE_ENV - The Node environment (defaults to "production"). |
| 5 | + @param BABEL_ENV - The Babel environment (defaults to NODE_ENV). |
| 6 | + @param unsafeCompress - Whether to use unsafe compression options (defaults to false). |
| 7 | +*/ |
| 8 | +export function getTerserOptions( |
| 9 | + NODE_ENV: string = "production", |
| 10 | + BABEL_ENV: string | undefined = undefined, |
| 11 | + unsafeCompress: boolean = false, |
| 12 | +) { |
| 13 | + const isDev = NODE_ENV === "development" |
| 14 | + const isTest = NODE_ENV === "test" |
| 15 | + const isReadable = isDev || isTest |
| 16 | + |
| 17 | + const ProductionCompress = { |
| 18 | + global_defs: { |
| 19 | + // remove dev and test specific code for production |
| 20 | + "process.env.NODE_ENV": NODE_ENV || "production", |
| 21 | + "process.env.BABEL_ENV": BABEL_ENV || NODE_ENV || "production", |
| 22 | + "@atom.inSpecMode": !isTest ? "() => false" : "() => true", |
| 23 | + "@atom.inDevMode": !isDev ? "() => false" : "() => true", |
| 24 | + }, |
| 25 | + toplevel: unsafeCompress, |
| 26 | + hoist_vars: false, |
| 27 | + hoist_funs: true, |
| 28 | + pure_getters: unsafeCompress || "strict", |
| 29 | + unsafe: unsafeCompress, |
| 30 | + unsafe_arrows: unsafeCompress, |
| 31 | + unsafe_comps: unsafeCompress, |
| 32 | + unsafe_Function: unsafeCompress, |
| 33 | + unsafe_math: unsafeCompress, |
| 34 | + unsafe_symbols: unsafeCompress, |
| 35 | + unsafe_methods: unsafeCompress, |
| 36 | + unsafe_proto: unsafeCompress, |
| 37 | + unsafe_regexp: unsafeCompress, |
| 38 | + unsafe_undefined: unsafeCompress, |
| 39 | + passes: 2, |
| 40 | + } |
| 41 | + |
| 42 | + const TerserOptions = { |
| 43 | + // "module": false, // controlled by Parcel |
| 44 | + compress: isDev ? false : ProductionCompress, |
| 45 | + mangle: isReadable ? false : true, |
| 46 | + format: { |
| 47 | + beautify: isReadable, |
| 48 | + }, |
| 49 | + } |
| 50 | + return TerserOptions |
| 51 | +} |
0 commit comments