UNPKG

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