-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(svgo): support any SVGO config format
Fixes #400
- Loading branch information
Showing
4 changed files
with
189 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import mergeDeep from 'merge-deep' | ||
|
||
export function getFilePath(state) { | ||
return state.filePath || process.cwd() | ||
} | ||
|
||
export function getBaseSvgoConfig(config) { | ||
const baseSvgoConfig = { | ||
plugins: [{ prefixIds: true }], | ||
} | ||
if (config.icon || config.dimensions === false) { | ||
baseSvgoConfig.plugins.push({ removeViewBox: false }) | ||
} | ||
return baseSvgoConfig | ||
} | ||
|
||
export function getPlugins(config) { | ||
if (!config || !config.plugins) { | ||
return [] | ||
} | ||
if (!Array.isArray(config.plugins)) { | ||
throw Error('`svgoConfig.plugins` must be an array') | ||
} | ||
return config.plugins | ||
} | ||
|
||
function extractPlugins(config) { | ||
if (!config) return [] | ||
if (!config.plugins) return [] | ||
if (!Array.isArray(config.plugins)) return [config.plugins] | ||
return config.plugins | ||
} | ||
|
||
export function mergeSvgoConfig(...configs) { | ||
return configs.reduce( | ||
(merged, config) => { | ||
const plugins = extractPlugins(config) | ||
merged.plugins = mergeDeep(merged.plugins, ...plugins) | ||
return merged | ||
}, | ||
{ plugins: {} }, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { getFilePath, getBaseSvgoConfig, mergeSvgoConfig } from './config' | ||
|
||
describe('svgo config', () => { | ||
describe('#getFilePath', () => { | ||
describe('if `state.filePath` exists', () => { | ||
it('returns `state.filePath', () => { | ||
expect(getFilePath({ filePath: '/foo/bar' })).toBe('/foo/bar') | ||
}) | ||
}) | ||
describe('if `state.filePath` does not exists', () => { | ||
it('returns current working directory', () => { | ||
expect(getFilePath({})).toBe(process.cwd()) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#getBaseSvgoConfig', () => { | ||
describe('with no specific config', () => { | ||
it('returns config with `prefixIds: true`', () => { | ||
expect(getBaseSvgoConfig({})).toEqual({ | ||
plugins: [{ prefixIds: true }], | ||
}) | ||
}) | ||
}) | ||
|
||
describe('with `config.icons` enabled', () => { | ||
it('returns config with `removeViewBox: false`', () => { | ||
expect(getBaseSvgoConfig({ icon: true })).toEqual({ | ||
plugins: [{ prefixIds: true }, { removeViewBox: false }], | ||
}) | ||
}) | ||
}) | ||
|
||
describe('with `config.dimensions` disabled', () => { | ||
it('returns config with `removeViewBox: false`', () => { | ||
expect(getBaseSvgoConfig({ dimensions: false })).toEqual({ | ||
plugins: [{ prefixIds: true }, { removeViewBox: false }], | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('#mergeSvgoConfig', () => { | ||
it('merges any config format', () => { | ||
expect( | ||
mergeSvgoConfig({ plugins: { removeViewBox: false } }, null), | ||
).toEqual({ | ||
plugins: { removeViewBox: false }, | ||
}) | ||
expect( | ||
mergeSvgoConfig({ plugins: { removeViewBox: false } }, {}), | ||
).toEqual({ | ||
plugins: { removeViewBox: false }, | ||
}) | ||
expect(mergeSvgoConfig({ plugins: { removeViewBox: false } })).toEqual({ | ||
plugins: { removeViewBox: false }, | ||
}) | ||
expect(mergeSvgoConfig({ plugins: [{ removeViewBox: false }] })).toEqual({ | ||
plugins: { removeViewBox: false }, | ||
}) | ||
expect( | ||
mergeSvgoConfig({ | ||
plugins: [{ removeViewBox: false }, { removeViewBox: true }], | ||
}), | ||
).toEqual({ | ||
plugins: { removeViewBox: true }, | ||
}) | ||
expect( | ||
mergeSvgoConfig({ | ||
plugins: [ | ||
{ | ||
convertColors: { | ||
currentColor: true, | ||
}, | ||
}, | ||
{ | ||
prefixIds: { | ||
prefix: 'foo', | ||
}, | ||
}, | ||
], | ||
}), | ||
).toEqual({ | ||
plugins: { | ||
convertColors: { | ||
currentColor: true, | ||
}, | ||
prefixIds: { | ||
prefix: 'foo', | ||
}, | ||
}, | ||
}) | ||
}) | ||
expect( | ||
mergeSvgoConfig( | ||
{ | ||
plugins: [ | ||
{ | ||
prefixIds: { | ||
prefix: 'foo', | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
plugins: [ | ||
{ | ||
prefixIds: { | ||
prefix: 'bar', | ||
}, | ||
}, | ||
], | ||
}, | ||
), | ||
).toEqual({ | ||
plugins: { | ||
prefixIds: { | ||
prefix: 'bar', | ||
}, | ||
}, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters