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

Add --disable command line option. #8

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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
1 change: 1 addition & 0 deletions lib/svgo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type Config = {
plugins?: PluginConfig[];
preset?: 'default' | 'next' | 'none';
enable?: string[];
disable?: string[];
/** Options for rendering optimized SVG from AST. */
js2svg?: StringifyOptions;
/** Output as Data URI string. */
Expand Down
2 changes: 1 addition & 1 deletion lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const optimize = (input, config) => {
);
}
/** @type {import('./svgo.js').Config} */
const globalOverrides = {};
const globalOverrides = { disable: config.disable };
if (config.floatPrecision != null) {
globalOverrides.floatPrecision = config.floatPrecision;
}
Expand Down
13 changes: 10 additions & 3 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ export default function makeProgram(program) {
'Specify which set of predefined plugins to use',
'default',
)
.option(
'--config <CONFIG>',
'Custom config file, only .js, .mjs, and .cjs is supported',
)
.option(
'--enable <plugin...>',
'Specify one or more builtin plugins to run in addition to the presets',
'Specify one or more builtin plugins to run in addition to those in the preset or config',
)
.option(
'--config <CONFIG>',
'Custom config file, only .js, .mjs, and .cjs is supported',
'--disable <plugin...>',
'Specify one or more plugins from the preset or config which should not be run ',
)
.option(
'--datauri <FORMAT>',
Expand Down Expand Up @@ -247,6 +251,9 @@ async function action(args, opts, command) {
if (opts.enable) {
config.enable = opts.enable;
}
if (opts.disable) {
config.disable = opts.disable;
}

// --pretty
if (opts.pretty) {
Expand Down
11 changes: 9 additions & 2 deletions lib/svgo/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export const invokePlugins = (
if (override === false) {
continue;
}
if (
globalOverrides.disable &&
globalOverrides.disable.includes(plugin.name)
) {
continue;
}
const params = { ...plugin.params, ...globalOverrides, ...override };

const visitor = plugin.fn(ast, params, info);
Expand All @@ -51,8 +57,9 @@ export const createPreset = ({ name, plugins, description }) => {
isPreset: true,
plugins: Object.freeze(plugins),
fn: (ast, params, info) => {
const { floatPrecision, overrides } = params;
const globalOverrides = {};
const { floatPrecision, disable, overrides } = params;
/** @type {import('../svgo.js').Config} */
const globalOverrides = { disable: disable };
if (floatPrecision != null) {
globalOverrides.floatPrecision = floatPrecision;
}
Expand Down
96 changes: 96 additions & 0 deletions test/coa/option.disable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import fs from 'fs';
import path from 'path';
import { Command } from 'commander';
import { fileURLToPath } from 'url';
import svgo from '../../lib/svgo/coa.js';

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

const tempFolder = 'temp';

/**
* @param {string[]} args
*/
function runProgram(args) {
const program = new Command();
svgo(program);
// prevent running process.exit
program.exitOverride(() => {});
// parser skips first two arguments
return program.parseAsync(['0', '1', ...args]);
}

const PLUGINOPT_DIR = path.resolve(__dirname, 'testPluginOpts');
const PLUGINOPT_FILE1 = path.resolve(PLUGINOPT_DIR, 'test1.svg');
const PLUGINOPT_FILE1_OPT = path.resolve(tempFolder, 'test1.svg');

const EXPECT_NO_CHANGE =
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><rect x="10" y="20" width="10" height="20" transform="matrix(1 0 0 1 10 20) "/></svg>';
const EXPECT_TRANS =
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><rect x="10" y="20" width="10" height="20" transform="translate(10 20)"/></svg>';

describe('test --disable option', function () {
afterAll(() => {
fs.rmSync(tempFolder, { force: true, recursive: true });
});

it('should not run convertShapeToPath with --preset=default and --disable=convertShapeToPath', async () => {
await runProgram([
'-i',
PLUGINOPT_FILE1,
'-o',
PLUGINOPT_FILE1_OPT,
'--quiet',
'--disable=convertShapeToPath',
]);
const opt = fs.readFileSync(PLUGINOPT_FILE1_OPT, { encoding: 'utf8' });
expect(opt).toBe(EXPECT_TRANS);
});

it('should run multiple plugins with --preset=default and --disable minifyTransforms convertShapeToPath cleanupAttrs', async () => {
await runProgram([
'-i',
PLUGINOPT_FILE1,
'-o',
PLUGINOPT_FILE1_OPT,
'--quiet',
'--disable',
'minifyTransforms',
'convertShapeToPath',
'cleanupAttrs',
]);
const opt = fs.readFileSync(PLUGINOPT_FILE1_OPT, { encoding: 'utf8' });
expect(opt).toBe(EXPECT_NO_CHANGE);
});

it('should ignore invalid plugin names', async () => {
await runProgram([
'-i',
PLUGINOPT_FILE1,
'-o',
PLUGINOPT_FILE1_OPT,
'--quiet',
'--disable',
'x',
'convertShapeToPath',
]);
const opt = fs.readFileSync(PLUGINOPT_FILE1_OPT, { encoding: 'utf8' });
expect(opt).toBe(EXPECT_TRANS);
});

it('should work when plugins are specified in custom config', async () => {
await runProgram([
'-i',
PLUGINOPT_FILE1,
'-o',
PLUGINOPT_FILE1_OPT,
'--quiet',
'--disable',
'minifyTransforms',
'--config',
path.resolve(PLUGINOPT_DIR, 'config1.js'),
]);
const opt = fs.readFileSync(PLUGINOPT_FILE1_OPT, { encoding: 'utf8' });
expect(opt).toBe(EXPECT_NO_CHANGE);
});
});
14 changes: 10 additions & 4 deletions test/regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ async function performTests(options) {
let totalCompression = 0;

/** @type {import('../lib/svgo.js').Config} */
const config = {};
config.preset = options.preset;
config.enable = options.enable;
const config = {
preset: options.preset,
enable: options.enable,
disable: options.disable,
};

/**
* @param {string[]} list
Expand Down Expand Up @@ -180,7 +182,11 @@ program
)
.option(
'--enable <plugin...>',
'Specify one or more builtin plugins to run in addition to the presets',
'Specify one or more builtin plugins to run in addition to those in the preset or config',
)
.option(
'--disable <plugin...>',
'Specify one or more plugins from the preset or config which should not be run ',
)
.option(
'--inputdir <dir>',
Expand Down