uglify-js-minify-css-allfiles 2.5.1 → 2.7.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 +11 -5
- package/demo.js +3 -0
- package/dist/module.js +57 -8
- package/dist/modules/minifier.js +11 -3
- package/dist/modules/postcssProcess.js +2 -0
- package/dist/plugins/append-polyfill-plugin.js +67 -0
- package/minify.d.ts +19 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -68,6 +68,8 @@ await minifyAll('./src/', {
|
|
|
68
68
|
modules: false,
|
|
69
69
|
useBuiltIns: 'usage',
|
|
70
70
|
corejs: 3,
|
|
71
|
+
useAppendTransform: true,
|
|
72
|
+
plugins: ['@babel/plugin-proposal-class-properties'],
|
|
71
73
|
},
|
|
72
74
|
usePostCSS: {
|
|
73
75
|
browsers: ['Chrome >= 40'],
|
|
@@ -108,6 +110,7 @@ await minifyAll('./src/', {
|
|
|
108
110
|
extensions: ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp'],
|
|
109
111
|
},
|
|
110
112
|
useJsMap: true,
|
|
113
|
+
useCssMap: true
|
|
111
114
|
});
|
|
112
115
|
```
|
|
113
116
|
|
|
@@ -233,19 +236,19 @@ await minifyAll('./src/', {
|
|
|
233
236
|
|
|
234
237
|
### Source Maps
|
|
235
238
|
|
|
236
|
-
Generate source maps for JavaScript files to aid in debugging minified code:
|
|
239
|
+
Generate source maps for both JavaScript and CSS files to aid in debugging minified code:
|
|
237
240
|
|
|
238
241
|
- Maps compressed code back to original source code
|
|
239
|
-
- Automatically creates `.map` files alongside minified
|
|
242
|
+
- Automatically creates `.map` files alongside minified files
|
|
240
243
|
- Seamless integration with browser developer tools
|
|
241
|
-
- Works with or without Babel transformation
|
|
244
|
+
- Works with or without Babel/PostCSS transformation
|
|
242
245
|
- Helps maintain debuggability in production environments
|
|
243
246
|
|
|
244
247
|
```js
|
|
245
248
|
await minifyAll('./src/', {
|
|
246
249
|
useJsMap: true,
|
|
250
|
+
useCssMap: true
|
|
247
251
|
});
|
|
248
|
-
```
|
|
249
252
|
|
|
250
253
|
## API Reference
|
|
251
254
|
|
|
@@ -264,6 +267,7 @@ Main function to process files.
|
|
|
264
267
|
- `useVersioning` (object): Image versioning configuration
|
|
265
268
|
- `extensions` (string[]): List of image extensions to version
|
|
266
269
|
- `useJsMap` (boolean): Enable source map generation for JavaScript files
|
|
270
|
+
- `useCssMap` (boolean): Enable source map generation for CSS files
|
|
267
271
|
|
|
268
272
|
### Babel Options
|
|
269
273
|
|
|
@@ -281,7 +285,9 @@ The `useBabel` object supports all @babel/preset-env options:
|
|
|
281
285
|
forceAllTransforms: boolean,
|
|
282
286
|
configPath: string,
|
|
283
287
|
ignoreBrowserslistConfig: boolean,
|
|
284
|
-
shippedProposals: boolean
|
|
288
|
+
shippedProposals: boolean,
|
|
289
|
+
useAppendTransform: boolean,
|
|
290
|
+
plugins: Array<string|Array|Function>
|
|
285
291
|
}
|
|
286
292
|
```
|
|
287
293
|
|
package/demo.js
CHANGED
|
@@ -16,9 +16,12 @@ minifyAll('./test/', {
|
|
|
16
16
|
},
|
|
17
17
|
useBabel: {
|
|
18
18
|
targets: 'chrome 40',
|
|
19
|
+
useAppendTransform: false,
|
|
20
|
+
plugins: [],
|
|
19
21
|
},
|
|
20
22
|
useVersioning: {
|
|
21
23
|
extensions: ['.png', '.jpg', '.webp', '.avif', '.svg'],
|
|
22
24
|
},
|
|
23
25
|
useJsMap: true,
|
|
26
|
+
useCssMap: true,
|
|
24
27
|
});
|
package/dist/module.js
CHANGED
|
@@ -149,6 +149,9 @@ async function updateImageReferences(filePath, versioningOptions, logger, hashMa
|
|
|
149
149
|
|
|
150
150
|
/**
|
|
151
151
|
* Resolves Babel options based on the provided configuration.
|
|
152
|
+
* Supports custom plugins through the useBabel.plugins option.
|
|
153
|
+
*
|
|
154
|
+
* @async
|
|
152
155
|
* @param {boolean|BabelOptions} useBabel - The Babel options object or boolean.
|
|
153
156
|
* @returns {Promise<BabelOptions|null>} - A promise that resolves to the Babel options or null if disabled.
|
|
154
157
|
*/
|
|
@@ -159,11 +162,34 @@ async function resolveBabelOptions(useBabel) {
|
|
|
159
162
|
const presetEnvUrl = resolveModulePath('@babel/preset-env');
|
|
160
163
|
const presetEnv = await import(presetEnvUrl);
|
|
161
164
|
|
|
162
|
-
|
|
163
|
-
|
|
165
|
+
const options = typeof useBabel === 'object' ? useBabel : {};
|
|
166
|
+
const { useAppendTransform, plugins, ...presetOptions } = options;
|
|
167
|
+
|
|
168
|
+
const babelConfig = {
|
|
169
|
+
presets: [[presetEnv.default, presetOptions]],
|
|
170
|
+
plugins: [],
|
|
164
171
|
};
|
|
172
|
+
|
|
173
|
+
// Handle custom plugins if provided
|
|
174
|
+
if (plugins && Array.isArray(plugins)) {
|
|
175
|
+
babelConfig.plugins.push(...plugins);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Add appendTransform plugin if useAppendTransform option is enabled
|
|
179
|
+
if (useAppendTransform) {
|
|
180
|
+
try {
|
|
181
|
+
const appendTransformPluginModule = await import('./plugins/append-polyfill-plugin.js');
|
|
182
|
+
const appendTransformPlugin = appendTransformPluginModule.default;
|
|
183
|
+
babelConfig.plugins.push(appendTransformPlugin);
|
|
184
|
+
} catch (pluginError) {
|
|
185
|
+
console.error('Error loading append transform plugin:', pluginError);
|
|
186
|
+
console.error(pluginError.stack);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return babelConfig;
|
|
165
191
|
} catch (error) {
|
|
166
|
-
console.error('Error loading @babel/preset-env:', error);
|
|
192
|
+
console.error('Error loading @babel/preset-env or plugins:', error);
|
|
167
193
|
return null;
|
|
168
194
|
}
|
|
169
195
|
}
|
|
@@ -179,6 +205,7 @@ async function resolveBabelOptions(useBabel) {
|
|
|
179
205
|
* @param {Object} [options.cssMinifyOptions] - CSS minification options.
|
|
180
206
|
* @param {Object} [options.postcssOptions] - PostCSS processing options.
|
|
181
207
|
* @param {boolean} [options.useJsMap] - JavaScript map file options.
|
|
208
|
+
* @param {boolean} [options.useCssMap] - Css map file options.
|
|
182
209
|
* @returns {Promise<void>}
|
|
183
210
|
*/
|
|
184
211
|
async function processFile(filePath, logger, options) {
|
|
@@ -233,16 +260,32 @@ async function processFile(filePath, logger, options) {
|
|
|
233
260
|
await writeFile(filePath, result.code, logger);
|
|
234
261
|
}
|
|
235
262
|
} else if (fileExtension === '.css') {
|
|
236
|
-
// PostCSS 옵션 설정
|
|
237
263
|
const postcssOptions = options.postcssOptions || null;
|
|
238
|
-
|
|
239
|
-
// CSS 처리 (PostCSS + CleanCSS)
|
|
240
|
-
const output = await minifyCSS(fileContent, options.cssMinifyOptions, postcssOptions, filePath);
|
|
264
|
+
const output = await minifyCSS(fileContent, options.cssMinifyOptions, postcssOptions, filePath, options.useCssMap);
|
|
241
265
|
|
|
242
266
|
if (output.warnings.length > 0) {
|
|
243
267
|
await logger?.warn('CSS minification warnings', { filePath, warnings: output.warnings });
|
|
244
268
|
}
|
|
245
|
-
|
|
269
|
+
|
|
270
|
+
if (options.useCssMap && output.sourceMap) {
|
|
271
|
+
const fileName = path.basename(filePath);
|
|
272
|
+
const mapFilePath = filePath.replace('.css', '.css.map');
|
|
273
|
+
|
|
274
|
+
// Add source map reference to the CSS file
|
|
275
|
+
const sourceMapComment = `\n/*# sourceMappingURL=${fileName}.map */`;
|
|
276
|
+
await writeFile(filePath, output.styles + sourceMapComment, logger);
|
|
277
|
+
|
|
278
|
+
// Write the source map file
|
|
279
|
+
try {
|
|
280
|
+
const sourceMap = JSON.stringify(output.sourceMap);
|
|
281
|
+
await writeFile(mapFilePath, sourceMap, logger);
|
|
282
|
+
await logger?.info('Generated CSS source map', { filePath: mapFilePath });
|
|
283
|
+
} catch (error) {
|
|
284
|
+
await logger?.error('Failed to write CSS source map', { filePath: mapFilePath, error: error.message });
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
await writeFile(filePath, output.styles, logger);
|
|
288
|
+
}
|
|
246
289
|
} else {
|
|
247
290
|
// `Unsupported file type, skipping: ${filePath}`;
|
|
248
291
|
return;
|
|
@@ -266,6 +309,8 @@ async function processFile(filePath, logger, options) {
|
|
|
266
309
|
* @property {string} [configPath] - Path to the configuration file.
|
|
267
310
|
* @property {boolean} [ignoreBrowserslistConfig] - Ignores the browserslist configuration.
|
|
268
311
|
* @property {boolean} [shippedProposals] - Enables support for shipped proposals.
|
|
312
|
+
* @property {boolean} [useAppendTransform] - Enables the append-to-appendChild transform plugin for compatibility with older browsers.
|
|
313
|
+
* @property {Array<string|Array|Function>} [plugins] - Additional Babel plugins to include in the transformation process.
|
|
269
314
|
*/
|
|
270
315
|
|
|
271
316
|
/**
|
|
@@ -317,6 +362,7 @@ async function processFile(filePath, logger, options) {
|
|
|
317
362
|
* @property {PostCSSOptions|boolean} [usePostCSS=false] - PostCSS configuration options.
|
|
318
363
|
* @property {string[]|null} [useVersioning=null] - Options for file versioning.
|
|
319
364
|
* @property {boolean} [useJsMap=false] - Whether to use JavaScript Map file.
|
|
365
|
+
* @property {boolean} [useCssMap=false] - Whether to use CSS Map file.
|
|
320
366
|
*/
|
|
321
367
|
|
|
322
368
|
/**
|
|
@@ -337,6 +383,7 @@ export default async function minifyAll(contentPath, options = {}) {
|
|
|
337
383
|
usePostCSS = false,
|
|
338
384
|
useVersioning = null,
|
|
339
385
|
useJsMap = false,
|
|
386
|
+
useCssMap = false,
|
|
340
387
|
} = options;
|
|
341
388
|
|
|
342
389
|
let logger = null;
|
|
@@ -351,6 +398,7 @@ export default async function minifyAll(contentPath, options = {}) {
|
|
|
351
398
|
usePostCSS: !!usePostCSS,
|
|
352
399
|
useVersioning: !!useVersioning,
|
|
353
400
|
useJsMap,
|
|
401
|
+
useCssMap,
|
|
354
402
|
});
|
|
355
403
|
}
|
|
356
404
|
|
|
@@ -369,6 +417,7 @@ export default async function minifyAll(contentPath, options = {}) {
|
|
|
369
417
|
cssMinifyOptions,
|
|
370
418
|
postcssOptions: usePostCSS === true ? {} : usePostCSS,
|
|
371
419
|
useJsMap,
|
|
420
|
+
useCssMap,
|
|
372
421
|
};
|
|
373
422
|
|
|
374
423
|
try {
|
package/dist/modules/minifier.js
CHANGED
|
@@ -43,15 +43,19 @@ export function minifyJS(content, options = {}) {
|
|
|
43
43
|
* @param {Object} [options={}] - Clean-CSS options.
|
|
44
44
|
* @param {Object} [postcssOptions=null] - PostCSS configuration options, if null PostCSS is skipped.
|
|
45
45
|
* @param {string} [filePath=''] - Path to the file being processed.
|
|
46
|
-
* @
|
|
46
|
+
* @param {boolean} [useCssMap=false] - Whether to generate and use a CSS source map file.
|
|
47
|
+
* @returns {Promise<{styles: string, warnings: string[], map: string}>} Minified CSS, warnings and source map.
|
|
47
48
|
*/
|
|
48
|
-
export async function minifyCSS(content, options = {}, postcssOptions = null, filePath = '') {
|
|
49
|
+
export async function minifyCSS(content, options = {}, postcssOptions = null, filePath = '', useCssMap = false) {
|
|
49
50
|
const defaultOptions = {
|
|
50
51
|
level: { 1: { all: false } },
|
|
52
|
+
sourceMap: !!useCssMap,
|
|
53
|
+
sourceMapInlineSources: !!useCssMap,
|
|
51
54
|
};
|
|
52
55
|
|
|
53
56
|
const mergedOptions = { ...defaultOptions, ...options };
|
|
54
57
|
let processedCSS = content;
|
|
58
|
+
let previousSourceMap = null;
|
|
55
59
|
|
|
56
60
|
// Check if PostCSS is available and enabled
|
|
57
61
|
if (postCSSAvailable === null) {
|
|
@@ -64,6 +68,10 @@ export async function minifyCSS(content, options = {}, postcssOptions = null, fi
|
|
|
64
68
|
const result = await processWithPostCSS(content, filePath, postcssOptions);
|
|
65
69
|
processedCSS = result.css;
|
|
66
70
|
|
|
71
|
+
if (result.map) {
|
|
72
|
+
previousSourceMap = result.map.toString();
|
|
73
|
+
}
|
|
74
|
+
|
|
67
75
|
// Handle warnings
|
|
68
76
|
if (result.messages && result.messages.length > 0) {
|
|
69
77
|
console.warn(`PostCSS warnings for ${filePath}:`, result.messages.map((msg) => msg.text).join('\n'));
|
|
@@ -76,6 +84,6 @@ export async function minifyCSS(content, options = {}, postcssOptions = null, fi
|
|
|
76
84
|
|
|
77
85
|
// Use CleanCSS for final minification
|
|
78
86
|
return new Promise((resolve) => {
|
|
79
|
-
new CleanCSS(mergedOptions).minify(processedCSS, (error, output) => resolve(output));
|
|
87
|
+
new CleanCSS(mergedOptions).minify(processedCSS, previousSourceMap, (error, output) => resolve(output));
|
|
80
88
|
});
|
|
81
89
|
}
|
|
@@ -52,11 +52,13 @@ export async function processWithPostCSS(content, filePath = '', options = {}) {
|
|
|
52
52
|
const result = await postcss(plugins).process(content, {
|
|
53
53
|
from: filePath || undefined,
|
|
54
54
|
to: filePath || undefined,
|
|
55
|
+
map: { inline: true },
|
|
55
56
|
});
|
|
56
57
|
|
|
57
58
|
return {
|
|
58
59
|
css: result.css,
|
|
59
60
|
messages: result.messages,
|
|
61
|
+
map: result.map,
|
|
60
62
|
};
|
|
61
63
|
} catch (error) {
|
|
62
64
|
console.error('Error processing CSS with PostCSS:', error);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Babel plugin to transform Element.append() calls to be compatible with older browsers.
|
|
3
|
+
* This plugin converts:
|
|
4
|
+
* - element.append("text") to element.appendChild(document.createTextNode("text"))
|
|
5
|
+
* - element.append(node) to element.appendChild(node)
|
|
6
|
+
* - element.append(node1, "text", node2) to a sequence of appendChild calls
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} param0 - Babel plugin parameters
|
|
9
|
+
* @param {Object} param0.types - Babel types utility
|
|
10
|
+
* @returns {Object} Babel plugin object
|
|
11
|
+
*/
|
|
12
|
+
export default function ({ types: t }) {
|
|
13
|
+
return {
|
|
14
|
+
name: 'append-polyfill-transform',
|
|
15
|
+
visitor: {
|
|
16
|
+
CallExpression(path) {
|
|
17
|
+
const callee = path.get('callee');
|
|
18
|
+
|
|
19
|
+
// Check for element.append(...) call pattern
|
|
20
|
+
if (!callee.isMemberExpression()) return;
|
|
21
|
+
if (!callee.get('property').isIdentifier({ name: 'append' })) return;
|
|
22
|
+
|
|
23
|
+
const args = path.node.arguments;
|
|
24
|
+
const objectNode = callee.get('object').node;
|
|
25
|
+
|
|
26
|
+
// Skip if no arguments
|
|
27
|
+
if (args.length === 0) return;
|
|
28
|
+
|
|
29
|
+
// Handle single argument case
|
|
30
|
+
if (args.length === 1) {
|
|
31
|
+
const arg = args[0];
|
|
32
|
+
|
|
33
|
+
// String literal case: element.append("text")
|
|
34
|
+
if (t.isStringLiteral(arg)) {
|
|
35
|
+
path.replaceWith(
|
|
36
|
+
t.callExpression(t.memberExpression(t.cloneNode(objectNode), t.identifier('appendChild')), [
|
|
37
|
+
t.callExpression(t.memberExpression(t.identifier('document'), t.identifier('createTextNode')), [arg]),
|
|
38
|
+
]),
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
41
|
+
// Node case: element.append(node)
|
|
42
|
+
callee.get('property').replaceWith(t.identifier('appendChild'));
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Handle multiple arguments: element.append(node1, "text", node2)
|
|
48
|
+
const statements = args.map((arg) => {
|
|
49
|
+
// String literal case
|
|
50
|
+
if (t.isStringLiteral(arg)) {
|
|
51
|
+
return t.callExpression(t.memberExpression(t.cloneNode(objectNode), t.identifier('appendChild')), [
|
|
52
|
+
t.callExpression(t.memberExpression(t.identifier('document'), t.identifier('createTextNode')), [arg]),
|
|
53
|
+
]);
|
|
54
|
+
} else {
|
|
55
|
+
// Node case
|
|
56
|
+
return t.callExpression(t.memberExpression(t.cloneNode(objectNode), t.identifier('appendChild')), [arg]);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (statements.length > 0) {
|
|
61
|
+
// Combine multiple statements into a sequence expression
|
|
62
|
+
path.replaceWith(t.sequenceExpression(statements));
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
package/minify.d.ts
CHANGED
|
@@ -56,6 +56,17 @@ declare module 'uglify-js-minify-css-allfiles' {
|
|
|
56
56
|
* Toggles enabling support for builtin/feature proposals that have shipped in browsers.
|
|
57
57
|
*/
|
|
58
58
|
shippedProposals?: boolean;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Enables the append-to-appendChild transform plugin.
|
|
62
|
+
* When true, Element.append() calls are transformed to Element.appendChild() calls for compatibility with older browsers like Chrome 35.
|
|
63
|
+
*/
|
|
64
|
+
useAppendTransform?: boolean;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Additional Babel plugins to include in the transformation process.
|
|
68
|
+
*/
|
|
69
|
+
plugins?: Array<string | Array | Function>;
|
|
59
70
|
}
|
|
60
71
|
|
|
61
72
|
/**
|
|
@@ -187,6 +198,13 @@ declare module 'uglify-js-minify-css-allfiles' {
|
|
|
187
198
|
* @default false
|
|
188
199
|
*/
|
|
189
200
|
useJsMap?: boolean;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Enables source map generation for CSS files during minification.
|
|
204
|
+
* Source maps help with debugging by mapping minified CSS back to original source code.
|
|
205
|
+
* @default false
|
|
206
|
+
*/
|
|
207
|
+
useCssMap?: boolean;
|
|
190
208
|
}
|
|
191
209
|
|
|
192
210
|
/**
|
|
@@ -215,6 +233,7 @@ declare module 'uglify-js-minify-css-allfiles' {
|
|
|
215
233
|
* retentionDays: 7
|
|
216
234
|
* },
|
|
217
235
|
* useJsMap: true // Enable source map generation
|
|
236
|
+
* useCssMap: true // Enable source map generation
|
|
218
237
|
* });
|
|
219
238
|
* ```
|
|
220
239
|
*/
|