@adamlui/minify.js 1.4.6 → 1.4.7
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 +4 -3
- package/minify.js +19 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
|
|
22
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>
|
|
23
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>
|
|
24
|
-
<a href="https://github.com/adamlui/js-utils/releases/tag/minify.js-1.4.
|
|
24
|
+
<a href="https://github.com/adamlui/js-utils/releases/tag/minify.js-1.4.7"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.7-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
25
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>
|
|
26
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>
|
|
27
27
|
|
|
@@ -218,8 +218,9 @@ console.log(searchResults);
|
|
|
218
218
|
|
|
219
219
|
/* sample output:
|
|
220
220
|
|
|
221
|
-
Searching for unminified JS files...
|
|
222
|
-
Search complete
|
|
221
|
+
findJS() » Searching for unminified JS files...
|
|
222
|
+
findJS() » Search complete! 2 files found.
|
|
223
|
+
findJS() » Check returned array.
|
|
223
224
|
[
|
|
224
225
|
'E:\\js\\utils\\minify.js\\assets\\js\\foo.js',
|
|
225
226
|
'E:\\js\\utils\\minify.js\\assets\\js\\bar.js'
|
package/minify.js
CHANGED
|
@@ -14,25 +14,23 @@ function findJS(searchDir, options = {}) {
|
|
|
14
14
|
options = { ...defaultOptions, ...options };
|
|
15
15
|
|
|
16
16
|
// Validate searchDir
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
else if (typeof searchDir !== 'string') return console.error(
|
|
20
|
-
'findJS() » ERROR: 1st arg `searchDir` must be a string.');
|
|
17
|
+
if (typeof searchDir !== 'string') return console.error(
|
|
18
|
+
'findJS() » ERROR: 1st arg <searchDir> must be a string.');
|
|
21
19
|
else { // verify searchDir path existence
|
|
22
20
|
const searchPath = path.resolve(process.cwd(), searchDir);
|
|
23
21
|
if (!fs.existsSync(searchPath)) return console.error(
|
|
24
|
-
'findJS() » ERROR:
|
|
25
|
-
|
|
22
|
+
'findJS() » ERROR: 1st arg <searchDir> must be an existing directory.\n'
|
|
23
|
+
+ `findJS() » ${ searchPath } does not exist.`);
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
// Validate options
|
|
29
27
|
for (const key of Object.keys(options)) {
|
|
30
28
|
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key))
|
|
31
29
|
if (key !== 'isRecursing') return console.error(
|
|
32
|
-
`findJS() » ERROR: \`${ key }\` is an invalid option
|
|
33
|
-
|
|
30
|
+
`findJS() » ERROR: \`${ key }\` is an invalid option.\n`
|
|
31
|
+
+ `findJS() » Valid options: [ ${ Object.keys(defaultOptions).join(', ') } ]`);
|
|
34
32
|
else if (typeof options[key] !== 'boolean') return console.error(
|
|
35
|
-
|
|
33
|
+
`findJS() » ERROR: \`${ key }\` option can only be set to \`true\` or \`false\`.`);
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
// Search for unminified JS
|
|
@@ -51,12 +49,11 @@ function findJS(searchDir, options = {}) {
|
|
|
51
49
|
});
|
|
52
50
|
|
|
53
51
|
// Log/return final result
|
|
54
|
-
if (!options.isRecursing && options.verbose)
|
|
55
|
-
|
|
56
|
-
+
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
52
|
+
if (!options.isRecursing && options.verbose) console.info(
|
|
53
|
+
'findJS() » Search complete! ' + ( jsFiles.length === 0 ? 'No' : jsFiles.length )
|
|
54
|
+
+ ` file${ jsFiles.length > 1 ? 's' : '' } found.`
|
|
55
|
+
+ ( findJS.caller.name !== 'minify' && require.main !== module ?
|
|
56
|
+
'\nfindJS() » Check returned array.' : '' ));
|
|
60
57
|
return options.isRecursing || jsFiles.length > 0 ? jsFiles : [];
|
|
61
58
|
}
|
|
62
59
|
|
|
@@ -69,15 +66,15 @@ function minify(input, options = {}) {
|
|
|
69
66
|
|
|
70
67
|
// Validate input
|
|
71
68
|
if (typeof input !== 'string') return console.error(
|
|
72
|
-
'minify() » ERROR:
|
|
69
|
+
'minify() » ERROR: 1st arg <input> must be a string.');
|
|
73
70
|
|
|
74
71
|
// Validate options
|
|
75
72
|
for (const key of Object.keys(options)) {
|
|
76
73
|
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key)) return console.error(
|
|
77
|
-
`minify() » ERROR: \`${ key }\` is an invalid option
|
|
78
|
-
|
|
74
|
+
`minify() » ERROR: \`${ key }\` is an invalid option.\n`
|
|
75
|
+
+ `minify() » Valid options: [ ${ Object.keys(defaultOptions).join(', ') } ]`);
|
|
79
76
|
else if (typeof options[key] !== 'boolean') return console.error(
|
|
80
|
-
`minify() » ERROR: \`${ key }\` option
|
|
77
|
+
`minify() » ERROR: \`${ key }\` option can only be set to \`true\` or \`false\`.`);
|
|
81
78
|
}
|
|
82
79
|
|
|
83
80
|
// Minify JS based on input
|
|
@@ -87,7 +84,6 @@ function minify(input, options = {}) {
|
|
|
87
84
|
if (options.verbose) console.info(`minify() » Minifying ${ input }...`);
|
|
88
85
|
const minifyResult = uglifyJS.minify(fs.readFileSync(input, 'utf8'), minifyOptions);
|
|
89
86
|
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
90
|
-
else console.info('minify() » Minification complete! Check returned object.');
|
|
91
87
|
return { code: minifyResult.code, srcPath: path.resolve(process.cwd(), input),
|
|
92
88
|
error: minifyResult.error };
|
|
93
89
|
} else { // dir path passed
|
|
@@ -98,7 +94,6 @@ function minify(input, options = {}) {
|
|
|
98
94
|
const srcCode = fs.readFileSync(jsPath, 'utf8'),
|
|
99
95
|
minifyResult = uglifyJS.minify(srcCode, minifyOptions);
|
|
100
96
|
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
101
|
-
else console.info('minify() » Minification complete! Check returned object.');
|
|
102
97
|
return { code: minifyResult.code, srcPath: jsPath, error: minifyResult.error };
|
|
103
98
|
}).filter(data => !data.error); // filter out failed minifications
|
|
104
99
|
}
|
|
@@ -106,7 +101,7 @@ function minify(input, options = {}) {
|
|
|
106
101
|
if (options.verbose) console.info('minify() » Minifying passed source code...');
|
|
107
102
|
const minifyResult = uglifyJS.minify(input, minifyOptions);
|
|
108
103
|
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
109
|
-
|
|
104
|
+
|
|
110
105
|
return { code: minifyResult.code, srcPath: undefined, error: minifyResult.error };
|
|
111
106
|
}
|
|
112
107
|
}
|
|
@@ -164,8 +159,8 @@ else { // run as CLI utility
|
|
|
164
159
|
// Validate input arg (output arg can be anything)
|
|
165
160
|
const inputPath = path.resolve(process.cwd(), inputArg);
|
|
166
161
|
if (inputArg && !fs.existsSync(inputPath)) {
|
|
167
|
-
console.error(`\n${br}Error: First argument
|
|
168
|
-
+ `\n
|
|
162
|
+
console.error(`\n${br}Error: First argument can only be an existing file or directory.`
|
|
163
|
+
+ `\n${ inputPath } does not exist.${nc}`
|
|
169
164
|
+ `\n\n${bg}Example valid command: \n>> minify-js . output.min.js${nc}`
|
|
170
165
|
+ `\n\n${by}For all command options: \n>> minify-js --help${nc}`);
|
|
171
166
|
process.exit(1);
|