uglify-js-minify-css-allfiles 2.4.0 → 2.5.0

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
@@ -17,8 +17,10 @@ You can easily minify all files in a specific folder, with the option to exclude
17
17
  - [Options](#options)
18
18
  - [Advanced Features](#advanced-features)
19
19
  - [Babel Integration](#babel-integration)
20
+ - [PostCSS Processing](#postcss-processing)
20
21
  - [Image Versioning](#image-versioning)
21
22
  - [Logging System](#logging-system)
23
+ - [Source Maps](#source-maps)
22
24
  - [API Reference](#api-reference)
23
25
  - [Minification Options](#minification-options)
24
26
  - [Contributing](#contributing)
@@ -28,11 +30,13 @@ You can easily minify all files in a specific folder, with the option to exclude
28
30
 
29
31
  - 🚀 JavaScript and CSS minification with advanced options
30
32
  - 📦 Babel transformation support for modern JavaScript
33
+ - 🎨 PostCSS processing for modern CSS features
31
34
  - 🖼️ Automatic image versioning and cache busting
32
35
  - 📝 Comprehensive logging system
33
36
  - 🛡️ Configurable file exclusion
34
37
  - 🔄 ES module support
35
38
  - 📊 Processing statistics and summaries
39
+ - 🔍 Source map generation for easier debugging
36
40
 
37
41
  ## Installation
38
42
 
@@ -65,6 +69,18 @@ await minifyAll('./src/', {
65
69
  useBuiltIns: 'usage',
66
70
  corejs: 3,
67
71
  },
72
+ usePostCSS: {
73
+ browsers: ['Chrome >= 40'],
74
+ stage: 2,
75
+ features: {
76
+ 'nesting-rules': true,
77
+ 'custom-properties': true,
78
+ 'color-functional-notation': true,
79
+ },
80
+ autoprefixer: {
81
+ grid: true,
82
+ },
83
+ },
68
84
  useLog: {
69
85
  logDir: 'logs',
70
86
  retentionDays: 30,
@@ -117,6 +133,61 @@ await minifyAll('./src/', {
117
133
  });
118
134
  ```
119
135
 
136
+ ### PostCSS Processing
137
+
138
+ Process modern CSS features with PostCSS integration:
139
+
140
+ - Process CSS with modern features like nesting rules, custom properties, and color functions
141
+ - Automatically transpile modern CSS to be compatible with older browsers
142
+ - Support for customizable browser targets
143
+ - Integrated with the CSS minification pipeline
144
+
145
+ ```js
146
+ await minifyAll('./src/', {
147
+ usePostCSS: {
148
+ browsers: ['Chrome >= 40'],
149
+ stage: 2,
150
+ features: {
151
+ 'nesting-rules': true, // Enable CSS nesting
152
+ 'custom-properties': true, // Enable CSS variables
153
+ 'color-functional-notation': true, // Enable modern color syntax
154
+ },
155
+ autoprefixer: {
156
+ grid: true, // Enable grid features with autoprefixer
157
+ },
158
+ },
159
+ });
160
+ ```
161
+
162
+ ### CSS Example with PostCSS Features
163
+
164
+ ```css
165
+ /* CSS Variables */
166
+ :root {
167
+ --primary-color: #3f51b5;
168
+ --secondary-color: #f50057;
169
+ }
170
+
171
+ /* Nesting Rules */
172
+ .card {
173
+ background-color: white;
174
+
175
+ & .card-header {
176
+ color: var(--primary-color);
177
+
178
+ &:hover {
179
+ color: var(--secondary-color);
180
+ }
181
+ }
182
+ }
183
+
184
+ /* Modern Color Syntax */
185
+ .color-examples {
186
+ color: rgb(0 0 0 / 0.8); /* Modern RGB syntax with alpha */
187
+ border-color: color-mix(in srgb, var(--primary-color) 70%, black 30%);
188
+ }
189
+ ```
190
+
120
191
  ### Image Versioning
121
192
 
122
193
  Automatic versioning for image references in JS and CSS files:
@@ -160,6 +231,22 @@ await minifyAll('./src/', {
160
231
  });
161
232
  ```
162
233
 
234
+ ### Source Maps
235
+
236
+ Generate source maps for JavaScript files to aid in debugging minified code:
237
+
238
+ - Maps compressed code back to original source code
239
+ - Automatically creates `.map` files alongside minified JavaScript
240
+ - Seamless integration with browser developer tools
241
+ - Works with or without Babel transformation
242
+ - Helps maintain debuggability in production environments
243
+
244
+ ```js
245
+ await minifyAll('./src/', {
246
+ useJsMap: true,
247
+ });
248
+ ```
249
+
163
250
  ## API Reference
164
251
 
165
252
  ### minifyAll(contentPath, options)
@@ -170,11 +257,13 @@ Main function to process files.
170
257
  - `options` (object): Configuration options
171
258
  - `excludeFolder` (string): Directory to exclude
172
259
  - `useBabel` (boolean|object): Babel configuration
260
+ - `usePostCSS` (boolean|object): PostCSS configuration
173
261
  - `useLog` (boolean|object): Logging configuration
174
262
  - `jsMinifyOptions` (object): JavaScript minification options
175
263
  - `cssMinifyOptions` (object): CSS minification options
176
264
  - `useVersioning` (object): Image versioning configuration
177
265
  - `extensions` (string[]): List of image extensions to version
266
+ - `useJsMap` (boolean): Enable source map generation for JavaScript files
178
267
 
179
268
  ### Babel Options
180
269
 
@@ -196,6 +285,28 @@ The `useBabel` object supports all @babel/preset-env options:
196
285
  }
197
286
  ```
198
287
 
288
+ ### PostCSS Options
289
+
290
+ The `usePostCSS` object supports the following options:
291
+
292
+ ```js
293
+ {
294
+ browsers: string[] | Object, // Browser targets
295
+ stage: 0 | 1 | 2 | 3 | 4 | 5, // CSS feature stage level
296
+ features: {
297
+ 'nesting-rules': boolean, // CSS nesting rules
298
+ 'custom-properties': boolean, // CSS variables
299
+ 'color-functional-notation': boolean, // Modern color syntax
300
+ // Other CSS features...
301
+ },
302
+ autoprefixer: {
303
+ grid: boolean | 'autoplace' | 'no-autoplace'
304
+ // Other autoprefixer options...
305
+ },
306
+ plugins: Array // Additional PostCSS plugins
307
+ }
308
+ ```
309
+
199
310
  ### JavaScript Minification Options
200
311
 
201
312
  Supports all UglifyJS options:
package/demo.js CHANGED
@@ -2,6 +2,18 @@ import minifyAll from './dist/module.js';
2
2
 
3
3
  minifyAll('./test/', {
4
4
  excludeFolder: 'lib',
5
+ usePostCSS: {
6
+ browsers: ['Chrome >= 40'],
7
+ stage: 2,
8
+ features: {
9
+ 'nesting-rules': true,
10
+ 'custom-properties': true,
11
+ 'color-functional-notation': true,
12
+ },
13
+ autoprefixer: {
14
+ grid: true,
15
+ },
16
+ },
5
17
  useBabel: {
6
18
  targets: 'chrome 40',
7
19
  },
package/dist/module.js CHANGED
@@ -177,6 +177,7 @@ async function resolveBabelOptions(useBabel) {
177
177
  * @param {BabelOptions} [options.babelOptions] - Babel transformation options.
178
178
  * @param {Object} [options.jsMinifyOptions] - JavaScript minification options.
179
179
  * @param {Object} [options.cssMinifyOptions] - CSS minification options.
180
+ * @param {Object} [options.postcssOptions] - PostCSS processing options.
180
181
  * @param {boolean} [options.useJsMap] - JavaScript map file options.
181
182
  * @returns {Promise<void>}
182
183
  */
@@ -232,7 +233,12 @@ async function processFile(filePath, logger, options) {
232
233
  await writeFile(filePath, result.code, logger);
233
234
  }
234
235
  } else if (fileExtension === '.css') {
235
- const output = await minifyCSS(fileContent, options.cssMinifyOptions);
236
+ // PostCSS 옵션 설정
237
+ const postcssOptions = options.postcssOptions || null;
238
+
239
+ // CSS 처리 (PostCSS + CleanCSS)
240
+ const output = await minifyCSS(fileContent, options.cssMinifyOptions, postcssOptions, filePath);
241
+
236
242
  if (output.warnings.length > 0) {
237
243
  await logger?.warn('CSS minification warnings', { filePath, warnings: output.warnings });
238
244
  }
@@ -290,6 +296,16 @@ async function processFile(filePath, logger, options) {
290
296
  * @property {string|Object} [format] - Output formatting options.
291
297
  */
292
298
 
299
+ /**
300
+ * Options for PostCSS configuration.
301
+ * @typedef {Object} PostCSSOptions
302
+ * @property {string[]|Object} [browsers] - Target browsers for the CSS compatibility.
303
+ * @property {0|1|2|3|4|5} [stage=2] - CSS features stage level.
304
+ * @property {Object} [features] - Specific CSS features to enable/disable.
305
+ * @property {Object} [autoprefixer] - Autoprefixer options.
306
+ * @property {Array} [plugins] - Additional PostCSS plugins to use.
307
+ */
308
+
293
309
  /**
294
310
  * Options for minification configuration.
295
311
  * @typedef {Object} MinifyOptions
@@ -298,6 +314,7 @@ async function processFile(filePath, logger, options) {
298
314
  * @property {boolean|LogOptions} [useLog=true] - Whether to use logging.
299
315
  * @property {JSMinifyOptions} [jsMinifyOptions={}] - Options for JavaScript minification.
300
316
  * @property {CSSMinifyOptions} [cssMinifyOptions={}] - Options for CSS minification.
317
+ * @property {PostCSSOptions|boolean} [usePostCSS=false] - PostCSS configuration options.
301
318
  * @property {string[]|null} [useVersioning=null] - Options for file versioning.
302
319
  * @property {boolean} [useJsMap=false] - Whether to use JavaScript Map file.
303
320
  */
@@ -317,6 +334,7 @@ export default async function minifyAll(contentPath, options = {}) {
317
334
  useLog = true,
318
335
  jsMinifyOptions = {},
319
336
  cssMinifyOptions = {},
337
+ usePostCSS = false,
320
338
  useVersioning = null,
321
339
  useJsMap = false,
322
340
  } = options;
@@ -329,7 +347,8 @@ export default async function minifyAll(contentPath, options = {}) {
329
347
  await logger.info('Starting minification process', {
330
348
  contentPath,
331
349
  excludeFolder,
332
- useBabel,
350
+ useBabel: !!useBabel,
351
+ usePostCSS: !!usePostCSS,
333
352
  useVersioning: !!useVersioning,
334
353
  useJsMap,
335
354
  });
@@ -348,6 +367,7 @@ export default async function minifyAll(contentPath, options = {}) {
348
367
  babelOptions,
349
368
  jsMinifyOptions,
350
369
  cssMinifyOptions,
370
+ postcssOptions: usePostCSS === true ? {} : usePostCSS,
351
371
  useJsMap,
352
372
  };
353
373
 
@@ -5,6 +5,10 @@
5
5
 
6
6
  import { minify as uglifyJS } from 'uglify-js';
7
7
  import CleanCSS from 'clean-css';
8
+ import { processWithPostCSS, checkPostCSSAvailability } from './postcssProcess.js';
9
+
10
+ // Track PostCSS availability
11
+ let postCSSAvailable = null;
8
12
 
9
13
  /**
10
14
  * Minifies JavaScript content.
@@ -37,16 +41,41 @@ export function minifyJS(content, options = {}) {
37
41
  * @async
38
42
  * @param {string} content - CSS content to minify.
39
43
  * @param {Object} [options={}] - Clean-CSS options.
44
+ * @param {Object} [postcssOptions=null] - PostCSS configuration options, if null PostCSS is skipped.
45
+ * @param {string} [filePath=''] - Path to the file being processed.
40
46
  * @returns {Promise<{styles: string, warnings: string[]}>} Minified CSS and warnings.
41
47
  */
42
- export function minifyCSS(content, options = {}) {
48
+ export async function minifyCSS(content, options = {}, postcssOptions = null, filePath = '') {
43
49
  const defaultOptions = {
44
50
  level: { 1: { all: false } },
45
51
  };
46
52
 
47
53
  const mergedOptions = { ...defaultOptions, ...options };
54
+ let processedCSS = content;
55
+
56
+ // Check if PostCSS is available and enabled
57
+ if (postCSSAvailable === null) {
58
+ postCSSAvailable = await checkPostCSSAvailability();
59
+ }
60
+
61
+ // Process with PostCSS if available and enabled
62
+ if (postCSSAvailable && postcssOptions !== null) {
63
+ try {
64
+ const result = await processWithPostCSS(content, filePath, postcssOptions);
65
+ processedCSS = result.css;
66
+
67
+ // Handle warnings
68
+ if (result.messages && result.messages.length > 0) {
69
+ console.warn(`PostCSS warnings for ${filePath}:`, result.messages.map((msg) => msg.text).join('\n'));
70
+ }
71
+ } catch (error) {
72
+ console.error(`PostCSS processing failed for ${filePath}:`, error);
73
+ // Continue with original content if PostCSS fails
74
+ }
75
+ }
48
76
 
77
+ // Use CleanCSS for final minification
49
78
  return new Promise((resolve) => {
50
- new CleanCSS(mergedOptions).minify(content, (error, output) => resolve(output));
79
+ new CleanCSS(mergedOptions).minify(processedCSS, (error, output) => resolve(output));
51
80
  });
52
81
  }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * PostCSS processor module for advanced CSS transformations
3
+ * @module postcssProcessor
4
+ */
5
+
6
+ import postcss from 'postcss';
7
+ import postcssPresetEnv from 'postcss-preset-env';
8
+ import { resolveModulePath } from './pathResolver.js';
9
+
10
+ /**
11
+ * Default PostCSS options
12
+ * @type {Object}
13
+ */
14
+ const DEFAULT_OPTIONS = {
15
+ browsers: ['Chrome >= 40'],
16
+ stage: 2,
17
+ features: {
18
+ 'nesting-rules': true,
19
+ 'custom-properties': true,
20
+ 'color-functional-notation': true,
21
+ },
22
+ autoprefixer: {
23
+ grid: true,
24
+ },
25
+ };
26
+
27
+ /**
28
+ * Processes CSS content using PostCSS
29
+ * @async
30
+ * @param {string} content - CSS content to process
31
+ * @param {string} [filePath=''] - Source file path
32
+ * @param {Object} [options={}] - PostCSS and preset-env options
33
+ * @returns {Promise<{css: string, messages: Array}>} Processed CSS content
34
+ */
35
+ export async function processWithPostCSS(content, filePath = '', options = {}) {
36
+ try {
37
+ // Merge options with defaults
38
+ const presetEnvOptions = {
39
+ ...DEFAULT_OPTIONS,
40
+ ...options,
41
+ };
42
+
43
+ // Configure plugins
44
+ const plugins = [postcssPresetEnv(presetEnvOptions)];
45
+
46
+ // Add any custom plugins from options
47
+ if (options.plugins && Array.isArray(options.plugins)) {
48
+ plugins.push(...options.plugins);
49
+ }
50
+
51
+ // Process the CSS
52
+ const result = await postcss(plugins).process(content, {
53
+ from: filePath || undefined,
54
+ to: filePath || undefined,
55
+ });
56
+
57
+ return {
58
+ css: result.css,
59
+ messages: result.messages,
60
+ };
61
+ } catch (error) {
62
+ console.error('Error processing CSS with PostCSS:', error);
63
+ throw error;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Resolves PostCSS related module paths
69
+ * @async
70
+ * @returns {Promise<boolean>} Whether all modules are available
71
+ */
72
+ export async function checkPostCSSAvailability() {
73
+ try {
74
+ await import(resolveModulePath('postcss'));
75
+ await import(resolveModulePath('postcss-preset-env'));
76
+ return true;
77
+ } catch (error) {
78
+ console.warn('PostCSS modules not available:', error.message);
79
+ return false;
80
+ }
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uglify-js-minify-css-allfiles",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
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",
@@ -44,11 +44,13 @@
44
44
  "email": "copstyle86@gmail.com"
45
45
  },
46
46
  "homepage": "https://github.com/oinochoe/uglify-js-minify-css-allfiles#readme",
47
- "dependencies": {
47
+ "devDependencies": {
48
+ "@babel/core": "^7.25.2",
49
+ "@babel/preset-env": "^7.25.3",
48
50
  "clean-css": "^4.2.4",
51
+ "postcss": "^8.5.3",
52
+ "postcss-preset-env": "^10.1.5",
49
53
  "uglify-js": "^3.19.2",
50
- "uglify-js-es6": "^2.8.9",
51
- "@babel/core": "^7.25.2",
52
- "@babel/preset-env": "^7.25.3"
54
+ "uglify-js-es6": "^2.8.9"
53
55
  }
54
56
  }