@adamlui/minify.js 1.4.4 → 1.4.6
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 +18 -6
- package/docs/LICENSE.md +2 -1
- package/docs/SECURITY.md +2 -1
- package/minify.js +85 -49
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
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
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
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/pa#readme">ਪੰਜਾਬੀ</a> |
|
|
14
|
+
<a href="https://github.com/adamlui/js-utils/tree/main/minify.js/docs/de#readme">Deutsch</a>
|
|
14
15
|
</h6>
|
|
15
16
|
</div>
|
|
16
17
|
|
|
@@ -20,7 +21,7 @@
|
|
|
20
21
|
|
|
21
22
|
<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&color=af68ff&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
22
23
|
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
23
|
-
<a href="https://
|
|
24
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/minify.js-1.4.6"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.6-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
24
25
|
<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>
|
|
25
26
|
<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>
|
|
26
27
|
|
|
@@ -192,7 +193,7 @@ Options are boolean, passed as object properties. For example:
|
|
|
192
193
|
minifyJS.minify(input, { dotFiles: true });
|
|
193
194
|
```
|
|
194
195
|
|
|
195
|
-
|
|
196
|
+
Available parameters (and their default settings) are:
|
|
196
197
|
|
|
197
198
|
Name | Desciption | Default value
|
|
198
199
|
-------------|---------------------------------------------------------|---------------
|
|
@@ -211,11 +212,22 @@ Searches for all unminified JavaScript files within the `searchDir` string passe
|
|
|
211
212
|
Options are boolean, passed as object properties. For example:
|
|
212
213
|
|
|
213
214
|
```js
|
|
214
|
-
//
|
|
215
|
-
minifyJS.findJS(
|
|
215
|
+
// Search for unminified JS files in exactly assets/js:
|
|
216
|
+
const searchResults = minifyJS.findJS('assets/js', { recursive: false });
|
|
217
|
+
console.log(searchResults);
|
|
218
|
+
|
|
219
|
+
/* sample output:
|
|
220
|
+
|
|
221
|
+
Searching for unminified JS files...
|
|
222
|
+
Search complete. 2 files found.
|
|
223
|
+
[
|
|
224
|
+
'E:\\js\\utils\\minify.js\\assets\\js\\foo.js',
|
|
225
|
+
'E:\\js\\utils\\minify.js\\assets\\js\\bar.js'
|
|
226
|
+
]
|
|
227
|
+
*/
|
|
216
228
|
```
|
|
217
229
|
|
|
218
|
-
|
|
230
|
+
Available parameters (and their default settings) are:
|
|
219
231
|
|
|
220
232
|
Name | Desciption | Default value
|
|
221
233
|
-------------|---------------------------------------------------------|---------------
|
package/docs/LICENSE.md
CHANGED
package/docs/SECURITY.md
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
<a href="hi/SECURITY.md">हिंदी</a> |
|
|
11
11
|
<a href="bn/SECURITY.md">বাংলা</a> |
|
|
12
12
|
<a href="mr/SECURITY.md">मराठी</a> |
|
|
13
|
-
<a href="pa/SECURITY.md">ਪੰਜਾਬੀ</a>
|
|
13
|
+
<a href="pa/SECURITY.md">ਪੰਜਾਬੀ</a> |
|
|
14
|
+
<a href="de/SECURITY.md">Deutsch</a>
|
|
14
15
|
</h6>
|
|
15
16
|
</div>
|
|
16
17
|
|
package/minify.js
CHANGED
|
@@ -13,9 +13,32 @@ function findJS(searchDir, options = {}) {
|
|
|
13
13
|
const defaultOptions = { recursive: true, verbose: true, dotFolders: false, dotFiles: false };
|
|
14
14
|
options = { ...defaultOptions, ...options };
|
|
15
15
|
|
|
16
|
+
// Validate searchDir
|
|
17
|
+
if (!searchDir) return console.error(
|
|
18
|
+
'findJS() » ERROR: Please supply a `searchDir` as 1st arg.');
|
|
19
|
+
else if (typeof searchDir !== 'string') return console.error(
|
|
20
|
+
'findJS() » ERROR: 1st arg `searchDir` must be a string.');
|
|
21
|
+
else { // verify searchDir path existence
|
|
22
|
+
const searchPath = path.resolve(process.cwd(), searchDir);
|
|
23
|
+
if (!fs.existsSync(searchPath)) return console.error(
|
|
24
|
+
'findJS() » ERROR: Arg `searchDir` must be an existing directory.'
|
|
25
|
+
+ `\n'${ searchPath }' does not exist.`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Validate options
|
|
29
|
+
for (const key of Object.keys(options)) {
|
|
30
|
+
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key))
|
|
31
|
+
if (key !== 'isRecursing') return console.error(
|
|
32
|
+
`findJS() » ERROR: \`${ key }\` is an invalid option.`
|
|
33
|
+
+ `\nfindJS() » Valid options: [ ${Object.keys(defaultOptions).join(', ')} ]`);
|
|
34
|
+
else if (typeof options[key] !== 'boolean') return console.error(
|
|
35
|
+
`findJS() » ERROR: \`${ key }\` option must be set to \`true\` or \`false\`.`);
|
|
36
|
+
}
|
|
37
|
+
|
|
16
38
|
// Search for unminified JS
|
|
17
39
|
const dirFiles = fs.readdirSync(searchDir), jsFiles = [];
|
|
18
|
-
if (options.verbose && !options.isRecursing) console.info(
|
|
40
|
+
if (options.verbose && !options.isRecursing) console.info(
|
|
41
|
+
'\nfindJS() » Searching for unminified JS files...');
|
|
19
42
|
dirFiles.forEach(file => {
|
|
20
43
|
const filePath = path.resolve(searchDir, file);
|
|
21
44
|
if (fs.statSync(filePath).isDirectory() && file != 'node_modules'
|
|
@@ -29,9 +52,10 @@ function findJS(searchDir, options = {}) {
|
|
|
29
52
|
|
|
30
53
|
// Log/return final result
|
|
31
54
|
if (!options.isRecursing && options.verbose) {
|
|
32
|
-
console.info('Search complete. '
|
|
55
|
+
console.info('findJS() » Search complete. '
|
|
33
56
|
+ ( jsFiles.length === 0 ? 'No' : jsFiles.length )
|
|
34
|
-
|
|
57
|
+
+ ` file${ jsFiles.length > 1 ? 's' : '' } found.`
|
|
58
|
+
+ ( findJS.caller.name !== 'minify' ? '\nfindJS() » Check returned object.' : '' ));
|
|
35
59
|
}
|
|
36
60
|
return options.isRecursing || jsFiles.length > 0 ? jsFiles : [];
|
|
37
61
|
}
|
|
@@ -44,41 +68,53 @@ function minify(input, options = {}) {
|
|
|
44
68
|
options = { ...defaultOptions, ...options };
|
|
45
69
|
|
|
46
70
|
// Validate input
|
|
47
|
-
if (typeof input !== 'string')
|
|
48
|
-
|
|
49
|
-
|
|
71
|
+
if (typeof input !== 'string') return console.error(
|
|
72
|
+
'minify() » ERROR: Arg `input` must be a string.');
|
|
73
|
+
|
|
74
|
+
// Validate options
|
|
75
|
+
for (const key of Object.keys(options)) {
|
|
76
|
+
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key)) return console.error(
|
|
77
|
+
`minify() » ERROR: \`${ key }\` is an invalid option.`
|
|
78
|
+
+ `\nminify() » Valid options: [ ${Object.keys(defaultOptions).join(', ')} ]`);
|
|
79
|
+
else if (typeof options[key] !== 'boolean') return console.error(
|
|
80
|
+
`minify() » ERROR: \`${ key }\` option must be set to \`true\` or \`false\`.`);
|
|
81
|
+
}
|
|
50
82
|
|
|
51
83
|
// Minify JS based on input
|
|
52
84
|
const minifyOptions = { mangle: options.mangle ? { toplevel: true } : false };
|
|
53
85
|
if (fs.existsSync(input)) { // minify based on path arg
|
|
54
86
|
if (input.endsWith('.js')) { // file path passed
|
|
55
|
-
if (options.verbose) console.info(`Minifying ${ input }...`);
|
|
87
|
+
if (options.verbose) console.info(`minify() » Minifying ${ input }...`);
|
|
56
88
|
const minifyResult = uglifyJS.minify(fs.readFileSync(input, 'utf8'), minifyOptions);
|
|
57
|
-
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
58
|
-
|
|
89
|
+
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
90
|
+
else console.info('minify() » Minification complete! Check returned object.');
|
|
91
|
+
return { code: minifyResult.code, srcPath: path.resolve(process.cwd(), input),
|
|
92
|
+
error: minifyResult.error };
|
|
59
93
|
} else { // dir path passed
|
|
60
|
-
return findJS(input, { recursive: options.recursive,
|
|
94
|
+
return findJS(input, { recursive: options.recursive, verbose: options.verbose,
|
|
61
95
|
dotFolders: options.dotFolders, dotFiles: options.dotFiles
|
|
62
96
|
})?.map(jsPath => { // minify found JS files
|
|
63
|
-
if (options.verbose) console.info(`Minifying ${ jsPath }...`);
|
|
97
|
+
if (options.verbose) console.info(`minify() » Minifying ${ jsPath }...`);
|
|
64
98
|
const srcCode = fs.readFileSync(jsPath, 'utf8'),
|
|
65
99
|
minifyResult = uglifyJS.minify(srcCode, minifyOptions);
|
|
66
|
-
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
100
|
+
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
101
|
+
else console.info('minify() » Minification complete! Check returned object.');
|
|
67
102
|
return { code: minifyResult.code, srcPath: jsPath, error: minifyResult.error };
|
|
68
103
|
}).filter(data => !data.error); // filter out failed minifications
|
|
69
104
|
}
|
|
70
105
|
} else { // minify based on src code arg
|
|
71
|
-
if (options.verbose) console.info('Minifying passed source code...');
|
|
106
|
+
if (options.verbose) console.info('minify() » Minifying passed source code...');
|
|
72
107
|
const minifyResult = uglifyJS.minify(input, minifyOptions);
|
|
73
|
-
if (minifyResult.error) console.error(`ERROR: ${ minifyResult.error.message }`);
|
|
74
|
-
|
|
108
|
+
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
109
|
+
else console.info('minify() » Minification complete! Check returned object.');
|
|
110
|
+
return { code: minifyResult.code, srcPath: undefined, error: minifyResult.error };
|
|
75
111
|
}
|
|
76
112
|
}
|
|
77
113
|
|
|
78
|
-
// EXPORT functions if script was required
|
|
114
|
+
// EXPORT main functions if script was required
|
|
79
115
|
if (require.main !== module) module.exports = { minify, findJS };
|
|
80
116
|
|
|
81
|
-
else { // run as CLI
|
|
117
|
+
else { // run as CLI utility
|
|
82
118
|
|
|
83
119
|
// Init UI colors
|
|
84
120
|
const nc = '\x1b[0m', // no color
|
|
@@ -92,7 +128,7 @@ else { // run as CLI tool
|
|
|
92
128
|
'dryRun': /^--?(?:n|dry-?run)$/,
|
|
93
129
|
'includeDotFolders': /^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?=?(?:true|1)?)$/,
|
|
94
130
|
'includeDotFiles': /^--?(?:df|D|(?:include-?)?dot-?files?=?(?:true|1)?)$/,
|
|
95
|
-
'noRecursion': /^--?(?:R|(?:disable|no)-?
|
|
131
|
+
'noRecursion': /^--?(?:R|(?:disable|no)-?recursi(?:on|ve)|recursi(?:on|ve)=(?:false|0))$/,
|
|
96
132
|
'noMangle': /^--?(?:M|(?:disable|no)-?mangle|mangle=(?:false|0))$/,
|
|
97
133
|
'quietMode': /^--?q(?:uiet)?(?:-?mode)?$/,
|
|
98
134
|
'help': /^--?h(?:elp)?$/,
|
|
@@ -103,14 +139,14 @@ else { // run as CLI tool
|
|
|
103
139
|
const matchedFlag = Object.keys(argRegex).find(flag => argRegex[flag].test(arg));
|
|
104
140
|
if (matchedFlag) config[matchedFlag] = true;
|
|
105
141
|
else {
|
|
106
|
-
console.error(`\n${br}ERROR: Arg
|
|
142
|
+
console.error(`\n${br}ERROR: Arg [${ arg }] not recognized.${nc}`);
|
|
107
143
|
console.info(`\n${by}Valid arguments are below.${nc}`);
|
|
108
|
-
|
|
144
|
+
printHelpSections(['configOptions', 'infoCmds']);
|
|
109
145
|
process.exit(1);
|
|
110
146
|
}});
|
|
111
147
|
|
|
112
148
|
// Show HELP screen if -h or --help passed
|
|
113
|
-
if (process.argv.some(arg => argRegex.help.test(arg)))
|
|
149
|
+
if (process.argv.some(arg => argRegex.help.test(arg))) printHelpSections();
|
|
114
150
|
|
|
115
151
|
// Show VERSION number if -v or --version passed
|
|
116
152
|
else if (process.argv.some(arg => argRegex.version.test(arg)))
|
|
@@ -129,7 +165,7 @@ else { // run as CLI tool
|
|
|
129
165
|
const inputPath = path.resolve(process.cwd(), inputArg);
|
|
130
166
|
if (inputArg && !fs.existsSync(inputPath)) {
|
|
131
167
|
console.error(`\n${br}Error: First argument must be an existing file or directory.`
|
|
132
|
-
+
|
|
168
|
+
+ `\n'${ inputPath }' does not exist.${nc}`
|
|
133
169
|
+ `\n\n${bg}Example valid command: \n>> minify-js . output.min.js${nc}`
|
|
134
170
|
+ `\n\n${by}For all command options: \n>> minify-js --help${nc}`);
|
|
135
171
|
process.exit(1);
|
|
@@ -190,32 +226,9 @@ else { // run as CLI tool
|
|
|
190
226
|
|
|
191
227
|
// Define LOGGING functions
|
|
192
228
|
|
|
193
|
-
function
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
// Split msg into lines of appropriate lengths
|
|
198
|
-
let currentLine = '';
|
|
199
|
-
words.forEach(word => {
|
|
200
|
-
const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
|
|
201
|
-
if (currentLine.length + word.length > lineLength) { // cap/store it
|
|
202
|
-
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
203
|
-
currentLine = '';
|
|
204
|
-
}
|
|
205
|
-
currentLine += word;
|
|
206
|
-
});
|
|
207
|
-
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
208
|
-
|
|
209
|
-
// Print formatted msg
|
|
210
|
-
lines.forEach((line, index) => console.info(
|
|
211
|
-
index === 0 ? line // print 1st line unindented
|
|
212
|
-
: ' '.repeat(indentation) + line // print subsequent lines indented
|
|
213
|
-
));
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function printHelpScreen(includeSections = ['sampleCmd', 'pathArgs', 'configOptions', 'infoCmds']) {
|
|
217
|
-
const sections = {
|
|
218
|
-
'sampleCmd': [
|
|
229
|
+
function printHelpSections(includeSections = ['cmdFormat', 'pathArgs', 'configOptions', 'infoCmds']) {
|
|
230
|
+
const helpSections = {
|
|
231
|
+
'cmdFormat': [
|
|
219
232
|
`\n${by}minify-js [inputPath] [outputPath] [options]${nc}`
|
|
220
233
|
],
|
|
221
234
|
'pathArgs': [
|
|
@@ -244,7 +257,30 @@ else { // run as CLI tool
|
|
|
244
257
|
]
|
|
245
258
|
};
|
|
246
259
|
includeSections.forEach(section => { // print valid arg elems
|
|
247
|
-
|
|
260
|
+
helpSections[section]?.forEach(line => printHelpMsg(line)); });
|
|
261
|
+
|
|
262
|
+
function printHelpMsg(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
|
|
263
|
+
const terminalWidth = process.stdout.columns || 80,
|
|
264
|
+
indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
|
|
265
|
+
|
|
266
|
+
// Split msg into lines of appropriate lengths
|
|
267
|
+
let currentLine = '';
|
|
268
|
+
words.forEach(word => {
|
|
269
|
+
const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
|
|
270
|
+
if (currentLine.length + word.length > lineLength) { // cap/store it
|
|
271
|
+
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
272
|
+
currentLine = '';
|
|
273
|
+
}
|
|
274
|
+
currentLine += word;
|
|
275
|
+
});
|
|
276
|
+
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
277
|
+
|
|
278
|
+
// Print formatted msg
|
|
279
|
+
lines.forEach((line, index) => console.info(
|
|
280
|
+
index === 0 ? line // print 1st line unindented
|
|
281
|
+
: ' '.repeat(indentation) + line // print subsequent lines indented
|
|
282
|
+
));
|
|
283
|
+
}
|
|
248
284
|
}
|
|
249
285
|
|
|
250
286
|
function printIfNotQuiet(msg) { if (!config.quietMode) console.info(msg); }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamlui/minify.js",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.6",
|
|
4
4
|
"description": "Recursively minify all JavaScript files",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -15,10 +15,11 @@
|
|
|
15
15
|
"minify-js": "minify.js"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
-
"test": "bash utils/test/minify.test.sh",
|
|
18
|
+
"test": "bash utils/test/minify-cli.test.sh",
|
|
19
19
|
"bump:patch": "bash utils/bump.sh patch",
|
|
20
20
|
"bump:minor": "bash utils/bump.sh minor",
|
|
21
21
|
"bump:major": "bash utils/bump.sh major",
|
|
22
|
+
"prepublish": "npm test",
|
|
22
23
|
"publish:patch": "bash utils/bump.sh patch --publish",
|
|
23
24
|
"publish:minor": "bash utils/bump.sh minor --publish",
|
|
24
25
|
"publish:major": "bash utils/bump.sh major --publish"
|