Skip to content

Commit

Permalink
Expose typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Oct 16, 2022
1 parent f5d1dd0 commit 53ed57c
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
145 changes: 145 additions & 0 deletions lib/svgo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import type { StringifyOptions, DataUri, Plugin as PluginFn } from './types';

type PluginDef = {
name: string;
fn: PluginFn<unknown>;
};

type Usage<T extends PluginDef> = {
name: T['name'];
params?: Parameters<T['fn']>[1];
};

type UsageReqParams<T extends PluginDef> = {
name: T['name'];
params: Parameters<T['fn']>[1];
};

type CustomPlugin = {
name: string;
fn: PluginFn<void>;
};

type DefaultPlugin =
| Usage<typeof import('../plugins/cleanupAttrs.js')>
| Usage<typeof import('../plugins/cleanupEnableBackground.js')>
| Usage<typeof import('../plugins/cleanupIDs.js')>
| Usage<typeof import('../plugins/cleanupNumericValues.js')>
| Usage<typeof import('../plugins/collapseGroups.js')>
| Usage<typeof import('../plugins/convertColors.js')>
| Usage<typeof import('../plugins/convertEllipseToCircle.js')>
| Usage<typeof import('../plugins/convertPathData.js')>
| Usage<typeof import('../plugins/convertShapeToPath.js')>
| Usage<typeof import('../plugins/convertTransform.js')>
| Usage<typeof import('../plugins/mergeStyles.js')>
| Usage<typeof import('../plugins/inlineStyles.js')>
| Usage<typeof import('../plugins/mergePaths.js')>
| Usage<typeof import('../plugins/minifyStyles.js')>
| Usage<typeof import('../plugins/moveElemsAttrsToGroup.js')>
| Usage<typeof import('../plugins/moveGroupAttrsToElems.js')>
| Usage<typeof import('../plugins/removeComments.js')>
| Usage<typeof import('../plugins/removeDesc.js')>
| Usage<typeof import('../plugins/removeDoctype.js')>
| Usage<typeof import('../plugins/removeEditorsNSData.js')>
| Usage<typeof import('../plugins/removeEmptyAttrs.js')>
| Usage<typeof import('../plugins/removeEmptyContainers.js')>
| Usage<typeof import('../plugins/removeEmptyText.js')>
| Usage<typeof import('../plugins/removeHiddenElems.js')>
| Usage<typeof import('../plugins/removeMetadata.js')>
| Usage<typeof import('../plugins/removeNonInheritableGroupAttrs.js')>
| Usage<typeof import('../plugins/removeTitle.js')>
| Usage<typeof import('../plugins/removeUnknownsAndDefaults.js')>
| Usage<typeof import('../plugins/removeUnusedNS.js')>
| Usage<typeof import('../plugins/removeUselessDefs.js')>
| Usage<typeof import('../plugins/removeUselessStrokeAndFill.js')>
| Usage<typeof import('../plugins/removeViewBox.js')>
| Usage<typeof import('../plugins/removeXMLProcInst.js')>
| Usage<typeof import('../plugins/sortAttrs.js')>
| Usage<typeof import('../plugins/sortDefsChildren.js')>;

type Overrides<T = DefaultPlugin> = T extends DefaultPlugin
? { [key in T['name']]?: T['params'] | false }
: never;

type PresetDefault = {
name: 'preset-default';
params?: {
floatPrecision?: number;
/**
* All default plugins can be customized or disabled here
* for example
* {
* sortAttrs: { xmlnsOrder: "alphabetical" },
* cleanupAttrs: false,
* }
*/
overrides?: Overrides;
};
};

type BuiltinPluginWithOptionalParams =
| DefaultPlugin
| PresetDefault
| Usage<typeof import('../plugins/cleanupListOfValues.js')>
| Usage<typeof import('../plugins/convertStyleToAttrs.js')>
| Usage<typeof import('../plugins/prefixIds.js')>
| Usage<typeof import('../plugins/removeDimensions.js')>
| Usage<typeof import('../plugins/removeOffCanvasPaths.js')>
| Usage<typeof import('../plugins/removeRasterImages.js')>
| Usage<typeof import('../plugins/removeScriptElement.js')>
| Usage<typeof import('../plugins/removeStyleElement.js')>
| Usage<typeof import('../plugins/removeXMLNS.js')>
| Usage<typeof import('../plugins/reusePaths.js')>;

type BuiltinPluginWithRequiredParams =
| UsageReqParams<typeof import('../plugins/addAttributesToSVGElement.js')>
| UsageReqParams<typeof import('../plugins/addClassesToSVGElement.js')>
| UsageReqParams<typeof import('../plugins/removeAttributesBySelector.js')>
| UsageReqParams<typeof import('../plugins/removeAttrs.js')>
| UsageReqParams<typeof import('../plugins/removeElementsByAttr.js')>;

type PluginConfig =
| BuiltinPluginWithOptionalParams['name']
| BuiltinPluginWithOptionalParams
| BuiltinPluginWithRequiredParams
| CustomPlugin;

export type Config = {
/** Can be used by plugins, for example prefixids */
path?: string;
/** Pass over SVGs multiple times to ensure all optimizations are applied. */
multipass?: boolean;
/** Precision of floating point numbers. Will be passed to each plugin that suppors this param. */
floatPrecision?: number;
/**
* Plugins configuration
* ['preset-default'] is default
* Can also specify any builtin plugin
* ['sortAttrs', { name: 'prefixIds', params: { prefix: 'my-prefix' } }]
* Or custom
* [{ name: 'myPlugin', fn: () => ({}) }]
*/
plugins?: PluginConfig[];
/** Options for rendering optimized SVG from AST. */
js2svg?: StringifyOptions;
/** Output as Data URI string. */
datauri?: DataUri;
};

type Output = {
data: string;
};

/** The core of SVGO */
export declare function optimize(input: string, config?: Config): Output;

/**
* If you write a tool on top of svgo you might need a way to load svgo config.
*
* You can also specify relative or absolute path and customize current working directory.
*/
export declare function loadConfig(
configFile: string,
cwd?: string
): Promise<Config>;
export declare function loadConfig(): Promise<Config | null>;
3 changes: 2 additions & 1 deletion lib/svgo/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

/**
* @typedef {import('../types').PathDataCommand} PathDataCommand
* @typedef {import('../types').DataUri} DataUri
*/

/**
* Encode plain SVG data string into Data URI string.
*
* @type {(str: string, type?: 'base64' | 'enc' | 'unenc') => string}
* @type {(str: string, type?: DataUri) => string}
*/
exports.encodeSVGDatauri = (str, type) => {
var prefix = 'data:image/svg+xml';
Expand Down
2 changes: 2 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,5 @@ export type PathDataItem = {
command: PathDataCommand;
args: Array<number>;
};

export type DataUri = 'base64' | 'enc' | 'unenc';
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"main": "./lib/svgo-node.js",
"bin": "./bin/svgo",
"types": "./lib/svgo.d.ts",
"files": [
"bin",
"lib",
Expand Down

0 comments on commit 53ed57c

Please sign in to comment.