|
| 1 | +import type { StringifyOptions, DataUri, Plugin as PluginFn } from './types'; |
| 2 | + |
| 3 | +type PluginDef = { |
| 4 | + name: string; |
| 5 | + fn: PluginFn<unknown>; |
| 6 | +}; |
| 7 | + |
| 8 | +type Usage<T extends PluginDef> = { |
| 9 | + name: T['name']; |
| 10 | + params?: Parameters<T['fn']>[1]; |
| 11 | +}; |
| 12 | + |
| 13 | +type UsageReqParams<T extends PluginDef> = { |
| 14 | + name: T['name']; |
| 15 | + params: Parameters<T['fn']>[1]; |
| 16 | +}; |
| 17 | + |
| 18 | +type CustomPlugin = { |
| 19 | + name: string; |
| 20 | + fn: PluginFn<void>; |
| 21 | +}; |
| 22 | + |
| 23 | +type DefaultPlugin = |
| 24 | + | Usage<typeof import('../plugins/cleanupAttrs.js')> |
| 25 | + | Usage<typeof import('../plugins/cleanupEnableBackground.js')> |
| 26 | + | Usage<typeof import('../plugins/cleanupIDs.js')> |
| 27 | + | Usage<typeof import('../plugins/cleanupNumericValues.js')> |
| 28 | + | Usage<typeof import('../plugins/collapseGroups.js')> |
| 29 | + | Usage<typeof import('../plugins/convertColors.js')> |
| 30 | + | Usage<typeof import('../plugins/convertEllipseToCircle.js')> |
| 31 | + | Usage<typeof import('../plugins/convertPathData.js')> |
| 32 | + | Usage<typeof import('../plugins/convertShapeToPath.js')> |
| 33 | + | Usage<typeof import('../plugins/convertTransform.js')> |
| 34 | + | Usage<typeof import('../plugins/mergeStyles.js')> |
| 35 | + | Usage<typeof import('../plugins/inlineStyles.js')> |
| 36 | + | Usage<typeof import('../plugins/mergePaths.js')> |
| 37 | + | Usage<typeof import('../plugins/minifyStyles.js')> |
| 38 | + | Usage<typeof import('../plugins/moveElemsAttrsToGroup.js')> |
| 39 | + | Usage<typeof import('../plugins/moveGroupAttrsToElems.js')> |
| 40 | + | Usage<typeof import('../plugins/removeComments.js')> |
| 41 | + | Usage<typeof import('../plugins/removeDesc.js')> |
| 42 | + | Usage<typeof import('../plugins/removeDoctype.js')> |
| 43 | + | Usage<typeof import('../plugins/removeEditorsNSData.js')> |
| 44 | + | Usage<typeof import('../plugins/removeEmptyAttrs.js')> |
| 45 | + | Usage<typeof import('../plugins/removeEmptyContainers.js')> |
| 46 | + | Usage<typeof import('../plugins/removeEmptyText.js')> |
| 47 | + | Usage<typeof import('../plugins/removeHiddenElems.js')> |
| 48 | + | Usage<typeof import('../plugins/removeMetadata.js')> |
| 49 | + | Usage<typeof import('../plugins/removeNonInheritableGroupAttrs.js')> |
| 50 | + | Usage<typeof import('../plugins/removeTitle.js')> |
| 51 | + | Usage<typeof import('../plugins/removeUnknownsAndDefaults.js')> |
| 52 | + | Usage<typeof import('../plugins/removeUnusedNS.js')> |
| 53 | + | Usage<typeof import('../plugins/removeUselessDefs.js')> |
| 54 | + | Usage<typeof import('../plugins/removeUselessStrokeAndFill.js')> |
| 55 | + | Usage<typeof import('../plugins/removeViewBox.js')> |
| 56 | + | Usage<typeof import('../plugins/removeXMLProcInst.js')> |
| 57 | + | Usage<typeof import('../plugins/sortAttrs.js')> |
| 58 | + | Usage<typeof import('../plugins/sortDefsChildren.js')>; |
| 59 | + |
| 60 | +type Overrides<T = DefaultPlugin> = T extends DefaultPlugin |
| 61 | + ? { [key in T['name']]?: T['params'] | false } |
| 62 | + : never; |
| 63 | + |
| 64 | +type PresetDefault = { |
| 65 | + name: 'preset-default'; |
| 66 | + params?: { |
| 67 | + floatPrecision?: number; |
| 68 | + /** |
| 69 | + * All default plugins can be customized or disabled here |
| 70 | + * for example |
| 71 | + * { |
| 72 | + * sortAttrs: { xmlnsOrder: "alphabetical" }, |
| 73 | + * cleanupAttrs: false, |
| 74 | + * } |
| 75 | + */ |
| 76 | + overrides?: Overrides; |
| 77 | + }; |
| 78 | +}; |
| 79 | + |
| 80 | +type BuiltinPluginWithOptionalParams = |
| 81 | + | DefaultPlugin |
| 82 | + | PresetDefault |
| 83 | + | Usage<typeof import('../plugins/cleanupListOfValues.js')> |
| 84 | + | Usage<typeof import('../plugins/convertStyleToAttrs.js')> |
| 85 | + | Usage<typeof import('../plugins/prefixIds.js')> |
| 86 | + | Usage<typeof import('../plugins/removeDimensions.js')> |
| 87 | + | Usage<typeof import('../plugins/removeOffCanvasPaths.js')> |
| 88 | + | Usage<typeof import('../plugins/removeRasterImages.js')> |
| 89 | + | Usage<typeof import('../plugins/removeScriptElement.js')> |
| 90 | + | Usage<typeof import('../plugins/removeStyleElement.js')> |
| 91 | + | Usage<typeof import('../plugins/removeXMLNS.js')> |
| 92 | + | Usage<typeof import('../plugins/reusePaths.js')>; |
| 93 | + |
| 94 | +type BuiltinPluginWithRequiredParams = |
| 95 | + | UsageReqParams<typeof import('../plugins/addAttributesToSVGElement.js')> |
| 96 | + | UsageReqParams<typeof import('../plugins/addClassesToSVGElement.js')> |
| 97 | + | UsageReqParams<typeof import('../plugins/removeAttributesBySelector.js')> |
| 98 | + | UsageReqParams<typeof import('../plugins/removeAttrs.js')> |
| 99 | + | UsageReqParams<typeof import('../plugins/removeElementsByAttr.js')>; |
| 100 | + |
| 101 | +type PluginConfig = |
| 102 | + | BuiltinPluginWithOptionalParams['name'] |
| 103 | + | BuiltinPluginWithOptionalParams |
| 104 | + | BuiltinPluginWithRequiredParams |
| 105 | + | CustomPlugin; |
| 106 | + |
| 107 | +export type Config = { |
| 108 | + /** Can be used by plugins, for example prefixids */ |
| 109 | + path?: string; |
| 110 | + /** Pass over SVGs multiple times to ensure all optimizations are applied. */ |
| 111 | + multipass?: boolean; |
| 112 | + /** Precision of floating point numbers. Will be passed to each plugin that suppors this param. */ |
| 113 | + floatPrecision?: number; |
| 114 | + /** |
| 115 | + * Plugins configuration |
| 116 | + * ['preset-default'] is default |
| 117 | + * Can also specify any builtin plugin |
| 118 | + * ['sortAttrs', { name: 'prefixIds', params: { prefix: 'my-prefix' } }] |
| 119 | + * Or custom |
| 120 | + * [{ name: 'myPlugin', fn: () => ({}) }] |
| 121 | + */ |
| 122 | + plugins?: PluginConfig[]; |
| 123 | + /** Options for rendering optimized SVG from AST. */ |
| 124 | + js2svg?: StringifyOptions; |
| 125 | + /** Output as Data URI string. */ |
| 126 | + datauri?: DataUri; |
| 127 | +}; |
| 128 | + |
| 129 | +type Output = { |
| 130 | + data: string; |
| 131 | +}; |
| 132 | + |
| 133 | +/** The core of SVGO */ |
| 134 | +export declare function optimize(input: string, config?: Config): Output; |
| 135 | + |
| 136 | +/** |
| 137 | + * If you write a tool on top of svgo you might need a way to load svgo config. |
| 138 | + * |
| 139 | + * You can also specify relative or absolute path and customize current working directory. |
| 140 | + */ |
| 141 | +export declare function loadConfig( |
| 142 | + configFile: string, |
| 143 | + cwd?: string |
| 144 | +): Promise<Config>; |
| 145 | +export declare function loadConfig(): Promise<Config | null>; |
0 commit comments