diff --git a/packages/plugin-svgo/src/__snapshots__/index.test.js.snap b/packages/plugin-svgo/src/__snapshots__/index.test.js.snap index 7b918340..ef9cec5e 100644 --- a/packages/plugin-svgo/src/__snapshots__/index.test.js.snap +++ b/packages/plugin-svgo/src/__snapshots__/index.test.js.snap @@ -1,17 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`svgo should be possible to disable id prefixing 1`] = `""`; +exports[`svgo should be possible to disable id prefixing 1`] = `""`; -exports[`svgo should not load runtime configuration with \`runtimeConfig: false\` 1`] = `""`; +exports[`svgo should not load runtime configuration with \`runtimeConfig: false\` 1`] = `""`; -exports[`svgo should not remove viewBox with icon option 1`] = `""`; +exports[`svgo should not remove viewBox with icon option 1`] = `""`; -exports[`svgo should not remove viewBox with when dimensions is false 1`] = `""`; +exports[`svgo should not remove viewBox with when dimensions is false 1`] = `""`; -exports[`svgo should optimize svg 1`] = `""`; +exports[`svgo should optimize svg 1`] = `""`; -exports[`svgo should support config.svgoConfig 1`] = `"Created with Sketch."`; +exports[`svgo should support config.svgoConfig 1`] = `"Created with Sketch."`; -exports[`svgo should support icon with config.svgoConfig plugins 1`] = `"Created with Sketch."`; +exports[`svgo should support icon with config.svgoConfig plugins 1`] = `"Created with Sketch."`; -exports[`svgo should use state.filePath to detect configuration 1`] = `""`; +exports[`svgo should use state.filePath to detect configuration 1`] = `""`; + +exports[`svgo should be possible to enable id prefixing as the only optimization 1`] = `"DismissCreated with Sketch."`; diff --git a/packages/plugin-svgo/src/index.js b/packages/plugin-svgo/src/index.js index cd315be1..acd2e711 100644 --- a/packages/plugin-svgo/src/index.js +++ b/packages/plugin-svgo/src/index.js @@ -96,12 +96,35 @@ function getFilePath(state) { return state.filePath || process.cwd() } +function getPlugins(config) { + return config && Array.isArray(config.plugins) ? config.plugins : [] +} + +function extendPlugins(...configs) { + const init = []; + let i = configs.length; + + while (i-- > 0) { + const plugins = configs[i]; + for (let j = 0; j < plugins.length; j++) { + const plugin = plugins[j]; + if (!init.some(item => Object.keys(item)[0] === Object.keys(plugin)[0])) { + init.push(plugin); + } + } + } + return init; +} + function createSvgo(config, rcConfig) { + const baseSvgoConfig = getBaseSvgoConfig(config); + const plugins = extendPlugins(getPlugins(baseSvgoConfig), getPlugins(rcConfig), getPlugins(config.svgoConfig)); const mergedConfig = mergeDeep( - getBaseSvgoConfig(config), + baseSvgoConfig, rcConfig, config.svgoConfig, ) + mergedConfig.plugins = plugins return new SVGO(mergedConfig) } diff --git a/packages/plugin-svgo/src/index.test.js b/packages/plugin-svgo/src/index.test.js index 4ddb9e8b..70177d88 100644 --- a/packages/plugin-svgo/src/index.test.js +++ b/packages/plugin-svgo/src/index.test.js @@ -7,7 +7,7 @@ const baseSvg = ` Dismiss Created with Sketch. - + @@ -107,4 +107,19 @@ describe('svgo', () => { expect(result).toMatchSnapshot() }) + + it('should be possible to enable id prefixing as the only optimization', () => { + const result = svgo( + baseSvg, + { + svgo: true, + icon: true, + runtimeConfig: true, + svgoConfig: { full: true, plugins: [{ prefixIds: {prefixIds: true, prefixClassNames: false} }] }, + }, + { filePath: path.join(__dirname, '../__fixtures__/svgo') }, + ) + + expect(result).toMatchSnapshot() + }) })