@adamlui/minify.js 1.4.6 → 1.4.8
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 +11 -10
- package/minify.js +37 -31
- package/package.json +7 -2
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.8"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.8-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
|
|
|
@@ -170,7 +170,7 @@ const srcCode = 'function add(first, second) { return first + second; }',
|
|
|
170
170
|
minifyResult = minifyJS.minify(srcCode);
|
|
171
171
|
|
|
172
172
|
console.log(minifyResult.error); // outputs runtime error, or `undefined` if no error
|
|
173
|
-
console.log(minifyResult.code); // outputs minified JS: function add(n,d){return n+d}
|
|
173
|
+
console.log(minifyResult.code); // outputs minified JS: 'function add(n,d){return n+d}'
|
|
174
174
|
```
|
|
175
175
|
|
|
176
176
|
If a **file path** is passed, the file's code is loaded then minified, returning an object like above.
|
|
@@ -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'
|
|
@@ -229,12 +230,12 @@ Search complete. 2 files found.
|
|
|
229
230
|
|
|
230
231
|
Available parameters (and their default settings) are:
|
|
231
232
|
|
|
232
|
-
Name | Desciption
|
|
233
|
-
|
|
234
|
-
`recursive` | Recursively search for nested files
|
|
235
|
-
`verbose` | Show logging in console/terminal.
|
|
236
|
-
`dotFolders` | Include dotfolders in file search.
|
|
237
|
-
`dotFiles` | Include dotfiles in file search.
|
|
233
|
+
Name | Desciption | Default value
|
|
234
|
+
-------------|-----------------------------------------------------------|---------------
|
|
235
|
+
`recursive` | Recursively search for nested files in searchDir passed. | `true`
|
|
236
|
+
`verbose` | Show logging in console/terminal. | `true`
|
|
237
|
+
`dotFolders` | Include dotfolders in file search. | `false`
|
|
238
|
+
`dotFiles` | Include dotfiles in file search. | `false`
|
|
238
239
|
|
|
239
240
|
<br>
|
|
240
241
|
|
package/minify.js
CHANGED
|
@@ -10,29 +10,32 @@ const fs = require('fs'),
|
|
|
10
10
|
function findJS(searchDir, options = {}) {
|
|
11
11
|
|
|
12
12
|
// Init options
|
|
13
|
-
const defaultOptions = {
|
|
13
|
+
const defaultOptions = {
|
|
14
|
+
recursive: true, // recursively search for nested files in searchDir passed
|
|
15
|
+
verbose: true, // enable logging
|
|
16
|
+
dotFolders: false, // include dotfolders in file search
|
|
17
|
+
dotFiles: false // include dotfiles in file search
|
|
18
|
+
};
|
|
14
19
|
options = { ...defaultOptions, ...options };
|
|
15
20
|
|
|
16
21
|
// Validate searchDir
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
else if (typeof searchDir !== 'string') return console.error(
|
|
20
|
-
'findJS() » ERROR: 1st arg `searchDir` must be a string.');
|
|
22
|
+
if (typeof searchDir !== 'string') return console.error(
|
|
23
|
+
'findJS() » ERROR: 1st arg <searchDir> must be a string.');
|
|
21
24
|
else { // verify searchDir path existence
|
|
22
25
|
const searchPath = path.resolve(process.cwd(), searchDir);
|
|
23
26
|
if (!fs.existsSync(searchPath)) return console.error(
|
|
24
|
-
'findJS() » ERROR:
|
|
25
|
-
|
|
27
|
+
'findJS() » ERROR: 1st arg <searchDir> must be an existing directory.\n'
|
|
28
|
+
+ `findJS() » ${ searchPath } does not exist.`);
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
// Validate options
|
|
29
|
-
for (const key
|
|
32
|
+
for (const key in options) {
|
|
30
33
|
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key))
|
|
31
34
|
if (key !== 'isRecursing') return console.error(
|
|
32
|
-
`findJS() » ERROR: \`${ key }\` is an invalid option
|
|
33
|
-
|
|
35
|
+
`findJS() » ERROR: \`${ key }\` is an invalid option.\n`
|
|
36
|
+
+ `findJS() » Valid options: [ ${ Object.keys(defaultOptions).join(', ') } ]`);
|
|
34
37
|
else if (typeof options[key] !== 'boolean') return console.error(
|
|
35
|
-
|
|
38
|
+
`findJS() » ERROR: [${ key }] option can only be set to \`true\` or \`false\`.`);
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
// Search for unminified JS
|
|
@@ -47,16 +50,15 @@ function findJS(searchDir, options = {}) {
|
|
|
47
50
|
...findJS(filePath, { ...options, isRecursing: true }));
|
|
48
51
|
else if (/\.js(?<!\.min\.js)$/.test(file)
|
|
49
52
|
&& (options.dotFiles || !file.startsWith('.')))
|
|
50
|
-
jsFiles.push(filePath); // store eligible unminified JS file for
|
|
53
|
+
jsFiles.push(filePath); // store eligible unminified JS file for returning
|
|
51
54
|
});
|
|
52
55
|
|
|
53
56
|
// Log/return final result
|
|
54
|
-
if (!options.isRecursing && options.verbose)
|
|
55
|
-
|
|
56
|
-
+ ( jsFiles.length === 0 ? 'No' : jsFiles.length )
|
|
57
|
+
if (!options.isRecursing && options.verbose) console.info(
|
|
58
|
+
'findJS() » Search complete! ' + ( jsFiles.length === 0 ? 'No' : jsFiles.length )
|
|
57
59
|
+ ` file${ jsFiles.length > 1 ? 's' : '' } found.`
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
+ ( findJS.caller.name !== 'minify' && require.main !== module ?
|
|
61
|
+
'\nfindJS() » Check returned array.' : '' ));
|
|
60
62
|
return options.isRecursing || jsFiles.length > 0 ? jsFiles : [];
|
|
61
63
|
}
|
|
62
64
|
|
|
@@ -64,20 +66,25 @@ function minify(input, options = {}) {
|
|
|
64
66
|
|
|
65
67
|
// Init options
|
|
66
68
|
const defaultOptions = {
|
|
67
|
-
recursive: true,
|
|
69
|
+
recursive: true, // recursively search for nested files if dir path passed
|
|
70
|
+
verbose: true, // enable logging
|
|
71
|
+
dotFolders: false, // include dotfolders in file search
|
|
72
|
+
dotFiles: false, // include dotfiles in file search
|
|
73
|
+
mangle: true // shorten var names (typically to one character)
|
|
74
|
+
};
|
|
68
75
|
options = { ...defaultOptions, ...options };
|
|
69
76
|
|
|
70
77
|
// Validate input
|
|
71
78
|
if (typeof input !== 'string') return console.error(
|
|
72
|
-
'minify() » ERROR:
|
|
79
|
+
'minify() » ERROR: 1st arg <input> must be a string.');
|
|
73
80
|
|
|
74
81
|
// Validate options
|
|
75
|
-
for (const key
|
|
82
|
+
for (const key in options) {
|
|
76
83
|
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key)) return console.error(
|
|
77
|
-
`minify() » ERROR: \`${ key }\` is an invalid option
|
|
78
|
-
|
|
84
|
+
`minify() » ERROR: \`${ key }\` is an invalid option.\n`
|
|
85
|
+
+ `minify() » Valid options: [ ${ Object.keys(defaultOptions).join(', ') } ]`);
|
|
79
86
|
else if (typeof options[key] !== 'boolean') return console.error(
|
|
80
|
-
`minify() » ERROR:
|
|
87
|
+
`minify() » ERROR: [${ key }] option can only be set to \`true\` or \`false\`.`);
|
|
81
88
|
}
|
|
82
89
|
|
|
83
90
|
// Minify JS based on input
|
|
@@ -87,7 +94,6 @@ function minify(input, options = {}) {
|
|
|
87
94
|
if (options.verbose) console.info(`minify() » Minifying ${ input }...`);
|
|
88
95
|
const minifyResult = uglifyJS.minify(fs.readFileSync(input, 'utf8'), minifyOptions);
|
|
89
96
|
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
90
|
-
else console.info('minify() » Minification complete! Check returned object.');
|
|
91
97
|
return { code: minifyResult.code, srcPath: path.resolve(process.cwd(), input),
|
|
92
98
|
error: minifyResult.error };
|
|
93
99
|
} else { // dir path passed
|
|
@@ -98,7 +104,6 @@ function minify(input, options = {}) {
|
|
|
98
104
|
const srcCode = fs.readFileSync(jsPath, 'utf8'),
|
|
99
105
|
minifyResult = uglifyJS.minify(srcCode, minifyOptions);
|
|
100
106
|
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
101
|
-
else console.info('minify() » Minification complete! Check returned object.');
|
|
102
107
|
return { code: minifyResult.code, srcPath: jsPath, error: minifyResult.error };
|
|
103
108
|
}).filter(data => !data.error); // filter out failed minifications
|
|
104
109
|
}
|
|
@@ -106,7 +111,7 @@ function minify(input, options = {}) {
|
|
|
106
111
|
if (options.verbose) console.info('minify() » Minifying passed source code...');
|
|
107
112
|
const minifyResult = uglifyJS.minify(input, minifyOptions);
|
|
108
113
|
if (minifyResult.error) console.error(`minify() » ERROR: ${ minifyResult.error.message }`);
|
|
109
|
-
|
|
114
|
+
|
|
110
115
|
return { code: minifyResult.code, srcPath: undefined, error: minifyResult.error };
|
|
111
116
|
}
|
|
112
117
|
}
|
|
@@ -164,8 +169,8 @@ else { // run as CLI utility
|
|
|
164
169
|
// Validate input arg (output arg can be anything)
|
|
165
170
|
const inputPath = path.resolve(process.cwd(), inputArg);
|
|
166
171
|
if (inputArg && !fs.existsSync(inputPath)) {
|
|
167
|
-
console.error(`\n${br}Error: First argument
|
|
168
|
-
+ `\n
|
|
172
|
+
console.error(`\n${br}Error: First argument can only be an existing file or directory.`
|
|
173
|
+
+ `\n${ inputPath } does not exist.${nc}`
|
|
169
174
|
+ `\n\n${bg}Example valid command: \n>> minify-js . output.min.js${nc}`
|
|
170
175
|
+ `\n\n${by}For all command options: \n>> minify-js --help${nc}`);
|
|
171
176
|
process.exit(1);
|
|
@@ -195,9 +200,10 @@ else { // run as CLI utility
|
|
|
195
200
|
minifyData?.forEach(({ code, srcPath }) => {
|
|
196
201
|
const outputDir = path.join(
|
|
197
202
|
path.dirname(srcPath), // path of file to be minified
|
|
198
|
-
/so?u?rce?$/.test(path.dirname(srcPath)) ?
|
|
199
|
-
|
|
200
|
-
|
|
203
|
+
/so?u?rce?$/.test(path.dirname(srcPath)) ? (
|
|
204
|
+
'../' + ( outputArg || 'min' ) // + ../outputArg|min/ if in *(src|source)/
|
|
205
|
+
) : outputArg.endsWith('.js') ? path.dirname(outputArg) // or path from file output arg
|
|
206
|
+
: outputArg || 'min' // or path from folder outputArg or min/ if no outputArg passed
|
|
201
207
|
);
|
|
202
208
|
const outputFilename = (
|
|
203
209
|
outputArg.endsWith('.js') && inputArg.endsWith('.js')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamlui/minify.js",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.8",
|
|
4
4
|
"description": "Recursively minify all JavaScript files",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -14,12 +14,17 @@
|
|
|
14
14
|
"minifyjs": "minify.js",
|
|
15
15
|
"minify-js": "minify.js"
|
|
16
16
|
},
|
|
17
|
+
"directories": {
|
|
18
|
+
"lib": ".",
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"test": "./utils/test"
|
|
21
|
+
},
|
|
17
22
|
"scripts": {
|
|
18
23
|
"test": "bash utils/test/minify-cli.test.sh",
|
|
19
24
|
"bump:patch": "bash utils/bump.sh patch",
|
|
20
25
|
"bump:minor": "bash utils/bump.sh minor",
|
|
21
26
|
"bump:major": "bash utils/bump.sh major",
|
|
22
|
-
"
|
|
27
|
+
"prepublishOnly": "npm test",
|
|
23
28
|
"publish:patch": "bash utils/bump.sh patch --publish",
|
|
24
29
|
"publish:minor": "bash utils/bump.sh minor --publish",
|
|
25
30
|
"publish:major": "bash utils/bump.sh major --publish"
|