@adamlui/minify.js 1.0.3 → 1.1.1

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 +5 -5
  2. package/minify.js +36 -26
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -2,7 +2,7 @@
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.3-fc7811.svg?logo=npm&logoColor=white&labelColor=464646&style=for-the-badge"></a>
5
+ <a href="https://www.npmjs.com/package/@adamlui/minify.js"><img height=31 src="https://img.shields.io/badge/Latest_Build-1.1.1-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">
@@ -35,16 +35,16 @@ minify-js
35
35
 
36
36
  #
37
37
 
38
- To specify **input/output** directories:
38
+ To specify **input/output** paths:
39
39
 
40
40
  ```
41
41
  minify-js <input_path> <output_path>
42
42
  ```
43
43
 
44
- - `<input_path>`: Path to directory containing JavaScript files to be minified, relative to the current working directory.
45
- - `<output_path>`: Path to directory where minified files will be stored, relative to original file location. (If not provided, `min/` is used.)
44
+ - `<input_path>`: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
45
+ - `<output_path>`: Path to file or directory where minified files will be stored, relative to original file location (if not provided, `min/` is used).
46
46
 
47
- **💡 Note:** The paths can be either folders or specific files. If folders are passed, files will be processed recursively.
47
+ **💡 Note:** If folders are passed, files will be processed recursively. To include dotfolders, pass `--include-dotfolders`. To include dotfiles, pass `--include-dotfiles`.
48
48
 
49
49
  #
50
50
 
package/minify.js CHANGED
@@ -11,38 +11,49 @@ const nc = '\x1b[0m', // no color
11
11
  by = '\x1b[1;33m', // bright yellow
12
12
  bg = '\x1b[1;92m'; // bright green
13
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(/^\/*/, '') : '';
14
+ // Init I/O args
15
+ const [inputArg = '', outputArg = ''] = ( // default to empty strings for error-less handling
16
+ process.argv.slice(2) // exclude executable and script path
17
+ .filter(arg => !arg.startsWith('-')) // exclude flags
18
+ .map(arg => arg.replace(/^\/*/, '')) // clean leading slashes to avoid parsing system root
19
+ );
17
20
 
18
21
  // 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
+ const inputPath = path.resolve(__dirname, inputArg);
23
+ if (inputArg && !fs.existsSync(inputPath)) {
24
+ console.error(`\n${br}Error: First arg must be an existing file or directory.`
25
+ + `\n${ inputPath } does not exist.${nc}`
26
+ + '\n\nExample valid command: \n>> minify-js . output.min.js');
22
27
  process.exit(1);
23
28
  }
24
29
 
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() && file !== 'node_modules')
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
- })();
30
+ // Store flag settings
31
+ const config = {
32
+ includeDotFolders: process.argv.some(arg => /--?include-dot-?folders?/.test(arg)),
33
+ includeDotFiles: process.argv.some(arg => /--?include-dot-?files?/.test(arg))
34
+ };
35
+
36
+ // Recursively find all eligible JavaScript files or arg-passed file
37
+ const jsFiles = [];
38
+ if (inputArg.endsWith('.js')) jsFiles.push(inputPath);
39
+ else (function findJSfiles(dir) {
40
+ const files = fs.readdirSync(dir);
41
+ files.forEach(file => {
42
+ const filePath = path.resolve(dir, file);
43
+ if (fs.statSync(filePath).isDirectory() && file != 'node_modules' &&
44
+ (config.includeDotFolders || !file.startsWith('.')))
45
+ findJSfiles(filePath); // recursively find unminified JS in eligible dir
46
+ else if (/\.js(?<!\.min\.js)$/.test(file) &&
47
+ (config.includeDotFiles || !file.startsWith('.')))
48
+ jsFiles.push(filePath); // store eligible unminified JS file for compilation
49
+ });
50
+ })(inputPath);
41
51
 
42
52
  // Minify JavaScript files
43
53
  let minifiedCnt = 0;
44
54
  console.log(''); // line break before first log
45
55
  jsFiles.forEach(jsPath => {
56
+ console.info(`Minifying ${ jsPath }...`);
46
57
  const outputDir = path.join(
47
58
  path.dirname(jsPath), // path of file to be minified
48
59
  /so?u?rce?$/.test(path.dirname(jsPath)) ? '../min' // + ../min/ if in *(src|source)/
@@ -54,9 +65,8 @@ jsFiles.forEach(jsPath => {
54
65
  ? path.basename(outputArg).replace(/(\.min)?\.js$/, '')
55
66
  : path.basename(jsPath, '.js')
56
67
  ) + '.min.js';
57
- const outputPath = path.join(outputDir, outputFilename);
58
- console.info(`Minifying ${ jsPath }...`);
59
- const minifiedCode = uglifyJS.minify(fs.readFileSync(jsPath, 'utf8')).code;
68
+ const outputPath = path.join(outputDir, outputFilename),
69
+ minifiedCode = uglifyJS.minify(fs.readFileSync(jsPath, 'utf8')).code;
60
70
  if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
61
71
  fs.writeFileSync(outputPath, minifiedCode, 'utf8');
62
72
  minifiedCnt++;
@@ -65,5 +75,5 @@ jsFiles.forEach(jsPath => {
65
75
  // Print final summary
66
76
  if (minifiedCnt) {
67
77
  console.info(`\n${bg}Minification complete!${nc}`);
68
- console.info(`\n${ minifiedCnt } file${ minifiedCnt > 1 ? 's' : '' } minified.`);
78
+ console.info(`${ minifiedCnt } file${ minifiedCnt > 1 ? 's' : '' } minified.`);
69
79
  } 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",
3
+ "version": "1.1.1",
4
4
  "description": "Minify JavaScript files",
5
5
  "author": {
6
6
  "name": "Adam Lui",
@@ -14,7 +14,7 @@
14
14
  "minify-js": "minify.js"
15
15
  },
16
16
  "scripts": {
17
- "test": "echo \"Error: no test specified\" && exit 1",
17
+ "test": "node test/test.js",
18
18
  "bump:patch": "bash utils/bump.sh patch",
19
19
  "bump:minor": "bash utils/bump.sh minor",
20
20
  "bump:major": "bash utils/bump.sh major",