Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for --disable and --enable flags back #1977

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ import { builtin } from './builtin.js';
import { invokePlugins } from './svgo/plugins.js';
import { encodeSVGDatauri } from './svgo/tools.js';

/**
* @template T {any}
* @typedef {import('./types.js').PluginConfig<T>} PluginConfig<T>
* @typedef {import('./types.js').PresetConfig} PresetConfig
*/

const pluginsMap = {};
for (const plugin of builtin) {
pluginsMap[plugin.name] = plugin;
}

/**
* @template T {any}
* @param {string | PresetConfig| PluginConfig<T>} plugin plugin name or plugin config
* @returns {PresetConfig| PluginConfig<T> | null} plugin config or null if plugin was not found
*/
const resolvePluginConfig = (plugin) => {
if (typeof plugin === 'string') {
// resolve builtin plugin specified as string
Expand Down Expand Up @@ -77,10 +88,33 @@ export const optimize = (input, config) => {
'Warning: plugins list includes null or undefined elements, these will be ignored.',
);
}

const disablePlugins = config.disable;
if (disablePlugins != null && !Array.isArray(disablePlugins)) {
throw Error('malformed config, `disable` property must be an array.');
}

const enablePlugins = config.enable;
if (enablePlugins != null && !Array.isArray(enablePlugins)) {
throw Error('malformed config, `enable` property must be an array.');
}

const globalOverrides = {};
if (config.floatPrecision != null) {
globalOverrides.floatPrecision = config.floatPrecision;
}
const overrides = {};
if (disablePlugins != null) {
for (const plugin of disablePlugins) {
overrides[plugin] = false;
}
}
if (enablePlugins != null) {
for (const plugin of enablePlugins) {
overrides[plugin] = true;
}
}
globalOverrides.overrides = overrides;
invokePlugins(ast, info, resolvedPlugins, null, globalOverrides);
output = stringifySvg(ast, config.js2svg);
if (output.length < prevResultSize) {
Expand Down
13 changes: 13 additions & 0 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export default function makeProgram(program) {
'Only output error messages, not regular status messages',
)
.option('--show-plugins', 'Show available plugins and exit')
.option('--disable <PLUGINS...>', 'Disable selected plugins')
.option(
'--enable <PLUGINS...>',
'Enable selected plugins. Takes precedence over `disable`',
)
// used by picocolors internally
.option('--no-color', 'Output plain text without color')
.action(action);
Expand Down Expand Up @@ -272,6 +277,14 @@ async function action(args, opts, command) {

return processSVGData(config, null, data, output[0]);
}

if (opts.disable) {
config.disable = opts.disable;
}

if (opts.enable) {
config.enable = opts.enable;
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ export type Plugin<Params> = (
info: PluginInfo,
) => Visitor | null | void;

export type PluginConfig<Params> = {
name: string;
params: any;
fn: Plugin<Params> | null;
};

export type PresetConfig = {
name: string;
plugins: PluginConfig<any>[];
};

export type Specificity = [number, number, number];

export type StylesheetDeclaration = {
Expand Down