@adamlui/minify.js 1.3.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -61
- package/docs/LICENSE.md +24 -21
- package/docs/SECURITY.md +20 -17
- package/minify.js +210 -200
- package/package.json +50 -48
package/README.md
CHANGED
|
@@ -6,7 +6,10 @@
|
|
|
6
6
|
</picture>
|
|
7
7
|
English |
|
|
8
8
|
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/zh-cn#readme">简体中文</a> |
|
|
9
|
-
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/zh-tw#readme">繁體中文</a>
|
|
9
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/zh-tw#readme">繁體中文</a> |
|
|
10
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/hi#readme">हिंदी</a> |
|
|
11
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/bn#readme">বাংলা</a> |
|
|
12
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/mr#readme">मराठी</a>
|
|
10
13
|
</h6>
|
|
11
14
|
</div>
|
|
12
15
|
|
|
@@ -16,7 +19,7 @@
|
|
|
16
19
|
|
|
17
20
|
<a href="https://www.npmjs.com/package/@adamlui/minify.js"><img height=31 src="https://img.shields.io/npm/dt/%40adamlui%2Fminify.js?logo=npm&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
18
21
|
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-red.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
19
|
-
<a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.
|
|
22
|
+
<a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.1-fc7811.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
20
23
|
<a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=code"><img height=31 src="https://img.shields.io/npm/unpacked-size/%40adamlui%2Fminify.js?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a>
|
|
21
24
|
<a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:minify.js/minify.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Aminify.js%2Fminify.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
|
|
22
25
|
|
|
@@ -34,12 +37,18 @@ As a **global utility**:
|
|
|
34
37
|
npm install -g @adamlui/minify.js
|
|
35
38
|
```
|
|
36
39
|
|
|
37
|
-
As a **dev dependency
|
|
40
|
+
As a **dev dependency** (e.g. for build scripts), from your project root:
|
|
38
41
|
|
|
39
42
|
```
|
|
40
43
|
npm install -D @adamlui/minify.js
|
|
41
44
|
```
|
|
42
45
|
|
|
46
|
+
As a **runtime dependency** (e.g. for on-the-fly minification), from your project root:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
npm install @adamlui/minify.js
|
|
50
|
+
```
|
|
51
|
+
|
|
43
52
|
## 💻 Command line usage
|
|
44
53
|
|
|
45
54
|
The basic **global command** is:
|
|
@@ -58,10 +67,10 @@ To specify **input/output** paths:
|
|
|
58
67
|
minify-js [input_path] [output_path]
|
|
59
68
|
```
|
|
60
69
|
|
|
61
|
-
- `[input_path]`: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
|
|
70
|
+
- `[input_path]`: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
|
|
62
71
|
- `[output_path]`: Path to file or directory where minified files will be stored, relative to original file location (if not provided, `min/` is used).
|
|
63
72
|
|
|
64
|
-
**💡 Note:** If folders are passed, files will be processed recursively.
|
|
73
|
+
**💡 Note:** If folders are passed, files will be processed recursively unless `-R` or `--no-recursion` is passed.
|
|
65
74
|
|
|
66
75
|
#
|
|
67
76
|
|
|
@@ -76,21 +85,6 @@ To use as a **package script**, in your project's `package.json`:
|
|
|
76
85
|
Replace `<minify-js-cmd>` with `minify-js` + optional args. Then, `npm run build:js` can be used to run the command.
|
|
77
86
|
<br><br>
|
|
78
87
|
|
|
79
|
-
### Command line options
|
|
80
|
-
|
|
81
|
-
```
|
|
82
|
-
Config options:
|
|
83
|
-
-n, --dry-run Don't actually minify the file(s), just show
|
|
84
|
-
if they will be processed.
|
|
85
|
-
-d, --include-dotfolders Include dotfolders in file search.
|
|
86
|
-
-D, --include-dotfiles Include dotfiles in file search.
|
|
87
|
-
-q, --quiet Suppress all logging except errors.
|
|
88
|
-
|
|
89
|
-
Info commands:
|
|
90
|
-
-h, --help Display help screen.
|
|
91
|
-
-v, --version Show version number.
|
|
92
|
-
```
|
|
93
|
-
|
|
94
88
|
### Example commands
|
|
95
89
|
|
|
96
90
|
Minify all JavaScript files in the **current directory** (outputs to `min/`):
|
|
@@ -117,52 +111,101 @@ Specify both **input and output** directories (outputs to `output_folder/`):
|
|
|
117
111
|
minify-js input_folder output_folder
|
|
118
112
|
```
|
|
119
113
|
|
|
120
|
-
|
|
114
|
+
### Command line options
|
|
121
115
|
|
|
122
|
-
|
|
116
|
+
```
|
|
117
|
+
Config options:
|
|
118
|
+
-n, --dry-run Don't actually minify the file(s), just show
|
|
119
|
+
if they will be processed.
|
|
120
|
+
-d, --include-dotfolders Include dotfolders in file search.
|
|
121
|
+
-D, --include-dotfiles Include dotfiles in file search.
|
|
122
|
+
-R, --no-recursion Disable recursive file searching.
|
|
123
|
+
-M, --no-mangle Disable mangling names.
|
|
124
|
+
-q, --quiet Suppress all logging except errors.
|
|
123
125
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
+
Info commands:
|
|
127
|
+
-h, --help Display help screen.
|
|
128
|
+
-v, --version Show version number.
|
|
126
129
|
```
|
|
127
130
|
|
|
128
|
-
|
|
131
|
+
## 🔌 API reference
|
|
129
132
|
|
|
130
|
-
|
|
133
|
+
You can also import **minify.js** into your app to use its API methods, both as an ECMAScript module or a CommonJS module.
|
|
131
134
|
|
|
132
|
-
|
|
135
|
+
#### ESM:
|
|
133
136
|
|
|
137
|
+
```js
|
|
138
|
+
import * as minifyJS from '@adamlui/minify.js';
|
|
134
139
|
```
|
|
135
|
-
|
|
136
|
-
|
|
140
|
+
|
|
141
|
+
#### CJS:
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
const minifyJS = require('@adamlui/minify.js');
|
|
137
145
|
```
|
|
138
146
|
|
|
139
|
-
### minify(input)
|
|
147
|
+
### minify(input, options)
|
|
140
148
|
|
|
141
|
-
|
|
149
|
+
This function minifies JavaScript code based on the string input supplied.
|
|
142
150
|
|
|
143
151
|
If **source code** is passed, it is directly minified, then an object containing `srcPath` + `code` + `error` is returned:
|
|
144
152
|
|
|
145
153
|
```js
|
|
146
154
|
const srcCode = 'function add(first, second) { return first + second; }',
|
|
147
|
-
|
|
148
|
-
console.log(
|
|
149
|
-
console.log(
|
|
155
|
+
minifyResult = minifyJS.minify(srcCode);
|
|
156
|
+
console.log(minifyResult.error); // runtime error, or `undefined` if no error
|
|
157
|
+
console.log(minifyResult.code); // minified output: function add(n,d){return n+d}
|
|
150
158
|
```
|
|
151
159
|
|
|
152
160
|
If a **file path** is passed, the file's code is loaded then minified, returning an object like above.
|
|
153
161
|
|
|
154
|
-
If a **directory path** is passed, JavaScript files are searched for (recursively by default), each is minified, then an array of objects containing `srcPath` + `code` + `error` is returned:
|
|
162
|
+
If a **directory path** is passed, JavaScript files are searched for (recursively by default), each one's code is loaded then minified, then an array of objects containing `srcPath` + `code` + `error` is returned:
|
|
155
163
|
|
|
156
164
|
```js
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
console.log(result.srcPath) // JS files in all
|
|
165
|
+
const results = minifyJS.minify('.');
|
|
166
|
+
results.forEach(result =>
|
|
167
|
+
console.log(result.srcPath) // paths to JS files in working directory + all nested directories
|
|
160
168
|
);
|
|
169
|
+
console.log(results[1].code) // minified code of 2nd JS file if found, or `undefined` if not found
|
|
170
|
+
```
|
|
161
171
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
);
|
|
172
|
+
Options are boolean, passed as object properties. For example:
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
minifyJS.minify(input, { dotFiles: true });
|
|
176
|
+
// returns data object where dotfiles are also processed if `input` is a path
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Possible parameters (and their default settings) are:
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
recursive (true) Recursively search for nested files if dir path
|
|
183
|
+
passed.
|
|
184
|
+
verbose (true) Show logging in console/terminal.
|
|
185
|
+
dotFolders (false) Include dotfolders in file search.
|
|
186
|
+
dotFiles (false) Include dotfiles in file search.
|
|
187
|
+
mangle (true) Shorten variable names (typically to one character).
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### findJS(searchDir, options)
|
|
191
|
+
|
|
192
|
+
This function searches for all unminified JavaScript files within the `searchDir` string passed (useful for discovering what files [`minify()`](#minifyinput-options) will process) and returns an array containing their filepaths.
|
|
193
|
+
|
|
194
|
+
Options are boolean, passed as object properties. For example:
|
|
195
|
+
|
|
196
|
+
```js
|
|
197
|
+
minifyJS.findJS(searchDir, { recursive: false });
|
|
198
|
+
// returns array containing filepaths to unminified JS files in exactly `searchDir`
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Possible parameters (and their default settings) are:
|
|
202
|
+
|
|
203
|
+
```
|
|
204
|
+
recursive (true) Recursively search for nested files if dir path
|
|
205
|
+
passed.
|
|
206
|
+
verbose (false) Show logging in console/terminal.
|
|
207
|
+
dotFolders (false) Include dotfolders in file search.
|
|
208
|
+
dotFiles (false) Include dotfiles in file search.
|
|
166
209
|
```
|
|
167
210
|
|
|
168
211
|
<br>
|
|
@@ -174,25 +217,13 @@ Please consider [giving a GitHub ⭐](https://github.com/adamlui/js-utils) if th
|
|
|
174
217
|
|
|
175
218
|
## 🏛️ MIT License
|
|
176
219
|
|
|
177
|
-
**Copyright
|
|
178
|
-
|
|
179
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
180
|
-
|
|
181
|
-
in
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
furnished to do so, subject to the following conditions:
|
|
185
|
-
|
|
186
|
-
The above copyright notice and this permission notice shall be included in all
|
|
187
|
-
copies or substantial portions of the Software.
|
|
188
|
-
|
|
189
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
190
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
191
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
192
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
193
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
194
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
195
|
-
SOFTWARE.
|
|
220
|
+
**Copyright © 2023–2024 [Adam Lui](https://github.com/adamlui)**
|
|
221
|
+
|
|
222
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
223
|
+
|
|
224
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
225
|
+
|
|
226
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
196
227
|
|
|
197
228
|
<br>
|
|
198
229
|
|
package/docs/LICENSE.md
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
<div align="right">
|
|
2
|
-
<h6>
|
|
3
|
-
<picture>
|
|
4
|
-
<source type="image/svg+xml" media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/white/icon32.svg">
|
|
5
|
-
<img height=14 src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/black/icon32.svg">
|
|
6
|
-
</picture>
|
|
7
|
-
English |
|
|
8
|
-
<a href="zh-cn/LICENSE.md">简体中文</a> |
|
|
9
|
-
<a href="zh-tw/LICENSE.md">繁體中文</a>
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
<div align="right">
|
|
2
|
+
<h6>
|
|
3
|
+
<picture>
|
|
4
|
+
<source type="image/svg+xml" media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/white/icon32.svg">
|
|
5
|
+
<img height=14 src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/black/icon32.svg">
|
|
6
|
+
</picture>
|
|
7
|
+
English |
|
|
8
|
+
<a href="zh-cn/LICENSE.md">简体中文</a> |
|
|
9
|
+
<a href="zh-tw/LICENSE.md">繁體中文</a> |
|
|
10
|
+
<a href="hi/LICENSE.md">हिंदी</a> |
|
|
11
|
+
<a href="bn/LICENSE.md">বাংলা</a> |
|
|
12
|
+
<a href="mr/LICENSE.md">मराठी</a>
|
|
13
|
+
</h6>
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
# 🏛️ MIT License
|
|
17
|
+
|
|
18
|
+
**Copyright © 2023–2024 [Adam Lui](https://github.com/adamlui)**
|
|
19
|
+
|
|
20
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
23
|
+
|
|
24
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/docs/SECURITY.md
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
<div align="right">
|
|
2
|
-
<h6>
|
|
3
|
-
<picture>
|
|
4
|
-
<source type="image/svg+xml" media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/white/icon32.svg">
|
|
5
|
-
<img height=14 src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/black/icon32.svg">
|
|
6
|
-
</picture>
|
|
7
|
-
English |
|
|
8
|
-
<a href="zh-cn/SECURITY.md">简体中文</a> |
|
|
9
|
-
<a href="zh-tw/SECURITY.md">繁體中文</a>
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
<div align="right">
|
|
2
|
+
<h6>
|
|
3
|
+
<picture>
|
|
4
|
+
<source type="image/svg+xml" media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/white/icon32.svg">
|
|
5
|
+
<img height=14 src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/earth-icon/black/icon32.svg">
|
|
6
|
+
</picture>
|
|
7
|
+
English |
|
|
8
|
+
<a href="zh-cn/SECURITY.md">简体中文</a> |
|
|
9
|
+
<a href="zh-tw/SECURITY.md">繁體中文</a> |
|
|
10
|
+
<a href="hi/SECURITY.md">हिंदी</a> |
|
|
11
|
+
<a href="bn/SECURITY.md">বাংলা</a> |
|
|
12
|
+
<a href="mr/SECURITY.md">मराठी</a>
|
|
13
|
+
</h6>
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
# 🛡️ Security Policy
|
|
17
|
+
|
|
18
|
+
If you find a vulnerability, please open a [draft security advisory](https://github.com/adamlui/js-utils/security/advisories/new).
|
|
19
|
+
|
|
20
|
+
Pull requests are also welcome, but for safety reasons, send an email to <adam@kudoai.com> and wait for a response before making it public.
|
package/minify.js
CHANGED
|
@@ -1,200 +1,210 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// Import LIBS
|
|
4
|
-
const fs = require('fs'),
|
|
5
|
-
path = require('path'),
|
|
6
|
-
uglifyJS = require('uglify-js');
|
|
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
|
-
printHelp(
|
|
90
|
-
|
|
91
|
-
printHelp('
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
printHelp('
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// Import LIBS
|
|
4
|
+
const fs = require('fs'),
|
|
5
|
+
path = require('path'),
|
|
6
|
+
uglifyJS = require('uglify-js');
|
|
7
|
+
|
|
8
|
+
// Define MAIN functions
|
|
9
|
+
|
|
10
|
+
function findJS(searchDir, options = {}) {
|
|
11
|
+
const defaultOptions = { recursive: true, verbose: false, dotFolders: false, dotFiles: false };
|
|
12
|
+
options = { ...defaultOptions, ...options };
|
|
13
|
+
const dirFiles = fs.readdirSync(searchDir), jsFiles = [];
|
|
14
|
+
dirFiles.forEach(file => {
|
|
15
|
+
const filePath = path.resolve(searchDir, file);
|
|
16
|
+
if (fs.statSync(filePath).isDirectory() && file != 'node_modules' &&
|
|
17
|
+
(options.dotFolders || !file.startsWith('.')) && options.recursive) {
|
|
18
|
+
if (options.verbose) console.info(`Searching for unminified JS files in: ${filePath}...`);
|
|
19
|
+
jsFiles.push( // recursively find unminified JS in eligible dir
|
|
20
|
+
...findJS(filePath, { ...options, isRecursing: true }));
|
|
21
|
+
} else if (/\.js(?<!\.min\.js)$/.test(file) &&
|
|
22
|
+
(options.dotFiles || !file.startsWith('.')))
|
|
23
|
+
jsFiles.push(filePath); // store eligible unminified JS file for minification
|
|
24
|
+
});
|
|
25
|
+
if (options.isRecursing || jsFiles.length > 0) return jsFiles;
|
|
26
|
+
else console.info('\nNo unminified JavaScript files found.');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function minify(input, options = {}) {
|
|
30
|
+
const defaultOptions = {
|
|
31
|
+
recursive: true, verbose: true, dotFolders: false, dotFiles: false, mangle: true };
|
|
32
|
+
options = { ...defaultOptions, ...options };
|
|
33
|
+
if (typeof input !== 'string')
|
|
34
|
+
return console.error('ERROR:'
|
|
35
|
+
+ ' First argument must be a string of source code or file/folder path.');
|
|
36
|
+
const minifyOptions = { mangle: options.mangle ? { toplevel: true } : false };
|
|
37
|
+
if (fs.existsSync(input)) { // minify based on path arg
|
|
38
|
+
if (input.endsWith('.js')) { // file path passed
|
|
39
|
+
if (options.verbose) console.info(`Minifying ${ input }...`);
|
|
40
|
+
const minifyResult = uglifyJS.minify(fs.readFileSync(input, 'utf8'), minifyOptions);
|
|
41
|
+
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
42
|
+
return { code: minifyResult.code, srcPath: input, error: minifyResult.error };
|
|
43
|
+
} else { // dir path passed
|
|
44
|
+
return findJS(input, { recursive: options.recursive,
|
|
45
|
+
dotFolders: options.dotFolders, dotFiles: options.dotFiles })
|
|
46
|
+
?.map(jsPath => { // minify found JS files
|
|
47
|
+
if (options.verbose) console.info(`Minifying ${ jsPath }...`);
|
|
48
|
+
const srcCode = fs.readFileSync(jsPath, 'utf8'),
|
|
49
|
+
minifyResult = uglifyJS.minify(srcCode, minifyOptions);
|
|
50
|
+
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
51
|
+
return { code: minifyResult.code, srcPath: jsPath, error: minifyResult.error };
|
|
52
|
+
}).filter(data => !data.error); // filter out failed minifications
|
|
53
|
+
}
|
|
54
|
+
} else { // minify based on src code arg
|
|
55
|
+
if (options.verbose) console.info('Minifying passed source code...');
|
|
56
|
+
const minifyResult = uglifyJS.minify(input, minifyOptions);
|
|
57
|
+
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
58
|
+
return { code: minifyResult.code, srcPath: input, error: minifyResult.error };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// EXPORT functions if script was required
|
|
63
|
+
if (require.main !== module) module.exports = { minify, findJS };
|
|
64
|
+
|
|
65
|
+
else { // run as CLI tool
|
|
66
|
+
|
|
67
|
+
// Init UI colors
|
|
68
|
+
const nc = '\x1b[0m', // no color
|
|
69
|
+
br = '\x1b[1;91m', // bright red
|
|
70
|
+
by = '\x1b[1;33m', // bright yellow
|
|
71
|
+
bg = '\x1b[1;92m'; // bright green
|
|
72
|
+
|
|
73
|
+
// Load FLAG settings
|
|
74
|
+
const config = {
|
|
75
|
+
dryRun: process.argv.some(arg => /^--?(?:n|dry-?run)$/.test(arg)),
|
|
76
|
+
includeDotFolders: process.argv.some(arg =>
|
|
77
|
+
/^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?)$/.test(arg)),
|
|
78
|
+
includeDotFiles: process.argv.some(arg =>
|
|
79
|
+
/^--?(?:df|D|(?:include-?)?dot-?files?)$/.test(arg)),
|
|
80
|
+
noRecursion: process.argv.some(arg =>
|
|
81
|
+
/^--?(?:R|(?:disable|no)-?recursion)$/.test(arg)),
|
|
82
|
+
noMangle: process.argv.some(arg =>
|
|
83
|
+
/^--?(?:M|(?:disable|no)-?mangle)$/.test(arg)),
|
|
84
|
+
quietMode: process.argv.some(arg => /^--?q(?:uiet)?$/.test(arg))
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// Show HELP screen if -h or --help passed
|
|
88
|
+
if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
|
|
89
|
+
printHelp(`\n${by}minify-js [inputPath] [outputPath] [options]${nc}`);
|
|
90
|
+
printHelp('\nPath arguments:');
|
|
91
|
+
printHelp(' [inputPath] '
|
|
92
|
+
+ 'Path to JS file or directory containing JS files to be minified,'
|
|
93
|
+
+ ' relative to the current working directory.');
|
|
94
|
+
printHelp(' [outputPath] '
|
|
95
|
+
+ 'Path to file or directory where minified files will be stored,'
|
|
96
|
+
+ ' relative to original file location (if not provided, min/ is used).');
|
|
97
|
+
printHelp('\nConfig options:');
|
|
98
|
+
printHelp(' -n, --dry-run Don\'t actually minify the file(s),'
|
|
99
|
+
+ ' just show if they will be processed.');
|
|
100
|
+
printHelp(' -d, --include-dotfolders Include dotfolders in file search.');
|
|
101
|
+
printHelp(' -D, --include-dotfiles Include dotfiles in file search.');
|
|
102
|
+
printHelp(' -R, --no-recursion Disable recursive file searching.');
|
|
103
|
+
printHelp(' -M, --no-mangle Disable mangling names.');
|
|
104
|
+
printHelp(' -q, --quiet Suppress all logging except errors.');
|
|
105
|
+
printHelp('\nInfo commands:');
|
|
106
|
+
printHelp(' -h, --help Display this help screen.');
|
|
107
|
+
printHelp(' -v, --version Show version number.');
|
|
108
|
+
|
|
109
|
+
// Show VERSION number if -v or --version passed
|
|
110
|
+
} else if (process.argv.some(arg => /^--?ve?r?s?i?o?n?$/.test(arg))) {
|
|
111
|
+
console.info('v' + require('./package.json').version);
|
|
112
|
+
|
|
113
|
+
} else { // run MAIN routine
|
|
114
|
+
|
|
115
|
+
// Init I/O args
|
|
116
|
+
const [inputArg = '', outputArg = ''] = ( // default to empty strings for error-less handling
|
|
117
|
+
process.argv.slice(2) // exclude executable and script paths
|
|
118
|
+
.filter(arg => !arg.startsWith('-')) // exclude flags
|
|
119
|
+
.map(arg => arg.replace(/^\/*/, '')) // clean leading slashes to avoid parsing system root
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
// Validate input arg (output arg can be anything)
|
|
123
|
+
const inputPath = path.resolve(process.cwd(), inputArg);
|
|
124
|
+
if (inputArg && !fs.existsSync(inputPath)) {
|
|
125
|
+
console.error(`\n${br}Error: First argument must be an existing file or directory.`
|
|
126
|
+
+ `'\n${ inputPath }' does not exist.${nc}`
|
|
127
|
+
+ `\n\n${bg}Example valid command: \n>> minify-js . output.min.js${nc}`
|
|
128
|
+
+ `\n\n${by}For all command options: \n>> minify-js --help${nc}`);
|
|
129
|
+
process.exit(1);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Find all eligible JavaScript files or arg-passed file
|
|
133
|
+
const unminnedJSfiles = inputArg.endsWith('.js') ? [inputPath]
|
|
134
|
+
: findJS(inputPath, { recursive: !config.noRecursion });
|
|
135
|
+
|
|
136
|
+
if (config.dryRun && unminnedJSfiles?.length > 0) { // print files to be processed
|
|
137
|
+
console.info(`\n${by}JS files to be minified:${nc}`);
|
|
138
|
+
unminnedJSfiles?.forEach(file => console.info(file));
|
|
139
|
+
|
|
140
|
+
} else { // actually minify JavaScript files
|
|
141
|
+
printIfNotQuiet(''); // line break before first log
|
|
142
|
+
|
|
143
|
+
// Build array of minification data
|
|
144
|
+
const failedPaths = [];
|
|
145
|
+
const minifyData = unminnedJSfiles?.map(jsPath => {
|
|
146
|
+
const minifyResult = minify(jsPath, { verbose: !config.quietMode, mangle: !config.noMangle });
|
|
147
|
+
if (minifyResult.error) failedPaths.push(jsPath);
|
|
148
|
+
return minifyResult;
|
|
149
|
+
}).filter(minifyResult => !minifyResult.error); // filter out failed minifications
|
|
150
|
+
|
|
151
|
+
// Write array data to files
|
|
152
|
+
minifyData?.forEach(({ code, srcPath }) => {
|
|
153
|
+
const outputDir = path.join(
|
|
154
|
+
path.dirname(srcPath), // path of file to be minified
|
|
155
|
+
/so?u?rce?$/.test(path.dirname(srcPath)) ? '../min' // + ../min/ if in *(src|source)/
|
|
156
|
+
: outputArg.endsWith('.js') ? path.dirname(outputArg) // or path from file output arg
|
|
157
|
+
: outputArg || 'min' // or path from folder output arg or min/ if no output arg passed
|
|
158
|
+
);
|
|
159
|
+
const outputFilename = (
|
|
160
|
+
outputArg.endsWith('.js') && inputArg.endsWith('.js')
|
|
161
|
+
? path.basename(outputArg).replace(/(\.min)?\.js$/, '')
|
|
162
|
+
: path.basename(srcPath, '.js')
|
|
163
|
+
) + '.min.js';
|
|
164
|
+
const outputPath = path.join(outputDir, outputFilename);
|
|
165
|
+
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
|
|
166
|
+
fs.writeFileSync(outputPath, code, 'utf8');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// Print final summary
|
|
170
|
+
if (minifyData?.length > 0) {
|
|
171
|
+
printIfNotQuiet(`\n${bg}Minification complete!${nc}`);
|
|
172
|
+
printIfNotQuiet(
|
|
173
|
+
`${ minifyData.length } file${ minifyData.length > 1 ? 's' : '' } minified.`);
|
|
174
|
+
} else printIfNotQuiet(`${by}No unminified JavaScript files processed successfully.${nc}`);
|
|
175
|
+
if (failedPaths.length > 0) {
|
|
176
|
+
printIfNotQuiet(`\n${br}`
|
|
177
|
+
+ `${ failedPaths.length } file${ failedPaths.length > 1 ? 's' : '' }`
|
|
178
|
+
+ ` failed to minify:${nc}`);
|
|
179
|
+
failedPaths.forEach(path => printIfNotQuiet(path));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Define LOGGING functions
|
|
185
|
+
|
|
186
|
+
function printHelp(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
|
|
187
|
+
const terminalWidth = process.stdout.columns || 80,
|
|
188
|
+
indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
|
|
189
|
+
|
|
190
|
+
// Split msg into lines of appropriate lengths
|
|
191
|
+
let currentLine = '';
|
|
192
|
+
words.forEach(word => {
|
|
193
|
+
const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
|
|
194
|
+
if (currentLine.length + word.length > lineLength) { // cap/store it
|
|
195
|
+
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
196
|
+
currentLine = '';
|
|
197
|
+
}
|
|
198
|
+
currentLine += word;
|
|
199
|
+
});
|
|
200
|
+
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
201
|
+
|
|
202
|
+
// Print formatted msg
|
|
203
|
+
lines.forEach((line, index) => console.info(
|
|
204
|
+
index === 0 ? line // print 1st line unindented
|
|
205
|
+
: ' '.repeat(indentation) + line // print subsequent lines indented
|
|
206
|
+
));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function printIfNotQuiet(msg) { if (!config.quietMode) console.info(msg); }
|
|
210
|
+
}
|
package/package.json
CHANGED
|
@@ -1,48 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@adamlui/minify.js",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Recursively minify all JavaScript files",
|
|
5
|
-
"author": {
|
|
6
|
-
"name": "Adam Lui",
|
|
7
|
-
"email": "adam@kudoai.com",
|
|
8
|
-
"url": "https://github.com/adamlui"
|
|
9
|
-
},
|
|
10
|
-
"homepage": "https://github.com/adamlui/js-utils",
|
|
11
|
-
"license": "MIT",
|
|
12
|
-
"main": "minify.js",
|
|
13
|
-
"bin": {
|
|
14
|
-
"minifyjs": "minify.js",
|
|
15
|
-
"minify-js": "minify.js"
|
|
16
|
-
},
|
|
17
|
-
"scripts": {
|
|
18
|
-
"test": "bash utils/test/minify.test.sh",
|
|
19
|
-
"bump:patch": "bash utils/bump.sh patch",
|
|
20
|
-
"bump:minor": "bash utils/bump.sh minor",
|
|
21
|
-
"bump:major": "bash utils/bump.sh major",
|
|
22
|
-
"publish:patch": "bash utils/bump.sh patch --publish",
|
|
23
|
-
"publish:minor": "bash utils/bump.sh minor --publish",
|
|
24
|
-
"publish:major": "bash utils/bump.sh major --publish"
|
|
25
|
-
},
|
|
26
|
-
"repository": {
|
|
27
|
-
"type": "git",
|
|
28
|
-
"url": "git+https://github.com/adamlui/js-utils.git"
|
|
29
|
-
},
|
|
30
|
-
"keywords": [
|
|
31
|
-
"javascript",
|
|
32
|
-
"
|
|
33
|
-
"js-utils",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@adamlui/minify.js",
|
|
3
|
+
"version": "1.4.1",
|
|
4
|
+
"description": "Recursively minify all JavaScript files",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Adam Lui",
|
|
7
|
+
"email": "adam@kudoai.com",
|
|
8
|
+
"url": "https://github.com/adamlui"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/adamlui/js-utils",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"main": "minify.js",
|
|
13
|
+
"bin": {
|
|
14
|
+
"minifyjs": "minify.js",
|
|
15
|
+
"minify-js": "minify.js"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "bash utils/test/minify.test.sh",
|
|
19
|
+
"bump:patch": "bash utils/bump.sh patch",
|
|
20
|
+
"bump:minor": "bash utils/bump.sh minor",
|
|
21
|
+
"bump:major": "bash utils/bump.sh major",
|
|
22
|
+
"publish:patch": "bash utils/bump.sh patch --publish",
|
|
23
|
+
"publish:minor": "bash utils/bump.sh minor --publish",
|
|
24
|
+
"publish:major": "bash utils/bump.sh major --publish"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/adamlui/js-utils.git"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"javascript",
|
|
32
|
+
"utilities",
|
|
33
|
+
"js-utils",
|
|
34
|
+
"minifier",
|
|
35
|
+
"api",
|
|
36
|
+
"js",
|
|
37
|
+
"cli",
|
|
38
|
+
"utils"
|
|
39
|
+
],
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/adamlui/js-utils/issues"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"uglify-js": "^3.17.4"
|
|
45
|
+
},
|
|
46
|
+
"funding": {
|
|
47
|
+
"type": "github",
|
|
48
|
+
"url": "https://github.com/sponsors/adamlui"
|
|
49
|
+
}
|
|
50
|
+
}
|