Skip to content

Commit 3a30462

Browse files
committed
feat: Add minify options
1 parent fe9f3db commit 3a30462

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
1. [Getting started](#getting-started)
1616
- [Installation](#installation)
17+
- [Options](#options)
1718
2. [Usage](#usage)
1819
3. [Contributors](#contributors)
1920
4. [Need help](#need-help)
@@ -36,6 +37,16 @@ npm install minimize-js -D
3637
yarn add minimize-js -D
3738
```
3839

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+
3950
## Usage
4051

4152
The code snippet below shows how to put into action `minimize-js` and lets you minimize your files inside a directory.

index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,24 @@ const encoding = 'utf8'
77
const program = new Command()
88

99
async function getFiles(directory: string) {
10+
const options = program.opts()
1011
if (directory) {
1112
const files = glob.sync(`${directory}/**`, {})
12-
await minimization(files, { encoding })
13+
await minimization(files, { encoding, ...options })
1314
}
1415
}
1516

1617
async function run() {
1718
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')
1828
await program.parseAsync()
1929
}
2030

minimization.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ const Bar = require('progress-barjs')
44

55
type Opts = {
66
encoding: 'utf8'
7+
minifyWhitespace?: boolean
8+
minifyIdentifiers?: boolean
9+
minifySyntax?: boolean
710
}
811

912
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 }
1116
const filesFiltered = files.filter((f) => f.endsWith('.js'))
1217
const bar = Bar({
1318
label: 'Minimize JS',
@@ -19,9 +24,7 @@ export async function minimization(files: string[], opts: Opts) {
1924
const file = filesFiltered[i]
2025
if (file) {
2126
const content = readFileSync(file, { encoding })
22-
const { code } = transformSync(content, {
23-
minify: true,
24-
})
27+
const { code } = transformSync(content, options)
2528
if (code) {
2629
const c =
2730
code.search('##!/usr/bin/env') !== -1

0 commit comments

Comments
 (0)