uglify-js-minify-css-allfiles 2.2.2 → 2.2.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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,25 @@ 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.2.4] - 2024-08-23
9
+
10
+ ### Changed
11
+
12
+ - Updated `resolveBabelOptions` function to dynamically import `@babel/preset-env` using `resolveModulePath`
13
+ - Modified `minifyAll` function to handle the new asynchronous `resolveBabelOptions`
14
+
15
+ ### Improved
16
+
17
+ - Enhanced flexibility in loading Babel presets, allowing for better compatibility with different environments
18
+
19
+ ## [2.2.3] - 2024-08-23
20
+
21
+ ### Improved
22
+
23
+ - Enhanced type definitions using JSDoc comments for better IDE support and code documentation
24
+ - Added more specific type definitions for JSMinifyOptions and CSSMinifyOptions
25
+ - Improved type annotations for function parameters and return types
26
+
8
27
  ## [2.2.2] - 2024-08-22
9
28
 
10
29
  ### Fixed
package/dist/module.js CHANGED
@@ -16,27 +16,37 @@ const require = createRequire(import.meta.url);
16
16
  const __filename = fileURLToPath(import.meta.url);
17
17
  const __dirname = path.dirname(__filename);
18
18
 
19
+ /**
20
+ * Resolves the path of a module.
21
+ * @param {string} moduleName - The name of the module to resolve.
22
+ * @returns {string} The resolved module path as a URL.
23
+ */
19
24
  function resolveModulePath(moduleName) {
20
25
  const modulePath = require.resolve(moduleName, { paths: [__dirname] });
21
26
  return pathToFileURL(modulePath).href;
22
27
  }
23
28
 
29
+ /**
30
+ * @typedef {Object} FileHandlerOptions
31
+ * @property {BabelOptions} [babelOptions] - Babel transformation options.
32
+ * @property {JSMinifyOptions} [jsMinifyOptions] - JavaScript minification options.
33
+ * @property {CSSMinifyOptions} [cssMinifyOptions] - CSS minification options.
34
+ */
35
+
36
+ /**
37
+ * @callback FileHandler
38
+ * @param {string} filePath - The path of the file to process.
39
+ * @param {string} content - The content of the file.
40
+ * @param {Logger} logger - The logger instance.
41
+ * @param {FileHandlerOptions} options - Options for processing.
42
+ * @returns {Promise<void>}
43
+ */
44
+
24
45
  /**
25
46
  * Object containing handlers for different file types.
26
- * @type {Object.<string, function>}
47
+ * @type {Object.<string, FileHandler>}
27
48
  */
