@adamlui/minify.js 1.4.7 → 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 +8 -8
- package/minify.js +24 -13
- 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.
|
|
@@ -230,12 +230,12 @@ findJS() » Check returned array.
|
|
|
230
230
|
|
|
231
231
|
Available parameters (and their default settings) are:
|
|
232
232
|
|
|
233
|
-
Name | Desciption
|
|
234
|
-
|
|
235
|
-
`recursive` | Recursively search for nested files
|
|
236
|
-
`verbose` | Show logging in console/terminal.
|
|
237
|
-
`dotFolders` | Include dotfolders in file search.
|
|
238
|
-
`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`
|
|
239
239
|
|
|
240
240
|
<br>
|
|
241
241
|
|
package/minify.js
CHANGED
|
@@ -10,7 +10,12 @@ 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
|
|
@@ -24,13 +29,13 @@ function findJS(searchDir, options = {}) {
|
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
// Validate options
|
|
27
|
-
for (const key
|
|
32
|
+
for (const key in options) {
|
|
28
33
|
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key))
|
|
29
34
|
if (key !== 'isRecursing') return console.error(
|
|
30
35
|
`findJS() » ERROR: \`${ key }\` is an invalid option.\n`
|
|
31
36
|
+ `findJS() » Valid options: [ ${ Object.keys(defaultOptions).join(', ') } ]`);
|
|
32
37
|
else if (typeof options[key] !== 'boolean') return console.error(
|
|
33
|
-
`findJS() » ERROR:
|
|
38
|
+
`findJS() » ERROR: [${ key }] option can only be set to \`true\` or \`false\`.`);
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
// Search for unminified JS
|
|
@@ -45,15 +50,15 @@ function findJS(searchDir, options = {}) {
|
|
|
45
50
|
...findJS(filePath, { ...options, isRecursing: true }));
|
|
46
51
|
else if (/\.js(?<!\.min\.js)$/.test(file)
|
|
47
52
|
&& (options.dotFiles || !file.startsWith('.')))
|
|
48
|
-
jsFiles.push(filePath); // store eligible unminified JS file for
|
|
53
|
+
jsFiles.push(filePath); // store eligible unminified JS file for returning
|
|
49
54
|
});
|
|
50
55
|
|
|
51
56
|
// Log/return final result
|
|
52
57
|
if (!options.isRecursing && options.verbose) console.info(
|
|
53
|
-
|
|
54
|
-
|
|
58
|
+
'findJS() » Search complete! ' + ( jsFiles.length === 0 ? 'No' : jsFiles.length )
|
|
59
|
+
+ ` file${ jsFiles.length > 1 ? 's' : '' } found.`
|
|
55
60
|
+ ( findJS.caller.name !== 'minify' && require.main !== module ?
|
|
56
|
-
|
|
61
|
+
'\nfindJS() » Check returned array.' : '' ));
|
|
57
62
|
return options.isRecursing || jsFiles.length > 0 ? jsFiles : [];
|
|
58
63
|
}
|
|
59
64
|
|
|
@@ -61,7 +66,12 @@ function minify(input, options = {}) {
|
|
|
61
66
|
|
|
62
67
|
// Init options
|
|
63
68
|
const defaultOptions = {
|
|
64
|
-
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
|
+
};
|
|
65
75
|
options = { ...defaultOptions, ...options };
|
|
66
76
|
|
|
67
77
|
// Validate input
|
|
@@ -69,12 +79,12 @@ function minify(input, options = {}) {
|
|
|
69
79
|
'minify() » ERROR: 1st arg <input> must be a string.');
|
|
70
80
|
|
|
71
81
|
// Validate options
|
|
72
|
-
for (const key
|
|
82
|
+
for (const key in options) {
|
|
73
83
|
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key)) return console.error(
|
|
74
84
|
`minify() » ERROR: \`${ key }\` is an invalid option.\n`
|
|
75
85
|
+ `minify() » Valid options: [ ${ Object.keys(defaultOptions).join(', ') } ]`);
|
|
76
86
|
else if (typeof options[key] !== 'boolean') return console.error(
|
|
77
|
-
`minify() » ERROR:
|
|
87
|
+
`minify() » ERROR: [${ key }] option can only be set to \`true\` or \`false\`.`);
|
|
78
88
|
}
|
|
79
89
|
|
|
80
90
|
// Minify JS based on input
|
|
@@ -190,9 +200,10 @@ else { // run as CLI utility
|
|
|
190
200
|
minifyData?.forEach(({ code, srcPath }) => {
|
|
191
201
|
const outputDir = path.join(
|
|
192
202
|
path.dirname(srcPath), // path of file to be minified
|
|
193
|
-
/so?u?rce?$/.test(path.dirname(srcPath)) ?
|
|
194
|
-
|
|
195
|
-
|
|
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
|
|
196
207
|
);
|
|
197
208
|
const outputFilename = (
|
|
198
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"
|