@adamlui/minify.js 1.0.0

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 ADDED
@@ -0,0 +1,107 @@
1
+ # <> minify.js
2
+
3
+ <a href="#%EF%B8%8F-mit-license"><img height=25 src="https://img.shields.io/badge/License-MIT-fcde7b.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
4
+
5
+ ### Minify JavaScript files from the CLI (recursively or indivdually)
6
+
7
+ <img height=10px width="100%" src="https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/aqua.png">
8
+
9
+ ## ⚡ Installation
10
+
11
+ - As a **global utility**:
12
+
13
+ ```
14
+ npm install -g @adamlui/minify.js
15
+ ```
16
+
17
+ - As a **dev dependency**, from your project root:
18
+
19
+ ```
20
+ npm install -D @adamlui/minify.js
21
+ ```
22
+
23
+ ## 💻 Usage
24
+
25
+ - The basic **global command** is:
26
+
27
+ ```
28
+ minify-js
29
+ ```
30
+
31
+ - To specify **input/output** directories:
32
+
33
+ ```
34
+ minify-js <input_path> <output_path>
35
+ ```
36
+
37
+ - `<input_path>`: Path to directory containing JavaScript files to be minified, relative to the current working directory.
38
+ - `<output_path>`: Path to directory where minified files will be stored, relative to original file location. (If not provided, `./minified` is used.)
39
+
40
+ **💡 Note:** The paths can be either folders or specific files. If folders are passed, files will be processed recursively.<br><br>
41
+
42
+ - To use as a **package script**, edit your project's `package.json` like this:
43
+
44
+ ```json
45
+ "scripts": {
46
+ "build:js": "<minify-js-cmd>"
47
+ },
48
+ ```
49
+
50
+ Replace `<minify-js-cmd>` with `minify-js` + optional args. Then, `npm run build:js` can be used to run the command.
51
+ <br><br>
52
+
53
+ ## 📃 Examples:
54
+
55
+ - Minify all JavaScript files in the **current directory** (outputs to `./minified/`):
56
+
57
+ ```
58
+ minify-js
59
+ ```
60
+
61
+ - Minify all JavaScript files in a **specific directory** (outputs to `path/to/your/directory/minified/`):
62
+
63
+ ```
64
+ minify-js path/to/your/directory
65
+ ```
66
+
67
+ - Minify a **specific file** (outputs to `path/to/your/minified/file.min.js`):
68
+
69
+ ```
70
+ minify-js path/to/your/file.js
71
+ ```
72
+
73
+ - Specify both **input and output** directories (outputs to `output_folder/`):
74
+
75
+ ```
76
+ minify-js input_folder output_folder
77
+ ```
78
+
79
+ ## 🏛️ MIT License
80
+
81
+ **Copyright (c) 2024 [Adam Lui](https://github.com/adamlui)**
82
+
83
+ Permission is hereby granted, free of charge, to any person obtaining a copy
84
+ of this software and associated documentation files (the "Software"), to deal
85
+ in the Software without restriction, including without limitation the rights
86
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
87
+ copies of the Software, and to permit persons to whom the Software is
88
+ furnished to do so, subject to the following conditions:
89
+
90
+ The above copyright notice and this permission notice shall be included in all
91
+ copies or substantial portions of the Software.
92
+
93
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
94
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
95
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
96
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
97
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
98
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
99
+ SOFTWARE.
100
+
101
+ <br>
102
+
103
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/aqua.png">
104
+
105
+ <a href="https://github.com/adamlui">**Home**</a> /
106
+ <a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> /
107
+ <a href="#--scss-to-cssjs">Back to top ↑</a>
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ **Copyright (c) 2023–2024 [Adam Lui](https://github.com/adamlui)**
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,5 @@
1
+ # 🛡️ Security Policy
2
+
3
+ If you find a vulnerability, please open a [draft security advisory](https://github.com/adamlui/js-utils/security/advisories/new).
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.
package/minify.js ADDED
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env node
2
+
3
+ // Import libs
4
+ const fs = require('fs'),
5
+ path = require('path'),
6
+ uglifyJS = require('uglify-js');
7
+
8
+ // Init UI colors
9
+ const nc = '\x1b[0m', // no color
10
+ br = '\x1b[1;91m', // bright red
11
+ by = '\x1b[1;33m', // bright yellow
12
+ bg = '\x1b[1;92m'; // bright green
13
+
14
+ // Clean leading slashes from args to avoid parsing system root
15
+ const inputArg = process.argv[2] ? process.argv[2].replace(/^\/*/, '') : '',
16
+ outputArg = process.argv[3] ? process.argv[3].replace(/^\/*/, '') : '';
17
+
18
+ // Validate input arg (output arg can be anything)
19
+ if (process.argv[2] && !fs.existsSync(inputArg)) {
20
+ console.error(`\n${br}Error: First arg must be an existing file or path.${nc}`
21
+ + '\nExample valid command: \n>> minify-js . output.min.js');
22
+ process.exit(1);
23
+ }
24
+
25
+ // Recursively find all JavaScript files or arg-passed file
26
+ const inputPath = path.resolve(process.cwd(), inputArg);
27
+ const jsFiles = inputArg.endsWith('.js') ? [inputPath]
28
+ : (() => {
29
+ const fileList = [];
30
+ (function findJSfiles(dir) {
31
+ const files = fs.readdirSync(dir);
32
+ files.forEach(file => {
33
+ const filePath = path.join(dir, file);
34
+ if (fs.statSync(filePath).isDirectory())
35
+ findJSfiles(filePath); // recursively find unminified JS
36
+ else if (/\.js(?<!\.min\.js)$/.test(file)) // unminified JS file found
37
+ fileList.push(filePath); // store it for minification
38
+ });
39
+ })(inputPath); return fileList;
40
+ })();
41
+
42
+ // Minify JavaScript files
43
+ let minifiedCnt = 0;
44
+ console.log(''); // line break before first log
45
+ jsFiles.forEach(jsPath => {
46
+ const outputDir = path.join(
47
+ path.dirname(jsPath), // path of file to be minified
48
+ path.dirname(outputArg), // path from output arg
49
+ outputArg ? '' : 'minified' // minified/ if no output arg used
50
+ );
51
+ const outputFilename = (
52
+ outputArg.endsWith('.js') && inputArg.endsWith('.js')
53
+ ? path.basename(outputArg).replace(/(\.min)?\.js$/, '')
54
+ : path.basename(jsPath, '.js')
55
+ ) + '.min.js';
56
+ console.info(`Minifying ${ jsPath }...`);
57
+ const minifiedCode = uglifyJS.minify(fs.readFileSync(jsPath, 'utf8')).code;
58
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
59
+ fs.writeFileSync(path.join(outputDir, outputFilename), minifiedCode, 'utf8');
60
+ minifiedCnt++;
61
+ });
62
+
63
+ // Final summary
64
+ if (minifiedCnt) console.info(`\n${bg}Minification complete!${nc}\n${ minifiedCnt } files minified.`);
65
+ else console.info(`\n${by}No unminified JavaScript files found.${nc}`);
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@adamlui/minify.js",
3
+ "version": "1.0.0",
4
+ "description": "Minify JavaScript files",
5
+ "author": {
6
+ "name": "Adam Lui",
7
+ "email": "adam@kudoai.com",
8
+ "url": "https://github.com/adamlui"
9
+ },
10
+ "homepage": "https://github.com/adamlui/js-utils",
11
+ "license": "MIT",
12
+ "main": "minify.js",
13
+ "bin": {
14
+ "minify-js": "minify.js"
15
+ },
16
+ "scripts": {
17
+ "test": "echo \"Error: no test specified\" && exit 1"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/adamlui/js-utils.git"
22
+ },
23
+ "keywords": [
24
+ "javascript",
25
+ "js",
26
+ "js-utils",
27
+ "utils",
28
+ "utilities",
29
+ "minifier"
30
+ ],
31
+ "bugs": {
32
+ "url": "https://github.com/adamlui/js-utils/issues"
33
+ },
34
+ "dependencies": {
35
+ "uglify-js": "^3.17.4"
36
+ }
37
+ }