@adamlui/minify.js 1.4.3 → 1.4.4

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 +56 -48
  2. package/minify.js +58 -34
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -18,9 +18,9 @@
18
18
 
19
19
  ### Recursively minify all JavaScript files.
20
20
 
21
- <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>
22
- <a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-red.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
23
- <a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.3-fc7811.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
21
+ <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&color=af68ff&logoColor=white&labelColor=464646&style=for-the-badge"></a>
22
+ <a href="#%EF%B8%8F-mit-license"><img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a>
23
+ <a href="https://www.npmjs.com/package/@adamlui/minify.js?activeTab=versions"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.4.4-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a>
24
24
  <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&color=blue"></a>
25
25
  <a href="https://sonarcloud.io/component_measures?metric=new_vulnerabilities&id=adamlui_js-utils:minify.js/minify.js"><img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Aminify.js%2Fminify.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonarcloud&logoColor=white&labelColor=464646&label=Vulnerabilities&color=gold"></a>
26
26
 
@@ -28,34 +28,38 @@
28
28
 
29
29
  <br>
30
30
 
31
- <img height=8px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
31
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
32
32
 
33
33
  ## ⚡ Installation
34
34
 
35
35
  As a **global utility**:
36
36
 
37
37
  ```
38
- npm install -g @adamlui/minify.js
38
+ $ npm install -g @adamlui/minify.js
39
39
  ```
40
40
 
41
41
  As a **dev dependency** (e.g. for build scripts), from your project root:
42
42
 
43
43
  ```
44
- npm install -D @adamlui/minify.js
44
+ $ npm install -D @adamlui/minify.js
45
45
  ```
46
46
 
47
47
  As a **runtime dependency** (e.g. for on-the-fly minification), from your project root:
48
48
 
49
49
  ```
50
- npm install @adamlui/minify.js
50
+ $ npm install @adamlui/minify.js
51
51
  ```
52
52
 
53
+ <br>
54
+
55
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
56
+
53
57
  ## 💻 Command line usage
54
58
 
55
59
  The basic **global command** is:
56
60
 
57
61
  ```
58
- minify-js
62
+ $ minify-js
59
63
  ```
60
64
 
61
65
  **💡 Note:** Pass `-n` or `--dry-run` to only see what files will be processed.
@@ -65,7 +69,7 @@ minify-js
65
69
  To specify **input/output** paths:
66
70
 
67
71
  ```
68
- minify-js [input_path] [output_path]
72
+ $ minify-js [input_path] [output_path]
69
73
  ```
70
74
 
71
75
  - `[input_path]`: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
@@ -84,57 +88,59 @@ To use as a **package script**, in your project's `package.json`:
84
88
  ```
85
89
 
86
90
  Replace `<minify-js-cmd>` with `minify-js` + optional args. Then, `npm run build:js` can be used to run the command.
87
- <br><br>
91
+
92
+ #
88
93
 
89
94
  ### Example commands
90
95
 
91
96
  Minify all JavaScript files in the **current directory** (outputs to `min/`):
92
97
 
93
98
  ```
94
- minify-js
99
+ $ minify-js
95
100
  ```
96
101
 
97
102
  Minify all JavaScript files in a **specific directory** (outputs to `path/to/your/directory/min/`):
98
103
 
99
104
  ```
100
- minify-js path/to/your/directory
105
+ $ minify-js path/to/your/directory
101
106
  ```
102
107
 
103
108
  Minify a **specific file** (outputs to `path/to/your/min/file.min.js`):
104
109
 
105
110
  ```
106
- minify-js path/to/your/file.js
111
+ $ minify-js path/to/your/file.js
107
112
  ```
108
113
 
109
114
  Specify both **input and output** directories (outputs to `output_folder/`):
110
115
 
111
116
  ```
