@adamlui/minify.js 1.2.1 → 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.
Files changed (3) hide show
  1. package/README.md +17 -10
  2. package/minify.js +48 -35
  3. 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.1-fc7811.svg?logo=icinga&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.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,15 +32,17 @@ npm install -D @adamlui/minify.js
32
32
  The basic **global command** is:
33
33
 
34
34
  ```
35
- minifyjs
35
+ minify-js
36
36
  ```
37
37
 
38
+ **💡 Note:** Pass `-n` or `--dry-run` to only see what files will be processed.
39
+
38
40
  #
39
41
 
40
42
  To specify **input/output** paths:
41
43
 
42
44
  ```
43
- minifyjs [input_path] [output_path]
45
+ minify-js [input_path] [output_path]
44
46
  ```
45
47
 
46
48
  - `[input_path]`: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
@@ -50,15 +52,15 @@ minifyjs [input_path] [output_path]
50
52
 
51
53
  #
52
54
 
53
- To use as a **package script**, edit your project's `package.json` like this:
55
+ To use as a **package script**, in your project's `package.json`:
54
56
 
55
57
  ```json
56
58
  "scripts": {
57
- "build:js": "<minify.js-cmd>"
59
+ "build:js": "<minify-js-cmd>"
58
60
  },
59
61
  ```
60
62
 
61
- Replace `<minify.js-cmd>` with `minifyjs` + optional args. Then, `npm run build:js` can be used to run the command.
63
+ Replace `<minify-js-cmd>` with `minify-js` + optional args. Then, `npm run build:js` can be used to run the command.
62
64
  <br><br>
63
65
 
64
66
  ## 📃 Example commands:
@@ -66,27 +68,32 @@ Replace `<minify.js-cmd>` with `minifyjs` + optional args. Then, `npm run build:
66
68
  - Minify all JavaScript files in the **current directory** (outputs to `min/`):
67
69
 
68
70
  ```
69
- minifyjs
71
+ minify-js
70
72
  ```
71
73
 
72
74
  - Minify all JavaScript files in a **specific directory** (outputs to `path/to/your/directory/min/`):
73
75
 
74
76
  ```
75
- minifyjs path/to/your/directory
77
+ minify-js path/to/your/directory
76
78
  ```
77
79
 
78
80
  - Minify a **specific file** (outputs to `path/to/your/min/file.min.js`):
79
81
 
80
82
  ```
81
- minifyjs path/to/your/file.js
83
+ minify-js path/to/your/file.js
82
84
  ```
83
85
 
84
86
  - Specify both **input and output** directories (outputs to `output_folder/`):
85
87
 
86
88
  ```
87
- minifyjs input_folder output_folder
89
+ minify-js input_folder output_folder
88
90
  ```
89
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
+
90
97
  ## 🏛️ MIT License
91
98
 
92
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}minifyjs [inputPath] [outputPath] [options]${nc}`);
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,6 +24,8 @@ 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(' -n, --dry-run Don\'t actually minify the file(s),'
28
+ + ' just show if they will be processed.');
27
29
  printWrappedMsg(' -dd, --include-dotfolders Include dotfolders in file search.');
28
30
  printWrappedMsg(' -df, --include-dotfilles Include dotfiles in file search.');
29
31
  console.info('\nInfo commands:');
