File tree Expand file tree Collapse file tree 3 files changed +29
-5
lines changed Expand file tree Collapse file tree 3 files changed +29
-5
lines changed Original file line number Diff line number Diff line change 14
14
15
15
1 . [ Getting started] ( #getting-started )
16
16
- [ Installation] ( #installation )
17
+ - [ Options] ( #options )
17
18
2 . [ Usage] ( #usage )
18
19
3 . [ Contributors] ( #contributors )
19
20
4 . [ Need help] ( #need-help )
@@ -36,6 +37,16 @@ npm install minimize-js -D
36
37
yarn add minimize-js -D
37
38
```
38
39
40
+ ### Options
41
+
42
+ Minimize JS contains all options by default.
43
+
44
+ ``` bash
45
+ -w --minifyWhitespace It only removes whitespace characters
46
+ -i --minifyIdentifiers It only transforms the identifiers
47
+ -s --minifySyntax It only transforms the syntax
48
+ ```
49
+
39
50
## Usage
40
51
41
52
The code snippet below shows how to put into action ` minimize-js ` and lets you minimize your files inside a directory.
Original file line number Diff line number Diff line change @@ -7,14 +7,24 @@ const encoding = 'utf8'
7
7
const program = new Command ( )
8
8
9
9
async function getFiles ( directory : string ) {
10
+ const options = program . opts ( )
10
11
if ( directory ) {
11
12
const files = glob . sync ( `${ directory } /**` , { } )
12
- await minimization ( files , { encoding } )
13
+ await minimization ( files , { encoding, ... options } )
13
14
}
14
15
}
15
16
16
17
async function run ( ) {
17
18
program . argument ( '<directory>' , 'your files directory' ) . action ( getFiles )
19
+ program . option (
20
+ '-w, --minifyWhitespace' ,
21
+ 'It only removes whitespace characters'
22
+ )
23
+ program . option (
24
+ '-i, --minifyIdentifiers' ,
25
+ 'It only transforms the identifiers'
26
+ )
27
+ program . option ( '-s, --minifySyntax' , 'It only transforms the syntax' )
18
28
await program . parseAsync ( )
19
29
}
20
30
Original file line number Diff line number Diff line change @@ -4,10 +4,15 @@ const Bar = require('progress-barjs')
4
4
5
5
type Opts = {
6
6
encoding : 'utf8'
7
+ minifyWhitespace ?: boolean
8
+ minifyIdentifiers ?: boolean
9
+ minifySyntax ?: boolean
7
10
}
8
11
9
12
export async function minimization ( files : string [ ] , opts : Opts ) {
10
- const { encoding = 'utf8' } = opts
13
+ const { encoding = 'utf8' , ...transformOpts } = opts
14
+ const options =
15
+ Object . keys ( transformOpts ) . length > 0 ? transformOpts : { minify : true }
11
16
const filesFiltered = files . filter ( ( f ) => f . endsWith ( '.js' ) )
12
17
const bar = Bar ( {
13
18
label : 'Minimize JS' ,
@@ -19,9 +24,7 @@ export async function minimization(files: string[], opts: Opts) {
19
24
const file = filesFiltered [ i ]
20
25
if ( file ) {
21
26
const content = readFileSync ( file , { encoding } )
22
- const { code } = transformSync ( content , {
23
- minify : true ,
24
- } )
27
+ const { code } = transformSync ( content , options )
25
28
if ( code ) {
26
29
const c =
27
30
code . search ( '##!/usr/bin/env' ) !== - 1
You can’t perform that action at this time.
0 commit comments