@adamlui/minify.js 1.0.1 → 1.0.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 +12 -8
- package/minify.js +9 -6
- package/package.json +11 -5
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# <> minify.js
|
|
2
2
|
|
|
3
|
-
###
|
|
3
|
+
### Recursively minify all JavaScript files.
|
|
4
4
|
|
|
5
|
-
<a href="https://www.npmjs.com/package/@adamlui/minify.js"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.0.
|
|
5
|
+
<a href="https://www.npmjs.com/package/@adamlui/minify.js"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.0.3-fc7811.svg?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
7
|
|
|
8
8
|
<img src="https://github.com/adamlui/js-utils/blob/main/minify.js/media/images/minify-js-docs-demo.png">
|
|
@@ -33,6 +33,8 @@ The basic **global command** is:
|
|
|
33
33
|
minify-js
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
#
|
|
37
|
+
|
|
36
38
|
To specify **input/output** directories:
|
|
37
39
|
|
|
38
40
|
```
|
|
@@ -40,9 +42,11 @@ minify-js <input_path> <output_path>
|
|
|
40
42
|
```
|
|
41
43
|
|
|
42
44
|
- `<input_path>`: Path to directory containing JavaScript files to be minified, relative to the current working directory.
|
|
43
|
-
- `<output_path>`: Path to directory where minified files will be stored, relative to original file location. (If not provided,
|
|
45
|
+
- `<output_path>`: Path to directory where minified files will be stored, relative to original file location. (If not provided, `min/` is used.)
|
|
46
|
+
|
|
47
|
+
**💡 Note:** The paths can be either folders or specific files. If folders are passed, files will be processed recursively.
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
#
|
|
46
50
|
|
|
47
51
|
To use as a **package script**, edit your project's `package.json` like this:
|
|
48
52
|
|
|
@@ -55,21 +59,21 @@ To use as a **package script**, edit your project's `package.json` like this:
|
|
|
55
59
|
Replace `<minify-js-cmd>` with `minify-js` + optional args. Then, `npm run build:js` can be used to run the command.
|
|
56
60
|
<br><br>
|
|
57
61
|
|
|
58
|
-
## 📃
|
|
62
|
+
## 📃 Example commands:
|
|
59
63
|
|
|
60
|
-
- Minify all JavaScript files in the **current directory** (outputs to
|
|
64
|
+
- Minify all JavaScript files in the **current directory** (outputs to `min/`):
|
|
61
65
|
|
|
62
66
|
```
|
|
63
67
|
minify-js
|
|
64
68
|
```
|
|
65
69
|
|
|
66
|
-
- Minify all JavaScript files in a **specific directory** (outputs to `path/to/your/directory/
|
|
70
|
+
- Minify all JavaScript files in a **specific directory** (outputs to `path/to/your/directory/min/`):
|
|
67
71
|
|
|
68
72
|
```
|
|
69
73
|
minify-js path/to/your/directory
|
|
70
74
|
```
|
|
71
75
|
|
|
72
|
-
- Minify a **specific file** (outputs to `path/to/your/
|
|
76
|
+
- Minify a **specific file** (outputs to `path/to/your/min/file.min.js`):
|
|
73
77
|
|
|
74
78
|
```
|
|
75
79
|
minify-js path/to/your/file.js
|
package/minify.js
CHANGED
|
@@ -31,7 +31,7 @@ const jsFiles = inputArg.endsWith('.js') ? [inputPath]
|
|
|
31
31
|
const files = fs.readdirSync(dir);
|
|
32
32
|
files.forEach(file => {
|
|
33
33
|
const filePath = path.join(dir, file);
|
|
34
|
-
if (fs.statSync(filePath).isDirectory())
|
|
34
|
+
if (fs.statSync(filePath).isDirectory() && file !== 'node_modules')
|
|
35
35
|
findJSfiles(filePath); // recursively find unminified JS
|
|
36
36
|
else if (/\.js(?<!\.min\.js)$/.test(file)) // unminified JS file found
|
|
37
37
|
fileList.push(filePath); // store it for minification
|
|
@@ -45,8 +45,9 @@ console.log(''); // line break before first log
|
|
|
45
45
|
jsFiles.forEach(jsPath => {
|
|
46
46
|
const outputDir = path.join(
|
|
47
47
|
path.dirname(jsPath), // path of file to be minified
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
/so?u?rce?$/.test(path.dirname(jsPath)) ? '../min' // + ../min/ if in *(src|source)/
|
|
49
|
+
: outputArg.endsWith('.js') ? path.dirname(outputArg) // or path from file output arg
|
|
50
|
+
: outputArg || 'min' // or path from folder output arg or min/ if no output arg passed
|
|
50
51
|
);
|
|
51
52
|
const outputFilename = (
|
|
52
53
|
outputArg.endsWith('.js') && inputArg.endsWith('.js')
|
|
@@ -55,12 +56,14 @@ jsFiles.forEach(jsPath => {
|
|
|
55
56
|
) + '.min.js';
|
|
56
57
|
const outputPath = path.join(outputDir, outputFilename);
|
|
57
58
|
console.info(`Minifying ${ jsPath }...`);
|
|
58
|
-
const minifiedCode = uglifyJS.minify(fs.readFileSync(
|
|
59
|
+
const minifiedCode = uglifyJS.minify(fs.readFileSync(jsPath, 'utf8')).code;
|
|
59
60
|
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
|
|
60
61
|
fs.writeFileSync(outputPath, minifiedCode, 'utf8');
|
|
61
62
|
minifiedCnt++;
|
|
62
63
|
});
|
|
63
64
|
|
|
64
65
|
// Print final summary
|
|
65
|
-
if (minifiedCnt)
|
|
66
|
-
|
|
66
|
+
if (minifiedCnt) {
|
|
67
|
+
console.info(`\n${bg}Minification complete!${nc}`);
|
|
68
|
+
console.info(`\n${ minifiedCnt } file${ minifiedCnt > 1 ? 's' : '' } minified.`);
|
|
69
|
+
} else console.info(`${by}No unminified JavaScript files found.${nc}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamlui/minify.js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Minify JavaScript files",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Adam Lui",
|
|
@@ -15,10 +15,12 @@
|
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"publish:
|
|
18
|
+
"bump:patch": "bash utils/bump.sh patch",
|
|
19
|
+
"bump:minor": "bash utils/bump.sh minor",
|
|
20
|
+
"bump:major": "bash utils/bump.sh major",
|
|
21
|
+
"publish:patch": "bash utils/bump.sh patch --publish",
|
|
22
|
+
"publish:minor": "bash utils/bump.sh minor --publish",
|
|
23
|
+
"publish:major": "bash utils/bump.sh major --publish"
|
|
22
24
|
},
|
|
23
25
|
"repository": {
|
|
24
26
|
"type": "git",
|
|
@@ -37,5 +39,9 @@
|
|
|
37
39
|
},
|
|
38
40
|
"dependencies": {
|
|
39
41
|
"uglify-js": "^3.17.4"
|
|
42
|
+
},
|
|
43
|
+
"funding": {
|
|
44
|
+
"type": "github",
|
|
45
|
+
"url": "https://github.com/sponsors/adamlui"
|
|
40
46
|
}
|
|
41
47
|
}
|