Skip to content

Commit 0c13740

Browse files
authored
feat: expose builtin plugins (#2027)
1 parent 1bb5bbe commit 0c13740

File tree

9 files changed

+59
-9
lines changed

9 files changed

+59
-9
lines changed

lib/svgo-node.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { VERSION, Config, optimize } from './svgo';
1+
import { VERSION, Config, optimize, builtinPlugins } from './svgo';
22

3-
export { VERSION, optimize };
3+
export { VERSION, optimize, builtinPlugins };
44

55
/**
66
* If you write a tool on top of svgo you might need a way to load svgo config.

lib/svgo-node.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import os from 'os';
22
import fs from 'fs';
33
import { pathToFileURL } from 'url';
44
import path from 'path';
5-
import { VERSION, optimize as optimizeAgnostic } from './svgo.js';
5+
import {
6+
VERSION,
7+
optimize as optimizeAgnostic,
8+
builtinPlugins,
9+
} from './svgo.js';
610

711
const importConfig = async (configFile) => {
812
// dynamic import expects file url instead of path and may fail
@@ -25,7 +29,7 @@ const isFile = async (file) => {
2529
}
2630
};
2731

28-
export { VERSION };
32+
export { VERSION, builtinPlugins };
2933

3034
export const loadConfig = async (configFile, cwd = process.cwd()) => {
3135
if (configFile != null) {
@@ -77,6 +81,7 @@ export const optimize = (input, config) => {
7781

7882
export default {
7983
VERSION,
84+
builtinPlugins,
8085
loadConfig,
8186
optimize,
8287
};

lib/svgo.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { StringifyOptions, DataUri, Plugin } from './types.js';
22
import type {
33
BuiltinsWithOptionalParams,
44
BuiltinsWithRequiredParams,
5+
PluginsParams,
56
} from '../plugins/plugins-types.js';
67

78
type CustomPlugin<T = any> = {
@@ -26,6 +27,31 @@ type PluginConfig =
2627
}[keyof BuiltinsWithRequiredParams]
2728
| CustomPlugin;
2829

30+
type BuiltinPlugin<Name, Params> = {
31+
/** Name of the plugin, also known as the plugin ID. */
32+
name: Name;
33+
description?: string;
34+
fn: Plugin<Params>;
35+
};
36+
37+
/**
38+
* Plugins that are bundled with SVGO. This includes plugin presets, and plugins
39+
* that are not enabled by default.
40+
*/
41+
export declare const builtinPlugins: Array<
42+
{
43+
[Name in keyof PluginsParams]: BuiltinPlugin<Name, PluginsParams[Name]> & {
44+
/** If the plugin is itself a preset that invokes other plugins. */
45+
isPreset: true | undefined;
46+
/**
47+
* If the plugin is a preset that invokes other plugins, this returns an
48+
* array of the plugins in the preset in the order that they are invoked.
49+
*/
50+
plugins?: BuiltinPlugin<unknown, unknown>[];
51+
};
52+
}[keyof PluginsParams]
53+
>;
54+
2955
export type Config = {
3056
/** Can be used by plugins, for example prefixids */
3157
path?: string;

lib/svgo.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const resolvePluginConfig = (plugin) => {
4646
return null;
4747
};
4848

49-
export { VERSION };
49+
export { VERSION, builtin as builtinPlugins };
5050

5151
export const optimize = (input, config) => {
5252
if (config == null) {
@@ -104,4 +104,5 @@ export const optimize = (input, config) => {
104104
export default {
105105
VERSION,
106106
optimize,
107+
builtinPlugins: builtin,
107108
};

lib/svgo/plugins.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export const invokePlugins = (
3434
export const createPreset = ({ name, plugins }) => {
3535
return {
3636
name,
37+
isPreset: true,
38+
plugins,
3739
fn: (ast, params, info) => {
3840
const { floatPrecision, overrides } = params;
3941
const globalOverrides = {};

plugins/plugins-types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ export type BuiltinsWithRequiredParams = {
290290
};
291291
};
292292

293-
type PluginsParams = BuiltinsWithOptionalParams & BuiltinsWithRequiredParams;
293+
export type PluginsParams = BuiltinsWithOptionalParams &
294+
BuiltinsWithRequiredParams;
294295

295296
export type Plugin<Name extends keyof PluginsParams> = PluginDef<
296297
PluginsParams[Name]

test/browser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ const expected = `<svg xmlns="http://www.w3.org/2000/svg">
3030

3131
const content = `
3232
<script type="module">
33-
import { VERSION, optimize } from '/svgo.browser.js';
33+
import { VERSION, optimize, builtinPlugins } from '/svgo.browser.js';
3434
const result = optimize(${JSON.stringify(fixture)}, {
3535
plugins : [],
3636
js2svg : { pretty: true, indent: 2 }
3737
});
3838
globalThis.version = VERSION;
39+
globalThis.builtinPlugins = builtinPlugins;
3940
globalThis.result = result.data;
4041
</script>
4142
`;
@@ -60,10 +61,12 @@ const runTest = async () => {
6061

6162
const actual = await page.evaluate(() => ({
6263
version: globalThis.version,
64+
builtinPlugins: globalThis.builtinPlugins,
6365
result: globalThis.result,
6466
}));
6567

6668
assert.strictEqual(actual.version, version);
69+
assert.notEqual(actual.builtinPlugins, undefined);
6770
assert.equal(actual.result, expected);
6871

6972
await browser.close();

test/svgo.cjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const assert = require('assert');
2-
const { VERSION, optimize, loadConfig } = require('../dist/svgo-node.cjs');
2+
const {
3+
VERSION,
4+
optimize,
5+
builtinPlugins,
6+
loadConfig,
7+
} = require('../dist/svgo-node.cjs');
38
const PKG = require('../package.json');
49

510
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
@@ -30,6 +35,7 @@ const runTest = () => {
3035

3136
assert.strictEqual(VERSION, PKG.version);
3237
assert.equal(actual, expected);
38+
assert.notEqual(builtinPlugins, undefined);
3339
assert.notEqual(loadConfig, undefined);
3440
};
3541

test/svgo/_index.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs/promises';
22
import path from 'path';
33
import { EOL } from 'os';
44
import { fileURLToPath } from 'url';
5-
import { VERSION, optimize } from '../../lib/svgo.js';
5+
import { VERSION, optimize, builtinPlugins } from '../../lib/svgo.js';
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url));
88

@@ -25,6 +25,12 @@ describe('svgo', () => {
2525
expect(VERSION).toStrictEqual(version);
2626
});
2727

28+
it('should have all exported members', async () => {
29+
expect(VERSION).toBeDefined();
30+
expect(optimize).toBeDefined();
31+
expect(builtinPlugins).toBeDefined();
32+
});
33+
2834
it('should create indent with 2 spaces', async () => {
2935
const [original, expected] = await parseFixture('test.svg.txt');
3036
const result = optimize(original, {

0 commit comments

Comments
 (0)