@adamlui/minify.js 1.2.2 → 1.2.3
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 +14 -9
- package/minify.js +21 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
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.3-fc7811.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
|
|
8
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>
|
|
9
9
|
|
|
10
10
|
<img src="https://github.com/adamlui/js-utils/blob/main/minify.js/media/images/minify.js-docs-demo.png">
|
|
@@ -32,7 +32,7 @@ npm install -D @adamlui/minify.js
|
|
|
32
32
|
The basic **global command** is:
|
|
33
33
|
|
|
34
34
|
```
|
|
35
|
-
|
|
35
|
+
minify-js
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
**💡 Note:** Pass `-n` or `--dry-run` to only see what files will be processed.
|
|
@@ -42,7 +42,7 @@ minifyjs
|
|
|
42
42
|
To specify **input/output** paths:
|
|
43
43
|
|
|
44
44
|
```
|
|
45
|
-
|
|
45
|
+
minify-js [input_path] [output_path]
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
- `[input_path]`: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
|
|
@@ -56,11 +56,11 @@ To use as a **package script**, in your project's `package.json`:
|
|
|
56
56
|
|
|
57
57
|
```json
|
|
58
58
|
"scripts": {
|
|
59
|
-
"build:js": "<minify
|
|
59
|
+
"build:js": "<minify-js-cmd>"
|
|
60
60
|
},
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
Replace `<minify
|
|
63
|
+
Replace `<minify-js-cmd>` with `minify-js` + optional args. Then, `npm run build:js` can be used to run the command.
|
|
64
64
|
<br><br>
|
|
65
65
|
|
|
66
66
|
## 📃 Example commands:
|
|
@@ -68,27 +68,32 @@ Replace `<minify.js-cmd>` with `minifyjs` + optional args. Then, `npm run build:
|
|
|
68
68
|
- Minify all JavaScript files in the **current directory** (outputs to `min/`):
|
|
69
69
|
|
|
70
70
|
```
|
|
71
|
-
|
|
71
|
+
minify-js
|
|
72
72
|
```
|
|
73
73
|
|
|
74
74
|
- Minify all JavaScript files in a **specific directory** (outputs to `path/to/your/directory/min/`):
|
|
75
75
|
|
|
76
76
|
```
|
|
77
|
-
|
|
77
|
+
minify-js path/to/your/directory
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
- Minify a **specific file** (outputs to `path/to/your/min/file.min.js`):
|
|
81
81
|
|
|
82
82
|
```
|
|
83
|
-
|
|
83
|
+
minify-js path/to/your/file.js
|
|
84
84
|
```
|
|
85
85
|
|
|
86
86
|
- Specify both **input and output** directories (outputs to `output_folder/`):
|
|
87
87
|
|
|
88
88
|
```
|
|
89
|
-
|
|
89
|
+
minify-js input_folder output_folder
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
+
## 💖 Support
|
|
93
|
+
|
|
94
|
+
Please consider giving a ⭐ on [GitHub](https://github.com/adamlui/js-utils) if this helped you!
|
|
95
|
+
<br><br>
|
|
96
|
+
|
|
92
97
|
## 🏛️ MIT License
|
|
93
98
|
|
|
94
99
|
**Copyright (c) 2024 [Adam Lui](https://github.com/adamlui)**
|
package/minify.js
CHANGED
|
@@ -15,7 +15,7 @@ const nc = '\x1b[0m', // no color
|
|
|
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}minify-js [inputPath] [outputPath] [options]${nc}`);
|
|
19
19
|
console.info('\nPath arguments:');
|
|
20
20
|
printWrappedMsg(' [inputPath] '
|
|
21
21
|
+ 'Path to JS file or directory containing JS files to be minified,'
|
|
@@ -24,13 +24,13 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
|
|
|
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('
|
|
27
|
+
printWrappedMsg(' -n, --dry-run Don\'t actually minify the file(s),'
|
|
28
28
|
+ ' just show if they will be processed.');
|
|
29
29
|
printWrappedMsg(' -dd, --include-dotfolders Include dotfolders in file search.');
|
|
30
30
|
printWrappedMsg(' -df, --include-dotfilles Include dotfiles in file search.');
|
|
31
31
|
console.info('\nInfo commands:');
|
|
32
|
-
printWrappedMsg('
|
|
33
|
-
printWrappedMsg('
|
|
32
|
+
printWrappedMsg(' -h, --help Display this help screen.');
|
|
33
|
+
printWrappedMsg(' -v, --version Show version number.');
|
|
34
34
|
|
|
35
35
|
// Show VERSION number if -v or --version passed
|
|
36
36
|
} else if (process.argv.some(arg => /^--?ve?r?s?i?o?n?$/.test(arg))) {
|
|
@@ -50,7 +50,8 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
|
|
|
50
50
|
if (inputArg && !fs.existsSync(inputPath)) {
|
|
51
51
|
console.error(`\n${br}Error: First arg must be an existing file or directory.`
|
|
52
52
|
+ `\n${ inputPath } does not exist.${nc}`
|
|
53
|
-
+
|
|
53
|
+
+ `\n\n${bg}Example valid command: \n>> minify-js . output.min.js${nc}`
|
|
54
|
+
+ `\n\n${by}For all command options: \n>> minify-js --help${nc}`);
|
|
54
55
|
process.exit(1);
|
|
55
56
|
}
|
|
56
57
|
|
|
@@ -64,30 +65,33 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
|
|
|
64
65
|
};
|
|
65
66
|
|
|
66
67
|
// Recursively find all eligible JavaScript files or arg-passed file
|
|
67
|
-
const
|
|
68
|
-
if (inputArg.endsWith('.js'))
|
|
69
|
-
else (function
|
|
68
|
+
const unminnedJSfiles = [];
|
|
69
|
+
if (inputArg.endsWith('.js')) unminnedJSfiles.push(inputPath);
|
|
70
|
+
else (function findUnminnedJSfiles(dir) {
|
|
70
71
|
const files = fs.readdirSync(dir);
|
|
71
72
|
files.forEach(file => {
|
|
72
73
|
const filePath = path.resolve(dir, file);
|
|
73
74
|
if (fs.statSync(filePath).isDirectory() && file != 'node_modules' &&
|
|
74
75
|
(config.includeDotFolders || !file.startsWith('.')))
|
|
75
|
-
|
|
76
|
+
findUnminnedJSfiles(filePath); // recursively find unminified JS in eligible dir
|
|
76
77
|
else if (/\.js(?<!\.min\.js)$/.test(file) &&
|
|
77
78
|
(config.includeDotFiles || !file.startsWith('.')))
|
|
78
|
-
|
|
79
|
+
unminnedJSfiles.push(filePath); // store eligible unminified JS file for minification
|
|
79
80
|
});
|
|
80
81
|
})(inputPath);
|
|
81
82
|
|
|
82
|
-
if (
|
|
83
|
-
console.info(
|
|
84
|
-
|
|
83
|
+
if (unminnedJSfiles.length === 0) { // print nothing found
|
|
84
|
+
console.info(`\n${by}No unminified JavaScript files found.${nc}`);
|
|
85
|
+
|
|
86
|
+
} else if (config.dryRun) { // print files to be processed
|
|
87
|
+
console.info(`\n${by}JS files to be minified:${nc}`);
|
|
88
|
+
unminnedJSfiles.forEach(file => console.info(file));
|
|
85
89
|
|
|
86
90
|
} else { // actually minify JavaScript files
|
|
87
|
-
|
|
91
|
+
|
|
88
92
|
let minifiedCnt = 0;
|
|
89
93
|
console.log(''); // line break before first log
|
|
90
|
-
|
|
94
|
+
unminnedJSfiles.forEach(jsPath => {
|
|
91
95
|
console.info(`Minifying ${ jsPath }...`);
|
|
92
96
|
const outputDir = path.join(
|
|
93
97
|
path.dirname(jsPath), // path of file to be minified
|
|
@@ -111,11 +115,11 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
|
|
|
111
115
|
if (minifiedCnt) {
|
|
112
116
|
console.info(`\n${bg}Minification complete!${nc}`);
|
|
113
117
|
console.info(`${ minifiedCnt } file${ minifiedCnt > 1 ? 's' : '' } minified.`);
|
|
114
|
-
} else console.info(`${by}No unminified JavaScript files
|
|
118
|
+
} else console.info(`${by}No unminified JavaScript files processed successfully.${nc}`);
|
|
115
119
|
}
|
|
116
120
|
}
|
|
117
121
|
|
|
118
|
-
function printWrappedMsg(msg) { //
|
|
122
|
+
function printWrappedMsg(msg) { // wraps msg, indents 2nd+ lines
|
|
119
123
|
const terminalWidth = process.stdout.columns || 80,
|
|
120
124
|
indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
|
|
121
125
|
|