The code is taken from https://github.com/svg/svgo/pull/976, refactored
with new api, covered types and simplified.
Plugin has no dependencies so can be used without changing.
```
const inlineDefs = require('./inlineDefs.js');
module.exports = {
plugins: [
'preset-default',
inlineDefs
]
};
```
- applyTransforms is prepared to be a separate plugin, in v3 I will
remove it from convertPathData
- migrated to tsdoc
- removed optimisation with stroke-width inside id (still not idea how
it worked)
- added deoptimisation when id is on element to fix redefining in `<use>`
Note: review with hidden whitespaces
This is a big one
- got rid from another closestByName usage
- delegated removing empty defs elements to removeEmptyContainers plugin
- got rid from all css-tools usages (most inlineStyles code was there
for some reason)
- combined a few loops
- fixed useMqs option (I would remove it in v3 for simplicity as it
seems nobody use it)
`prefixIds` plugin currently breaks url()-links inside `style` attributes:
```javascript
optimize(
`<g style="fill:url(#brush-id);stroke:url(#pen-id)"/>`,
{ plugins: ['prefixIds'] }
).data
```
will generate `<g style=""/>`. Seems like `prefixIds` assumes that attribute's whole value might be `url()`, but this is not the case for the `style` attribute.
This fix solves the issue by preserving all attribute's content other than #id inside url(). It also adds some more tests for the `prefixIds` plugin.
nanocolors checks for --color and --no-color flags to force or disable
output coloring. Which can be used instead of strip-ansi package which
btw introduced vulnerability recently.
- covered with tsdoc
- replace another closestByName usage with visitSkip symbol to skip
subtree instead of skipping element by ancestor
Better review with hidden whitespaces as most code just got bigger indent.
Ref https://github.com/svg/svgo/issues/1579
In config of course. Projects with type:module can now use modules to
export config
```js
export default {
plugins: []
}
```
Also added support for resolving svgo.config.mjs and svgo.config.cjs.
Moved loadConfig tests to svgo-node tests.
mjs test is skipped for now in node 10, just don't use modules there
- covered with tsdoc
- migrated to visitor plugin api
- slightly simplified (hope so) logic by avoiding loop over order array
in every compare function call
- rewrote tests
Ref https://github.com/svg/svgo/issues/1499
- migrated to visitor plugin api
- covered with tsdoc
- made the plugin idempotent as requested a few times
Now even manually running svgo a few times will not duplicate
prefix in ids and classes
- run each plugin test twice to see which plugin need to run many times
ideally idempotent plugins will allow to get rid of multipass option in v3
Old SVGO errors were not very helpful. Packages like cssnano
(postcss-svgo) had to deal with a lot of issues which are hard to debug
with old errors.
```
Error: Error in parsing SVG: Unquoted attribute value
Line: 1
Column: 29
Char: 6
File: input.svg
```
New errors are more informative and may solve many struggles
```
Error: SvgoParserError: input.svg:2:29: Unquoted attribute value
1 | <svg viewBox="0 0 120 120">
> 2 | <circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
| ^
3 | </svg>
4 |
```
The logic is a little messy. Will be better when we drop node 12 support
and use optional chaining.
- migrated to visitor plugin api
- covered with types
- get rid from patching params as plugin state
- replaced many node.computedAttr() with style manager
- enabled and fixed removeNone param test (was merged as muted back in 2017)
- added ability to return null and not run visitor in plugins
- migrated to visitor plugin api; combination of enter and exit helped
to fit into single traverse
- got rid from the only node.clone() usage in the project so no need to
reimplement it
- the logic is a bit simplified
- refactored with visitor plugin api
- covered with types
- replaced parentNode traverse with visitSkip symbol
- replaced regex path parser with parsePathData
- get rid from global state which lead test case to invalid state
I saw complaints about `extendDefaultPlugins` api
- it cannot be used when svgo is installed globally
- it requires svgo to be installed when using svgo-loader or svgo-jsx
- it prevents using serializable config formats like json
In this diff I introduced the new plugin which is a bundle of all
default plugins.
```js
module.exports = {
plugins: [
'preset_default',
// or
{
name: 'preset_default',
floatPrecision: 4,
overrides: {
convertPathData: {
applyTransforms: false
}
}
}
]
}
```
Mocha doesn't have a lot of features provided by jest.
There is a great assertion library out of the box.
And the most cool feature is inline snapshots.
Mocha also hides errors which makes debugging a nightmare sometimes.
Visitor is a simple pattern which helps to avoid many type checks
and provide both "perItem" and "perItemReverse" functionality without
fragmentation.
The most important case is an ability to define state which in many
plugins specified either on module level or by polluting `params`.
In this diff I added visit and detachFromParent utilities and refactored
new mergeStyles plugin with it.
Also fixed bug when cdata content is merged into "text" node which is
not always valid.
A lot of new sources of regression tests may come and it's important to
make debug simpler.
Now regression-extract.js downloads and write svg files into
test/regression-fixtures. regression.js run each svg in this folder.
Mismatched svg diff is written into test/regression-diffs.
"full" plugins prevents from possible optimisation. We need to migrate
all plugins to "perItem" type and later implement visitor plugin api to
allow state.
- handle each command separately
- handle both relative and absolute commands
- moved into _applyTransforms.js to convert eventually into plugin
- apply transforms before converting into relative
These changes makes code independent and easy to work with.
There is a lot of attributes manipulation which is hard to remove at
once. In this diff I added `attributes` object and wrapped it as proxy
for `attrs` field.
Ref https://github.com/syntax-tree/xast
- added type: root | element
- renamed elem to name
- replaced "elem" property checks with check for correct type
Here we add [xast](https://github.com/syntax-tree/xast) support
to three basic nodes: doctype, instruction and comment
Some tests are rewritten instead of checking each field to `.include`
assertion which is able to match shape of object.