@adamlui/minify.js 1.2.0 → 1.2.2
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 +15 -12
- package/docs/SECURITY.md +1 -1
- package/minify.js +71 -60
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# </> minify.js
|
|
2
2
|
|
|
3
3
|
### Recursively minify all JavaScript files.
|
|
4
4
|
|
|
5
5
|
<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>
|
|
6
6
|
<a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-fcde7b.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
7
|
-
<a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.2.
|
|
7
|
+
<a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.2.2-fc7811.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
8
|
+
<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"></a>
|
|
8
9
|
|
|
9
|
-
<img src="https://github.com/adamlui/js-utils/blob/main/minify.js/media/images/minify
|
|
10
|
+
<img src="https://github.com/adamlui/js-utils/blob/main/minify.js/media/images/minify.js-docs-demo.png">
|
|
10
11
|
|
|
11
12
|
<br>
|
|
12
13
|
|
|
@@ -31,15 +32,17 @@ npm install -D @adamlui/minify.js
|
|
|
31
32
|
The basic **global command** is:
|
|
32
33
|
|
|
33
34
|
```
|
|
34
|
-
|
|
35
|
+
minifyjs
|
|
35
36
|
```
|
|
36
37
|
|
|
38
|
+
**💡 Note:** Pass `-n` or `--dry-run` to only see what files will be processed.
|
|
39
|
+
|
|
37
40
|
#
|
|
38
41
|
|
|
39
42
|
To specify **input/output** paths:
|
|
40
43
|
|
|
41
44
|
```
|
|
42
|
-
|
|
45
|
+
minifyjs [input_path] [output_path]
|
|
43
46
|
```
|
|
44
47
|
|
|
45
48
|
- `[input_path]`: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
|
|
@@ -49,15 +52,15 @@ minify-js [input_path] [output_path]
|
|
|
49
52
|
|
|
50
53
|
#
|
|
51
54
|
|
|
52
|
-
To use as a **package script**,
|
|
55
|
+
To use as a **package script**, in your project's `package.json`:
|
|
53
56
|
|
|
54
57
|
```json
|
|
55
58
|
"scripts": {
|
|
56
|
-
"build:js": "<minify
|
|
59
|
+
"build:js": "<minify.js-cmd>"
|
|
57
60
|
},
|
|
58
61
|
```
|
|
59
62
|
|
|
60
|
-
Replace `<minify
|
|
63
|
+
Replace `<minify.js-cmd>` with `minifyjs` + optional args. Then, `npm run build:js` can be used to run the command.
|
|
61
64
|
<br><br>
|
|
62
65
|
|
|
63
66
|
## 📃 Example commands:
|
|
@@ -65,25 +68,25 @@ Replace `<minify-js-cmd>` with `minify-js` + optional args. Then, `npm run build
|
|
|
65
68
|
- Minify all JavaScript files in the **current directory** (outputs to `min/`):
|
|
66
69
|
|
|
67
70
|
```
|
|
68
|
-
|
|
71
|
+
minifyjs
|
|
69
72
|
```
|
|
70
73
|
|
|
71
74
|
- Minify all JavaScript files in a **specific directory** (outputs to `path/to/your/directory/min/`):
|
|
72
75
|
|
|
73
76
|
```
|
|
74
|
-
|
|
77
|
+
minifyjs path/to/your/directory
|
|
75
78
|
```
|
|
76
79
|
|
|
77
80
|
- Minify a **specific file** (outputs to `path/to/your/min/file.min.js`):
|
|
78
81
|
|
|
79
82
|
```
|
|
80
|
-
|
|
83
|
+
minifyjs path/to/your/file.js
|
|
81
84
|
```
|
|
82
85
|
|
|
83
86
|
- Specify both **input and output** directories (outputs to `output_folder/`):
|
|
84
87
|
|
|
85
88
|
```
|
|
86
|
-
|
|
89
|
+
minifyjs input_folder output_folder
|
|
87
90
|
```
|
|
88
91
|
|
|
89
92
|
## 🏛️ MIT License
|
package/docs/SECURITY.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
If you find a vulnerability, please open a [draft security advisory](https://github.com/adamlui/js-utils/security/advisories/new).
|
|
4
4
|
|
|
5
|
-
Pull requests are also welcome, but for safety reasons, send an email to adam@kudoai.com and wait for a response before making it public.
|
|
5
|
+
Pull requests are also welcome, but for safety reasons, send an email to <adam@kudoai.com> and wait for a response before making it public.
|
package/minify.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// Import
|
|
3
|
+
// Import LIBS
|
|
4
4
|
const fs = require('fs'),
|
|
5
5
|
path = require('path'),
|
|
6
6
|
uglifyJS = require('uglify-js');
|
|
@@ -11,51 +11,32 @@ const nc = '\x1b[0m', // no color
|
|
|
11
11
|
by = '\x1b[1;33m', // bright yellow
|
|
12
12
|
bg = '\x1b[1;92m'; // bright green
|
|
13
13
|
|
|
14
|
-
// Show
|
|
14
|
+
// Show HELP screen if -h or --help passed
|
|
15
15
|
if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
|
|
16
16
|
|
|
17
17
|
// Print help
|
|
18
|
-
console.info(`\n${by}
|
|
18
|
+
console.info(`\n${by}minifyjs [inputPath] [outputPath] [options]${nc}`);
|
|
19
19
|
console.info('\nPath arguments:');
|
|
20
|
-
printWrappedMsg(' [
|
|
20
|
+
printWrappedMsg(' [inputPath] '
|
|
21
21
|
+ 'Path to JS file or directory containing JS files to be minified,'
|
|
22
22
|
+ ' relative to the current working directory.');
|
|
23
|
-
printWrappedMsg(' [
|
|
23
|
+
printWrappedMsg(' [outputPath] '
|
|
24
24
|
+ 'Path to file or directory where minified files will be stored,'
|
|
25
25
|
+ ' relative to original file location (if not provided, min/ is used).');
|
|
26
26
|
console.info('\nConfig options:');
|
|
27
|
-
printWrappedMsg(' --
|
|
28
|
-
|
|
27
|
+
printWrappedMsg(' -n, --dry-run Don\'t actually minify the file(s),'
|
|
28
|
+
+ ' just show if they will be processed.');
|
|
29
|
+
printWrappedMsg(' -dd, --include-dotfolders Include dotfolders in file search.');
|
|
30
|
+
printWrappedMsg(' -df, --include-dotfilles Include dotfiles in file search.');
|
|
29
31
|
console.info('\nInfo commands:');
|
|
30
|
-
printWrappedMsg('
|
|
31
|
-
printWrappedMsg('
|
|
32
|
-
|
|
33
|
-
function printWrappedMsg(msg) { // indents 2nd+ lines
|
|
34
|
-
const terminalWidth = process.stdout.columns || 80,
|
|
35
|
-
indentation = 23, lines = [], words = msg.match(/\S+|\s+/g);
|
|
36
|
-
|
|
37
|
-
// Split msg into lines of appropriate lengths
|
|
38
|
-
let currentLine = '';
|
|
39
|
-
words.forEach(word => {
|
|
40
|
-
const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
|
|
41
|
-
if (currentLine.length + word.length > lineLength) { // cap/store it
|
|
42
|
-
lines.push(currentLine); currentLine = ''; }
|
|
43
|
-
currentLine += word;
|
|
44
|
-
});
|
|
45
|
-
lines.push(currentLine); // Push the last line without trimming
|
|
46
|
-
|
|
47
|
-
// Print formatted msg
|
|
48
|
-
lines.forEach((line, index) => {
|
|
49
|
-
if (index === 0) console.info(line); // print 1st line unindented
|
|
50
|
-
else console.info(' '.repeat(indentation) + line); // print subsequent lines indented
|
|
51
|
-
});
|
|
52
|
-
}
|
|
32
|
+
printWrappedMsg(' -h, --help Display this help screen.');
|
|
33
|
+
printWrappedMsg(' -v, --version Show version number.');
|
|
53
34
|
|
|
54
|
-
// Show
|
|
55
|
-
} else if (process.argv.some(arg => /^--?
|
|
35
|
+
// Show VERSION number if -v or --version passed
|
|
36
|
+
} else if (process.argv.some(arg => /^--?ve?r?s?i?o?n?$/.test(arg))) {
|
|
56
37
|
console.info('v' + require('./package.json').version);
|
|
57
38
|
|
|
58
|
-
} else { // run
|
|
39
|
+
} else { // run MAIN routine
|
|
59
40
|
|
|
60
41
|
// Init I/O args
|
|
61
42
|
const [inputArg = '', outputArg = ''] = ( // default to empty strings for error-less handling
|
|
@@ -69,12 +50,13 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
|
|
|
69
50
|
if (inputArg && !fs.existsSync(inputPath)) {
|
|
70
51
|
console.error(`\n${br}Error: First arg must be an existing file or directory.`
|
|
71
52
|
+ `\n${ inputPath } does not exist.${nc}`
|
|
72
|
-
+ '\n\nExample valid command: \n>>
|
|
53
|
+
+ '\n\nExample valid command: \n>> minifyjs . output.min.js');
|
|
73
54
|
process.exit(1);
|
|
74
55
|
}
|
|
75
56
|
|
|
76
57
|
// Load flag settings
|
|
77
58
|
const config = {
|
|
59
|
+
dryRun: process.argv.some(arg => /^--?(?:n|dry-?run)$/.test(arg)),
|
|
78
60
|
includeDotFolders: process.argv.some(arg =>
|
|
79
61
|
/^--?(?:dd|(?:include-)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?)$/.test(arg)),
|
|
80
62
|
includeDotFiles: process.argv.some(arg =>
|
|
@@ -97,32 +79,61 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
|
|
|
97
79
|
});
|
|
98
80
|
})(inputPath);
|
|
99
81
|
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
82
|
+
if (config.dryRun) { // print files to be processed
|
|
83
|
+
console.info('\nJS files to be minified:');
|
|
84
|
+
jsFiles.forEach(file => console.info(file));
|
|
85
|
+
|
|
86
|
+
} else { // actually minify JavaScript files
|
|
87
|
+
|
|
88
|
+
let minifiedCnt = 0;
|
|
89
|
+
console.log(''); // line break before first log
|
|
90
|
+
jsFiles.forEach(jsPath => {
|
|
91
|
+
console.info(`Minifying ${ jsPath }...`);
|
|
92
|
+
const outputDir = path.join(
|
|
93
|
+
path.dirname(jsPath), // path of file to be minified
|
|
94
|
+
/so?u?rce?$/.test(path.dirname(jsPath)) ? '../min' // + ../min/ if in *(src|source)/
|
|
95
|
+
: outputArg.endsWith('.js') ? path.dirname(outputArg) // or path from file output arg
|
|
96
|
+
: outputArg || 'min' // or path from folder output arg or min/ if no output arg passed
|
|
97
|
+
);
|
|
98
|
+
const outputFilename = (
|
|
99
|
+
outputArg.endsWith('.js') && inputArg.endsWith('.js')
|
|
100
|
+
? path.basename(outputArg).replace(/(\.min)?\.js$/, '')
|
|
101
|
+
: path.basename(jsPath, '.js')
|
|
102
|
+
) + '.min.js';
|
|
103
|
+
const outputPath = path.join(outputDir, outputFilename),
|
|
104
|
+
minifiedCode = uglifyJS.minify(fs.readFileSync(jsPath, 'utf8')).code;
|
|
105
|
+
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
|
|
106
|
+
fs.writeFileSync(outputPath, minifiedCode, 'utf8');
|
|
107
|
+
minifiedCnt++;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Print final summary
|
|
111
|
+
if (minifiedCnt) {
|
|
112
|
+
console.info(`\n${bg}Minification complete!${nc}`);
|
|
113
|
+
console.info(`${ minifiedCnt } file${ minifiedCnt > 1 ? 's' : '' } minified.`);
|
|
114
|
+
} else console.info(`${by}No unminified JavaScript files found.${nc}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function printWrappedMsg(msg) { // truncates msg, indents 2nd+ lines
|
|
119
|
+
const terminalWidth = process.stdout.columns || 80,
|
|
120
|
+
indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
|
|
121
|
+
|
|
122
|
+
// Split msg into lines of appropriate lengths
|
|
123
|
+
let currentLine = '';
|
|
124
|
+
words.forEach(word => {
|
|
125
|
+
const lineLength = terminalWidth - ( lines.length === 0 ? 0 : indentation );
|
|
126
|
+
if (currentLine.length + word.length > lineLength) { // cap/store it
|
|
127
|
+
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
128
|
+
currentLine = '';
|
|
129
|
+
}
|
|
130
|
+
currentLine += word;
|
|
121
131
|
});
|
|
132
|
+
lines.push(lines.length === 0 ? currentLine : currentLine.trimStart());
|
|
122
133
|
|
|
123
|
-
// Print
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
134
|
+
// Print formatted msg
|
|
135
|
+
lines.forEach((line, index) => console.info(
|
|
136
|
+
index === 0 ? line // print 1st line unindented
|
|
137
|
+
: ' '.repeat(indentation) + line // print subsequent lines indented
|
|
138
|
+
));
|
|
128
139
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamlui/minify.js",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Recursively minify all JavaScript files",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"main": "minify.js",
|
|
13
13
|
"bin": {
|
|
14
|
+
"minifyjs": "minify.js",
|
|
14
15
|
"minify-js": "minify.js"
|
|
15
16
|
},
|
|
16
17
|
"scripts": {
|