You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/06-migrations/01-migration-from-v3-to-v4.mdx
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,20 @@
2
2
title: Migration from v3 to v4
3
3
---
4
4
5
-
This is a summary of the changes necessary to migrate from SVGO v3 to SVGO v4. If you want more details or have any questions, please refer to our [release notes for SVGO v4.0.0](https://github.com/svg/svgo/releases/tag/v4.0.0) or related pull requests. You're also encouraged to leave comments in pull requests if the existing content doesn't already answer your question.
5
+
This is a summary of the changes necessary to migrate from SVGO v3 to SVGO v4. If you want more details or have any questions, please refer to our [release notes for SVGO v4.0.0](https://github.com/svg/svgo/releases/tag/v4.0.0) or related pull requests. You're encouraged to leave comments in pull requests if the existing content doesn't answer your question.
6
6
7
7
## Version Requirements
8
8
9
-
SVGO now requires [Node.js >=16.0.0](https://nodejs.org/en/blog/release/v16.0.0).
9
+
SVGO now requires [Node.js >=16.0.0](https://nodejs.org/blog/release/v16.0.0).
10
10
11
11
## Default Behavior
12
12
13
-
The following changes have been made to default plugins, also known as [preset-default](/docs/preset-default/).
13
+
The following changes have been made to default plugins, also known as [preset-default](/docs/preset-default/):
14
14
15
-
-**[removeViewBox](/docs/plugins/removeViewBox/)** is no longer a default plugin, to preserve scalability.
16
-
-**[removeTitle](/docs/plugins/removeTitle)** is no longer a default plugin, to preserve accessibility.
17
-
-**removeScriptElement** has been renamed to **[removeScripts](/docs/plugins/removeScripts)**, as it not only removes the `<script>` element, but also event handlers, and script URIs.
15
+
-**[removeViewBox](/docs/plugins/removeViewBox/)** is no longer a default plugin to preserve scalability.
16
+
-**[removeTitle](/docs/plugins/removeTitle)** is no longer a default plugin to preserve accessibility.
18
17
19
-
To continue using removeViewBox or removeTitle, configure it in the SVGO config, see the [README](https://github.com/svg/svgo?tab=readme-ov-file#configuration) for more context, but please consider reading the warnings in the respective plugin's documentation first:
18
+
To continue using removeViewBox or removeTitle, configure it in the SVGO config. See the [README](https://github.com/svg/svgo?tab=readme-ov-file#configuration) for more context, but please consider reading the warnings in the respective plugin's documentation first:
20
19
21
20
```diff
22
21
export default {
@@ -28,7 +27,9 @@ To continue using removeViewBox or removeTitle, configure it in the SVGO config,
28
27
};
29
28
```
30
29
31
-
If you were using the removeScriptElement plugin, amend your SVGO config to use removeScripts instead:
30
+
## Plugins
31
+
32
+
The removeScriptElement plugin has been renamed to **[removeScripts](/docs/plugins/removeScripts)**, as it not only removes the `<script>` element but also event handlers and script URIs. If you were using the removeScriptElement plugin, amend your SVGO config to use removeScripts instead:
32
33
33
34
```diff
34
35
export default {
@@ -40,9 +41,9 @@ If you were using the removeScriptElement plugin, amend your SVGO config to use
40
41
};
41
42
```
42
43
43
-
## Public vs Internal API
44
+
## Public vs. Internal API
44
45
45
-
We now enforce a boundary between public and internal API. It's no longer possible to arbitrarily import any code declared by SVGO, but rather only the public API we declare as part of our semantic versioning.
46
+
We now enforce a boundary between public and internal APIs. It's no longer possible to arbitrarily import any code declared by SVGO, but rather only the public API we declare as part of our semantic versioning.
Xast/CSS helpers for selecting nodes must be imported from `svgo` instead, and have different behavior. This effects custom plugins that use any of the following functions, where the `selector` (2nd) argument could reference parent or sibling nodes (i.e. `div > span`):
87
+
Xast/CSS helpers for selecting nodes must be imported from `svgo` instead and have different behavior. This affects custom plugins that use any of the following functions, where the `selector` (2nd) argument could reference parent or sibling nodes (i.e. `div > span`):
87
88
88
89
-`querySelectorAll`
89
90
-`querySelector`
90
91
-`matches`
91
92
92
93
If this applies to you, then you need to pass a `Map` of nodes to their parent node as a third argument.
93
94
94
-
A helper has been provided named `#mapNodesToParents`, which does this for you. This can be used to easily migrate to the new API. If you're not sure if you need it, then it's safer to take this approach. The third argument won't be necessary if `selector`does not traverse nodes, for example querying using one or more attributes of a single node.
95
+
A helper has been provided named `#mapNodesToParents`, which does this for you. This can be used to easily migrate to the new API. If you're not sure if you need it, then it's safer to take this approach. The third argument won't be necessary if `selector`doesn't traverse nodes. For example, querying using one or more attributes of a single node.
//this usage has the same behavior as v3, as `rootNode` is already the entire node tree
114
+
// same behavior as v3, as `rootNode` is already the entire node tree
115
115
constnodes=querySelectorAll(rootNode, selector);
116
116
```
117
117
118
-
## Named vs Default Exports
118
+
## Named vs. Default Exports
119
119
120
-
SVGO now only has named exports, there are no default exports in the public API.
120
+
SVGO now only has named exports—there are no default exports in the public API.
121
121
122
-
If you use SVGO from the CLI, or have a Common JS project (you import SVGO using `require`), or if you were already using named exports, you don't have to do anything.
122
+
If you use SVGO from the CLI, have a Common JS project (you import SVGO using `require`), or are already using named exports, you don't have to do anything.
123
123
124
-
If one way or another you're using the `import` keyword to import SVGO, then any instance where you use a default export must be switched for the named equivilent.
124
+
If one way or another you're using the `import` keyword to import SVGO, then any instance where you use the default export must be replaced with the named equivalent.
This is a summary of the changes necessary to migrate from SVGO v2 to SVGO v3. If you want more details or have any questions, please refer to our [release notes for SVGO v3.0.0](https://github.com/svg/svgo/releases/tag/v3.0.0).
6
+
7
+
## Version Requirements
8
+
9
+
SVGO now requires [Node.js >=14.0.0](https://nodejs.org/blog/release/v14.0.0).
10
+
11
+
## Default Behavior
12
+
13
+
The **[sortAttrs](/docs/plugins/sortAttrs)** plugin is now enabled by default to improve gzip compression.
14
+
15
+
To disable this behavior, configure it in the SVGO config. See the [README](https://github.com/svg/svgo?tab=readme-ov-file#configuration) for more context.
16
+
17
+
```diff
18
+
export default {
19
+
plugins: [
20
+
- 'preset-default',
21
+
+ {
22
+
+ name: 'preset-default',
23
+
+ params: {
24
+
+ overrides: {
25
+
+ sortAttrs: false,
26
+
+ },
27
+
+ },
28
+
+ },
29
+
],
30
+
};
31
+
```
32
+
33
+
## Plugins
34
+
35
+
The cleanupIDs plugin has been renamed to **[cleanupIds](/docs/plugins/cleanupIds)** to bring more consistency between plugin names. If you were using cleanupIDs explicitly, amend your SVGO config to use cleanupIds instead:
36
+
37
+
```diff
38
+
export default {
39
+
plugins: [
40
+
- 'cleanupIDs',
41
+
+ 'cleanupIds',
42
+
],
43
+
};
44
+
```
45
+
46
+
The cleanupIds plugin no longer accepts the `prefix` parameter. Use the **[prefixIds](/docs/plugins/prefixIds)** plugin instead. You must put prefixIds **_after_** cleanupIds. Doing so the other way around would just remove the prefixes again.
47
+
48
+
```diff
49
+
export default {
50
+
plugins: [
51
+
{
52
+
name: 'cleanupIds',
53
+
- params: {
54
+
- prefix: 'my-prefix',
55
+
- },
56
+
},
57
+
+ {
58
+
+ name: 'prefixIds',
59
+
+ params: {
60
+
+ prefix: 'my-prefix',
61
+
+ },
62
+
+ },
63
+
]
64
+
}
65
+
```
66
+
67
+
## Configuration
68
+
69
+
### Active Plugins
70
+
71
+
Plugins defined in the `plugins` array no longer accept the property `active`. A plugin is enabled if it is named, and disabled if it is not named. Remove these entries from your config outright if you do not want them enabled.
72
+
73
+
```diff
74
+
export default {
75
+
plugins: [
76
+
{
77
+
name: 'removeDoctype',
78
+
- active: true,
79
+
},
80
+
- {
81
+
- name: 'removeComments',
82
+
- active: false,
83
+
- },
84
+
],
85
+
}
86
+
```
87
+
88
+
### Overriding Default Plugins
89
+
90
+
The `extendDefaultPlugins` helper utility has been removed. You can disable plugins that are enabled by default in `preset-default` through the `override` parameter instead.
91
+
92
+
```diff
93
+
- import { extendDefaultPlugins } from 'svgo';
94
+
95
+
export default {
96
+
- plugins: extendDefaultPlugins([
97
+
- {
98
+
- name: 'collapseGroups',
99
+
- active: false,
100
+
- },
101
+
- ]),
102
+
+ plugins: [
103
+
+ {
104
+
+ name: 'preset-default',
105
+
+ params: {
106
+
+ overrides: {
107
+
+ collapseGroups: false,
108
+
+ },
109
+
+ },
110
+
+ },
111
+
+ ],
112
+
}
113
+
```
114
+
115
+
## JavaScript API
116
+
117
+
We've removed the `info` property from the optimization result. This means we no longer return a width and height of the SVG. We've also removed `error` and `modernError` from the optimization result. All exceptions are thrown to the caller.
We previously supported different types of plugins: `full`, `perItem`, `perItemReverse`, and `visitor`.
135
+
136
+
We now only support the `visitor` plugin API. Any other type would need to be migrated to it. See the [Plugin Architecture documentation](/docs/plugins-api) for more information.
137
+
138
+
## TypeScript
139
+
140
+
SVGO now bundles TypeScript declarations in the npm package. There is no need to install `@types/svgo` separately. This does not affect users that never depended on `@types/svgo` directly or indirectly. You can check your lockfile to be sure.
141
+
142
+
If you depended on `@types/svgo` before, uninstall it:
0 commit comments