Skip to content

Commit 53ed57c

Browse files
committed
Expose typescript types
1 parent f5d1dd0 commit 53ed57c

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

lib/svgo.d.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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>;

lib/svgo/tools.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
/**
44
* @typedef {import('../types').PathDataCommand} PathDataCommand
5+
* @typedef {import('../types').DataUri} DataUri
56
*/
67

78
/**
89
* Encode plain SVG data string into Data URI string.
910
*
10-
* @type {(str: string, type?: 'base64' | 'enc' | 'unenc') => string}
11+
* @type {(str: string, type?: DataUri) => string}
1112
*/
1213
exports.encodeSVGDatauri = (str, type) => {
1314
var prefix = 'data:image/svg+xml';

lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,5 @@ export type PathDataItem = {
170170
command: PathDataCommand;
171171
args: Array<number>;
172172
};
173+
174+
export type DataUri = 'base64' | 'enc' | 'unenc';

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
},
4747
"main": "./lib/svgo-node.js",
4848
"bin": "./bin/svgo",
49+
"types": "./lib/svgo.d.ts",
4950
"files": [
5051
"bin",
5152
"lib",

0 commit comments

Comments
 (0)