├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── gruntfile.js ├── license ├── package.json ├── readme.md ├── tasks └── svgmin.js └── test ├── fixtures ├── test.svg └── test_02.svg └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | - 16 16 | steps: 17 | - uses: actions/checkout@v3 18 | - uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = grunt => { 4 | grunt.initConfig({ 5 | svgmin: { 6 | compile: { 7 | files: { 8 | 'test/tmp/test.svg': 'test/fixtures/test.svg', 9 | }, 10 | }, 11 | withconfig: { 12 | options: { 13 | plugins: [ 14 | { 15 | name: 'preset-default', 16 | params: { 17 | overrides: { 18 | removeViewBox: false, 19 | }, 20 | }, 21 | }, 22 | ], 23 | }, 24 | files: { 25 | 'test/tmp/withconfig.svg': 'test/fixtures/test.svg', 26 | }, 27 | }, 28 | multiple: { 29 | files: [{ 30 | expand: true, 31 | cwd: 'test/fixtures/', 32 | src: ['**/*.svg'], 33 | dest: 'test/tmp/', 34 | }], 35 | }, 36 | }, 37 | simplemocha: { 38 | test: { 39 | src: 'test/*.js', 40 | }, 41 | }, 42 | clean: { 43 | test: ['test/tmp'], 44 | }, 45 | }); 46 | 47 | grunt.loadTasks('tasks'); 48 | grunt.loadNpmTasks('grunt-contrib-clean'); 49 | grunt.loadNpmTasks('grunt-simple-mocha'); 50 | 51 | grunt.registerTask('default', ['clean', 'svgmin', 'simplemocha', 'clean']); 52 | }; 53 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-svgmin", 3 | "version": "7.0.0", 4 | "description": "Minify SVG", 5 | "license": "MIT", 6 | "repository": "sindresorhus/grunt-svgmin", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "engines": { 14 | "node": ">=14.16" 15 | }, 16 | "scripts": { 17 | "test": "xo && grunt" 18 | }, 19 | "files": [ 20 | "tasks" 21 | ], 22 | "keywords": [ 23 | "gruntplugin", 24 | "svg", 25 | "vector", 26 | "graphic", 27 | "image", 28 | "optimize", 29 | "minify" 30 | ], 31 | "dependencies": { 32 | "chalk": "^4.1.2", 33 | "log-symbols": "^4.1.0", 34 | "pretty-bytes": "^5.6.0", 35 | "svgo": "^3.0.2" 36 | }, 37 | "devDependencies": { 38 | "grunt": "^1.0.3", 39 | "grunt-cli": "^1.3.2", 40 | "grunt-contrib-clean": "^2.0.0", 41 | "grunt-simple-mocha": "^0.4.1", 42 | "xo": "^0.56.0" 43 | }, 44 | "peerDependencies": { 45 | "grunt": ">=1" 46 | }, 47 | "xo": { 48 | "rules": { 49 | "unicorn/prefer-module": "off" 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # grunt-svgmin 2 | 3 | > Minify SVG using [SVGO](https://github.com/svg/svgo) 4 | 5 | *Issues with the output should be reported on the SVGO [issue tracker](https://github.com/svg/svgo/issues).* 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install --save-dev grunt-svgmin 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | require('load-grunt-tasks')(grunt); 17 | 18 | grunt.initConfig({ 19 | svgmin: { 20 | options: { 21 | plugins: [ 22 | { 23 | name: 'preset-default', 24 | params: { 25 | overrides: { 26 | sortAttrs: false 27 | } 28 | } 29 | } 30 | ] 31 | }, 32 | dist: { 33 | files: { 34 | 'dist/unicorn.svg': 'app/unicorn.svg' 35 | } 36 | } 37 | } 38 | }); 39 | 40 | grunt.registerTask('default', ['svgmin']); 41 | ``` 42 | 43 | ### Options 44 | 45 | The provided options are passed directly to [SVGO](https://github.com/svg/svgo#configuration). 46 | 47 | ## Note 48 | 49 | Per-file savings are only printed in verbose mode (`grunt svgmin --verbose`). 50 | -------------------------------------------------------------------------------- /tasks/svgmin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const chalk = require('chalk'); 3 | const prettyBytes = require('pretty-bytes'); 4 | const logSymbols = require('log-symbols'); 5 | const {optimize} = require('svgo'); 6 | 7 | module.exports = grunt => { 8 | grunt.registerMultiTask('svgmin', 'Minify SVG', async function () { 9 | const done = this.async(); 10 | const options = this.options(); 11 | let totalSavedBytes = 0; 12 | 13 | for (const file of this.files) { 14 | const sourcePath = file.src[0]; 15 | const sourceSvg = grunt.file.read(sourcePath); 16 | 17 | let result; 18 | try { 19 | result = optimize(sourceSvg, { 20 | ...options, 21 | path: sourcePath, 22 | }); 23 | } catch (error) { 24 | grunt.warn(`${sourcePath}: ${error}`); 25 | continue; 26 | } 27 | 28 | const savedBytes = sourceSvg.length - result.data.length; 29 | const percentage = savedBytes / sourceSvg.length * 100; 30 | totalSavedBytes += savedBytes; 31 | 32 | grunt.verbose.writeln(logSymbols.success + ' ' + sourcePath + chalk.gray(' (saved ' + chalk.bold(prettyBytes(savedBytes)) + ' ' + Math.round(percentage) + '%)')); 33 | grunt.file.write(file.dest, result.data); 34 | } 35 | 36 | grunt.log.writeln(`Total saved: ${chalk.green(prettyBytes(totalSavedBytes))}`); 37 | done(); 38 | }); 39 | }; 40 | -------------------------------------------------------------------------------- /test/fixtures/test.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 23 | 25 | 27 | 29 | 31 | 34 | 37 | 40 | 42 | 44 | 47 | 49 | 50 | 51 | 54 | 56 | 58 | 59 | 62 | 67 | 69 | 71 | 73 | 74 | 77 | 79 | 80 | 81 | 82 | 92 | 93 | 94 | 99 | 100 | 101 | 107 | 108 | 109 | 110 | 111 | 113 | 114 | 115 | 121 | 122 | 123 | 127 | 129 | 130 | 132 | 133 | 138 | 142 | 146 | 147 | 149 | 151 | 153 | 155 | 157 | 160 | 163 | 166 | 168 | 170 | 173 | 175 | 176 | 177 | 180 | 182 | 184 | 185 | 190 | 192 | 194 | 196 | 197 | 200 | 202 | 203 | 204 | 205 | 215 | 220 | 221 | 222 | 227 | 228 | 229 | 235 | 236 | 237 | 238 | 239 | 241 | 242 | 243 | 249 | 250 | 251 | 255 | 257 | 259 | 262 | 263 | 265 | 266 | 271 | 275 | 279 | 280 | 281 | 289 | 290 | 292 | 293 | 294 | 295 | -------------------------------------------------------------------------------- /test/fixtures/test_02.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 23 | 25 | 27 | 29 | 31 | 34 | 37 | 40 | 42 | 44 | 47 | 49 | 50 | 51 | 54 | 56 | 58 | 59 | 62 | 67 | 69 | 71 | 73 | 74 | 77 | 79 | 80 | 81 | 82 | 92 | 93 | 94 | 99 | 100 | 101 | 107 | 108 | 109 | 110 | 111 | 113 | 114 | 115 | 121 | 122 | 123 | 127 | 129 | 130 | 132 | 133 | 138 | 142 | 146 | 147 | 149 | 151 | 153 | 155 | 157 | 160 | 163 | 166 | 168 | 170 | 173 | 175 | 176 | 177 | 180 | 182 | 184 | 185 | 190 | 192 | 194 | 196 | 197 | 200 | 202 | 203 | 204 | 205 | 215 | 220 | 221 | 222 | 227 | 228 | 229 | 235 | 236 | 237 | 238 | 239 | 241 | 242 | 243 | 249 | 250 | 251 | 255 | 257 | 259 | 262 | 263 | 265 | 266 | 271 | 275 | 279 | 280 | 281 | 289 | 290 | 292 | 293 | 294 | 295 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /* eslint-env mocha */ 3 | const assert = require('node:assert'); 4 | const fs = require('node:fs'); 5 | 6 | it('minifies SVG', () => { 7 | const original = fs.statSync('test/fixtures/test.svg').size; 8 | const minified = fs.statSync('test/tmp/test.svg').size; 9 | assert(minified < original); 10 | }); 11 | 12 | it('minifies svgo but preserves viewBox attribute', () => { 13 | const original = fs.statSync('test/fixtures/test.svg').size; 14 | const minified = fs.statSync('test/tmp/withconfig.svg').size; 15 | assert(minified < original); 16 | // The output svg should still contain the viewBox attribute 17 | const svg = fs.readFileSync('test/tmp/withconfig.svg').toString(); 18 | assert(svg.indexOf('viewBox="0 0 360 334.99"') > 0); 19 | }); 20 | --------------------------------------------------------------------------------