uglify-js-minify-css-allfiles 2.0.4 → 2.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.
- package/CHANGELOG.md +36 -24
- package/README.md +88 -45
- package/demo.js +6 -16
- package/dist/module.js +98 -81
- package/dist/modules/fileHandler.js +19 -19
- package/dist/modules/logger.js +177 -98
- package/dist/modules/minifier.js +9 -9
- package/logs/log-08-20-2024.log +65 -0
- package/minify.d.ts +165 -0
- package/package.json +5 -1
- package/test/lib/test.css +4 -4
- package/test/lib/test.js +1 -1
- package/test/test.css +4 -4
- package/test/test.js +80 -80
- package/.vscode/settings.json +0 -13
- package/logs/error.log +0 -6
- package/logs/summary.log +0 -112
package/CHANGELOG.md
CHANGED
|
@@ -5,78 +5,90 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [2.1.0] - 2024-08-20
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- Logging Enhancements:
|
|
13
|
+
Structured Log Levels: Implemented a new logging system with defined log levels (e.g., info, warn, error) to improve log readability and management.
|
|
14
|
+
Date-based Log Files: Logs are now stored in files organized by date, making it easier to track and analyze logs over time.
|
|
15
|
+
- Type Generation:
|
|
16
|
+
Introduced a new type generation feature to improve type safety and consistency across the application.
|
|
17
|
+
|
|
8
18
|
## [2.0.0] - 2024-08-19
|
|
9
19
|
|
|
10
20
|
### Added
|
|
11
21
|
|
|
12
|
-
-
|
|
13
|
-
- Optional Babel integration for advanced JavaScript transpilation
|
|
14
|
-
- Support for ES modules, allowing use of `import` statements in your projects
|
|
22
|
+
- See Breaking Changes
|
|
15
23
|
|
|
16
24
|
### Changed
|
|
17
25
|
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
26
|
+
- Switched from CommonJS (`require`) to ES modules (`import`) for better compatibility with modern JavaScript
|
|
27
|
+
- Improved error handling and reporting, providing clearer messages for troubleshooting
|
|
28
|
+
- Enhanced CLI interface with more user-friendly options and feedback
|
|
21
29
|
|
|
22
30
|
### Improved
|
|
23
31
|
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
32
|
+
- Significantly faster processing of large directories with many files
|
|
33
|
+
- More robust handling of different file types and encodings
|
|
34
|
+
- Better preservation of source maps in minified files
|
|
27
35
|
|
|
28
36
|
### Breaking Changes
|
|
29
37
|
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
38
|
+
- Minimum Node.js version requirement increased to v14.0.0
|
|
39
|
+
- Configuration file format changed (see migration guide for details)
|
|
40
|
+
- Some CLI options have been renamed for clarity (see documentation for new option names)
|
|
41
|
+
- Support for ES modules, allowing use of `import` statements in your projects
|
|
42
|
+
- New logging system for better error tracking and debugging
|
|
43
|
+
- Optional Babel integration for advanced JavaScript transpilation
|
|
33
44
|
|
|
34
45
|
## [1.3.2] - 2023-05-28
|
|
35
46
|
|
|
36
47
|
### Added
|
|
37
48
|
|
|
38
|
-
-
|
|
49
|
+
- New `CHANGELOG.md` file
|
|
39
50
|
|
|
40
51
|
### Changed
|
|
41
52
|
|
|
42
|
-
-
|
|
43
|
-
-
|
|
44
|
-
-
|
|
53
|
+
- When CSS warnings exist, no code is minified (2022-04-13)
|
|
54
|
+
- Updated UglifyJS to remove 'console.info, console.warn, console.error' (2021-11-12)
|
|
55
|
+
- Updated UglifyJS to work without 'console.log' but keep console.error, info, etc. (2021-03-17)
|
|
45
56
|
|
|
46
57
|
### Fixed
|
|
47
58
|
|
|
48
|
-
-
|
|
49
|
-
-
|
|
59
|
+
- Bug fix (v1.1.7): Solved CleanCSS first line delete error (2020-08-21)
|
|
60
|
+
- Bug fix (v1.1.3): [Description missing] (2020-08-15)
|
|
50
61
|
|
|
51
62
|
## [1.3.1] - 2020-11-03
|
|
52
63
|
|
|
53
64
|
### Changed
|
|
54
65
|
|
|
55
|
-
-
|
|
66
|
+
- Updated UglifyJS to support minification of JavaScript files with ES6 syntax
|
|
56
67
|
|
|
57
68
|
## [1.3.0] - 2020-08-26
|
|
58
69
|
|
|
59
70
|
### Changed
|
|
60
71
|
|
|
61
|
-
-
|
|
72
|
+
- Moved DevDependencies to Dependencies for production use
|
|
62
73
|
|
|
63
74
|
## [1.2.0] - 2020-08-21
|
|
64
75
|
|
|
65
76
|
### Fixed
|
|
66
77
|
|
|
67
|
-
-
|
|
78
|
+
- Solved CleanCSS commentary issue due to changed CSS options
|
|
68
79
|
|
|
69
80
|
### Added
|
|
70
81
|
|
|
71
|
-
-
|
|
82
|
+
- Support for command line execution with 2 parameters
|
|
72
83
|
|
|
73
84
|
## [1.1.0] - 2020-08-20
|
|
74
85
|
|
|
75
86
|
### Added
|
|
76
87
|
|
|
77
|
-
-
|
|
78
|
-
-
|
|
88
|
+
- New `exceptFolder` parameter to exclude specific folders from minification
|
|
89
|
+
- Support for using relative paths
|
|
79
90
|
|
|
91
|
+
[2.1.0]: https://github.com/oinochoe/uglify-js-minify-css-allfiles/compare/v2.0.0...v2.1.0
|
|
80
92
|
[2.0.0]: https://github.com/oinochoe/uglify-js-minify-css-allfiles/compare/v1.3.2...v2.0.0
|
|
81
93
|
[1.3.2]: https://github.com/oinochoe/uglify-js-minify-css-allfiles/compare/v1.3.1...v1.3.2
|
|
82
94
|
[1.3.1]: https://github.com/oinochoe/uglify-js-minify-css-allfiles/compare/v1.3.0...v1.3.1
|
package/README.md
CHANGED
|
@@ -23,12 +23,12 @@ You can easily minify all files in a specific folder, with the option to exclude
|
|
|
23
23
|
|
|
24
24
|
## Table of Contents
|
|
25
25
|
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
-
|
|
31
|
-
-
|
|
26
|
+
- [Installation](#installation)
|
|
27
|
+
- [Usage](#usage)
|
|
28
|
+
- [Parameters](#parameters)
|
|
29
|
+
- [Options](#options)
|
|
30
|
+
- [Changelog](#changelog)
|
|
31
|
+
- [License](#license)
|
|
32
32
|
|
|
33
33
|
## Installation
|
|
34
34
|
|
|
@@ -42,57 +42,100 @@ $ npm i uglify-js-minify-css-allfiles
|
|
|
42
42
|
|
|
43
43
|
1. Basic usage (no Babel):
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
```js
|
|
46
|
+
import minifyAll from 'uglify-js-minify-css-allfiles';
|
|
47
|
+
|
|
48
|
+
await minifyAll('./src/');
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
2. Using options:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import minifyAll from 'uglify-js-minify-css-allfiles';
|
|
55
|
+
|
|
56
|
+
await minifyAll('./src/', {
|
|
57
|
+
excludeFolder: 'node_modules',
|
|
58
|
+
useBabel: {
|
|
59
|
+
targets: 'chrome 40',
|
|
60
|
+
modules: false,
|
|
61
|
+
useBuiltIns: 'usage',
|
|
62
|
+
corejs: 3,
|
|
63
|
+
},
|
|
64
|
+
useLog: {
|
|
65
|
+
logDir: 'logs',
|
|
66
|
+
retentionDays: 30,
|
|
67
|
+
logLevel: 'info',
|
|
68
|
+
dateFormat: 'YYYY-MM-DD',
|
|
69
|
+
timeZone: 'UTC',
|
|
70
|
+
logToConsole: true,
|
|
71
|
+
logToFile: true,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
3. Using Babel and custom logging options:
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
import minifyAll from 'uglify-js-minify-css-allfiles';
|
|
80
|
+
|
|
81
|
+
await minifyAll('./src/', {
|
|
82
|
+
useBabel: { targets: 'chrome 40' },
|
|
83
|
+
useLog: {
|
|
84
|
+
logDir: 'logs',
|
|
85
|
+
retentionDays: 30,
|
|
86
|
+
logLevel: 'info',
|
|
87
|
+
dateFormat: 'YYYY-MM-DD',
|
|
88
|
+
timeZone: 'UTC',
|
|
89
|
+
logToConsole: true,
|
|
90
|
+
logToFile: true,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
```
|
|
47
94
|
|
|
48
|
-
|
|
49
|
-
```
|
|
95
|
+
## Parameters
|
|
50
96
|
|
|
51
|
-
|
|
97
|
+
The `minifyAll` function accepts the following parameters:
|
|
52
98
|
|
|
53
|
-
|
|
54
|
-
|
|
99
|
+
| Parameter | Type | Default | Description |
|
|
100
|
+
| ------------- | ------ | ------- | -------------------------------------------------------------------------------------------- |
|
|
101
|
+
| `contentPath` | string | - | The path to the directory containing the files to be minified. This is a required parameter. |
|
|
102
|
+
| `options` | object | `{}` | An object containing options for minification, Babel, and logging. |
|
|
55
103
|
|
|
56
|
-
|
|
57
|
-
```
|
|
104
|
+
## Options
|
|
58
105
|
|
|
59
|
-
|
|
106
|
+
The `options` object can have the following properties:
|
|
60
107
|
|
|
61
|
-
|
|
62
|
-
|
|
108
|
+
| Property | Type | Default | Description |
|
|
109
|
+
| --------------- | ----------------- | ------- | ------------------------------------------------------------------------------------------ |
|
|
110
|
+
| `excludeFolder` | string | `''` | The name of a folder to exclude from minification. |
|
|
111
|
+
| `useBabel` | boolean \| object | `false` | If `true`, enables Babel with default settings. If an object, specifies Babel options. |
|
|
112
|
+
| `useLog` | boolean \| object | `true` | If `true`, enables logging with default settings. If an object, specifies logging options. |
|
|
63
113
|
|
|
64
|
-
|
|
65
|
-
presets: [
|
|
66
|
-
[
|
|
67
|
-
'@babel/preset-env',
|
|
68
|
-
{
|
|
69
|
-
targets: {
|
|
70
|
-
esmodules: false, // Target ES2015
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
],
|
|
74
|
-
],
|
|
75
|
-
});
|
|
76
|
-
```
|
|
114
|
+
### Babel Options
|
|
77
115
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
The `minifyAll` function accepts the following parameters:
|
|
116
|
+
When `useBabel` is an object, it can have the following properties:
|
|
81
117
|
|
|
82
|
-
|
|
|
83
|
-
|
|
|
84
|
-
| `
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
118
|
+
| Property | Type | Description |
|
|
119
|
+
| ------------------------------------- | --------------------------- | --------------------------------------------------------------------------------------------------------- |
|
|
120
|
+
| `targets` | string \| object | Specifies the target environments. |
|
|
121
|
+
| `modules` | string \| false | Enables transformation of ES6 module syntax to another module type. |
|
|
122
|
+
| `useBuiltIns` | 'usage' \| 'entry' \| false | Configures how babel-preset-env handles polyfills. |
|
|
123
|
+
| `corejs` | number \| object | Specifies the core-js version. |
|
|
124
|
+
| ... (other @babel/preset-env options) | Various | Refer to [@babel/preset-env documentation](https://babeljs.io/docs/en/babel-preset-env) for more options. |
|
|
87
125
|
|
|
88
|
-
|
|
126
|
+
### Log Options
|
|
89
127
|
|
|
90
|
-
|
|
128
|
+
When `useLog` is an object, it can have the following properties:
|
|
91
129
|
|
|
92
|
-
| Property
|
|
93
|
-
|
|
|
94
|
-
| `
|
|
95
|
-
| `
|
|
130
|
+
| Property | Type | Default | Description |
|
|
131
|
+
| --------------- | ------- | -------------- | --------------------------------------------------------------------- |
|
|
132
|
+
| `logDir` | string | `'logs'` | Specifies the directory for log files. |
|
|
133
|
+
| `retentionDays` | number | 30 | Number of days to retain log files. |
|
|
134
|
+
| `logLevel` | string | `'info'` | Specifies the level of logging (e.g., `'info'`, `'warn'`, `'error'`). |
|
|
135
|
+
| `dateFormat` | string | `'YYYY-MM-DD'` | Format for the date in log entries. |
|
|
136
|
+
| `timeZone` | string | `'UTC'` | Time zone for timestamps in log entries. |
|
|
137
|
+
| `logToConsole` | boolean | `true` | Determines if logs should also be output to the console. |
|
|
138
|
+
| `logToFile` | boolean | `true` | Determines if logs should be written to a file. |
|
|
96
139
|
|
|
97
140
|
## Changelog
|
|
98
141
|
|
package/demo.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
import minifyAll from './dist/module.js';
|
|
2
2
|
|
|
3
|
-
minifyAll('./test/',
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// {
|
|
10
|
-
// targets: {
|
|
11
|
-
// esmodules: false, // Target ES2015
|
|
12
|
-
// },
|
|
13
|
-
// },
|
|
14
|
-
// ],
|
|
15
|
-
// ],
|
|
16
|
-
// });
|
|
17
|
-
// console.log(process.argv.slice(2)[0], process.argv.slice(2)[1]);
|
|
18
|
-
// minifyAll(process.argv.slice(2)[0], process.argv.slice(2)[1]);
|
|
3
|
+
minifyAll('./test/', {
|
|
4
|
+
excludeFolder: 'lib',
|
|
5
|
+
useBabel: {
|
|
6
|
+
targets: 'chrome 40',
|
|
7
|
+
},
|
|
8
|
+
});
|
package/dist/module.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* uglify-js and minify-css for all files
|
|
3
3
|
* Released under the terms of MIT license
|
|
4
|
-
*
|
|
5
4
|
* Copyright (C) 2024 yeongmin
|
|
6
5
|
*/
|
|
7
6
|
|
|
@@ -13,31 +12,29 @@ import { minifyJS, minifyCSS } from './modules/minifier.js';
|
|
|
13
12
|
import babelCore from '@babel/core';
|
|
14
13
|
|
|
15
14
|
const FILE_HANDLERS = {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
},
|
|
15
|
+
'.js': async (filePath, content, logger, babelOptions) => {
|
|
16
|
+
try {
|
|
17
|
+
let transformed = content;
|
|
18
|
+
if (babelOptions) {
|
|
19
|
+
transformed = babelCore.transformSync(content, babelOptions).code;
|
|
20
|
+
}
|
|
21
|
+
const result = minifyJS(transformed);
|
|
22
|
+
await writeFile(filePath, result, logger);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
await logger?.error('JavaScript minification failed', { filePath, error: error.message });
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
'.css': async (filePath, content, logger) => {
|
|
28
|
+
try {
|
|
29
|
+
const output = await minifyCSS(content);
|
|
30
|
+
if (0 < output.warnings.length) {
|
|
31
|
+
await logger?.warn('CSS minification warnings', { filePath, warnings: output.warnings });
|
|
32
|
+
}
|
|
33
|
+
await writeFile(filePath, output.styles, logger);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
await logger?.error('CSS minification failed', { filePath, error: error.message });
|
|
36
|
+
}
|
|
37
|
+
},
|
|
41
38
|
};
|
|
42
39
|
|
|
43
40
|
/**
|
|
@@ -49,43 +46,72 @@ const FILE_HANDLERS = {
|
|
|
49
46
|
* @returns {Promise<void>}
|
|
50
47
|
*/
|
|
51
48
|
async function processFile(filePath, logger, babelOptions = null) {
|
|
49
|
+
try {
|
|
52
50
|
const fileContent = await fs.readFile(filePath, 'utf-8');
|
|
53
|
-
const fileExtension = path.extname(filePath);
|
|
51
|
+
const fileExtension = path.extname(filePath).toLowerCase();
|
|
54
52
|
const handler = FILE_HANDLERS[fileExtension];
|
|
55
53
|
|
|
56
54
|
if (handler) {
|
|
57
|
-
|
|
55
|
+
await handler(filePath, fileContent, logger, babelOptions);
|
|
58
56
|
} else {
|
|
59
|
-
|
|
57
|
+
await logger?.info(`Unsupported file type, skipping: ${filePath}`);
|
|
60
58
|
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
await logger?.error('Error processing file', { filePath, error: error.message });
|
|
61
|
+
}
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
/**
|
|
64
65
|
* Resolves Babel options based on the provided configuration.
|
|
65
66
|
*
|
|
66
|
-
* @param {Object|null}
|
|
67
|
+
* @param {Object|null} useBabel - The Babel options object or null.
|
|
67
68
|
* @returns {Object|null} The resolved Babel options or null if no valid options are provided.
|
|
68
69
|
*/
|
|
69
|
-
function resolveBabelOptions(
|
|
70
|
-
|
|
70
|
+
function resolveBabelOptions(useBabel) {
|
|
71
|
+
if (!useBabel) return null;
|
|
72
|
+
return {
|
|
73
|
+
presets: [['@babel/preset-env', typeof useBabel === 'object' ? useBabel : {}]],
|
|
74
|
+
};
|
|
75
|
+
}
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Options for Babel configuration.
|
|
79
|
+
*
|
|
80
|
+
* @typedef {Object} BabelOptions
|
|
81
|
+
* @property {string|string[]|Object<string, string>} [targets] - Specifies the target environments for the code.
|
|
82
|
+
* @property {'amd'|'umd'|'systemjs'|'commonjs'|'cjs'|'auto'|false} [modules] - Module format to use for the output.
|
|
83
|
+
* @property {boolean} [debug] - Enables or disables debug mode.
|
|
84
|
+
* @property {string[]} [include] - List of plugins or features to include.
|
|
85
|
+
* @property {string[]} [exclude] - List of plugins or features to exclude.
|
|
86
|
+
* @property {'usage'|'entry'|false} [useBuiltIns] - Determines how polyfills are added.
|
|
87
|
+
* @property {2|3|{version: 2|3, proposals: boolean}} [corejs] - Specifies the version of core-js to use and whether to include proposals.
|
|
88
|
+
* @property {boolean} [forceAllTransforms] - Forces the application of all transformations.
|
|
89
|
+
* @property {string} [configPath] - Path to the configuration file.
|
|
90
|
+
* @property {boolean} [ignoreBrowserslistConfig] - Ignores the browserslist configuration.
|
|
91
|
+
* @property {boolean} [shippedProposals] - Enables support for shipped proposals.
|
|
92
|
+
*/
|
|
86
93
|
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Options for logging configuration.
|
|
96
|
+
*
|
|
97
|
+
* @typedef {Object} LogOptions
|
|
98
|
+
* @property {string} [logDir] - Specifies the directory for log files.
|
|
99
|
+
* @property {number} [retentionDays] - Number of days to retain log files.
|
|
100
|
+
* @property {string} [logLevel] - Specifies the level of logging (e.g., 'info', 'warn', 'error').
|
|
101
|
+
* @property {string} [dateFormat] - Format for the date in log entries.
|
|
102
|
+
* @property {string} [timeZone] - Time zone for timestamps in log entries.
|
|
103
|
+
* @property {boolean} [logToConsole] - Determines if logs should also be output to the console.
|
|
104
|
+
* @property {boolean} [logToFile] - Determines if logs should be written to a file.
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Options for minification configuration.
|
|
109
|
+
*
|
|
110
|
+
* @typedef {Object} MinifyOptions
|
|
111
|
+
* @property {string} [excludeFolder] - Folder to exclude from minification.
|
|
112
|
+
* @property {boolean|BabelOptions} [useBabel] - Whether to use Babel for transformation, and the options for Babel if used.
|
|
113
|
+
* @property {boolean|LogOptions} [useLog] - Whether to use logging, and the options for logging if used.
|
|
114
|
+
*/
|
|
89
115
|
|
|
90
116
|
/**
|
|
91
117
|
* Minifies all JavaScript and CSS files in the specified directory and its subdirectories.
|
|
@@ -93,47 +119,38 @@ function resolveBabelOptions(options) {
|
|
|
93
119
|
* @async
|
|
94
120
|
* @function minifyAll
|
|
95
121
|
* @param {string} contentPath - The path to the directory containing the files to be minified.
|
|
96
|
-
* @param {
|
|
97
|
-
* @param {Object} [babelOptions=null] - Babel options for converting JavaScript files.
|
|
122
|
+
* @param {MinifyOptions} [options={}] - Options for minification, Babel, and logging.
|
|
98
123
|
* @returns {Promise<void>} A promise that resolves when all files have been processed.
|
|
99
124
|
* @throws {Error} If there's an issue reading or writing files.
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* // Minify all files in './test/' directory, excluding 'lib' folder
|
|
103
|
-
* import minifyAll from './main.js';
|
|
104
|
-
* await minifyAll('./test/', 'lib');
|
|
105
|
-
*
|
|
106
|
-
* @example
|
|
107
|
-
* // Minify all files in a directory specified as a command line argument
|
|
108
|
-
* import minifyAll from './main.js';
|
|
109
|
-
* await minifyAll(process.argv[2], process.argv[3]);
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* // Add babel options for converting JavaScript files.
|
|
113
|
-
* minifyAll('./test/', 'lib', {
|
|
114
|
-
* presets: [
|
|
115
|
-
* [
|
|
116
|
-
* '@babel/preset-env',
|
|
117
|
-
* {
|
|
118
|
-
* targets: {
|
|
119
|
-
* esmodules: false, // Target ES2015
|
|
120
|
-
* },
|
|
121
|
-
* },
|
|
122
|
-
* ],
|
|
123
|
-
* ],
|
|
124
|
-
* });
|
|
125
125
|
*/
|
|
126
|
-
export default async function minifyAll(contentPath,
|
|
127
|
-
|
|
126
|
+
export default async function minifyAll(contentPath, options = {}) {
|
|
127
|
+
const { excludeFolder = '', useBabel = false, useLog = true, logOptions = {} } = options;
|
|
128
|
+
|
|
129
|
+
const logger = useLog ? new Logger(logOptions) : null;
|
|
130
|
+
|
|
131
|
+
if (logger) {
|
|
128
132
|
await logger.initialize();
|
|
133
|
+
await logger.info('Starting minification process', { contentPath, excludeFolder, useBabel });
|
|
134
|
+
}
|
|
129
135
|
|
|
130
|
-
|
|
131
|
-
|
|
136
|
+
const rootDir = path.resolve(contentPath || '');
|
|
137
|
+
const finalBabelOptions = resolveBabelOptions(useBabel);
|
|
132
138
|
|
|
139
|
+
try {
|
|
133
140
|
await getAllFiles(rootDir, async (filePath) => {
|
|
134
|
-
|
|
135
|
-
await
|
|
141
|
+
if (excludeFolder && path.relative(rootDir, filePath).startsWith(excludeFolder)) {
|
|
142
|
+
await logger?.debug('Skipping excluded file', { filePath });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
await processFile(filePath, logger, finalBabelOptions);
|
|
147
|
+
logger?.incrementProcessedFiles(filePath);
|
|
136
148
|
});
|
|
149
|
+
} catch (error) {
|
|
150
|
+
await logger?.error('Error in minification process', { error: error.message });
|
|
151
|
+
}
|
|
137
152
|
|
|
153
|
+
if (logger) {
|
|
138
154
|
await logger.summarize();
|
|
155
|
+
}
|
|
139
156
|
}
|
|
@@ -15,16 +15,16 @@ import path from 'path';
|
|
|
15
15
|
* @returns {Promise<void>}
|
|
16
16
|
*/
|
|
17
17
|
export async function getAllFiles(dirPath, callback) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
18
|
+
const files = await fs.readdir(dirPath);
|
|
19
|
+
for (const file of files) {
|
|
20
|
+
const filePath = path.join(dirPath, file);
|
|
21
|
+
const fileStat = await fs.stat(filePath);
|
|
22
|
+
if (fileStat.isFile()) {
|
|
23
|
+
await callback(filePath, fileStat);
|
|
24
|
+
} else if (fileStat.isDirectory()) {
|
|
25
|
+
await getAllFiles(filePath, callback);
|
|
27
26
|
}
|
|
27
|
+
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
|
@@ -37,15 +37,15 @@ export async function getAllFiles(dirPath, callback) {
|
|
|
37
37
|
* @returns {Promise<void>}
|
|
38
38
|
*/
|
|
39
39
|
export async function writeFile(filePath, content, logger) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
if (typeof content === 'undefined' || content === '' || content === null) {
|
|
41
|
+
await logger?.error('Invalid or empty content', { filePath });
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
await logger?.info(`Writing file: ${filePath}`);
|
|
46
|
+
try {
|
|
47
|
+
await fs.writeFile(filePath, content, 'utf-8');
|
|
48
|
+
} catch (error) {
|
|
49
|
+
await logger?.error(`Write failed: ${error.message}`, { filePath });
|
|
50
|
+
}
|
|
51
51
|
}
|