Skip to content

Commit

Permalink
feat: add support for --disable and --enable flags back
Browse files Browse the repository at this point in the history
Closes #1356
Closes #1622

In #1622 (comment) we read:

> I consider adding it back at some point but it should be able to work with presets somehow. This is still need to be figured out.

I believe that adding it at the very last possible moment, AFTER initial list is resolved, will make it work with presets, and preserve the original behavior if the options aren't used at all.

I've also increased JSDoc coverage here, mainly for my own sanity when working with these arrays of plugins. Hope you don't mind that scope creep :)
  • Loading branch information
wojtekmaj committed Apr 8, 2024
1 parent 8d6385b commit 3ce4fa6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
54 changes: 53 additions & 1 deletion lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ 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>
*/

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

/**
* @template T {any}
* @param {string | PluginConfig<T>} plugin plugin name or plugin config
* @returns {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,11 +87,53 @@ 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 resolvedPluginsWithDisable = disablePlugins
? resolvedPlugins.filter(
(plugin) => !disablePlugins.includes(plugin.name),
)
: resolvedPlugins;

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

const resolvedEnablePlugins = enablePlugins
? enablePlugins
.filter((plugin) => plugin != null)
.map(resolvePluginConfig)
: null;

if (
resolvedEnablePlugins &&
resolvedEnablePlugins.length < enablePlugins.length
) {
console.warn(
'Warning: enable plugins list includes null or undefined elements, these will be ignored.',
);
}

const resolvedPluginsWithDisableEnable = resolvedEnablePlugins
? resolvedPluginsWithDisable.concat(resolvedEnablePlugins)
: resolvedPluginsWithDisable;

const globalOverrides = {};
if (config.floatPrecision != null) {
globalOverrides.floatPrecision = config.floatPrecision;
}
invokePlugins(ast, info, resolvedPlugins, null, globalOverrides);
invokePlugins(
ast,
info,
resolvedPluginsWithDisableEnable,
null,
globalOverrides,
);
output = stringifySvg(ast, config.js2svg);
if (output.length < prevResultSize) {
input = output;
Expand Down
5 changes: 5 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
6 changes: 6 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ export type Plugin<Params> = (
info: PluginInfo,
) => Visitor | null | void;

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

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

export type StylesheetDeclaration = {
Expand Down

0 comments on commit 3ce4fa6

Please sign in to comment.