UNPKG

19.3 kBTypeScriptView Raw
1// Type definitions for svgo 2.3
2// Project: https://github.com/svg/svgo
3// Definitions by: Bradley Ayers <https://github.com/bradleyayers>
4// Gilad Gray <https://github.com/giladgray>
5// Aankhen <https://github.com/Aankhen>
6// Jan Karres <https://github.com/jankarres>
7// Gavin Gregory <https://github.com/gavingregory>
8// Piotr Błażejewicz <https://github.com/peterblazejewicz>
9// Remco Haszing <https://github.com/remcohaszing>
10// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11
12export interface DefaultPlugin<N extends string, P = never> {
13 active?: boolean;
14 name: N;
15 params?: P;
16}
17
18/**
19 * adds attributes to an outer <svg> element
20 */
21export type AddAttributesToSVGElementPlugin = DefaultPlugin<'addAttributesToSVGElement'>;
22
23/**
24 * adds classnames to an outer <svg> element
25 */
26export type AddClassesToSVGElementPlugin = DefaultPlugin<'addClassesToSVGElement'>;
27
28/**
29 * cleanups attributes from newlines, trailing and repeating spaces
30 */
31export type CleanupAttrsPlugin = DefaultPlugin<
32 'cleanupAttrs',
33 {
34 /** @default true */
35 newlines?: boolean;
36 /** @default true */
37 trim?: boolean;
38 /** @default true */
39 spaces?: boolean;
40 }
41>;
42
43/**
44 * remove or cleanup enable-background attribute when possible
45 */
46export type CleanupEnableBackgroundPlugin = DefaultPlugin<'cleanupEnableBackground'>;
47
48/**
49 * removes unused IDs and minifies used
50 */
51export type CleanupIDsPlugin = DefaultPlugin<
52 'cleanupIDs',
53 {
54 /** @default true */
55 remove?: boolean;
56 /** @default true */
57 minify?: boolean;
58 /** @default '' */
59 prefix?: string;
60 /** @default [] */
61 preserve?: any[];
62 /** @default [] */
63 preservePrefixes?: any[];
64 /** @default false */
65 force?: boolean;
66 }
67>;
68
69/**
70 * rounds list of values to the fixed precision
71 */
72export type CleanupListOfValuesPlugin = DefaultPlugin<
73 'cleanupListOfValues',
74 {
75 /** @default 3 */
76 floatPrecision?: number;
77 /** @default true */
78 leadingZero?: boolean;
79 /** @default true */
80 defaultPx?: boolean;
81 /** @default true */
82 convertToPx?: boolean;
83 }
84>;
85
86/**
87 * rounds numeric values to the fixed precision, removes default ‘px’ units
88 */
89export type CleanupNumericValuesPlugin = DefaultPlugin<
90 'cleanupNumericValues',
91 {
92 /** @default 3 */
93 floatPrecision?: number;
94 /** @default true */
95 leadingZero?: boolean;
96 /** @default true */
97 defaultPx?: boolean;
98 /** @default true */
99 convertToPx?: boolean;
100 }
101>;
102
103/**
104 * collapses useless groups
105 */
106export type CollapseGroupsPlugin = DefaultPlugin<'collapseGroups'>;
107
108/**
109 * converts colors: rgb() to #rrggbb and #rrggbb to #rgb
110 */
111export type ConvertColorsPlugin = DefaultPlugin<
112 'convertColors',
113 {
114 /** @default false */
115 currentColor?: boolean;
116 /** @default true */
117 names2hex?: boolean;
118 /** @default true */
119 rgb2hex?: boolean;
120 /** @default true */
121 shorthex?: boolean;
122 /** @default true */
123 shortname?: boolean;
124 }
125>;
126
127/**
128 * converts non-eccentric <ellipse>s to <circle>s
129 */
130export type ConvertEllipseToCirclePlugin = DefaultPlugin<'convertEllipseToCircle'>;
131
132/**
133 * optimizes path data: writes in shorter form, applies transformations
134 */
135export type ConvertPathDataPlugin = DefaultPlugin<
136 'convertPathData',
137 {
138 /** @default true */
139 applyTransforms?: boolean;
140 /** @default true */
141 applyTransformsStroked?: boolean;
142 makeArcs?: {
143 /** @default 2.5 */
144 threshold?: number;
145 /** @default 0.5 */
146 tolerance?: number;
147 };
148 /** @default true */
149 straightCurves?: boolean;
150 /** @default true */
151 lineShorthands?: boolean;
152 /** @default true */
153 curveSmoothShorthands?: boolean;
154 /** @default 3 */
155 floatPrecision?: number;
156 /** @default 5 */
157 transformPrecision?: number;
158 /** @default true */
159 removeUseless?: boolean;
160 /** @default true */
161 collapseRepeated?: boolean;
162 /** @default true */
163 utilizeAbsolute?: boolean;
164 /** @default true */
165 leadingZero?: boolean;
166 /** @default true */
167 negativeExtraSpace?: boolean;
168 /** @default false */
169 noSpaceAfterFlags?: boolean;
170 /** @default false */
171 forceAbsolutePath?: boolean;
172 }
173>;
174
175/**
176 * converts basic shapes to more compact path form
177 */
178export type ConvertShapeToPathPlugin = DefaultPlugin<
179 'convertShapeToPath',
180 {
181 /** @default false */
182 convertArcs?: boolean;
183 }
184>;
185
186/**
187 * converts style to attributes
188 */
189export type ConvertStyleToAttrsPlugin = DefaultPlugin<
190 'convertStyleToAttrs',
191 {
192 /** @default false */
193 keepImportant?: boolean;
194 }
195>;
196
197/**
198 * collapses multiple transformations and optimizes it
199 */
200export type ConvertTransformPlugin = DefaultPlugin<
201 'convertTransform',
202 {
203 /** @default true */
204 convertToShorts?: boolean;
205 /** @default 3 */
206 floatPrecision?: number;
207 /** @default 5 */
208 transformPrecision?: number;
209 /** @default true */
210 matrixToTransform?: boolean;
211 /** @default true */
212 shortTranslate?: boolean;
213 /** @default true */
214 shortScale?: boolean;
215 /** @default true */
216 shortRotate?: boolean;
217 /** @default true */
218 removeUseless?: boolean;
219 /** @default true */
220 collapseIntoOne?: boolean;
221 /** @default true */
222 leadingZero?: boolean;
223 /** @default false */
224 negativeExtraSpace?: boolean;
225 }
226>;
227
228/**
229 * inline styles (additional options)
230 */
231export type InlineStylesPlugin = DefaultPlugin<
232 'inlineStyles',
233 {
234 /** @default true */
235 onlyMatchedOnce?: boolean;
236 /** @default true */
237 removeMatchedSelectors?: boolean;
238 /** @default ['', 'screen'] */
239 useMqs?: string[];
240 /** @default [''] */
241 usePseudos?: string[];
242 }
243>;
244
245/**
246 * merges multiple paths in one if possible
247 */
248export type MergePathsPlugin = DefaultPlugin<
249 'mergePaths',
250 {
251 /** @default true */
252 collapseRepeated?: boolean;
253 /** @default false */
254 force?: boolean;
255 /** @default true */
256 leadingZero?: boolean;
257 /** @default true */
258 negativeExtraSpace?: boolean;
259 /** @default false */
260 noSpaceAfterFlags?: boolean;
261 }
262>;
263
264/**
265 * merge multiple style elements into one
266 */
267export type MergeStylesPlugin = DefaultPlugin<'mergeStyles'>;
268
269/**
270 * minifies styles and removes unused styles based on usage data
271 */
272export type MinifyStylesPlugin = DefaultPlugin<
273 'minifyStyles',
274 {
275 usage: {
276 /** @default false */
277 force?: boolean;
278 /** @default true */
279 ids?: boolean;
280 /** @default true */
281 classes?: boolean;
282 /** @default true */
283 tags?: boolean;
284 };
285 }
286>;
287
288/**
289 * moves elements attributes to the existing group wrapper
290 */
291export type MoveElemsAttrsToGroupPlugin = DefaultPlugin<'moveElemsAttrsToGroup'>;
292
293/**
294 * moves some group attributes to the content elements
295 */
296export type MoveGroupAttrsToElemsPlugin = DefaultPlugin<'moveGroupAttrsToElems'>;
297
298/**
299 * undefined
300 */
301export type PluginsPlugin = DefaultPlugin<'plugins'>;
302
303/**
304 * prefix IDs
305 */
306export type PrefixIdsPlugin = DefaultPlugin<
307 'prefixIds',
308 {
309 /** @default '__' */
310 delim?: string;
311 /** @default true */
312 prefixIds?: boolean;
313 /** @default true */
314 prefixClassNames?: boolean;
315 }
316>;
317
318/**
319 * removes attributes of elements that match a css selector
320 */
321export type RemoveAttributesBySelectorPlugin = DefaultPlugin<'removeAttributesBySelector'>;
322
323/**
324 * removes specified attributes
325 */
326export type RemoveAttrsPlugin = DefaultPlugin<
327 'removeAttrs',
328 {
329 /** @default ':' */
330 elemSeparator?: string;
331 /** @default false */
332 preserveCurrentColor?: boolean;
333 /** @default [] */
334 attrs?: any[];
335 }
336>;
337
338/**
339 * removes comments
340 */
341export type RemoveCommentsPlugin = DefaultPlugin<'removeComments'>;
342
343/**
344 * removes <desc>
345 */
346export type RemoveDescPlugin = DefaultPlugin<
347 'removeDesc',
348 {
349 /** @default true */
350 removeAny?: boolean;
351 }
352>;
353
354/**
355 * removes width and height in presence of viewBox (opposite to removeViewBox, disable it first)
356 */
357export type RemoveDimensionsPlugin = DefaultPlugin<'removeDimensions'>;
358
359/**
360 * removes doctype declaration
361 */
362export type RemoveDoctypePlugin = DefaultPlugin<'removeDoctype'>;
363
364/**
365 * removes editors namespaces, elements and attributes
366 */
367export type RemoveEditorsNSDataPlugin = DefaultPlugin<
368 'removeEditorsNSData',
369 {
370 /** @default [] */
371 additionalNamespaces?: any[];
372 }
373>;
374
375/**
376 * removes arbitrary elements by ID or className (disabled by default)
377 */
378export type RemoveElementsByAttrPlugin = DefaultPlugin<
379 'removeElementsByAttr',
380 {
381 /** @default [] */
382 id?: any[];
383 /** @default [] */
384 class?: any[];
385 }
386>;
387
388/**
389 * removes empty attributes
390 */
391export type RemoveEmptyAttrsPlugin = DefaultPlugin<'removeEmptyAttrs'>;
392
393/**
394 * removes empty container elements
395 */
396export type RemoveEmptyContainersPlugin = DefaultPlugin<'removeEmptyContainers'>;
397
398/**
399 * removes empty <text> elements
400 */
401export type RemoveEmptyTextPlugin = DefaultPlugin<
402 'removeEmptyText',
403 {
404 /** @default true */
405 text?: boolean;
406 /** @default true */
407 tspan?: boolean;
408 /** @default true */
409 tref?: boolean;
410 }
411>;
412
413/**
414 * removes hidden elements (zero sized, with absent attributes)
415 */
416export type RemoveHiddenElemsPlugin = DefaultPlugin<
417 'removeHiddenElems',
418 {
419 /** @default true */
420 isHidden?: boolean;
421 /** @default true */
422 displayNone?: boolean;
423 /** @default true */
424 opacity0?: boolean;
425 /** @default true */
426 circleR0?: boolean;
427 /** @default true */
428 ellipseRX0?: boolean;
429 /** @default true */
430 ellipseRY0?: boolean;
431 /** @default true */
432 rectWidth0?: boolean;
433 /** @default true */
434 rectHeight0?: boolean;
435 /** @default true */
436 patternWidth0?: boolean;
437 /** @default true */
438 patternHeight0?: boolean;
439 /** @default true */
440 imageWidth0?: boolean;
441 /** @default true */
442 imageHeight0?: boolean;
443 /** @default true */
444 pathEmptyD?: boolean;
445 /** @default true */
446 polylineEmptyPoints?: boolean;
447 /** @default true */
448 polygonEmptyPoints?: boolean;
449 }
450>;
451
452/**
453 * removes <metadata>
454 */
455export type RemoveMetadataPlugin = DefaultPlugin<'removeMetadata'>;
456
457/**
458 * removes non-inheritable group’s presentational attributes
459 */
460export type RemoveNonInheritableGroupAttrsPlugin = DefaultPlugin<'removeNonInheritableGroupAttrs'>;
461
462/**
463 * removes elements that are drawn outside of the viewbox (disabled by default)
464 */
465export type RemoveOffCanvasPathsPlugin = DefaultPlugin<'removeOffCanvasPaths'>;
466
467/**
468 * removes raster images (disabled by default)
469 */
470export type RemoveRasterImagesPlugin = DefaultPlugin<'removeRasterImages'>;
471
472/**
473 * removes <script> elements (disabled by default)
474 */
475export type RemoveScriptElementPlugin = DefaultPlugin<'removeScriptElement'>;
476
477/**
478 * removes <style> element (disabled by default)
479 */
480export type RemoveStyleElementPlugin = DefaultPlugin<'removeStyleElement'>;
481
482/**
483 * removes <title>
484 */
485export type RemoveTitlePlugin = DefaultPlugin<'removeTitle'>;
486
487/**
488 * removes unknown elements content and attributes, removes attrs with default values
489 */
490export type RemoveUnknownsAndDefaultsPlugin = DefaultPlugin<
491 'removeUnknownsAndDefaults',
492 {
493 /** @default true */
494 unknownContent?: boolean;
495 /** @default true */
496 unknownAttrs?: boolean;
497 /** @default true */
498 defaultAttrs?: boolean;
499 /** @default true */
500 uselessOverrides?: boolean;
501 /** @default true */
502 keepDataAttrs?: boolean;
503 /** @default true */
504 keepAriaAttrs?: boolean;
505 /** @default false */
506 keepRoleAttr?: boolean;
507 }
508>;
509
510/**
511 * removes unused namespaces declaration
512 */
513export type RemoveUnusedNSPlugin = DefaultPlugin<'removeUnusedNS'>;
514
515/**
516 * removes elements in <defs> without id
517 */
518export type RemoveUselessDefsPlugin = DefaultPlugin<'removeUselessDefs'>;
519
520/**
521 * removes useless stroke and fill attributes
522 */
523export type RemoveUselessStrokeAndFillPlugin = DefaultPlugin<
524 'removeUselessStrokeAndFill',
525 {
526 /** @default true */
527 stroke?: boolean;
528 /** @default true */
529 fill?: boolean;
530 /** @default false */
531 removeNone?: boolean;
532 /** @default false */
533 hasStyleOrScript?: boolean;
534 }
535>;
536
537/**
538 * removes viewBox attribute when possible
539 */
540export type RemoveViewBoxPlugin = DefaultPlugin<'removeViewBox'>;
541
542/**
543 * removes xmlns attribute (for inline svg, disabled by default)
544 */
545export type RemoveXMLNSPlugin = DefaultPlugin<'removeXMLNS'>;
546
547/**
548 * removes XML processing instructions
549 */
550export type RemoveXMLProcInstPlugin = DefaultPlugin<'removeXMLProcInst'>;
551
552/**
553 * Finds <path> elements with the same d, fill, and stroke, and converts them to <use> elements referencing a single <path> def.
554 */
555export type ReusePathsPlugin = DefaultPlugin<'reusePaths'>;
556
557/**
558 * sorts element attributes (disabled by default)
559 */
560export type SortAttrsPlugin = DefaultPlugin<
561 'sortAttrs',
562 {
563 /**
564 * @default ['id', 'width', 'height', 'x', 'x1', 'x2', 'y', 'y1', 'y2', 'cx', 'cy', 'r', 'fill', 'stroke', 'marker', 'd', 'points']
565 */
566 order?: string[];
567 }
568>;
569
570/**
571 * Sorts children of <defs> to improve compression
572 */
573export type SortDefsChildrenPlugin = DefaultPlugin<'sortDefsChildren'>;
574
575export type DefaultPlugins =
576 | AddAttributesToSVGElementPlugin
577 | AddClassesToSVGElementPlugin
578 | CleanupAttrsPlugin
579 | CleanupEnableBackgroundPlugin
580 | CleanupIDsPlugin
581 | CleanupListOfValuesPlugin
582 | CleanupNumericValuesPlugin
583 | CollapseGroupsPlugin
584 | ConvertColorsPlugin
585 | ConvertEllipseToCirclePlugin
586 | ConvertPathDataPlugin
587 | ConvertShapeToPathPlugin
588 | ConvertStyleToAttrsPlugin
589 | ConvertTransformPlugin
590 | InlineStylesPlugin
591 | MergePathsPlugin
592 | MergeStylesPlugin
593 | MinifyStylesPlugin
594 | MoveElemsAttrsToGroupPlugin
595 | MoveGroupAttrsToElemsPlugin
596 | PrefixIdsPlugin
597 | RemoveAttributesBySelectorPlugin
598 | RemoveAttrsPlugin
599 | RemoveAttrsPlugin
600 | RemoveCommentsPlugin
601 | RemoveDescPlugin
602 | RemoveDimensionsPlugin
603 | RemoveDoctypePlugin
604 | RemoveEditorsNSDataPlugin
605 | RemoveElementsByAttrPlugin
606 | RemoveEmptyAttrsPlugin
607 | RemoveEmptyContainersPlugin
608 | RemoveEmptyTextPlugin
609 | RemoveHiddenElemsPlugin
610 | RemoveMetadataPlugin
611 | RemoveNonInheritableGroupAttrsPlugin
612 | RemoveOffCanvasPathsPlugin
613 | RemoveRasterImagesPlugin
614 | RemoveScriptElementPlugin
615 | RemoveStyleElementPlugin
616 | RemoveTitlePlugin
617 | RemoveUnknownsAndDefaultsPlugin
618 | RemoveUnusedNSPlugin
619 | RemoveUselessDefsPlugin
620 | RemoveUselessStrokeAndFillPlugin
621 | RemoveViewBoxPlugin
622 | RemoveXMLNSPlugin
623 | RemoveXMLProcInstPlugin
624 | ReusePathsPlugin
625 | SortAttrsPlugin
626 | SortDefsChildrenPlugin;
627
628export interface CustomPlugin<P extends object = never> {
629 name: string;
630 type: 'perItem' | 'perItemReverse' | 'full';
631 params?: P;
632 fn: (ast: any, params: P, info: any) => any;
633}
634
635export interface OptimizedSvg {
636 data: string;
637 info: {
638 width: string;
639 height: string;
640 };
641 path?: string;
642}
643
644export type Plugin = DefaultPlugins | DefaultPlugins['name'] | CustomPlugin;
645
646export interface Js2SvgOptions {
647 /** @default '<!DOCTYPE' */
648 doctypeStart?: string;
649 /** @default '>' */
650 doctypeEnd?: string;
651 /** @default '<?' */
652 procInstStart?: string;
653 /** @default '?>' */
654 procInstEnd?: string;
655 /** @default '<' */
656 tagOpenStart?: string;
657 /** @default '>' */
658 tagOpenEnd?: string;
659 /** @default '</' */
660 tagCloseStart?: string;
661 /** @default '>' */
662 tagCloseEnd?: string;
663 /** @default '<' */
664 tagShortStart?: string;
665 /** @default '/>' */
666 tagShortEnd?: string;
667 /** @default '="' */
668 attrStart?: string;
669 /** @default '"' */
670 attrEnd?: string;
671 /** @default '<!--' */
672 commentStart?: string;
673 /** @default '-->' */
674 commentEnd?: string;
675 /** @default '<![CDATA[' */
676 cdataStart?: string;
677 /** @default ']]>' */
678 cdataEnd?: string;
679 /** @default '' */
680 textStart?: string;
681 /** @default '' */
682 textEnd?: string;
683 /** @default 4 */
684 indent?: number;
685 /** @default /[&'"<>]/g */
686 regEntities?: RegExp;
687 /** @default /[&"<>]/g */
688 regValEntities?: RegExp;
689 /** @default encodeEntity */
690 encodeEntity?: (char?: string) => string;
691 /** @default false */
692 pretty?: boolean;
693 /** @default true */
694 useShortTags?: boolean;
695}
696
697export interface Svg2JsOptions {
698 /** @default true */
699 strict?: boolean;
700 /** @default false */
701 trim?: boolean;
702 /** @default true */
703 normalize?: boolean;
704 /** @default true */
705 lowercase?: boolean;
706 /** @default true */
707 xmlns?: boolean;
708 /** @default true */
709 position?: boolean;
710}
711
712/**
713 * If plugins field is specified default list is fully overrided. To extend default list use extendDefaultPlugins
714 * utility.
715 *
716 * To disable one of default plugins use active field.
717 */
718export function extendDefaultPlugins(plugins: Plugin[]): Plugin[];
719
720export interface OptimizeOptions {
721 /** Output as Data URI string. */
722 datauri?: 'base64' | 'enc' | 'unenc';
723
724 /** Precision of floating point numbers. Will be passed to each plugin that suppors this param. */
725 floatPrecision?: number;
726
727 /** Use full set of plugins. */
728 full?: boolean;
729
730 path?: string;
731
732 /** Pass over SVGs multiple times to ensure all optimizations are applied */
733 multipass?: boolean;
734
735 /** Options for rendering optimized SVG from AST. */
736 js2svg?: Js2SvgOptions;
737
738 /**
739 * Individual plugin configurations.
740 * For specific options, see plugin source in https://github.com/svg/svgo/tree/master/plugins.
741 */
742 plugins?: Plugin[];
743
744 /** Options for parsing original SVG into AST. */
745 svg2js?: Svg2JsOptions;
746}
747
748/* The core of SVGO is optimize function. */
749export function optimize(svgString: string, options?: OptimizeOptions): OptimizedSvg;
750
751/**
752 * If you write a tool on top of svgo you might need a way to load svgo config.
753 *
754 * You can also specify relative or absolute path and customize current working directory.
755 */
756export function loadConfig(configFile: string, cwd?: string): Promise<OptimizeOptions>;