postcss-svgo

Optimise inline SVG with PostCSS.

MIT 62 个版本
安装
npm install postcss-svgo
yarn add postcss-svgo
pnpm add postcss-svgo
bun add postcss-svgo
README

postcss-svgo

Optimise inline SVG with PostCSS.

Install

With npm do:

npm install postcss-svgo --save

Example

Input

h1 {
    background: url('data:image/svg+xml;charset=utf-8,<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve"><circle cx="50" cy="50" r="40" fill="yellow" /></svg>');
}

h2 {
    background: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+PGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNDAiIGZpbGw9InllbGxvdyIgLz48IS0tdGVzdCBjb21tZW50LS0+PC9zdmc+');
}

Output

h1 {
    background: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="40" fill="%23ff0"/></svg>');
}

h2 {
    background: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxjaXJjbGUgY3g9IjUwIiBjeT0iNTAiIHI9IjQwIiBmaWxsPSIjZmYwIi8+PC9zdmc+');
}

API

svgo([options])

options

encode

Type: boolean Default: undefined

If true, it will encode URL-unsafe characters such as <, > and &; false will decode these characters, and undefined will neither encode nor decode the original input. Note that regardless of this setting, # will always be URL-encoded.

plugins

Optionally, you can customise the output by specifying the plugins option. You will need to provide the config in comma separated objects, like the example below. Note that you can either disable the plugin by setting it to false, or pass different options to change the default behaviour.

const postcss = require('postcss');
const svgo = require('postcss-svgo');

const opts = {
    plugins: [{
        name: 'preset-default',
        params: {
            overrides: {
                removeViewBox: false,
                removeComments: false,
                cleanupNumericValues: {
                    floatPrecision: 2
                }
            }
        }
    }]
};


postcss([ svgo(opts) ]).process(css).then((result) => {
    console.log(result.css)
});

You can view the full list of plugins here.

Usage

See the PostCSS documentation for examples for your environment.

Contributors

See CONTRIBUTORS.md.

License

MIT © Ben Briggs

版本列表
8.0.3 2026-08-05
8.0.2 2026-08-04
8.0.1 2026-06-11
8.0.0 2026-05-06
7.1.3 2026-05-02
7.1.2 2026-04-20
7.1.1 2026-03-06
7.1.0 2025-07-14
7.0.2 2025-05-06
7.0.1 2024-06-05
7.0.0 2024-04-24
6.0.3 2024-03-06
6.0.2 2024-01-03
6.0.1 2023-12-14
6.0.0 2023-03-27
5.1.0 2022-03-01
5.0.4 2022-02-07
5.0.3 2021-11-02
5.0.2 2021-05-28
5.0.1 2021-05-19
5.0.0-rc.2 2021-03-15
5.0.0-rc.1 2021-03-05
5.0.0-rc.0 2021-02-19
5.0.0 2021-04-10
4.0.3 2021-04-06
4.0.2 2019-02-12
4.0.1 2018-09-25
4.0.0-rc.2 2017-08-06
4.0.0-rc.0 2017-06-01
4.0.0 2018-07-04
2.1.6 2016-12-17
2.1.5 2016-09-18
2.1.4 2016-07-11
2.1.3 2016-04-22
2.1.2 2016-02-12
2.1.1 2015-12-15
2.1.0 2015-11-25
2.0.4 2015-10-08
2.0.3 2015-09-24
2.0.2 2015-09-22
2.0.1 2015-09-11
2.0.0 2015-08-27
1.1.0 2015-07-18
1.0.1 2015-07-09
1.0.0 2015-07-09