112
- minify-js input_folder output_folder
117
+ $ minify-js input_folder output_folder
113
118
  ```
114
119
 
115
- ### Command line options
116
-
117
- #### Config options:
120
+ #
118
121
 
119
- Flag | Description
120
- -------------------------------|-------------------------------------------------------------------------
121
- `-n` or `--dry-run` | Don't actually minify the file(s), just show if they will be processed.
122
- `-d` or `--include-dotfolders` | Include dotfolders in file search.
123
- `-D` or `--include-dotfiles` | Include dotfiles in file search.
124
- `-R` or `--no-recursion` | Disable recursive file searching.
125
- `-M` or `--no-mangle` | Disable mangling names.
126
- `-q` or `--quiet` | Suppress all logging except errors.
122
+ ### Command line options
127
123
 
128
- #### Info commands:
124
+ ```
125
+ Config options:
126
+ -n, --dry-run Don't actually minify the file(s), just show
127
+ if they will be processed.
128
+ -d, --include-dotfolders Include dotfolders in file search.
129
+ -D, --include-dotfiles Include dotfiles in file search.
130
+ -R, --no-recursion Disable recursive file searching.
131
+ -M, --no-mangle Disable mangling names.
132
+ -q, --quiet Suppress all logging except errors.
129
133
 
130
- Command | Description
131
- --------------------|----------------------
132
- `-h` or `--help` | Display help screen.
133
- `-v` or `--version` | Show version number.
134
+ Info commands:
135
+ -h, --help Display help screen.
136
+ -v, --version Show version number.
137
+ ```
134
138
 
135
139
  <br>
136
140
 
137
- ## 🔌 API reference
141
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
142
+
143
+ ## 🔌 API usage
138
144
 
139
145
  You can also import **minify.js** into your app to use its API methods, both as an ECMAScript module or a CommonJS module.
140
146
 
@@ -150,17 +156,20 @@ import * as minifyJS from '@adamlui/minify.js';
150
156
  const minifyJS = require('@adamlui/minify.js');
151
157
  ```
152
158
 
153
- ### minify(input, options)
159
+ #
160
+
161
+ ### `minify(input[, options])`
154
162
 
155
- This function minifies JavaScript code based on the string input supplied.
163
+ Minifies JavaScript code based on the string input supplied.
156
164
 
157
165
  If **source code** is passed, it is directly minified, then an object containing `srcPath` + `code` + `error` is returned:
158
166
 
159
167
  ```js
160
168
  const srcCode = 'function add(first, second) { return first + second; }',
161
169
  minifyResult = minifyJS.minify(srcCode);
162
- console.log(minifyResult.error); // runtime error, or `undefined` if no error
163
- console.log(minifyResult.code); // minified output: function add(n,d){return n+d}
170
+
171
+ console.log(minifyResult.error); // outputs runtime error, or `undefined` if no error
172
+ console.log(minifyResult.code); // outputs minified JS: function add(n,d){return n+d}
164
173
  ```
165
174
 
166
175
  If a **file path** is passed, the file's code is loaded then minified, returning an object like above.
@@ -168,17 +177,19 @@ If a **file path** is passed, the file's code is loaded then minified, returning
168
177
  If a **directory path** is passed, JavaScript files are searched for (recursively by default), each one's code is loaded then minified, then an array of objects containing `srcPath` + `code` + `error` is returned:
169
178
 
170
179
  ```js
171
- const results = minifyJS.minify('.');
172
- results.forEach(result =>
173
- console.log(result.srcPath)); // paths to JS files in working directory + all nested directories
174
- console.log(results[1].code); // minified code of 2nd JS file if found, or `undefined` if not found
180
+ // Outputs paths to source JS files in working directory + all nested directories
181
+ const minifyResults = minifyJS.minify('.');
182
+ minifyResults.forEach(result => console.log(result.srcPath));
183
+
184
+ // Outputs minified code of 2nd JS file if found, or `undefined` if not found
185
+ console.log(minifyResults[1].code);
175
186
  ```
176
187
 
177
188
  Options are boolean, passed as object properties. For example:
178
189
 
179
190
  ```js
191
+ // Returns data object where dotfiles are also processed if `input` is a path
180
192
  minifyJS.minify(input, { dotFiles: true });
181
- // returns data object where dotfiles are also processed if `input` is a path
182
193
  ```
183
194
 
184
195
  Possible parameters (and their default settings) are:
@@ -191,17 +202,17 @@ Name | Desciption | Default
191
202
  `dotFiles` | Include dotfiles in file search. | `false`
192
203
  `mangle` | Shorten variable names (typically to one character). | `true`
193
204
 
194
- <br>
205
+ #
195
206
 
196
- ### findJS(searchDir, options)
207
+ ### `findJS(searchDir[, options])`
197
208
 
