Git Product home page Git Product logo

grunt-svgmin's Introduction

grunt-svgmin Build Status

Grunt tasks to minify SVG using SVGO

SVG files, especially exported from various editors, usually contains a lot of redundant and useless information such as editor metadata, comments, hidden elements, default or non-optimal values and other stuff that can be safely removed or converted without affecting SVG rendering result.

Getting Started

If you haven't used grunt before, be sure to check out the Getting Started guide, as it explains how to create a gruntfile as well as install and use grunt plugins. Once you're familiar with that process, install this plugin with this command:

npm install --save-dev grunt-svgmin

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-svgmin');

Tip: the load-grunt-tasks module makes it easier to load multiple grunt tasks.

Documentation

See the Gruntfile in this repo for a full example.

Example config (static)

grunt.initConfig({
	svgmin: {											// Task
		options: {										// Configuration that will be passed directly to SVGO
			plugins: [
			  { removeViewBox: false },
			  { removeUselessStrokeAndFill: false }
			]
		},
		dist: {											// Target
			files: {									// Dictionary of files
				'dist/figure.svg': 'app/figure.svg'		// 'destination': 'source'
			}
		}
	}
});

grunt.loadNpmTasks('grunt-svgmin');
grunt.registerTask('default', ['svgmin']);

Example config (dynamic)

grunt.initConfig({
	svgmin: {						// Task
		options: {					// Configuration that will be passed directly to SVGO
			plugins: [
			  { removeViewBox: false },
			  { removeUselessStrokeAndFill: false }
			]
		},
		dist: {						// Target
			files: [{				// Dictionary of files
				expand: true,		// Enable dynamic expansion.
				cwd: 'img/src',		// Src matches are relative to this path.
				src: ['**/*.svg'],	// Actual pattern(s) to match.
				dest: 'img/',		// Destination path prefix.
				ext: '.min.svg'		// Dest filepaths will have this extension.
				// ie: optimise img/src/branding/logo.svg and store it in img/branding/logo.min.svg
			}]
		}
});

grunt.loadNpmTasks('grunt-svgmin');
grunt.registerTask('default', ['svgmin']);

Available Options/Plugins

svgmin makes use of the standard SVGO plugin architecture. Therefore, to customize SVG optimisation, you can disable/enable any SVGO plugins listed at the SVGO repository.

To disable plugins with the Gruntfile.js, look for the plugin name at the SVGO repository and copy the plugin name (minus the file extension). Then set its value in the JSON to false in comma-separated objects. To exemplify, here is how the plugins section in the example configuration (illustrated above) might be written with some of the standard SVGO plugins disabled:

plugins: [
	{ removeViewBox: false }, 				// don't remove the viewbox atribute from the SVG
	{ removeUselessStrokeAndFill: false },	// don't remove Useless Strokes and Fills
	{ removeEmptyAttrs: false }				// don't remove Empty Attributes from the SVG
}]

Check each plugin for exports.active = [true/false] to see if the plugin is enabled. Most of the plugins are enabled by default but you may want to prevent a couple, particularly removeUselessStrokeAndFill as that may remove small details with subtracted / extruded complex paths.

License

MIT © Sindre Sorhus

grunt-svgmin's People

Contributors

sindresorhus avatar felthy avatar sapegin avatar benfrain avatar charleslouis avatar pk-nb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.