grunt-contrib-imagemin
The first task I want to mention is grunt-contrib-imagemin. The reason why I want to discuss it first is that images are killing the web. Seriously! If you take a look at HTTParchive.org statistics you’ll see that images represent more than 63% of the total size of a page. The reason for such bloat is that often images aren’t compressed to have a weight as low as possible. grunt-contrib-imagemin is one of the tasks you can use to solve this issue. This task comes with the following optimizers to be able to compress most of the image formats employed on the web:- gifsicle to compress GIF images
- jpegtran to compress JPEG images
- optipng to compress PNG images
- svgo to compress SVG images
imagemin: {
dist: {
options: {
optimizationLevel: 5
},
files: [{
expand: true,
cwd: 'src/images',
src: ['**/*.{png,jpg,gif}'],
dest: 'dist/'
}]
}
}
This configuration allows for a high level of optimizations by using the optimizationLevel
option. This value ranges from 0 to 7, with 3 as the default. In this case the images optimized are those with the extension png, jpg, or gif, and located in the “src/images” folder and all its subfolders. The result of the optimization will be stored in the “dist” folder.
grunt-contrib-uglify
The grunt-contrib-uglify task is used to minify JavaScript files. This task removes all the unnecessary whitespace that your source code has, and renames the variables and functions consistently to use a name as short as possible. Some options that you’ll often use for this task aresourceMap
and banner
. The former creates a source map file in the same directory as the destination file. To enable this option you have to set it to true
(the default value is false
). banner
is a string to prepend to the minified output in which you usually write the name of the file/library/framework, its version, your name as the author, and the license. Its default value is an empty string.
To give you an idea of what a minified source looks like, let’s say that you have the following JavaScript code:
var MyApplication = function() {
var data = 'hello';
this.sum = function(first, second) {
return first + second;
}
this.showData = function() {
return data;
}
};
The minification process will convert it to the following code:
var MyApplication=function(){var a="hello";this.sum=function(a,b){return a+b},this.showData=function(){return a}};
An example configuration of this tool is shown below:
uglify: {
dist: {
options: {
sourceMap: true,
banner: '/*! MyLib.js 1.0.0 | Aurelio De Rosa (@AurelioDeRosa) | MIT Licensed */'
},
files: {
'dest/output.min.js': ['src/input.js'],
}
}
}
grunt-contrib-cssmin
As the name suggests, grunt-contrib-cssmin compresses CSS files. Like the grunt-contrib-uglify task, this one provides abanner
option.
A simple configuration for this task is:
cssmin: {
dist: {
options: {
banner: '/*! MyLib.js 1.0.0 | Aurelio De Rosa (@AurelioDeRosa) | MIT Licensed */'
},
files: {
'dist/css/style.min.css': ['src/css/**/*.css']
}
}
}
This example minifies all the CSS files stored in “src/css” and its subfolders, and stores the result in a single style sheet called “style.min.css” that is placed in the folder “dist/css”. In addition, the configuration adds a banner at the top of the minified file.
grunt-uncss
Another task that deals with CSS is grunt-uncss. This task removes unused CSS from a project, so it reduces the size of the final CSS file, and thus improves the download time. It’s particularly well suited if you’re developing using a framework like Boostrap or Foundation. This task has some important limitations that you can read on the official documentation. Some nice options worth mentioning areignore
, that allows us to specify a list of selectors that should not be removed, and ignoreSheets
, that allows us to specify style sheets to ignore.
An example use of this task is shown below:
uncss: {
dist: {
options: {
ignore: [/js-.+/, '.special-class'],
ignoreSheets: [/fonts.googleapis/],
},
files: {
'dist/css/unused-removed.css': ['src/index.html', 'src/contact.html', 'src/service.html']
}
}
}
grunt-contrib-htmlmin
The last Grunt task I want to discuss in this article is grunt-contrib-htmlmin, a task to minify HTML code. It doesn’t speed up your website a lot because it often saves just a few Kbs, and if you’re serving your content using gzip compression, the gain is even lower. Therefore, if you’re looking to minify your HTML, well… compliments; it means your website was already pretty optimized. Nonetheless, the philosophy to follow when dealing with performance on the web is that every Kb counts. So, let’s see a simple configuration that integrates this task into our workflow:htmlmin: {
dist: {
options: {
removeComments: true,
collapseWhitespace: true
},
files: [{
expand: true,
cwd: 'src',
src: '**/*.html',
dest: 'dist/'
}]
}
}
The code above processes all the pages placed in the “src” directory and its subfolders. For each of these pages, the task removes all the comments and collapses the spaces it finds, storing the results in the “dist” directory.
Conclusion
In this article I introduced you to five Grunt tasks to easily improve the performance of your website. They’re so simple that you really have no excuses to avoid using them and offer your users a better experience through a faster service. I hope you liked this article and you’ll employ these tasks soon. Have you ever used them? How much did they improve your website? Are there any tasks you love and want to share with us?Frequently Asked Questions (FAQs) about Grunt Tasks for Website Performance
What is the role of Grunt in improving website performance?
Grunt is a JavaScript task runner that automates repetitive tasks like minification, compilation, unit testing, and linting. It helps in improving the performance of a website by reducing the size of the files, thus making the website load faster. Grunt also helps in maintaining the quality of the code by running tasks like linting and unit testing, which can catch errors early in the development process.
How does Grunt minification improve website performance?
Minification is a process that removes unnecessary characters (like spaces, new lines, and comments) from source code without changing its functionality. This results in smaller file sizes, which means faster download times and improved website performance. Grunt has plugins like grunt-contrib-uglify for JavaScript minification and grunt-contrib-cssmin for CSS minification.
How does Grunt compilation help in website performance?
Compilation is the process of converting source code written in one language into another language. For example, Grunt can compile Sass files into CSS. This allows developers to write code in a more efficient and maintainable way, while still delivering optimized CSS to the end user. Grunt has plugins like grunt-contrib-sass for Sass compilation.
What is linting and how does Grunt help in it?
Linting is the process of running a program that will analyze code for potential errors. Grunt has plugins like grunt-contrib-jshint for JavaScript linting. Linting helps in maintaining a high quality of code by catching errors early in the development process.
How does Grunt help in unit testing?
Unit testing is a method of software testing that verifies the individual parts of the software. Grunt has plugins like grunt-contrib-jasmine for JavaScript unit testing. Unit testing helps in maintaining a high quality of code by ensuring that each individual part of the software works as expected.
How to install Grunt?
Grunt and Grunt plugins are installed and managed via npm, the Node.js package manager. To install Grunt, you need to first download and install node.js (which includes npm). npm stands for Node Package Manager, which is an online repository for the publishing of open-source Node.js projects.
What are some popular Grunt plugins?
Some popular Grunt plugins include grunt-contrib-uglify for JavaScript minification, grunt-contrib-cssmin for CSS minification, grunt-contrib-sass for Sass compilation, grunt-contrib-jshint for JavaScript linting, and grunt-contrib-jasmine for JavaScript unit testing.
How to configure Grunt tasks?
Grunt tasks are configured in a file named Gruntfile.js. This file is a valid JavaScript or CoffeeScript file that belongs in the root directory of your project, next to the package.json file, and should be committed with your project source.
Can Grunt be used with other task runners?
Yes, Grunt can be used in conjunction with other task runners like Gulp. While Grunt focuses on configuration, Gulp focuses on code. You can use Grunt for tasks where you need a lot of control over the configuration, and Gulp for tasks where you want to write code.
What are the alternatives to Grunt?
There are several alternatives to Grunt, including Gulp, Webpack, and npm scripts. Gulp is a task runner like Grunt, but it uses a code-over-configuration approach. Webpack is a module bundler, but it can also perform many of the same tasks as a task runner. npm scripts can be used to automate tasks by running shell commands in your package.json file.
I'm a (full-stack) web and app developer with more than 5 years' experience programming for the web using HTML, CSS, Sass, JavaScript, and PHP. I'm an expert of JavaScript and HTML5 APIs but my interests include web security, accessibility, performance, and SEO. I'm also a regular writer for several networks, speaker, and author of the books jQuery in Action, third edition and Instant jQuery Selectors.