198
- This function searches for all unminified JavaScript files within the `searchDir` string passed (useful for discovering what files [`minify()`](#minifyinput-options) will process) and returns an array containing their filepaths.
209
+ Searches for all unminified JavaScript files within the `searchDir` string passed (useful for discovering what files [`minify()`](#minifyinput-options) will process) and returns an array containing their filepaths.
199
210
 
200
211
  Options are boolean, passed as object properties. For example:
201
212
 
202
213
  ```js
214
+ // Returns array containing filepaths to unminified JS files in exactly `searchDir`
203
215
  minifyJS.findJS(searchDir, { recursive: false });
204
- // returns array containing filepaths to unminified JS files in exactly `searchDir`
205
216
  ```
206
217
 
207
218
  Possible parameters (and their default settings) are:
@@ -215,10 +226,7 @@ Name | Desciption | Default
215
226
 
216
227
  <br>
217
228
 
218
- ## 💖 Support
219
-
220
- Please consider [giving a GitHub ⭐](https://github.com/adamlui/js-utils) if this helped you!
221
- <br><br>
229
+ <img height=6px width="100%" src="https://raw.githubusercontent.com/adamlui/js-utils/main/docs/images/aqua-separator.png">
222
230
 
223
231
  ## 🏛️ MIT License
224
232
 
package/minify.js CHANGED
@@ -87,46 +87,36 @@ else { // run as CLI tool
87
87
  bg = '\x1b[1;92m'; // bright green
88
88
 
89
89
  // Load FLAG settings
90
- const config = {
91
- dryRun: process.argv.some(arg => /^--?(?:n|dry-?run)$/.test(arg)),
92
- includeDotFolders: process.argv.some(arg =>
93
- /^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?=?(?:true|1)?)$/.test(arg)),
94
- includeDotFiles: process.argv.some(arg =>
95
- /^--?(?:df|D|(?:include-?)?dot-?files?=?(?:true|1)?)$/.test(arg)),
96
- noRecursion: process.argv.some(arg =>
97
- /^--?(?:R|(?:disable|no)-?recursion|recursion=(?:false|0))$/.test(arg)),
98
- noMangle: process.argv.some(arg =>
99
- /^--?(?:M|(?:disable|no)-?mangle|mangle=(?:false|0))$/.test(arg)),
100
- quietMode: process.argv.some(arg => /^--?q(?:uiet)?(?:-?mode)?$/.test(arg))
90
+ const config = {};
91
+ const argRegex = {
92
+ 'dryRun': /^--?(?:n|dry-?run)$/,
93
+ 'includeDotFolders': /^--?(?:dd?|(?:include-?)?dot-?(?:folder|dir(?:ector(?:y|ie))?)s?=?(?:true|1)?)$/,
94
+ 'includeDotFiles': /^--?(?:df|D|(?:include-?)?dot-?files?=?(?:true|1)?)$/,
95
+ 'noRecursion': /^--?(?:R|(?:disable|no)-?recursion|recursion=(?:false|0))$/,
96
+ 'noMangle': /^--?(?:M|(?:disable|no)-?mangle|mangle=(?:false|0))$/,
97
+ 'quietMode': /^--?q(?:uiet)?(?:-?mode)?$/,
98
+ 'help': /^--?h(?:elp)?$/,
99
+ 'version': /^--?ve?r?s?i?o?n?$/
101
100
  };
101
+ process.argv.forEach(arg => {
102
+ if (!arg.startsWith('-')) return;
103
+ const matchedFlag = Object.keys(argRegex).find(flag => argRegex[flag].test(arg));
104
+ if (matchedFlag) config[matchedFlag] = true;
105
+ else {
106
+ console.error(`\n${br}ERROR: Arg '${ arg }' not recognized.${nc}`);
107
+ console.info(`\n${by}Valid arguments are below.${nc}`);
108
+ printHelpScreen(['configOptions', 'infoCmds']);
109
+ process.exit(1);
110
+ }});
102
111
 
103
112
  // Show HELP screen if -h or --help passed
104
- if (process.argv.some(arg => /^--?h(?:elp)?$/.test(arg))) {
105
- printHelp(`\n${by}minify-js [inputPath] [outputPath] [options]${nc}`);
106
- printHelp('\nPath arguments:');
107
- printHelp(' [inputPath] '
108
- + 'Path to JS file or directory containing JS files to be minified,'
109
- + ' relative to the current working directory.');
110
- printHelp(' [outputPath] '
111
- + 'Path to file or directory where minified files will be stored,'
112
- + ' relative to original file location (if not provided, min/ is used).');
113
- printHelp('\nConfig options:');
114
- printHelp(' -n, --dry-run Don\'t actually minify the file(s),'
115
- + ' just show if they will be processed.');
116
- printHelp(' -d, --include-dotfolders Include dotfolders in file search.');
117
- printHelp(' -D, --include-dotfiles Include dotfiles in file search.');
118
- printHelp(' -R, --no-recursion Disable recursive file searching.');
119
- printHelp(' -M, --no-mangle Disable mangling names.');
120
- printHelp(' -q, --quiet Suppress all logging except errors.');
121
- printHelp('\nInfo commands:');
122
- printHelp(' -h, --help Display this help screen.');
123
- printHelp(' -v, --version Show version number.');
113
+ if (process.argv.some(arg => argRegex.help.test(arg))) printHelpScreen();
124
114
 
125
115
  // Show VERSION number if -v or --version passed
126
- } else if (process.argv.some(arg => /^--?ve?r?s?i?o?n?$/.test(arg))) {
116
+ else if (process.argv.some(arg => argRegex.version.test(arg)))
127
117
  console.info('v' + require('./package.json').version);
128
118
 
129
- } else { // run MAIN routine
119
+ else { // run MAIN routine
130
120
 
131
121
  // Init I/O args
132
122
  const [inputArg = '', outputArg = ''] = ( // default to empty strings for error-less handling
@@ -200,7 +190,7 @@ else { // run as CLI tool
200
190
 
201
191
  // Define LOGGING functions
202
192
 
203
- function printHelp(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
193
+ function printHelpMsg(msg) { // wrap msg + indent 2nd+ lines (for --help screen)
204
194
  const terminalWidth = process.stdout.columns || 80,
205
195
  indentation = 29, lines = [], words = msg.match(/\S+|\s+/g);
206
196
 
@@ -223,5 +213,39 @@ else { // run as CLI tool
223
213
  ));
224
214
  }
225
215
 
216
+ function printHelpScreen(includeSections = ['sampleCmd', 'pathArgs', 'configOptions', 'infoCmds']) {
217
+ const sections = {
218
+ 'sampleCmd': [
219
+ `\n${by}minify-js [inputPath] [outputPath] [options]${nc}`
220
+ ],
221
+ 'pathArgs': [
222
+ '\nPath arguments:',
223
+ ' [inputPath] '
224
+ + 'Path to JS file or directory containing JS files to be minified,'
225
+ + ' relative to the current working directory.',
226
+ ' [outputPath] '
227
+ + 'Path to file or directory where minified files will be stored,'
228
+ + ' relative to original file location (if not provided, min/ is used).'
229
+ ],
230
+ 'configOptions': [
231
+ '\nConfig options:',
232
+ ' -n, --dry-run Don\'t actually minify the file(s),'
233
+ + ' just show if they will be processed.',
234
+ ' -d, --include-dotfolders Include dotfolders in file search.',
235
+ ' -D, --include-dotfiles Include dotfiles in file search.',
236
+ ' -R, --no-recursion Disable recursive file searching.',
237
+ ' -M, --no-mangle Disable mangling names.',
238
+ ' -q, --quiet Suppress all logging except errors.'
239
+ ],
240
+ 'infoCmds': [
241
+ '\nInfo commands:',
242
+ ' -h, --help Display help screen.',
243
+ ' -v, --version Show version number.'
244
+ ]
245
+ };
246
+ includeSections.forEach(section => { // print valid arg elems
247
+ if (sections[section]) sections[section].forEach(line => printHelpMsg(line)); });
248
+ }
249
+
226
250
  function printIfNotQuiet(msg) { if (!config.quietMode) console.info(msg); }
227
251
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamlui/minify.js",
3
- "version": "1.4.3",
3
+ "version": "1.4.4",
4
4
  "description": "Recursively minify all JavaScript files",
5
5
  "author": {
6
6
  "name": "Adam Lui",