UNPKG

22.4 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import { RequestOptions as HttpRequestOptions } from "http";
4import { RequestOptions as HttpsRequestOptions } from "https";
5
6import { RawSourceMap, SourceMapGenerator } from "source-map";
7
8/**
9 * Shared options passed when initializing a new instance of CleanCSS that returns either a promise or output
10 */
11interface OptionsBase {
12 /**
13 * Controls compatibility mode used; defaults to ie10+ using `'*'`.
14 * Compatibility hash exposes the following properties: `colors`, `properties`, `selectors`, and `units`
15 */
16 compatibility?: "*" | "ie9" | "ie8" | "ie7" | CleanCSS.CompatibilityOptions | undefined;
17
18 /**
19 * Controls a function for handling remote requests; Defaults to the build in `loadRemoteResource` function
20 */
21 fetch?:
22 | ((
23 uri: string,
24 inlineRequest: HttpRequestOptions | HttpsRequestOptions,
25 inlineTimeout: number,
26 done: (message: string | number, body: string) => void,
27 ) => void)
28 | undefined;
29
30 /**
31 * Controls output CSS formatting; defaults to `false`.
32 * Format hash exposes the following properties: `breaks`, `breakWith`, `indentBy`, `indentWith`, `spaces`, and `wrapAt`.
33 */
34 format?: "beautify" | "keep-breaks" | CleanCSS.FormatOptions | false | undefined;
35
36 /**
37 * inline option whitelists which @import rules will be processed. Defaults to `'local'`
38 * Accepts the following values:
39 * 'local': enables local inlining;
40 * 'remote': enables remote inlining;
41 * 'none': disables all inlining;
42 * 'all': enables all inlining, same as ['local', 'remote'];
43 * '[uri]': enables remote inlining from the specified uri;
44 * '![url]': disables remote inlining from the specified uri;
45 */
46 inline?: readonly string[] | false | undefined;
47
48 /**
49 * Controls extra options for inlining remote @import rules
50 */
51 inlineRequest?: HttpRequestOptions | HttpsRequestOptions | undefined;
52
53 /**
54 * Controls number of milliseconds after which inlining a remote @import fails; defaults to `5000`;
55 */
56 inlineTimeout?: number | undefined;
57
58 /**
59 * Controls optimization level used; defaults to `1`.
60 * Level hash exposes `1`, and `2`.
61 */
62 level?: 0 | 1 | 2 | CleanCSS.OptimizationsOptions | undefined;
63
64 /**
65 * Controls URL rebasing; defaults to `true`;
66 */
67 rebase?: boolean | undefined;
68
69 /**
70 * controls a directory to which all URLs are rebased, most likely the directory under which the output file
71 * will live; defaults to the current directory;
72 */
73 rebaseTo?: string | undefined;
74
75 /**
76 * Controls whether an output source map is built; defaults to `false`
77 */
78 sourceMap?: boolean | undefined;
79
80 /**
81 * Controls embedding sources inside a source map's `sourcesContent` field; defaults to `false`
82 */
83 sourceMapInlineSources?: boolean | undefined;
84}
85
86declare namespace CleanCSS {
87 /**
88 * Output returned when calling minify functions
89 */
90 interface Output {
91 /**
92 * Optimized output CSS as a string
93 */
94 styles: string;
95
96 /**
97 * Output source map if requested with `sourceMap` option
98 */
99 sourceMap?: SourceMapGenerator;
100
101 /**
102 * A list of errors raised
103 */
104 errors: string[];
105
106 /**
107 * A list of warnings raised
108 */
109 warnings: string[];
110
111 /**
112 * Contains statistics on the minify process
113 */
114 stats: {
115 /**
116 * Original content size after import inlining
117 */
118 originalSize: number;
119
120 /**
121 * Optimized content size
122 */
123 minifiedSize: number;
124
125 /**
126 * Time spent on optimizations in milliseconds
127 */
128 timeSpent: number;
129
130 /**
131 * `(originalSize - minifiedSize) / originalSize`, e.g. 0.25 if size is reduced from 100 bytes to 75 bytes
132 */
133 efficiency: number;
134 };
135 }
136
137 /**
138 * Fine grained configuration for compatibility option
139 */
140 interface CompatibilityOptions {
141 /**
142 * A hash of compatibility options related to color
143 */
144 colors?: {
145 /**
146 * Controls `rgba()` / `hsla()` color support; defaults to `true`
147 */
148 opacity?: boolean | undefined;
149 } | undefined;
150 /**
151 * A hash of properties that can be set with compatibility
152 */
153 properties?: {
154 /**
155 * Controls background-clip merging into shorthand; defaults to `true`
156 */
157 backgroundClipMerging?: boolean | undefined;
158
159 /**
160 * Controls background-origin merging into shorthand; defaults to `true`
161 */
162 backgroundOriginMerging?: boolean | undefined;
163
164 /**
165 * Controls background-size merging into shorthand; defaults to `true`
166 */
167 backgroundSizeMerging?: boolean | undefined;
168
169 /**
170 * controls color optimizations; defaults to `true`
171 */
172 colors?: boolean | undefined;
173
174 /**
175 * Controls keeping IE bang hack; defaults to `false`
176 */
177 ieBangHack?: boolean | undefined;
178
179 /**
180 * Controls keeping IE `filter` / `-ms-filter`; defaults to `false`
181 */
182 ieFilters?: boolean | undefined;
183
184 /**
185 * Controls keeping IE prefix hack; defaults to `false`
186 */
187 iePrefixHack?: boolean | undefined;
188
189 /**
190 * Controls keeping IE suffix hack; defaults to `false`
191 */
192 ieSuffixHack?: boolean | undefined;
193
194 /**
195 * Controls property merging based on understandably; defaults to `true`
196 */
197 merging?: boolean | undefined;
198
199 /**
200 * Controls shortening pixel units into `pc`, `pt`, or `in` units; defaults to `false`
201 */
202 shorterLengthUnits?: false | undefined;
203
204 /**
205 * Controls keeping space after closing brace - `url() no-repeat` into `url()no-repeat`; defaults to `true`
206 */
207 spaceAfterClosingBrace?: true | undefined;
208
209 /**
210 * Controls keeping quoting inside `url()`; defaults to `false`
211 */
212 urlQuotes?: boolean | undefined;
213
214 /**
215 * Controls removal of units `0` value; defaults to `true`
216 */
217 zeroUnits?: boolean | undefined;
218 } | undefined;
219 /**
220 * A hash of options related to compatibility of selectors
221 */
222 selectors?: {
223 /**
224 * Controls extra space before `nav` element; defaults to `false`
225 */
226 adjacentSpace?: boolean | undefined;
227
228 /**
229 * Controls removal of IE7 selector hacks, e.g. `*+html...`; defaults to `true`
230 */
231 ie7Hack?: boolean | undefined;
232
233 /**
234 * Controls a whitelist of mergeable pseudo classes; defaults to `[':active', ...]`
235 */
236 mergeablePseudoClasses?: readonly string[] | undefined;
237
238 /**
239 * Controls a whitelist of mergeable pseudo elements; defaults to `['::after', ...]`
240 */
241 mergeablePseudoElements: readonly string[];
242
243 /**
244 * Controls maximum number of selectors in a single rule (since 4.1.0); defaults to `8191`
245 */
246 mergeLimit: number;
247
248 /**
249 * Controls merging of rules with multiple pseudo classes / elements (since 4.1.0); defaults to `true`
250 */
251 multiplePseudoMerging: boolean;
252 } | undefined;
253 /**
254 * A hash of options related to comparability of supported units
255 */
256 units?: {
257 /**
258 * Controls treating `ch` as a supported unit; defaults to `true`
259 */
260 ch?: boolean | undefined;
261
262 /**
263 * Controls treating `in` as a supported unit; defaults to `true`
264 */
265 in?: boolean | undefined;
266
267 /**
268 * Controls treating `pc` as a supported unit; defaults to `true`
269 */
270 pc?: boolean | undefined;
271
272 /**
273 * Controls treating `pt` as a supported unit; defaults to `true`
274 */
275 pt?: boolean | undefined;
276
277 /**
278 * Controls treating `rem` as a supported unit; defaults to `true`
279 */
280 rem?: boolean | undefined;
281
282 /**
283 * Controls treating `vh` as a supported unit; defaults to `true`
284 */
285 vh?: boolean | undefined;
286
287 /**
288 * Controls treating `vm` as a supported unit; defaults to `true`
289 */
290 vm?: boolean | undefined;
291
292 /**
293 * Controls treating `vmax` as a supported unit; defaults to `true`
294 */
295 vmax?: boolean | undefined;
296
297 /**
298 * Controls treating `vmin` as a supported unit; defaults to `true`
299 */
300 vmin?: boolean | undefined;
301 } | undefined;
302 }
303
304 /**
305 * Fine grained options for configuring the CSS formatting
306 */
307 interface FormatOptions {
308 /**
309 * Controls where to insert breaks
310 */
311 breaks?: {
312 /**
313 * Controls if a line break comes after an at-rule; e.g. `@charset`; defaults to `false`
314 */
315 afterAtRule?: boolean | undefined;
316
317 /**
318 * Controls if a line break comes after a block begins; e.g. `@media`; defaults to `false`
319 */
320 afterBlockBegins?: boolean | undefined;
321
322 /**
323 * Controls if a line break comes after a block ends, defaults to `false`
324 */
325 afterBlockEnds?: boolean | undefined;
326
327 /**
328 * Controls if a line break comes after a comment; defaults to `false`
329 */
330 afterComment?: boolean | undefined;
331
332 /**
333 * Controls if a line break comes after a property; defaults to `false`
334 */
335 afterProperty?: boolean | undefined;
336
337 /**
338 * Controls if a line break comes after a rule begins; defaults to `false`
339 */
340 afterRuleBegins?: boolean | undefined;
341
342 /**
343 * Controls if a line break comes after a rule ends; defaults to `false`
344 */
345 afterRuleEnds?: boolean | undefined;
346
347 /**
348 * Controls if a line break comes before a block ends; defaults to `false`
349 */
350 beforeBlockEnds?: boolean | undefined;
351
352 /**
353 * Controls if a line break comes between selectors; defaults to `false`
354 */
355 betweenSelectors?: boolean | undefined;
356 } | undefined;
357 /**
358 * Controls the new line character, can be `'\r\n'` or `'\n'`(aliased as `'windows'` and `'unix'`
359 * or `'crlf'` and `'lf'`); defaults to system one, so former on Windows and latter on Unix
360 */
361 breakWith?: string | undefined;
362
363 /**
364 * Controls number of characters to indent with; defaults to `0`
365 */
366 indentBy?: number | undefined;
367
368 /**
369 * Controls a character to indent with, can be `'space'` or `'tab'`; defaults to `'space'`
370 */
371 indentWith?: "space" | "tab" | undefined;
372
373 /**
374 * Controls where to insert spaces
375 */
376 spaces?: {
377 /**
378 * Controls if spaces come around selector relations; e.g. `div > a`; defaults to `false`
379 */
380 aroundSelectorRelation?: boolean | undefined;
381
382 /**
383 * Controls if a space comes before a block begins; e.g. `.block {`; defaults to `false`
384 */
385 beforeBlockBegins?: boolean | undefined;
386
387 /**
388 * Controls if a space comes before a value; e.g. `width: 1rem`; defaults to `false`
389 */
390 beforeValue?: boolean | undefined;
391 } | undefined;
392 /**
393 * Controls maximum line length; defaults to `false`
394 */
395 wrapAt?: false | number | undefined;
396
397 /**
398 * Controls removing trailing semicolons in rule; defaults to `false` - means remove
399 */
400 semicolonAfterLastProperty?: boolean | undefined;
401 }
402
403 /**
404 * Fine grained options for configuring optimizations
405 */
406 interface OptimizationsOptions {
407 1?: {
408 /**
409 * Sets all optimizations at this level unless otherwise specified
410 */
411 all?: boolean | undefined;
412
413 /**
414 * Controls `@charset` moving to the front of a stylesheet; defaults to `true`
415 */
416 cleanupCharsets?: boolean | undefined;
417
418 /**
419 * Controls URL normalization; defaults to `true`
420 */
421 normalizeUrls?: boolean | undefined;
422
423 /**
424 * Controls `background` property optimizations; defaults to `true`
425 */
426 optimizeBackground?: boolean | undefined;
427
428 /**
429 * Controls `border-radius` property optimizations; defaults to `true`
430 */
431 optimizeBorderRadius?: boolean | undefined;
432
433 /**
434 * Controls `filter` property optimizations; defaults to `true`
435 */
436 optimizeFilter?: boolean | undefined;
437
438 /**
439 * Controls `font` property optimizations; defaults to `true`
440 */
441 optimizeFont?: boolean | undefined;
442
443 /**
444 * Controls `font-weight` property optimizations; defaults to `true`
445 */
446 optimizeFontWeight?: boolean | undefined;
447
448 /**
449 * Controls `outline` property optimizations; defaults to `true`
450 */
451 optimizeOutline?: boolean | undefined;
452
453 /**
454 * Controls removing empty rules and nested blocks; defaults to `true`
455 */
456 removeEmpty?: boolean | undefined;
457
458 /**
459 * Controls removing negative paddings; defaults to `true`
460 */
461 removeNegativePaddings?: boolean | undefined;
462
463 /**
464 * Controls removing quotes when unnecessary; defaults to `true`
465 */
466 removeQuotes?: boolean | undefined;
467
468 /**
469 * Controls removing unused whitespace; defaults to `true`
470 */
471 removeWhitespace?: boolean | undefined;
472
473 /**
474 * Contols removing redundant zeros; defaults to `true`
475 */
476 replaceMultipleZeros?: boolean | undefined;
477
478 /**
479 * Controls replacing time units with shorter values; defaults to `true`
480 */
481 replaceTimeUnits?: boolean | undefined;
482
483 /**
484 * Controls replacing zero values with units; defaults to `true`
485 */
486 replaceZeroUnits?: boolean | undefined;
487
488 /**
489 * Rounds pixel values to `N` decimal places; `false` disables rounding; defaults to `false`
490 */
491 roundingPrecision?: boolean | undefined;
492
493 /**
494 * denotes selector sorting method; can be `'natural'` or `'standard'`, `'none'`, or false (the last two
495 * since 4.1.0); defaults to `'standard'`
496 */
497 selectorsSortingMethod?: "standard" | "natural" | "none" | undefined;
498
499 /**
500 * denotes a number of /*! ... * / comments preserved; defaults to `all`
501 */
502 specialComments?: string | undefined;
503
504 /**
505 * Controls at-rules (e.g. `@charset`, `@import`) optimizing; defaults to `true`
506 */
507 tidyAtRules?: boolean | undefined;
508
509 /**
510 * Controls block scopes (e.g. `@media`) optimizing; defaults to `true`
511 */
512 tidyBlockScopes?: boolean | undefined;
513
514 /**
515 * Controls selectors optimizing; defaults to `true`
516 */
517 tidySelectors?: boolean | undefined;
518
519 /**
520 * Defines a callback for fine-grained property optimization; defaults to no-op
521 */
522 transform?: ((propertyName: string, propertyValue: string, selector?: string) => string) | undefined;
523 } | undefined;
524 2?: {
525 /**
526 * Sets all optimizations at this level unless otherwise specified
527 */
528 all?: boolean | undefined;
529
530 /**
531 * Controls adjacent rules merging; defaults to true
532 */
533 mergeAdjacentRules?: boolean | undefined;
534
535 /**
536 * Controls merging properties into shorthands; defaults to true
537 */
538 mergeIntoShorthands?: boolean | undefined;
539
540 /**
541 * Controls `@media` merging; defaults to true
542 */
543 mergeMedia?: boolean | undefined;
544
545 /**
546 * Controls non-adjacent rule merging; defaults to true
547 */
548 mergeNonAdjacentRules?: boolean | undefined;
549
550 /**
551 * Controls semantic merging; defaults to false
552 */
553 mergeSemantically?: boolean | undefined;
554
555 /**
556 * Controls property overriding based on understandably; defaults to true
557 */
558 overrideProperties?: boolean | undefined;
559
560 /**
561 * Controls removing empty rules and nested blocks; defaults to `true`
562 */
563 removeEmpty?: boolean | undefined;
564
565 /**
566 * Controls non-adjacent rule reducing; defaults to true
567 */
568 reduceNonAdjacentRules?: boolean | undefined;
569
570 /**
571 * Controls duplicate `@font-face` removing; defaults to true
572 */
573 removeDuplicateFontRules?: boolean | undefined;
574
575 /**
576 * Controls duplicate `@media` removing; defaults to true
577 */
578 removeDuplicateMediaBlocks?: boolean | undefined;
579
580 /**
581 * Controls duplicate rules removing; defaults to true
582 */
583 removeDuplicateRules?: boolean | undefined;
584
585 /**
586 * Controls unused at rule removing; defaults to false (available since 4.1.0)
587 */
588 removeUnusedAtRules?: boolean | undefined;
589
590 /**
591 * Controls rule restructuring; defaults to false
592 */
593 restructureRules?: boolean | undefined;
594
595 /**
596 * Controls which properties won't be optimized, defaults to `[]` which means all will be optimized (since 4.1.0)
597 */
598 skipProperties?: readonly string[] | undefined;
599 } | undefined;
600 }
601
602 /**
603 * Hash of input source(s). Passing an array of hashes allows you to explicitly specify the order in which the input files
604 * are concatenated. Whereas when you use a single hash the order is determined by the traversal order of object properties
605 */
606 interface Source {
607 /**
608 * Path to file
609 */
610 [path: string]: {
611 /**
612 * The contents of the file, should be css
613 */
614 styles: string;
615
616 /**
617 * The source map of the file, if needed
618 */
619 sourceMap?: RawSourceMap | string | undefined;
620 };
621 }
622
623 /**
624 * Callback type when fetch is used
625 */
626 type FetchCallback = (message: string | number, body: string) => void;
627
628 /**
629 * Union of all types acceptable as input for the minify function
630 */
631 type Sources = string | readonly string[] | Source | readonly Source[] | Buffer;
632
633 /**
634 * Union type for both types of minifier functions
635 */
636 type Minifier = MinifierOutput | MinifierPromise;
637
638 /**
639 * Interface exposed when a new CleanCSS object is created
640 */
641 interface MinifierOutput {
642 minify(sources: Sources, callback?: (error: any, output: Output) => void): Output;
643 minify(
644 sources: Sources,
645 sourceMap: RawSourceMap | string,
646 callback?: (error: any, output: Output) => void,
647 ): Output;
648 }
649 /**
650 * Interface exposed when a new CleanCSS object is created with returnPromise set to true
651 */
652 interface MinifierPromise {
653 minify(sources: Sources, sourceMap?: RawSourceMap | string): Promise<Output>;
654 }
655
656 /**
657 * Options when returning a promise
658 */
659 type OptionsPromise = OptionsBase & {
660 /**
661 * If you prefer clean-css to return a Promise object then you need to explicitly ask for it; defaults to `false`
662 */
663 returnPromise: true;
664 };
665
666 /**
667 * Options when returning an output
668 */
669 type OptionsOutput = OptionsBase & {
670 /**
671 * If you prefer clean-css to return a Promise object then you need to explicitly ask for it; defaults to `false`
672 */
673 returnPromise?: false | undefined;
674 };
675
676 /**
677 * Discriminant union of both sets of options types. If you initialize without setting `returnPromise: true`
678 * and want to return a promise, you will need to cast to the correct options type so that TypeScript
679 * knows what the expected return type will be:
680 * `(options = options as CleanCSS.OptionsPromise).returnPromise = true`
681 */
682 type Options = OptionsPromise | OptionsOutput;
683
684 /**
685 * Constructor interface for CleanCSS
686 */
687 interface Constructor {
688 new(options: OptionsPromise): MinifierPromise;
689 new(options?: OptionsOutput): MinifierOutput;
690 }
691}
692
693/**
694 * Creates a new CleanCSS object which can be used to minify css
695 */
696declare const CleanCSS: CleanCSS.Constructor;
697
698export = CleanCSS;