28
49
  const FILE_HANDLERS = {
29
- /**
30
- * Handles JavaScript file minification.
31
- * @async
32
- * @param {string} filePath - The path of the JavaScript file.
33
- * @param {string} content - The content of the JavaScript file.
34
- * @param {Logger} logger - The logger instance.
35
- * @param {Object} options - Options for processing.
36
- * @param {Object} [options.babelOptions] - Babel transformation options.
37
- * @param {Object} [options.jsMinifyOptions] - JavaScript minification options.
38
- * @returns {Promise<void>}
39
- */
40
50
  '.js': async (filePath, content, logger, options) => {
41
51
  try {
42
52
  let transformed = content;
@@ -51,17 +61,6 @@ const FILE_HANDLERS = {
51
61
  await logger?.error('JavaScript minification failed', { filePath, error: error.message });
52
62
  }
53
63
  },
54
-
55
- /**
56
- * Handles CSS file minification.
57
- * @async
58
- * @param {string} filePath - The path of the CSS file.
59
- * @param {string} content - The content of the CSS file.
60
- * @param {Logger} logger - The logger instance.
61
- * @param {Object} options - Options for processing.
62
- * @param {Object} [options.cssMinifyOptions] - CSS minification options.
63
- * @returns {Promise<void>}
64
- */
65
64
  '.css': async (filePath, content, logger, options) => {
66
65
  try {
67
66
  const output = await minifyCSS(content, options.cssMinifyOptions);
@@ -80,7 +79,7 @@ const FILE_HANDLERS = {
80
79
  * @async
81
80
  * @param {string} filePath - The path of the file to process.
82
81
  * @param {Logger} logger - The logger instance.
83
- * @param {Object} options - Options for processing.
82
+ * @param {FileHandlerOptions} options - Options for processing.
84
83
  * @returns {Promise<void>}
85
84
  */
86
85
  async function processFile(filePath, logger, options) {
@@ -101,21 +100,29 @@ async function processFile(filePath, logger, options) {
101
100
 
102
101
  /**
103
102
  * Resolves Babel options based on the provided configuration.
104
- * @param {boolean|Object} useBabel - The Babel options object or boolean.
105
- * @returns {Object|null} The resolved Babel options or null if no valid options are provided.
103
+ * @param {boolean|BabelOptions} useBabel - The Babel options object or boolean.
104
+ * @returns {Promise<BabelOptions|null>} A promise that resolves to the Babel options or null if no valid options are provided.
106
105
  */
107
- function resolveBabelOptions(useBabel) {
106
+ async function resolveBabelOptions(useBabel) {
108
107
  if (!useBabel) return null;
109
- return {
110
- presets: [['@babel/preset-env', typeof useBabel === 'object' ? useBabel : {}]],
111
- };
108
+
109
+ try {
110
+ const presetEnvUrl = resolveModulePath('@babel/preset-env');
111
+ const presetEnv = await import(presetEnvUrl);
112
+
113
+ return {
114
+ presets: [[presetEnv.default, typeof useBabel === 'object' ? useBabel : {}]],
115
+ };
116
+ } catch (error) {
117
+ console.error('Error loading @babel/preset-env:', error);
118
+ return null;
119
+ }
112
120
  }
113
121
 
114
122
  /**
115
123
  * Options for Babel configuration.
116
- *
117
124
  * @typedef {Object} BabelOptions
118
- * @property {string|string[]|Object<string, string>} [targets] - Specifies the target environments for the code.
125
+ * @property {string|string[]|Object.<string, string>} [targets] - Specifies the target environments for the code.
119
126
  * @property {'amd'|'umd'|'systemjs'|'commonjs'|'cjs'|'auto'|false} [modules] - Module format to use for the output.
120
127
  * @property {boolean} [debug] - Enables or disables debug mode.
121
128
  * @property {string[]} [include] - List of plugins or features to include.
@@ -130,7 +137,6 @@ function resolveBabelOptions(useBabel) {
130
137
 
131
138
  /**
132
139
  * Options for logging configuration.
133
- *
134
140
  * @typedef {Object} LogOptions
135
141
  * @property {string} [logDir] - Specifies the directory for log files.
136
142
  * @property {number} [retentionDays] - Number of days to retain log files.
@@ -141,21 +147,35 @@ function resolveBabelOptions(useBabel) {
141
147
  * @property {boolean} [logToFile] - Determines if logs should be written to a file.
142
148
  */
143
149
 
150
+ /**
151
+ * Options for JavaScript minification (UglifyJS options).
152
+ * @typedef {Object} JSMinifyOptions
153
+ * @property {Object} [compress] - Compression options.
154
+ * @property {boolean|Object} [mangle] - Mangling options.
155
+ * @property {Object} [output] - Output format options.
156
+ */
157
+
158
+ /**
159
+ * Options for CSS minification (Clean-CSS options).
160
+ * @typedef {Object} CSSMinifyOptions
161
+ * @property {0|1|2|Object} [level] - Optimization level.
162
+ * @property {string|string[]} [compatibility] - Browser compatibility options.
163
+ * @property {string|Object} [format] - Output formatting options.
164
+ */
165
+
144
166
  /**
145
167
  * Options for minification configuration.
146
168
  * @typedef {Object} MinifyOptions
147
169
  * @property {string} [excludeFolder=''] - Folder to exclude from minification.
148
- * @property {boolean|Object} [useBabel=false] - Whether to use Babel for transformation, and the options for Babel if used.
149
- * @property {boolean|Object} [useLog=true] - Whether to use logging, and the options for logging if used.
150
- * @property {Object} [jsMinifyOptions={}] - Options for JavaScript minification.
151
- * @property {Object} [cssMinifyOptions={}] - Options for CSS minification.
170
+ * @property {boolean|BabelOptions} [useBabel=false] - Whether to use Babel for transformation, and the options for Babel if used.
171
+ * @property {boolean|LogOptions} [useLog=true] - Whether to use logging, and the options for logging if used.
172
+ * @property {JSMinifyOptions} [jsMinifyOptions={}] - Options for JavaScript minification.
173
+ * @property {CSSMinifyOptions} [cssMinifyOptions={}] - Options for CSS minification.
152
174
  */
153
175
 
154
176
  /**
155
177
  * Minifies all JavaScript and CSS files in the specified directory and its subdirectories.
156
178
  *
157
- * @async
158
- * @function minifyAll
159
179
  * @param {string} contentPath - The path to the directory containing the files to be minified.
160
180
  * @param {MinifyOptions} [options={}] - Options for minification, Babel, and logging.
161
181
  * @returns {Promise<void>} A promise that resolves when all files have been processed.
@@ -179,7 +199,7 @@ export default async function minifyAll(contentPath, options = {}) {
179
199
  }
180
200
 
181
201
  const rootDir = path.resolve(contentPath || '');
182
- const babelOptions = resolveBabelOptions(useBabel);
202
+ const babelOptions = await resolveBabelOptions(useBabel);
183
203
 
184
204
  const processOptions = {
185
205
  babelOptions,
package/minify.d.ts CHANGED
@@ -105,6 +105,43 @@ declare module 'uglify-js-minify-css-allfiles' {
105
105
  logToFile?: boolean;
106
106
  }
107
107
 
108
+ /**
109
+ * Configuration options for JavaScript minification (UglifyJS options).
110
+ */
111
+ export interface JSMinifyOptions {
112
+ compress?: {
113
+ dead_code?: boolean;
114
+ drop_debugger?: boolean;
115
+ conditionals?: boolean;
116
+ evaluate?: boolean;
117
+ booleans?: boolean;
118
+ loops?: boolean;
119
+ unused?: boolean;
120
+ hoist_funs?: boolean;
121
+ keep_fargs?: boolean;
122
+ hoist_vars?: boolean;
123
+ if_return?: boolean;
124
+ join_vars?: boolean;
125
+ cascade?: boolean;
126
+ side_effects?: boolean;
127
+ warnings?: boolean;
128
+ [key: string]: any;
129
+ };
130
+ mangle?: boolean | object;
131
+ output?: object;
132
+ [key: string]: any;
133
+ }
134
+
135
+ /**
136
+ * Configuration options for CSS minification (Clean-CSS options).
137
+ */
138
+ export interface CSSMinifyOptions {
139
+ level?: 0 | 1 | 2 | { [key: string]: any };
140
+ compatibility?: string | string[];
141
+ format?: string | object;
142
+ [key: string]: any;
143
+ }
144
+
108
145
  /**
109
146
  * Configuration options for the minifyAll function.
110
147
  */
@@ -131,6 +168,18 @@ declare module 'uglify-js-minify-css-allfiles' {
131
168
  * @default true
132
169
  */
133
170
  useLog?: boolean | LogOptions;
171
+
172
+ /**
173
+ * Options for JavaScript minification (passed to UglifyJS).
174
+ * @default {}
175
+ */
176
+ jsMinifyOptions?: JSMinifyOptions;
177
+
178
+ /**
179
+ * Options for CSS minification (passed to CleanCSS).
180
+ * @default {}
181
+ */
182
+ cssMinifyOptions?: CSSMinifyOptions;
134
183
  }
135
184
 
136
185
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uglify-js-minify-css-allfiles",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "you will be able to minify all files as same file names which is js or css",
5
5
  "main": "minify.js",
6
6
  "type": "module",