Skip to content

Commit e13a99a

Browse files
plaggregberge
authored andcommitted
fix(core): config conflict with icon option (#137)
1 parent 8fb24b6 commit e13a99a

5 files changed

Lines changed: 77 additions & 2 deletions

File tree

packages/core/src/plugins/__snapshots__/svgo.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ exports[`svgo should optimize svg 1`] = `"<svg width=\\"88\\" height=\\"88\\" xm
2020
2121
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>"`;
2222
23+
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>"`;
24+
2325
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>"`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import mergeWith from 'lodash/mergeWith'
2+
3+
function concatArrays(objValue, srcValue) {
4+
if (Array.isArray(objValue)) {
5+
return objValue.concat(srcValue)
6+
}
7+
8+
return undefined // default value
9+
}
10+
11+
export default (...configs) => mergeWith(...configs, concatArrays)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import mergeConfigs from './mergeConfigs'
2+
3+
describe('mergeConfigs', () => {
4+
it('should merge objects', () => {
5+
const result = mergeConfigs(
6+
{ param1: 'value1' },
7+
{ param2: 'value2' },
8+
{ param3: 'value3' },
9+
)
10+
expect(result).toEqual({
11+
param1: 'value1',
12+
param2: 'value2',
13+
param3: 'value3',
14+
})
15+
})
16+
17+
it('should merge arrays in objects', () => {
18+
const result = mergeConfigs(
19+
{ param1: ['value1'] },
20+
{ param1: ['value2'] },
21+
{ param1: ['value3'] },
22+
)
23+
expect(result).toEqual({ param1: ['value1', 'value2', 'value3'] })
24+
})
25+
26+
it('should merge arrays on object second level', () => {
27+
const result = mergeConfigs(
28+
{ level1: { level2: ['value1'] } },
29+
{ level1: { level2: ['value2'] } },
30+
{ level1: { level2: ['value3'] } },
31+
)
32+
expect(result).toEqual({
33+
level1: { level2: ['value1', 'value2', 'value3'] },
34+
})
35+
})
36+
37+
it('should merge arrays on objects second level with additional params', () => {
38+
const result = mergeConfigs(
39+
{ level1: { level2: ['value1'], param1: 'value1' } },
40+
{ level1: { level2: ['value2'], param2: 'value2' } },
41+
{ level1: { level2: ['value3'], param3: 'value3' } },
42+
)
43+
expect(result).toEqual({
44+
level1: {
45+
level2: ['value1', 'value2', 'value3'],
46+
param1: 'value1',
47+
param2: 'value2',
48+
param3: 'value3',
49+
},
50+
})
51+
})
52+
})

packages/core/src/plugins/svgo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import SVGO from 'svgo'
22
import cosmiconfig from 'cosmiconfig'
3-
import merge from 'lodash/merge'
3+
import mergeConfigs from './mergeConfigs'
44

55
const explorer = cosmiconfig('svgo', {
66
searchPlaces: [
@@ -26,7 +26,7 @@ export default async (code, config = {}, state = {}) => {
2626
const filePath = state.filePath || process.cwd()
2727
const svgoRcConfig = await explorer.search(filePath)
2828
const svgo = new SVGO(
29-
merge(getBaseSvgoConfig(config), svgoRcConfig, config.svgoConfig),
29+
mergeConfigs(getBaseSvgoConfig(config), svgoRcConfig, config.svgoConfig),
3030
)
3131
const { data } = await svgo.optimize(code)
3232
return data

packages/core/src/plugins/svgo.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ describe('svgo', () => {
3131
expect(result).toMatchSnapshot()
3232
})
3333

34+
it('should support icon with config.svgoConfig plugins', async () => {
35+
const result = await svgo(baseSvg, {
36+
svgo: true,
37+
icon: true,
38+
svgoConfig: { plugins: [{ removeDesc: false }] },
39+
})
40+
41+
expect(result).toMatchSnapshot()
42+
})
43+
3444
it('should use state.filePath to detect configuration', async () => {
3545
const result = await svgo(
3646
baseSvg,

0 commit comments

Comments
 (0)