@adamlui/minify.js 1.4.1 → 1.4.3
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 +40 -35
- package/docs/LICENSE.md +3 -2
- package/docs/SECURITY.md +2 -1
- package/minify.js +41 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,17 +9,18 @@
|
|
|
9
9
|
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/zh-tw#readme">繁體中文</a> |
|
|
10
10
|
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/hi#readme">हिंदी</a> |
|
|
11
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>
|
|
12
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/mr#readme">मराठी</a> |
|
|
13
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/pa#readme">ਪੰਜਾਬੀ</a>
|
|
13
14
|
</h6>
|
|
14
15
|
</div>
|
|
15
16
|
|
|
16
|
-
# </> minify.js
|
|
17
|
+
# </> minify.js
|
|
17
18
|
|
|
18
19
|
### Recursively minify all JavaScript files.
|
|
19
20
|
|
|
20
21
|
<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>
|
|
21
22
|
<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>
|
|
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.
|
|
23
|
+
<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.3-fc7811.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
23
24
|
<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>
|
|
24
25
|
<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>
|
|
25
26
|
|
|
@@ -113,20 +114,25 @@ minify-js input_folder output_folder
|
|
|
113
114
|
|
|
114
115
|
### Command line options
|
|
115
116
|
|
|
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.
|
|
117
|
+
#### Config options:
|
|
125
118
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
-
|
|
129
|
-
|
|
119
|
+
Flag | Description
|
|
120
|
+
-------------------------------|-------------------------------------------------------------------------
|
|
121
|
+
`-n` or `--dry-run` | Don't actually minify the file(s), just show if they will be processed.
|
|
122
|
+
`-d` or `--include-dotfolders` | Include dotfolders in file search.
|
|
123
|
+
`-D` or `--include-dotfiles` | Include dotfiles in file search.
|
|
124
|
+
`-R` or `--no-recursion` | Disable recursive file searching.
|
|
125
|
+
`-M` or `--no-mangle` | Disable mangling names.
|
|
126
|
+
`-q` or `--quiet` | Suppress all logging except errors.
|
|
127
|
+
|
|
128
|
+
#### Info commands:
|
|
129
|
+
|
|
130
|
+
Command | Description
|
|
131
|
+
--------------------|----------------------
|
|
132
|
+
`-h` or `--help` | Display help screen.
|
|
133
|
+
`-v` or `--version` | Show version number.
|
|
134
|
+
|
|
135
|
+
<br>
|
|
130
136
|
|
|
131
137
|
## 🔌 API reference
|
|
132
138
|
|
|
@@ -164,9 +170,8 @@ If a **directory path** is passed, JavaScript files are searched for (recursivel
|
|
|
164
170
|
```js
|
|
165
171
|
const results = minifyJS.minify('.');
|
|
166
172
|
results.forEach(result =>
|
|
167
|
-
console.log(result.srcPath) // paths to JS files in working directory + all nested directories
|
|
168
|
-
);
|
|
169
|
-
console.log(results[1].code) // minified code of 2nd JS file if found, or `undefined` if not found
|
|
173
|
+
console.log(result.srcPath)); // paths to JS files in working directory + all nested directories
|
|
174
|
+
console.log(results[1].code); // minified code of 2nd JS file if found, or `undefined` if not found
|
|
170
175
|
```
|
|
171
176
|
|
|
172
177
|
Options are boolean, passed as object properties. For example:
|
|
@@ -178,14 +183,15 @@ minifyJS.minify(input, { dotFiles: true });
|
|
|
178
183
|
|
|
179
184
|
Possible parameters (and their default settings) are:
|
|
180
185
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
Name | Desciption | Default value
|
|
187
|
+
-------------|---------------------------------------------------------|---------------
|
|
188
|
+
`recursive` | Recursively search for nested files if dir path passed. | `true`
|
|
189
|
+
`verbose` | Show logging in console/terminal. | `true`
|
|
190
|
+
`dotFolders` | Include dotfolders in file search. | `false`
|
|
191
|
+
`dotFiles` | Include dotfiles in file search. | `false`
|
|
192
|
+
`mangle` | Shorten variable names (typically to one character). | `true`
|
|
193
|
+
|
|
194
|
+
<br>
|
|
189
195
|
|
|
190
196
|
### findJS(searchDir, options)
|
|
191
197
|
|
|
@@ -200,13 +206,12 @@ minifyJS.findJS(searchDir, { recursive: false });
|
|
|
200
206
|
|
|
201
207
|
Possible parameters (and their default settings) are:
|
|
202
208
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
```
|
|
209
|
+
Name | Desciption | Default value
|
|
210
|
+
-------------|---------------------------------------------------------|---------------
|
|
211
|
+
`recursive` | Recursively search for nested files if dir path passed. | `true`
|
|
212
|
+
`verbose` | Show logging in console/terminal. | `true`
|
|
213
|
+
`dotFolders` | Include dotfolders in file search. | `false`
|
|
214
|
+
`dotFiles` | Include dotfiles in file search. | `false`
|
|
210
215
|
|
|
211
216
|
<br>
|
|
212
217
|
|
|
@@ -217,7 +222,7 @@ Please consider [giving a GitHub ⭐](https://github.com/adamlui/js-utils) if th
|
|
|
217
222
|
|
|
218
223
|
## 🏛️ MIT License
|
|
219
224
|
|
|
220
|
-
**Copyright © 2023–2024 [Adam Lui](https://github.com/adamlui)**
|
|
225
|
+
**Copyright © 2023–2024 [Adam Lui](https://github.com/adamlui) & contributors**
|
|
221
226
|
|
|
222
227
|
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
228
|
|
package/docs/LICENSE.md
CHANGED
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
<a href="zh-tw/LICENSE.md">繁體中文</a> |
|
|
10
10
|
<a href="hi/LICENSE.md">हिंदी</a> |
|
|
11
11
|
<a href="bn/LICENSE.md">বাংলা</a> |
|
|
12
|
-
<a href="mr/LICENSE.md">मराठी</a>
|
|
12
|
+
<a href="mr/LICENSE.md">मराठी</a> |
|
|
13
|
+
<a href="pa/LICENSE.md">ਪੰਜਾਬੀ</a>
|
|
13
14
|
</h6>
|
|
14
15
|
</div>
|
|
15
16
|
|
|
16
17
|
# 🏛️ MIT License
|
|
17
18
|
|
|
18
|
-
**Copyright © 2023–2024 [Adam Lui](https://github.com/adamlui)**
|
|
19
|
+
**Copyright © 2023–2024 [Adam Lui](https://github.com/adamlui) & contributors**
|
|
19
20
|
|
|
20
21
|
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
|
|
package/docs/SECURITY.md
CHANGED
package/minify.js
CHANGED
|
@@ -8,31 +8,47 @@ const fs = require('fs'),
|
|
|
8
8
|
// Define MAIN functions
|
|
9
9
|
|
|
10
10
|
function findJS(searchDir, options = {}) {
|
|
11
|
-
|
|
11
|
+
|
|
12
|
+
// Init options
|
|
13
|
+
const defaultOptions = { recursive: true, verbose: true, dotFolders: false, dotFiles: false };
|
|
12
14
|
options = { ...defaultOptions, ...options };
|
|
15
|
+
|
|
16
|
+
// Search for unminified JS
|
|
13
17
|
const dirFiles = fs.readdirSync(searchDir), jsFiles = [];
|
|
18
|
+
if (options.verbose && !options.isRecursing) console.info('\nSearching for unminified JS files...');
|
|
14
19
|
dirFiles.forEach(file => {
|
|
15
20
|
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}...`);
|
|
21
|
+
if (fs.statSync(filePath).isDirectory() && file != 'node_modules'
|
|
22
|
+
&& (options.dotFolders || !file.startsWith('.')) && options.recursive)
|
|
19
23
|
jsFiles.push( // recursively find unminified JS in eligible dir
|
|
20
24
|
...findJS(filePath, { ...options, isRecursing: true }));
|
|
21
|
-
|
|
22
|
-
(options.dotFiles || !file.startsWith('.')))
|
|
25
|
+
else if (/\.js(?<!\.min\.js)$/.test(file)
|
|
26
|
+
&& (options.dotFiles || !file.startsWith('.')))
|
|
23
27
|
jsFiles.push(filePath); // store eligible unminified JS file for minification
|
|
24
28
|
});
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
|
|
30
|
+
// Log/return final result
|
|
31
|
+
if (!options.isRecursing && options.verbose) {
|
|
32
|
+
console.info('Search complete. '
|
|
33
|
+
+ ( jsFiles.length === 0 ? 'No' : jsFiles.length )
|
|
34
|
+
+ ` file${ jsFiles.length > 1 ? 's' : '' } found.`);
|
|
35
|
+
}
|
|
36
|
+
return options.isRecursing || jsFiles.length > 0 ? jsFiles : [];
|
|
27
37
|
}
|
|
28
38
|
|
|
29
39
|
function minify(input, options = {}) {
|
|
40
|
+
|
|
41
|
+
// Init options
|
|
30
42
|
const defaultOptions = {
|
|
31
43
|
recursive: true, verbose: true, dotFolders: false, dotFiles: false, mangle: true };
|
|
32
44
|
options = { ...defaultOptions, ...options };
|
|
45
|
+
|
|
46
|
+
// Validate input
|
|
33
47
|
if (typeof input !== 'string')
|
|
34
48
|
return console.error('ERROR:'
|
|
35
49
|
+ ' First argument must be a string of source code or file/folder path.');
|
|
50
|
+
|
|
51
|
+
// Minify JS based on input
|
|
36
52
|
const minifyOptions = { mangle: options.mangle ? { toplevel: true } : false };
|
|
37
53
|
if (fs.existsSync(input)) { // minify based on path arg
|
|
38
54
|
if (input.endsWith('.js')) { // file path passed
|
|
@@ -41,9 +57,9 @@ function minify(input, options = {}) {
|
|
|
41
57
|
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
42
58
|
return { code: minifyResult.code, srcPath: input, error: minifyResult.error };
|
|
43
59
|
} 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
|
|
60
|
+
return findJS(input, { recursive: options.recursive, verbosity: options.verbose,
|
|
61
|
+
dotFolders: options.dotFolders, dotFiles: options.dotFiles
|
|
62
|
+
})?.map(jsPath => { // minify found JS files
|
|
47
63
|
if (options.verbose) console.info(`Minifying ${ jsPath }...`);
|
|
48
64
|
const srcCode = fs.readFileSync(jsPath, 'utf8'),
|
|
49
65
|
minifyResult = uglifyJS.minify(srcCode, minifyOptions);
|
|
@@ -65,7 +81,7 @@ if (require.main !== module) module.exports = { minify, findJS };
|
|
|
65
81
|
else { // run as CLI tool
|
|
66
82
|
|
|
67
83
|
// Init UI colors
|
|
68
|
-
const nc = '\x1b[0m',
|
|
84
|
+
const nc = '\x1b[0m', // no color
|
|
69
85
|
br = '\x1b[1;91m', // bright red
|
|
70
86
|
by = '\x1b[1;33m', // bright yellow
|
|
71
87
|
bg = '\x1b[1;92m'; // bright green
|
|
@@ -74,14 +90,14 @@ else { // run as CLI tool
|
|
|
74
90
|
const config = {
|
|
75
91
|
dryRun: process.argv.some(arg => /^--?(?:n|dry-?run)$/.test(arg)),
|
|
76
92
|
includeDotFolders: process.argv.some(arg =>
|
|
77
|
-
/^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?)$/.test(arg)),
|
|
93
|
+
/^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?=?(?:true|1)?)$/.test(arg)),
|
|
78
94
|
includeDotFiles: process.argv.some(arg =>
|
|
79
|
-
/^--?(?:df|D|(?:include-?)?dot-?files?)$/.test(arg)),
|
|
95
|
+
/^--?(?:df|D|(?:include-?)?dot-?files?=?(?:true|1)?)$/.test(arg)),
|
|
80
96
|
noRecursion: process.argv.some(arg =>
|
|
81
|
-
/^--?(?:R|(?:disable|no)-?recursion)$/.test(arg)),
|
|
97
|
+
/^--?(?:R|(?:disable|no)-?recursion|recursion=(?:false|0))$/.test(arg)),
|
|
82
98
|
noMangle: process.argv.some(arg =>
|
|
83
|
-
/^--?(?:M|(?:disable|no)-?mangle)$/.test(arg)),
|
|
84
|
-
quietMode: process.argv.some(arg => /^--?q(?:uiet)?$/.test(arg))
|
|
99
|
+
/^--?(?:M|(?:disable|no)-?mangle|mangle=(?:false|0))$/.test(arg)),
|
|
100
|
+
quietMode: process.argv.some(arg => /^--?q(?:uiet)?(?:-?mode)?$/.test(arg))
|
|
85
101
|
};
|
|
86
102
|
|
|
87
103
|
// Show HELP screen if -h or --help passed
|
|
@@ -131,18 +147,19 @@ else { // run as CLI tool
|
|
|
131
147
|
|
|
132
148
|
// Find all eligible JavaScript files or arg-passed file
|
|
133
149
|
const unminnedJSfiles = inputArg.endsWith('.js') ? [inputPath]
|
|
134
|
-
: findJS(inputPath, { recursive: !config.noRecursion });
|
|
150
|
+
: findJS(inputPath, { recursive: !config.noRecursion, verbose: !config.quietMode });
|
|
135
151
|
|
|
136
|
-
if (config.dryRun
|
|
137
|
-
|
|
138
|
-
|
|
152
|
+
if (config.dryRun) { // -n or --dry-run passed
|
|
153
|
+
if (unminnedJSfiles.length > 0) { // print files to be processed
|
|
154
|
+
console.info(`\n${by}JS files to be minified:${nc}`);
|
|
155
|
+
unminnedJSfiles.forEach(file => console.info(file));
|
|
156
|
+
} else console.info(`${by}\nNo JS files will be minified.${nc}`);
|
|
139
157
|
|
|
140
158
|
} else { // actually minify JavaScript files
|
|
141
|
-
printIfNotQuiet(''); // line break before first log
|
|
142
159
|
|
|
143
160
|
// Build array of minification data
|
|
144
161
|
const failedPaths = [];
|
|
145
|
-
const minifyData = unminnedJSfiles
|
|
162
|
+
const minifyData = unminnedJSfiles.map(jsPath => {
|
|
146
163
|
const minifyResult = minify(jsPath, { verbose: !config.quietMode, mangle: !config.noMangle });
|
|
147
164
|
if (minifyResult.error) failedPaths.push(jsPath);
|
|
148
165
|
return minifyResult;
|
|
@@ -171,7 +188,7 @@ else { // run as CLI tool
|
|
|
171
188
|
printIfNotQuiet(`\n${bg}Minification complete!${nc}`);
|
|
172
189
|
printIfNotQuiet(
|
|
173
190
|
`${ minifyData.length } file${ minifyData.length > 1 ? 's' : '' } minified.`);
|
|
174
|
-
} else printIfNotQuiet(`${by}No unminified JavaScript files processed
|
|
191
|
+
} else printIfNotQuiet(`${by}No unminified JavaScript files processed.${nc}`);
|
|
175
192
|
if (failedPaths.length > 0) {
|
|
176
193
|
printIfNotQuiet(`\n${br}`
|
|
177
194
|
+ `${ failedPaths.length } file${ failedPaths.length > 1 ? 's' : '' }`
|