mirror of
https://github.com/svg/svgo.git
synced 2025-07-28 09:22:00 +03:00
feat: expose builtin plugins (#2027)
This commit is contained in:
4
lib/svgo-node.d.ts
vendored
4
lib/svgo-node.d.ts
vendored
@ -1,6 +1,6 @@
|
|||||||
import { VERSION, Config, optimize } from './svgo';
|
import { VERSION, Config, optimize, builtinPlugins } from './svgo';
|
||||||
|
|
||||||
export { VERSION, optimize };
|
export { VERSION, optimize, builtinPlugins };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If you write a tool on top of svgo you might need a way to load svgo config.
|
* If you write a tool on top of svgo you might need a way to load svgo config.
|
||||||
|
@ -2,7 +2,11 @@ import os from 'os';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { pathToFileURL } from 'url';
|
import { pathToFileURL } from 'url';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { VERSION, optimize as optimizeAgnostic } from './svgo.js';
|
import {
|
||||||
|
VERSION,
|
||||||
|
optimize as optimizeAgnostic,
|
||||||
|
builtinPlugins,
|
||||||
|
} from './svgo.js';
|
||||||
|
|
||||||
const importConfig = async (configFile) => {
|
const importConfig = async (configFile) => {
|
||||||
// dynamic import expects file url instead of path and may fail
|
// dynamic import expects file url instead of path and may fail
|
||||||
@ -25,7 +29,7 @@ const isFile = async (file) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { VERSION };
|
export { VERSION, builtinPlugins };
|
||||||
|
|
||||||
export const loadConfig = async (configFile, cwd = process.cwd()) => {
|
export const loadConfig = async (configFile, cwd = process.cwd()) => {
|
||||||
if (configFile != null) {
|
if (configFile != null) {
|
||||||
@ -77,6 +81,7 @@ export const optimize = (input, config) => {
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
VERSION,
|
VERSION,
|
||||||
|
builtinPlugins,
|
||||||
loadConfig,
|
loadConfig,
|
||||||
optimize,
|
optimize,
|
||||||
};
|
};
|
||||||
|
26
lib/svgo.d.ts
vendored
26
lib/svgo.d.ts
vendored
@ -2,6 +2,7 @@ import type { StringifyOptions, DataUri, Plugin } from './types.js';
|
|||||||
import type {
|
import type {
|
||||||
BuiltinsWithOptionalParams,
|
BuiltinsWithOptionalParams,
|
||||||
BuiltinsWithRequiredParams,
|
BuiltinsWithRequiredParams,
|
||||||
|
PluginsParams,
|
||||||
} from '../plugins/plugins-types.js';
|
} from '../plugins/plugins-types.js';
|
||||||
|
|
||||||
type CustomPlugin<T = any> = {
|
type CustomPlugin<T = any> = {
|
||||||
@ -26,6 +27,31 @@ type PluginConfig =
|
|||||||
}[keyof BuiltinsWithRequiredParams]
|
}[keyof BuiltinsWithRequiredParams]
|
||||||
| CustomPlugin;
|
| CustomPlugin;
|
||||||
|
|
||||||
|
type BuiltinPlugin<Name, Params> = {
|
||||||
|
/** Name of the plugin, also known as the plugin ID. */
|
||||||
|
name: Name;
|
||||||
|
description?: string;
|
||||||
|
fn: Plugin<Params>;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plugins that are bundled with SVGO. This includes plugin presets, and plugins
|
||||||
|
* that are not enabled by default.
|
||||||
|
*/
|
||||||
|
export declare const builtinPlugins: Array<
|
||||||
|
{
|
||||||
|
[Name in keyof PluginsParams]: BuiltinPlugin<Name, PluginsParams[Name]> & {
|
||||||
|
/** If the plugin is itself a preset that invokes other plugins. */
|
||||||
|
isPreset: true | undefined;
|
||||||
|
/**
|
||||||
|
* If the plugin is a preset that invokes other plugins, this returns an
|
||||||
|
* array of the plugins in the preset in the order that they are invoked.
|
||||||
|
*/
|
||||||
|
plugins?: BuiltinPlugin<unknown, unknown>[];
|
||||||
|
};
|
||||||
|
}[keyof PluginsParams]
|
||||||
|
>;
|
||||||
|
|
||||||
export type Config = {
|
export type Config = {
|
||||||
/** Can be used by plugins, for example prefixids */
|
/** Can be used by plugins, for example prefixids */
|
||||||
path?: string;
|
path?: string;
|
||||||
|
@ -46,7 +46,7 @@ const resolvePluginConfig = (plugin) => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { VERSION };
|
export { VERSION, builtin as builtinPlugins };
|
||||||
|
|
||||||
export const optimize = (input, config) => {
|
export const optimize = (input, config) => {
|
||||||
if (config == null) {
|
if (config == null) {
|
||||||
@ -104,4 +104,5 @@ export const optimize = (input, config) => {
|
|||||||
export default {
|
export default {
|
||||||
VERSION,
|
VERSION,
|
||||||
optimize,
|
optimize,
|
||||||
|
builtinPlugins: builtin,
|
||||||
};
|
};
|
||||||
|
@ -34,6 +34,8 @@ export const invokePlugins = (
|
|||||||
export const createPreset = ({ name, plugins }) => {
|
export const createPreset = ({ name, plugins }) => {
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
|
isPreset: true,
|
||||||
|
plugins,
|
||||||
fn: (ast, params, info) => {
|
fn: (ast, params, info) => {
|
||||||
const { floatPrecision, overrides } = params;
|
const { floatPrecision, overrides } = params;
|
||||||
const globalOverrides = {};
|
const globalOverrides = {};
|
||||||
|
3
plugins/plugins-types.d.ts
vendored
3
plugins/plugins-types.d.ts
vendored
@ -290,7 +290,8 @@ export type BuiltinsWithRequiredParams = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
type PluginsParams = BuiltinsWithOptionalParams & BuiltinsWithRequiredParams;
|
export type PluginsParams = BuiltinsWithOptionalParams &
|
||||||
|
BuiltinsWithRequiredParams;
|
||||||
|
|
||||||
export type Plugin<Name extends keyof PluginsParams> = PluginDef<
|
export type Plugin<Name extends keyof PluginsParams> = PluginDef<
|
||||||
PluginsParams[Name]
|
PluginsParams[Name]
|
||||||
|
@ -30,12 +30,13 @@ const expected = `<svg xmlns="http://www.w3.org/2000/svg">
|
|||||||
|
|
||||||
const content = `
|
const content = `
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import { VERSION, optimize } from '/svgo.browser.js';
|
import { VERSION, optimize, builtinPlugins } from '/svgo.browser.js';
|
||||||
const result = optimize(${JSON.stringify(fixture)}, {
|
const result = optimize(${JSON.stringify(fixture)}, {
|
||||||
plugins : [],
|
plugins : [],
|
||||||
js2svg : { pretty: true, indent: 2 }
|
js2svg : { pretty: true, indent: 2 }
|
||||||
});
|
});
|
||||||
globalThis.version = VERSION;
|
globalThis.version = VERSION;
|
||||||
|
globalThis.builtinPlugins = builtinPlugins;
|
||||||
globalThis.result = result.data;
|
globalThis.result = result.data;
|
||||||
</script>
|
</script>
|
||||||
`;
|
`;
|
||||||
@ -60,10 +61,12 @@ const runTest = async () => {
|
|||||||
|
|
||||||
const actual = await page.evaluate(() => ({
|
const actual = await page.evaluate(() => ({
|
||||||
version: globalThis.version,
|
version: globalThis.version,
|
||||||
|
builtinPlugins: globalThis.builtinPlugins,
|
||||||
result: globalThis.result,
|
result: globalThis.result,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
assert.strictEqual(actual.version, version);
|
assert.strictEqual(actual.version, version);
|
||||||
|
assert.notEqual(actual.builtinPlugins, undefined);
|
||||||
assert.equal(actual.result, expected);
|
assert.equal(actual.result, expected);
|
||||||
|
|
||||||
await browser.close();
|
await browser.close();
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const { VERSION, optimize, loadConfig } = require('../dist/svgo-node.cjs');
|
const {
|
||||||
|
VERSION,
|
||||||
|
optimize,
|
||||||
|
builtinPlugins,
|
||||||
|
loadConfig,
|
||||||
|
} = require('../dist/svgo-node.cjs');
|
||||||
const PKG = require('../package.json');
|
const PKG = require('../package.json');
|
||||||
|
|
||||||
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
|
const fixture = `<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
@ -30,6 +35,7 @@ const runTest = () => {
|
|||||||
|
|
||||||
assert.strictEqual(VERSION, PKG.version);
|
assert.strictEqual(VERSION, PKG.version);
|
||||||
assert.equal(actual, expected);
|
assert.equal(actual, expected);
|
||||||
|
assert.notEqual(builtinPlugins, undefined);
|
||||||
assert.notEqual(loadConfig, undefined);
|
assert.notEqual(loadConfig, undefined);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import fs from 'node:fs/promises';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { EOL } from 'os';
|
import { EOL } from 'os';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import { VERSION, optimize } from '../../lib/svgo.js';
|
import { VERSION, optimize, builtinPlugins } from '../../lib/svgo.js';
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
@ -25,6 +25,12 @@ describe('svgo', () => {
|
|||||||
expect(VERSION).toStrictEqual(version);
|
expect(VERSION).toStrictEqual(version);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should have all exported members', async () => {
|
||||||
|
expect(VERSION).toBeDefined();
|
||||||
|
expect(optimize).toBeDefined();
|
||||||
|
expect(builtinPlugins).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
it('should create indent with 2 spaces', async () => {
|
it('should create indent with 2 spaces', async () => {
|
||||||
const [original, expected] = await parseFixture('test.svg.txt');
|
const [original, expected] = await parseFixture('test.svg.txt');
|
||||||
const result = optimize(original, {
|
const result = optimize(original, {
|
||||||
|
Reference in New Issue
Block a user