Skip to content

Commit 0ae385f

Browse files
committed
feat: add getTerserOptions with unsafeCompress option
1 parent ac69f4a commit 0ae385f

5 files changed

Lines changed: 81 additions & 44 deletions

File tree

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ The config is adapted based on `NODE_ENV`, so make sure to run your scripts with
4343

4444
**Note**: [`cross-env`](https://www.npmjs.com/package/cross-env) is an npm package that you need to install.
4545

46+
## Options
47+
48+
You can import the builder function to create a custom config:
49+
50+
```ts
51+
import { getTerserOptions } from "terser-config-atomic/dist/index.js"
52+
module.exports = getTerserOptions(process.env.NODE_ENV, process.env.BABEL_ENV)
53+
```
54+
55+
The builder function:
56+
57+
```ts
58+
/**
59+
Get the terser options for the given environment.
60+
61+
@param NODE_ENV - The Node environment (defaults to "production").
62+
@param BABEL_ENV - The Babel environment (defaults to NODE_ENV).
63+
@param unsafeCompress - Whether to use unsafe compression options (defaults to false).
64+
*/
65+
export function getTerserOptions(
66+
NODE_ENV: string = "production",
67+
BABEL_ENV: string | undefined = undefined,
68+
unsafeCompress: boolean = false,
69+
)
70+
```
71+
4672
## Modifying the config
4773

4874
To change the config use the following pattern:

__tests__/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "assert"
2-
import { getTerserOptions } from "../src/get-terserrc"
2+
import { getTerserOptions } from "../src"
33

44
describe("Terser-Config-Atomic", () => {
55
it("production", () => {
@@ -13,7 +13,7 @@ describe("Terser-Config-Atomic", () => {
1313
"@atom.inSpecMode": "() => false",
1414
"@atom.inDevMode": "() => false",
1515
})
16-
expect(TerserOptions.compress.passes).toBe(3)
16+
expect(TerserOptions.compress.passes).toBe(2)
1717
expect(TerserOptions.mangle).toBe(true)
1818
expect(TerserOptions.format.beautify).toBe(false)
1919
})
@@ -40,7 +40,7 @@ describe("Terser-Config-Atomic", () => {
4040
"@atom.inSpecMode": "() => true",
4141
"@atom.inDevMode": "() => false",
4242
})
43-
expect(TerserOptions.compress.passes).toBe(3)
43+
expect(TerserOptions.compress.passes).toBe(2)
4444
expect(TerserOptions.mangle).toBe(false)
4545
expect(TerserOptions.format.beautify).toBe(true)
4646
})

src/.terserrc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import { getTerserOptions } from "./get-terserrc.js"
1+
import { getTerserOptions } from "./index.js"
22
module.exports = getTerserOptions(process.env.NODE_ENV, process.env.BABEL_ENV)

src/get-terserrc.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)