@@ -48,12 +50,14 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
48
50
  if (inputArg && !fs.existsSync(inputPath)) {
49
51
  console.error(`\n${br}Error: First arg must be an existing file or directory.`
50
52
  + `\n${ inputPath } does not exist.${nc}`
51
- + '\n\nExample valid command: \n>> minifyjs . output.min.js');
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}`);
52
55
  process.exit(1);
53
56
  }
54
57
 
55
58
  // Load flag settings
56
59
  const config = {
60
+ dryRun: process.argv.some(arg => /^--?(?:n|dry-?run)$/.test(arg)),
57
61
  includeDotFolders: process.argv.some(arg =>
58
62
  /^--?(?:dd|(?:include-)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?)$/.test(arg)),
59
63
  includeDotFiles: process.argv.some(arg =>
@@ -61,52 +65,61 @@ if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
61
65
  };
62
66
 
63
67
  // Recursively find all eligible JavaScript files or arg-passed file
64
- const jsFiles = [];
65
- if (inputArg.endsWith('.js')) jsFiles.push(inputPath);
66
- else (function findJSfiles(dir) {
68
+ const unminnedJSfiles = [];
69
+ if (inputArg.endsWith('.js')) unminnedJSfiles.push(inputPath);
70
+ else (function findUnminnedJSfiles(dir) {
67
71
  const files = fs.readdirSync(dir);
68
72
  files.forEach(file => {
69
73
  const filePath = path.resolve(dir, file);
70
74
  if (fs.statSync(filePath).isDirectory() && file != 'node_modules' &&
71
75
  (config.includeDotFolders || !file.startsWith('.')))
72
- findJSfiles(filePath); // recursively find unminified JS in eligible dir
76
+ findUnminnedJSfiles(filePath); // recursively find unminified JS in eligible dir
73
77
  else if (/\.js(?<!\.min\.js)$/.test(file) &&
74
78
  (config.includeDotFiles || !file.startsWith('.')))
75
- jsFiles.push(filePath); // store eligible unminified JS file for minification
79
+ unminnedJSfiles.push(filePath); // store eligible unminified JS file for minification
76
80
  });
77
81
  })(inputPath);
78
82
 
79
- // Minify JavaScript files
80
- let minifiedCnt = 0;
81
- console.log(''); // line break before first log
82
- jsFiles.forEach(jsPath => {
83
- console.info(`Minifying ${ jsPath }...`);
84
- const outputDir = path.join(
85
- path.dirname(jsPath), // path of file to be minified
86
- /so?u?rce?$/.test(path.dirname(jsPath)) ? '../min' // + ../min/ if in *(src|source)/
87
- : outputArg.endsWith('.js') ? path.dirname(outputArg) // or path from file output arg
88
- : outputArg || 'min' // or path from folder output arg or min/ if no output arg passed
89
- );
90
- const outputFilename = (
91
- outputArg.endsWith('.js') && inputArg.endsWith('.js')
92
- ? path.basename(outputArg).replace(/(\.min)?\.js$/, '')
93
- : path.basename(jsPath, '.js')
94
- ) + '.min.js';
95
- const outputPath = path.join(outputDir, outputFilename),
96
- minifiedCode = uglifyJS.minify(fs.readFileSync(jsPath, 'utf8')).code;
97
- if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
98
- fs.writeFileSync(outputPath, minifiedCode, 'utf8');
99
- minifiedCnt++;
100
- });
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));
89
+
90
+ } else { // actually minify JavaScript files
101
91
 
102
- // Print final summary
103
- if (minifiedCnt) {
104
- console.info(`\n${bg}Minification complete!${nc}`);
105
- console.info(`${ minifiedCnt } file${ minifiedCnt > 1 ? 's' : '' } minified.`);
106
- } else console.info(`${by}No unminified JavaScript files found.${nc}`);
92
+ let minifiedCnt = 0;
93
+ console.log(''); // line break before first log
94
+ unminnedJSfiles.forEach(jsPath => {
95
+ console.info(`Minifying ${ jsPath }...`);
96
+ const outputDir = path.join(
97
+ path.dirname(jsPath), // path of file to be minified
98
+ /so?u?rce?$/.test(path.dirname(jsPath)) ? '../min' // + ../min/ if in *(src|source)/
99
+ : outputArg.endsWith('.js') ? path.dirname(outputArg) // or path from file output arg
100
+ : outputArg || 'min' // or path from folder output arg or min/ if no output arg passed
101
+ );
102
+ const outputFilename = (
103
+ outputArg.endsWith('.js') && inputArg.endsWith('.js')
104
+ ? path.basename(outputArg).replace(/(\.min)?\.js$/, '')
105
+ : path.basename(jsPath, '.js')
106
+ ) + '.min.js';
107
+ const outputPath = path.join(outputDir, outputFilename),
108
+ minifiedCode = uglifyJS.minify(fs.readFileSync(jsPath, 'utf8')).code;
109
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
110
+ fs.writeFileSync(outputPath, minifiedCode, 'utf8');
111
+ minifiedCnt++;
112
+ });
113
+
114
+ // Print final summary
115
+ if (minifiedCnt) {
116
+ console.info(`\n${bg}Minification complete!${nc}`);
117
+ console.info(`${ minifiedCnt } file${ minifiedCnt > 1 ? 's' : '' } minified.`);
118
+ } else console.info(`${by}No unminified JavaScript files processed successfully.${nc}`);
119
+ }
107
120
  }
108
121
 
109
- function printWrappedMsg(msg) { // truncates msg, indents 2nd+ lines
122
+ function printWrappedMsg(msg) { // wraps msg, indents 2nd+ lines
110
123
  const terminalWidth = process.stdout.columns || 80,
111
124
  indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
112
125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamlui/minify.js",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "Recursively minify all JavaScript files",
5
5
  "author": {
6
6
  "name": "Adam Lui",