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

fix(core): not working first plugin when icon option enabled #137

Merged
merged 4 commits into from
Jul 11, 2018
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
2 changes: 2 additions & 0 deletions packages/core/src/plugins/__snapshots__/svgo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ exports[`svgo should optimize svg 1`] = `"<svg width=\\"88\\" height=\\"88\\" xm

exports[`svgo should support config.svgoConfig 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><desc>Created with Sketch.</desc><g stroke=\\"#063855\\" stroke-width=\\"2\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><path d=\\"M51 37L37 51M51 51L37 37\\"/></g></svg>"`;

exports[`svgo should support icon with config.svgoConfig plugins 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><desc>Created with Sketch.</desc><g stroke=\\"#063855\\" stroke-width=\\"2\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><path d=\\"M51 37L37 51M51 51L37 37\\"/></g></svg>"`;

exports[`svgo should use state.filePath to detect configuration 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><desc>Created with Sketch.</desc><g stroke=\\"#063855\\" stroke-width=\\"2\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><path d=\\"M51 37L37 51M51 51L37 37\\"/></g></svg>"`;
11 changes: 11 additions & 0 deletions packages/core/src/plugins/mergeConfigs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import mergeWith from 'lodash/mergeWith'

function concatArrays(objValue, srcValue) {
if (Array.isArray(objValue)) {
return objValue.concat(srcValue)
}

return undefined // default value
}

export default (...configs) => mergeWith(...configs, concatArrays)
52 changes: 52 additions & 0 deletions packages/core/src/plugins/mergeConfigs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import mergeConfigs from './mergeConfigs'

describe('mergeConfigs', () => {
it('should merge objects', () => {
const result = mergeConfigs(
{ param1: 'value1' },
{ param2: 'value2' },
{ param3: 'value3' },
)
expect(result).toEqual({
param1: 'value1',
param2: 'value2',
param3: 'value3',
})
})

it('should merge arrays in objects', () => {
const result = mergeConfigs(
{ param1: ['value1'] },
{ param1: ['value2'] },
{ param1: ['value3'] },
)
expect(result).toEqual({ param1: ['value1', 'value2', 'value3'] })
})

it('should merge arrays on object second level', () => {
const result = mergeConfigs(
{ level1: { level2: ['value1'] } },
{ level1: { level2: ['value2'] } },
{ level1: { level2: ['value3'] } },
)
expect(result).toEqual({
level1: { level2: ['value1', 'value2', 'value3'] },
})
})

it('should merge arrays on objects second level with additional params', () => {
const result = mergeConfigs(
{ level1: { level2: ['value1'], param1: 'value1' } },
{ level1: { level2: ['value2'], param2: 'value2' } },
{ level1: { level2: ['value3'], param3: 'value3' } },
)
expect(result).toEqual({
level1: {
level2: ['value1', 'value2', 'value3'],
param1: 'value1',
param2: 'value2',
param3: 'value3',
},
})
})
})
4 changes: 2 additions & 2 deletions packages/core/src/plugins/svgo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SVGO from 'svgo'
import cosmiconfig from 'cosmiconfig'
import merge from 'lodash/merge'
import mergeConfigs from './mergeConfigs'

const explorer = cosmiconfig('svgo', {
searchPlaces: [
Expand All @@ -26,7 +26,7 @@ export default async (code, config = {}, state = {}) => {
const filePath = state.filePath || process.cwd()
const svgoRcConfig = await explorer.search(filePath)
const svgo = new SVGO(
merge(getBaseSvgoConfig(config), svgoRcConfig, config.svgoConfig),
mergeConfigs(getBaseSvgoConfig(config), svgoRcConfig, config.svgoConfig),
)
const { data } = await svgo.optimize(code)
return data
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/plugins/svgo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ describe('svgo', () => {
expect(result).toMatchSnapshot()
})

it('should support icon with config.svgoConfig plugins', async () => {
const result = await svgo(baseSvg, {
svgo: true,
icon: true,
svgoConfig: { plugins: [{ removeDesc: false }] },
})

expect(result).toMatchSnapshot()
})

it('should use state.filePath to detect configuration', async () => {
const result = await svgo(
baseSvg,
Expand Down