uglify-js-minify-css-allfiles 2.3.3 → 2.4.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/README.md CHANGED
@@ -19,6 +19,7 @@ You can easily minify all files in a specific folder, with the option to exclude
19
19
  - [Babel Integration](#babel-integration)
20
20
  - [Image Versioning](#image-versioning)
21
21
  - [Logging System](#logging-system)
22
+ - [Source Maps](#source-maps)
22
23
  - [API Reference](#api-reference)
23
24
  - [Minification Options](#minification-options)
24
25
  - [Contributing](#contributing)
@@ -33,6 +34,7 @@ You can easily minify all files in a specific folder, with the option to exclude
33
34
  - 🛡️ Configurable file exclusion
34
35
  - 🔄 ES module support
35
36
  - 📊 Processing statistics and summaries
37
+ - 🔍 Source map generation for easier debugging
36
38
 
37
39
  ## Installation
38
40
 
@@ -91,6 +93,7 @@ await minifyAll('./src/', {
91
93
  useVersioning: {
92
94
  extensions: ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp'],
93
95
  },
96
+ useJsMap: true,
94
97
  });
95
98
  ```
96
99
 
@@ -159,6 +162,22 @@ await minifyAll('./src/', {
159
162
  });
160
163
  ```
161
164
 
165
+ ### Source Maps
166
+
167
+ Generate source maps for JavaScript files to aid in debugging minified code:
168
+
169
+ - Maps compressed code back to original source code
170
+ - Automatically creates `.map` files alongside minified JavaScript
171
+ - Seamless integration with browser developer tools
172
+ - Works with or without Babel transformation
173
+ - Helps maintain debuggability in production environments
174
+
175
+ ```js
176
+ await minifyAll('./src/', {
177
+ useJsMap: true,
178
+ });
179
+ ```
180
+
162
181
  ## API Reference
163
182
 
164
183
  ### minifyAll(contentPath, options)
@@ -174,6 +193,7 @@ Main function to process files.
174
193
  - `cssMinifyOptions` (object): CSS minification options
175
194
  - `useVersioning` (object): Image versioning configuration
176
195
  - `extensions` (string[]): List of image extensions to version
196
+ - `useJsMap` (boolean): Enable source map generation for JavaScript files
177
197
 
178
198
  ### Babel Options
179
199
 
package/demo.js CHANGED
@@ -8,4 +8,5 @@ minifyAll('./test/', {
8
8
  useVersioning: {
9
9
  extensions: ['.png', '.jpg', '.webp', '.avif', '.svg'],
10
10
  },
11
+ useJsMap: true,
11
12
  });
package/dist/module.js CHANGED
@@ -13,6 +13,7 @@ import HashManager from './modules/hashManager.js';
13
13
  import { minifyJS, minifyCSS } from './modules/minifier.js';
14
14
  import { DEFAULT_IMAGE_EXTENSIONS, IMAGE_PATTERNS } from './modules/imageUtils.js';
15
15
  import { resolveImagePath, resolveModulePath, makeRelativePath, containsFolder, getExtension, resolvePath } from './modules/pathResolver.js';
16
+ import path from 'path';
16
17
 
17
18
  /**
18
19
  * Processes image references in files and adds/updates version hashes for cache busting.
@@ -176,6 +177,7 @@ async function resolveBabelOptions(useBabel) {
176
177
  * @param {BabelOptions} [options.babelOptions] - Babel transformation options.
177
178
  * @param {Object} [options.jsMinifyOptions] - JavaScript minification options.
178
179
  * @param {Object} [options.cssMinifyOptions] - CSS minification options.
180
+ * @param {boolean} [options.useJsMap] - JavaScript map file options.
179
181
  * @returns {Promise<void>}
180
182
  */
181
183
  async function processFile(filePath, logger, options) {
@@ -185,25 +187,60 @@ async function processFile(filePath, logger, options) {
185
187
 
186
188
  let result;
187
189
  if (fileExtension === '.js') {
188
- let transformed = fileContent;
190
+ let content = fileContent;
189
191
  if (options.babelOptions) {
190
192
  const babelCoreUrl = resolveModulePath('@babel/core');
191
193
  const { transformSync } = await import(babelCoreUrl);
192
- transformed = transformSync(fileContent, options.babelOptions).code;
194
+
195
+ let babelOptions = {
196
+ ...options.babelOptions,
197
+ };
198
+
199
+ if (options.useJsMap) {
200
+ babelOptions = {
201
+ ...babelOptions,
202
+ filename: filePath,
203
+ sourceMaps: true,
204
+ sourceFileName: filePath,
205
+ };
206
+ }
207
+
208
+ content = transformSync(fileContent, babelOptions);
209
+ }
210
+
211
+ let jsMinifyOptions = { ...options.jsMinifyOptions };
212
+
213
+ if (options.useJsMap) {
214
+ jsMinifyOptions.sourceMap = {
215
+ includeSources: true,
216
+ };
217
+
218
+ if (options.babelOptions && content.map) {
219
+ jsMinifyOptions.sourceMap.content = JSON.stringify(content.map);
220
+ }
221
+ }
222
+
223
+ result = minifyJS(options.babelOptions ? content.code : content, jsMinifyOptions);
224
+
225
+ if (options.useJsMap) {
226
+ const fileName = path.basename(filePath);
227
+ const mapFilePath = filePath.replace('.js', '.js.map');
228
+
229
+ await writeFile(filePath, result.code + `\n//# sourceMappingURL=${fileName}.map`, logger);
230
+ await writeFile(mapFilePath, result.map, logger);
231
+ } else {
232
+ await writeFile(filePath, result.code, logger);
193
233
  }
194
- result = minifyJS(transformed, options.jsMinifyOptions);
195
234
  } else if (fileExtension === '.css') {
196
235
  const output = await minifyCSS(fileContent, options.cssMinifyOptions);
197
236
  if (output.warnings.length > 0) {
198
237
  await logger?.warn('CSS minification warnings', { filePath, warnings: output.warnings });
199
238
  }
200
- result = output.styles;
239
+ await writeFile(filePath, output.styles, logger);
201
240
  } else {
202
241
  // `Unsupported file type, skipping: ${filePath}`;
203
242
  return;
204
243
  }
205
-
206
- await writeFile(filePath, result, logger);
207
244
  } catch (error) {
208
245
  await logger?.error('Error processing file', { filePath, error: error.message });
209
246
  }
@@ -262,6 +299,7 @@ async function processFile(filePath, logger, options) {
262
299
  * @property {JSMinifyOptions} [jsMinifyOptions={}] - Options for JavaScript minification.
263
300
  * @property {CSSMinifyOptions} [cssMinifyOptions={}] - Options for CSS minification.
264
301
  * @property {string[]|null} [useVersioning=null] - Options for file versioning.
302
+ * @property {boolean} [useJsMap=false] - Whether to use JavaScript Map file.
265
303
  */
266
304
 
267
305
  /**
@@ -273,7 +311,15 @@ async function processFile(filePath, logger, options) {
273
311
  * @throws {Error} If there's an issue reading or writing files.
274
312
  */
275
313
  export default async function minifyAll(contentPath, options = {}) {
276
- const { excludeFolder = '', useBabel = false, useLog = true, jsMinifyOptions = {}, cssMinifyOptions = {}, useVersioning = null } = options;
314
+ const {
315
+ excludeFolder = '',
316
+ useBabel = false,
317
+ useLog = true,
318
+ jsMinifyOptions = {},
319
+ cssMinifyOptions = {},
320
+ useVersioning = null,
321
+ useJsMap = false,
322
+ } = options;
277
323
 
278
324
  let logger = null;
279
325
  if (useLog) {
@@ -285,6 +331,7 @@ export default async function minifyAll(contentPath, options = {}) {
285
331
  excludeFolder,
286
332
  useBabel,
287
333
  useVersioning: !!useVersioning,
334
+ useJsMap,
288
335
  });
289
336
  }
290
337
 
@@ -301,6 +348,7 @@ export default async function minifyAll(contentPath, options = {}) {
301
348
  babelOptions,
302
349
  jsMinifyOptions,
303
350
  cssMinifyOptions,
351
+ useJsMap,
304
352
  };
305
353
 
306
354
  try {
@@ -24,7 +24,12 @@ export function minifyJS(content, options = {}) {
24
24
  };
25
25
 
26
26
  const mergedOptions = { ...defaultOptions, ...options };
27
- return uglifyJS(content, mergedOptions).code;
27
+ const result = uglifyJS(content, mergedOptions);
28
+
29
+ return {
30
+ code: result.code,
31
+ map: result.map,
32
+ };
28
33
  }
29
34
 
30
35
  /**
package/minify.d.ts CHANGED
@@ -180,6 +180,13 @@ declare module 'uglify-js-minify-css-allfiles' {
180
180
  * @default {}
181
181
  */
182
182
  cssMinifyOptions?: CSSMinifyOptions;
183
+
184
+ /**
185
+ * Enables source map generation for JavaScript files during minification.
186
+ * Source maps help with debugging by mapping minified code back to original source code.
187
+ * @default false
188
+ */
189
+ useJsMap?: boolean;
183
190
  }
184
191
 
185
192
  /**
@@ -206,7 +213,8 @@ declare module 'uglify-js-minify-css-allfiles' {
206
213
  * useLog: {
207
214
  * logLevel: 'warn',
208
215
  * retentionDays: 7
209
- * }
216
+ * },
217
+ * useJsMap: true // Enable source map generation
210
218
  * });
211
219
  * ```
212
220
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uglify-js-minify-css-allfiles",
3
- "version": "2.3.3",
3
+ "version": "2.4.1",
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",