├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2018 Rasmus Porsager 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![version](https://img.shields.io/npm/v/rollup-plugin-svgo.svg)]() [![license](https://img.shields.io/github/license/porsager/rollup-plugin-svgo.svg)]() 2 | 3 | # ✂️ `rollup-plugin-svgo` 4 | 5 | A rollup plugin to import svg files processed with [svgo](https://github.com/svg/svgo) 6 | 7 | > SVG files, especially those exported from various editors, usually contain a lot of redundant and useless information. This can include editor metadata, comments, hidden elements, default or non-optimal values and other stuff that can be safely removed or converted without affecting the SVG rendering result. 8 | 9 | ## Usage 10 | 11 | ```bash 12 | npm i -D rollup-plugin-svgo 13 | ``` 14 | 15 | ```js 16 | import svgo from 'rollup-plugin-svgo' 17 | 18 | export default { 19 | plugins: [ 20 | svgo(/* options */) 21 | ] 22 | } 23 | ``` 24 | 25 | ### Options 26 | 27 | If you want to skip any svgo processing you can pass 28 | ```js 29 | { 30 | raw: true 31 | } 32 | ``` 33 | in options. This will import the svg content as is. 34 | 35 | Other options are passed directly to svgo to toggle various svgo plugins. You can find all plugins here: https://github.com/svg/svgo#what-it-can-do 36 | 37 | Svgo options are a bit verbose to write, so see the defaults used below for how to do it: 38 | 39 | ### Defaults 40 | 41 | ```js 42 | { 43 | plugins: [{ 44 | removeViewBox: false 45 | }, { 46 | removeDimensions: true 47 | }] 48 | } 49 | ``` 50 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function svg(options = { 2 | plugins: [ 3 | 'removeDimensions', 4 | { 5 | name: 'preset-default', 6 | params: { 7 | overrides: { 8 | removeViewBox: false 9 | } 10 | } 11 | } 12 | ] 13 | }) { 14 | const optimize = options.raw 15 | ? x => Promise.resolve({ data: x }) 16 | : options.optimize || require('svgo').optimize 17 | 18 | return { 19 | name: 'svgo', 20 | transform: (code, id) => { 21 | if (id.endsWith('.svg')) { 22 | const result = options?.raw ? { data: code } : optimize(code, { path: id, ...options }) 23 | return { 24 | map: { mappings: '' }, 25 | code: 'export default ' + JSON.stringify(result.data) 26 | } 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-svgo", 3 | "version": "2.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "rollup-plugin-svgo", 9 | "version": "2.0.0", 10 | "license": "WTFPL", 11 | "dependencies": { 12 | "svgo": "2.8.0" 13 | } 14 | }, 15 | "node_modules/@trysound/sax": { 16 | "version": "0.2.0", 17 | "resolved": "https://registry.npmjs.org/@trysound/sax/-/sax-0.2.0.tgz", 18 | "integrity": "sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA==", 19 | "engines": { 20 | "node": ">=10.13.0" 21 | } 22 | }, 23 | "node_modules/boolbase": { 24 | "version": "1.0.0", 25 | "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz", 26 | "integrity": "sha1-aN/1++YMUes3cl6p4+0xDcwed24=" 27 | }, 28 | "node_modules/commander": { 29 | "version": "7.2.0", 30 | "resolved": "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz", 31 | "integrity": "sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==", 32 | "engines": { 33 | "node": ">= 10" 34 | } 35 | }, 36 | "node_modules/css-select": { 37 | "version": "4.3.0", 38 | "resolved": "https://registry.npmjs.org/css-select/-/css-select-4.3.0.tgz", 39 | "integrity": "sha512-wPpOYtnsVontu2mODhA19JrqWxNsfdatRKd64kmpRbQgh1KtItko5sTnEpPdpSaJszTOhEMlF/RPz28qj4HqhQ==", 40 | "dependencies": { 41 | "boolbase": "^1.0.0", 42 | "css-what": "^6.0.1", 43 | "domhandler": "^4.3.1", 44 | "domutils": "^2.8.0", 45 | "nth-check": "^2.0.1" 46 | }, 47 | "funding": { 48 | "url": "https://github.com/sponsors/fb55" 49 | } 50 | }, 51 | "node_modules/css-tree": { 52 | "version": "1.1.3", 53 | "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-1.1.3.tgz", 54 | "integrity": "sha512-tRpdppF7TRazZrjJ6v3stzv93qxRcSsFmW6cX0Zm2NVKpxE1WV1HblnghVv9TreireHkqI/VDEsfolRF1p6y7Q==", 55 | "dependencies": { 56 | "mdn-data": "2.0.14", 57 | "source-map": "^0.6.1" 58 | }, 59 | "engines": { 60 | "node": ">=8.0.0" 61 | } 62 | }, 63 | "node_modules/css-what": { 64 | "version": "6.0.1", 65 | "resolved": "https://registry.npmjs.org/css-what/-/css-what-6.0.1.tgz", 66 | "integrity": "sha512-z93ZGFLNc6yaoXAmVhqoSIb+BduplteCt1fepvwhBUQK6MNE4g6fgjpuZKJKp0esUe+vXWlIkwZZjNWoOKw0ZA==", 67 | "engines": { 68 | "node": ">= 6" 69 | }, 70 | "funding": { 71 | "url": "https://github.com/sponsors/fb55" 72 | } 73 | }, 74 | "node_modules/csso": { 75 | "version": "4.2.0", 76 | "resolved": "https://registry.npmjs.org/csso/-/csso-4.2.0.tgz", 77 | "integrity": "sha512-wvlcdIbf6pwKEk7vHj8/Bkc0B4ylXZruLvOgs9doS5eOsOpuodOV2zJChSpkp+pRpYQLQMeF04nr3Z68Sta9jA==", 78 | "dependencies": { 79 | "css-tree": "^1.1.2" 80 | }, 81 | "engines": { 82 | "node": ">=8.0.0" 83 | } 84 | }, 85 | "node_modules/dom-serializer": { 86 | "version": "1.3.2", 87 | "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-1.3.2.tgz", 88 | "integrity": "sha512-5c54Bk5Dw4qAxNOI1pFEizPSjVsx5+bpJKmL2kPn8JhBUq2q09tTCa3mjijun2NfK78NMouDYNMBkOrPZiS+ig==", 89 | "dependencies": { 90 | "domelementtype": "^2.0.1", 91 | "domhandler": "^4.2.0", 92 | "entities": "^2.0.0" 93 | }, 94 | "funding": { 95 | "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1" 96 | } 97 | }, 98 | "node_modules/domelementtype": { 99 | "version": "2.2.0", 100 | "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.2.0.tgz", 101 | "integrity": "sha512-DtBMo82pv1dFtUmHyr48beiuq792Sxohr+8Hm9zoxklYPfa6n0Z3Byjj2IV7bmr2IyqClnqEQhfgHJJ5QF0R5A==", 102 | "funding": [ 103 | { 104 | "type": "github", 105 | "url": "https://github.com/sponsors/fb55" 106 | } 107 | ] 108 | }, 109 | "node_modules/domhandler": { 110 | "version": "4.3.1", 111 | "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.3.1.tgz", 112 | "integrity": "sha512-GrwoxYN+uWlzO8uhUXRl0P+kHE4GtVPfYzVLcUxPL7KNdHKj66vvlhiweIHqYYXWlw+T8iLMp42Lm67ghw4WMQ==", 113 | "dependencies": { 114 | "domelementtype": "^2.2.0" 115 | }, 116 | "engines": { 117 | "node": ">= 4" 118 | }, 119 | "funding": { 120 | "url": "https://github.com/fb55/domhandler?sponsor=1" 121 | } 122 | }, 123 | "node_modules/domutils": { 124 | "version": "2.8.0", 125 | "resolved": "https://registry.npmjs.org/domutils/-/domutils-2.8.0.tgz", 126 | "integrity": "sha512-w96Cjofp72M5IIhpjgobBimYEfoPjx1Vx0BSX9P30WBdZW2WIKU0T1Bd0kz2eNZ9ikjKgHbEyKx8BB6H1L3h3A==", 127 | "dependencies": { 128 | "dom-serializer": "^1.0.1", 129 | "domelementtype": "^2.2.0", 130 | "domhandler": "^4.2.0" 131 | }, 132 | "funding": { 133 | "url": "https://github.com/fb55/domutils?sponsor=1" 134 | } 135 | }, 136 | "node_modules/entities": { 137 | "version": "2.2.0", 138 | "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz", 139 | "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==", 140 | "funding": { 141 | "url": "https://github.com/fb55/entities?sponsor=1" 142 | } 143 | }, 144 | "node_modules/mdn-data": { 145 | "version": "2.0.14", 146 | "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz", 147 | "integrity": "sha512-dn6wd0uw5GsdswPFfsgMp5NSB0/aDe6fK94YJV/AJDYXL6HVLWBsxeq7js7Ad+mU2K9LAlwpk6kN2D5mwCPVow==" 148 | }, 149 | "node_modules/nth-check": { 150 | "version": "2.0.1", 151 | "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-2.0.1.tgz", 152 | "integrity": "sha512-it1vE95zF6dTT9lBsYbxvqh0Soy4SPowchj0UBGj/V6cTPnXXtQOPUbhZ6CmGzAD/rW22LQK6E96pcdJXk4A4w==", 153 | "dependencies": { 154 | "boolbase": "^1.0.0" 155 | }, 156 | "funding": { 157 | "url": "https://github.com/fb55/nth-check?sponsor=1" 158 | } 159 | }, 160 | "node_modules/picocolors": { 161 | "version": "1.0.0", 162 | "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", 163 | "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==" 164 | }, 165 | "node_modules/source-map": { 166 | "version": "0.6.1", 167 | "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", 168 | "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", 169 | "engines": { 170 | "node": ">=0.10.0" 171 | } 172 | }, 173 | "node_modules/stable": { 174 | "version": "0.1.8", 175 | "resolved": "https://registry.npmjs.org/stable/-/stable-0.1.8.tgz", 176 | "integrity": "sha512-ji9qxRnOVfcuLDySj9qzhGSEFVobyt1kIOSkj1qZzYLzq7Tos/oUUWvotUPQLlrsidqsK6tBH89Bc9kL5zHA6w==" 177 | }, 178 | "node_modules/svgo": { 179 | "version": "2.8.0", 180 | "resolved": "https://registry.npmjs.org/svgo/-/svgo-2.8.0.tgz", 181 | "integrity": "sha512-+N/Q9kV1+F+UeWYoSiULYo4xYSDQlTgb+ayMobAXPwMnLvop7oxKMo9OzIrX5x3eS4L4f2UHhc9axXwY8DpChg==", 182 | "dependencies": { 183 | "@trysound/sax": "0.2.0", 184 | "commander": "^7.2.0", 185 | "css-select": "^4.1.3", 186 | "css-tree": "^1.1.3", 187 | "csso": "^4.2.0", 188 | "picocolors": "^1.0.0", 189 | "stable": "^0.1.8" 190 | }, 191 | "bin": { 192 | "svgo": "bin/svgo" 193 | }, 194 | "engines": { 195 | "node": ">=10.13.0" 196 | } 197 | } 198 | }, 199 | "dependencies": { 200 | "@trysound/sax": { 201 | "version": "0.2.0", 202 | "resolved": "https://registry.npmjs.org/@trysound/sax/-/sax-0.2.0.tgz", 203 | "integrity": "sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA==" 204 | }, 205 | "boolbase": { 206 | "version": "1.0.0", 207 | "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz", 208 | "integrity": "sha1-aN/1++YMUes3cl6p4+0xDcwed24=" 209 | }, 210 | "commander": { 211 | "version": "7.2.0", 212 | "resolved": "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz", 213 | "integrity": "sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==" 214 | }, 215 | "css-select": { 216 | "version": "4.3.0", 217 | "resolved": "https://registry.npmjs.org/css-select/-/css-select-4.3.0.tgz", 218 | "integrity": "sha512-wPpOYtnsVontu2mODhA19JrqWxNsfdatRKd64kmpRbQgh1KtItko5sTnEpPdpSaJszTOhEMlF/RPz28qj4HqhQ==", 219 | "requires": { 220 | "boolbase": "^1.0.0", 221 | "css-what": "^6.0.1", 222 | "domhandler": "^4.3.1", 223 | "domutils": "^2.8.0", 224 | "nth-check": "^2.0.1" 225 | } 226 | }, 227 | "css-tree": { 228 | "version": "1.1.3", 229 | "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-1.1.3.tgz", 230 | "integrity": "sha512-tRpdppF7TRazZrjJ6v3stzv93qxRcSsFmW6cX0Zm2NVKpxE1WV1HblnghVv9TreireHkqI/VDEsfolRF1p6y7Q==", 231 | "requires": { 232 | "mdn-data": "2.0.14", 233 | "source-map": "^0.6.1" 234 | } 235 | }, 236 | "css-what": { 237 | "version": "6.0.1", 238 | "resolved": "https://registry.npmjs.org/css-what/-/css-what-6.0.1.tgz", 239 | "integrity": "sha512-z93ZGFLNc6yaoXAmVhqoSIb+BduplteCt1fepvwhBUQK6MNE4g6fgjpuZKJKp0esUe+vXWlIkwZZjNWoOKw0ZA==" 240 | }, 241 | "csso": { 242 | "version": "4.2.0", 243 | "resolved": "https://registry.npmjs.org/csso/-/csso-4.2.0.tgz", 244 | "integrity": "sha512-wvlcdIbf6pwKEk7vHj8/Bkc0B4ylXZruLvOgs9doS5eOsOpuodOV2zJChSpkp+pRpYQLQMeF04nr3Z68Sta9jA==", 245 | "requires": { 246 | "css-tree": "^1.1.2" 247 | } 248 | }, 249 | "dom-serializer": { 250 | "version": "1.3.2", 251 | "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-1.3.2.tgz", 252 | "integrity": "sha512-5c54Bk5Dw4qAxNOI1pFEizPSjVsx5+bpJKmL2kPn8JhBUq2q09tTCa3mjijun2NfK78NMouDYNMBkOrPZiS+ig==", 253 | "requires": { 254 | "domelementtype": "^2.0.1", 255 | "domhandler": "^4.2.0", 256 | "entities": "^2.0.0" 257 | } 258 | }, 259 | "domelementtype": { 260 | "version": "2.2.0", 261 | "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.2.0.tgz", 262 | "integrity": "sha512-DtBMo82pv1dFtUmHyr48beiuq792Sxohr+8Hm9zoxklYPfa6n0Z3Byjj2IV7bmr2IyqClnqEQhfgHJJ5QF0R5A==" 263 | }, 264 | "domhandler": { 265 | "version": "4.3.1", 266 | "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.3.1.tgz", 267 | "integrity": "sha512-GrwoxYN+uWlzO8uhUXRl0P+kHE4GtVPfYzVLcUxPL7KNdHKj66vvlhiweIHqYYXWlw+T8iLMp42Lm67ghw4WMQ==", 268 | "requires": { 269 | "domelementtype": "^2.2.0" 270 | } 271 | }, 272 | "domutils": { 273 | "version": "2.8.0", 274 | "resolved": "https://registry.npmjs.org/domutils/-/domutils-2.8.0.tgz", 275 | "integrity": "sha512-w96Cjofp72M5IIhpjgobBimYEfoPjx1Vx0BSX9P30WBdZW2WIKU0T1Bd0kz2eNZ9ikjKgHbEyKx8BB6H1L3h3A==", 276 | "requires": { 277 | "dom-serializer": "^1.0.1", 278 | "domelementtype": "^2.2.0", 279 | "domhandler": "^4.2.0" 280 | } 281 | }, 282 | "entities": { 283 | "version": "2.2.0", 284 | "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz", 285 | "integrity": "sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==" 286 | }, 287 | "mdn-data": { 288 | "version": "2.0.14", 289 | "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz", 290 | "integrity": "sha512-dn6wd0uw5GsdswPFfsgMp5NSB0/aDe6fK94YJV/AJDYXL6HVLWBsxeq7js7Ad+mU2K9LAlwpk6kN2D5mwCPVow==" 291 | }, 292 | "nth-check": { 293 | "version": "2.0.1", 294 | "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-2.0.1.tgz", 295 | "integrity": "sha512-it1vE95zF6dTT9lBsYbxvqh0Soy4SPowchj0UBGj/V6cTPnXXtQOPUbhZ6CmGzAD/rW22LQK6E96pcdJXk4A4w==", 296 | "requires": { 297 | "boolbase": "^1.0.0" 298 | } 299 | }, 300 | "picocolors": { 301 | "version": "1.0.0", 302 | "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", 303 | "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==" 304 | }, 305 | "source-map": { 306 | "version": "0.6.1", 307 | "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", 308 | "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" 309 | }, 310 | "stable": { 311 | "version": "0.1.8", 312 | "resolved": "https://registry.npmjs.org/stable/-/stable-0.1.8.tgz", 313 | "integrity": "sha512-ji9qxRnOVfcuLDySj9qzhGSEFVobyt1kIOSkj1qZzYLzq7Tos/oUUWvotUPQLlrsidqsK6tBH89Bc9kL5zHA6w==" 314 | }, 315 | "svgo": { 316 | "version": "2.8.0", 317 | "resolved": "https://registry.npmjs.org/svgo/-/svgo-2.8.0.tgz", 318 | "integrity": "sha512-+N/Q9kV1+F+UeWYoSiULYo4xYSDQlTgb+ayMobAXPwMnLvop7oxKMo9OzIrX5x3eS4L4f2UHhc9axXwY8DpChg==", 319 | "requires": { 320 | "@trysound/sax": "0.2.0", 321 | "commander": "^7.2.0", 322 | "css-select": "^4.1.3", 323 | "css-tree": "^1.1.3", 324 | "csso": "^4.2.0", 325 | "picocolors": "^1.0.0", 326 | "stable": "^0.1.8" 327 | } 328 | } 329 | } 330 | } 331 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-svgo", 3 | "version": "2.0.0", 4 | "description": "Import & clean svg files and export their content as strings", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo please PR" 8 | }, 9 | "keywords": [ 10 | "rollup-plugin", 11 | "svg", 12 | "svgo" 13 | ], 14 | "license": "WTFPL", 15 | "author": "Rasmus Porsager ", 16 | "repository": "porsager/rollup-plugin-svgo", 17 | "dependencies": { 18 | "svgo": "2.8.0" 19 | } 20 | } 21 | --------------------------------------------------------------------------------