-
-
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.
BREAKING CHANGE: config format of SVGO changes & SVGR does not merge SVGO config.
- Loading branch information
Showing
18 changed files
with
196 additions
and
390 deletions.
There are no files selected for viewing
File renamed without changes
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,12 @@ | ||
module.exports = { | ||
plugins: [ | ||
{ | ||
name: 'preset-default', | ||
params: { | ||
overrides: { | ||
removeTitle: false, | ||
}, | ||
}, | ||
}, | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
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,12 @@ | ||
module.exports = { | ||
plugins: [ | ||
{ | ||
name: 'preset-default', | ||
params: { | ||
overrides: { | ||
removeDesc: false, | ||
}, | ||
}, | ||
}, | ||
] | ||
} |
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 |
---|---|---|
|
@@ -28,6 +28,6 @@ | |
"dependencies": { | ||
"cosmiconfig": "^7.0.1", | ||
"deepmerge": "^4.2.2", | ||
"svgo": "^1.2.2" | ||
"svgo": "^2.5.0" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,21 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`svgo disables id prefixing using svgo config 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"Blocks\\" class=\\"blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo does not load runtime configuration with \`runtimeConfig: false\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo does not load runtime configuration with \`runtimeConfig: false\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo does not remove viewBox with \`icon\` option 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo does not remove viewBox with \`icon\` option 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo does not remove viewBox with when \`dimensions\` is false 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo does not remove viewBox with when \`dimensions\` is false 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo optimizes svg 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo is possible to enable id prefixing as the only optimization 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><svg width=\\"88px\\" height=\\"88px\\" viewBox=\\"0 0 88 88\\" version=\\"1.1\\" xmlns=\\"http://www.w3.org/2000/svg\\" xmlns:xlink=\\"http://www.w3.org/1999/xlink\\"><!--Generator: Sketch 46.2 (44496) - http://www.bohemiancoding.com/sketch--><title>Dismiss</title><desc>Created with Sketch.</desc><defs/><g id=\\"svgo__Blocks\\" class=\\"blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51,37 L37,51\\" id=\\"svgo__Shape\\"/><path d=\\"M51,51 L37,37\\" id=\\"svgo__Shape\\"/><style>#svgo__Shape{}</style></g></g></svg>"`; | ||
exports[`svgo supports \`config.svgoConfig\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><desc>Created with Sketch.</desc><g id=\\"Blocks\\" class=\\"blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo optimizes svg 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo supports \`config.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 id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo supports \`config.svgoConfig.multipass\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__prefix__Blocks\\" class=\\"prefix__prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"prefix__prefix__Shape\\"/><path d=\\"M51 51L37 37\\"/><style/></g></g></svg>"`; | ||
exports[`svgo supports \`config.svgoConfig\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><desc>Created with Sketch.</desc><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo users \`state.filePath\` to detect configuration 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`; | ||
exports[`svgo uses \`state.filePath\` to detect configuration 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`; |
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 |
---|---|---|
@@ -1,48 +1,43 @@ | ||
import deepmerge from 'deepmerge' | ||
import { cosmiconfigSync } from 'cosmiconfig' | ||
|
||
export function getFilePath(state) { | ||
return state.filePath || process.cwd() | ||
} | ||
const explorer = cosmiconfigSync('svgo', { | ||
searchPlaces: [ | ||
'package.json', | ||
'.svgorc', | ||
'.svgorc.js', | ||
'.svgorc.json', | ||
'.svgorc.yaml', | ||
'.svgorc.yml', | ||
'svgo.config.js', | ||
'.svgo.yml', | ||
], | ||
transform: (result) => result && result.config, | ||
cache: true, | ||
}) | ||
|
||
export function getBaseSvgoConfig(config) { | ||
const baseSvgoConfig = { | ||
plugins: [{ prefixIds: true }], | ||
} | ||
if (config.icon || config.dimensions === false) { | ||
baseSvgoConfig.plugins.push({ removeViewBox: false }) | ||
function getSvgoConfigFromSvgrConfig(config) { | ||
const preset = | ||
config.icon || config.dimensions === false | ||
? { | ||
name: 'preset-default', | ||
params: { | ||
overrides: { | ||
removeViewBox: false, | ||
}, | ||
}, | ||
} | ||
: 'preset-default' | ||
return { | ||
plugins: [preset, 'prefixIds'], | ||
} | ||
return baseSvgoConfig | ||
} | ||
|
||
export function getPlugins(config) { | ||
if (!config || !config.plugins) { | ||
return [] | ||
} | ||
if (!Array.isArray(config.plugins)) { | ||
throw Error('`svgoConfig.plugins` must be an array') | ||
export function getSvgoConfig(config, state) { | ||
const cwd = state.filePath || process.cwd() | ||
if (config.svgoConfig) return config.svgoConfig | ||
if (config.runtimeConfig) { | ||
const userConfig = explorer.search(cwd) | ||
if (userConfig) return userConfig | ||
} | ||
return config.plugins | ||
} | ||
|
||
function extractPlugins(config) { | ||
if (!config) return [] | ||
if (!config.plugins) return [] | ||
if (!Array.isArray(config.plugins)) return [config.plugins] | ||
return config.plugins | ||
} | ||
|
||
function mergePlugins(configs) { | ||
const plugins = configs.reduce( | ||
(merged, config) => deepmerge.all([merged, ...extractPlugins(config)]), | ||
{}, | ||
) | ||
return Object.keys(plugins).reduce((array, key) => { | ||
array.push({ [key]: plugins[key] }) | ||
return array | ||
}, []) | ||
} | ||
|
||
export function mergeSvgoConfig(...configs) { | ||
const plugins = mergePlugins(configs) | ||
return { ...deepmerge.all(configs.filter(Boolean)), plugins } | ||
return getSvgoConfigFromSvgrConfig(config) | ||
} |
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
Oops, something went wrong.