Build Tools Basic

Grunt vs gulp vs Webpack

Introduction
With the increasing complexity of Javascript projects and the need to automate tasks, track app performances and execute some maintenance activities, it has become very salient to decide what type of build tool to adopt. Before build tools became a necessity, it was not clear how to handle routine tasks. This was not very necessary because Javascript did not particularly need to be compiled; however, with projects like single page applications, and the advent of build tools, tasks that reoccur are now automated. Seeing that it has become more than a necessity to use build tools, software developers in this present time are faced with the challenge of picking one which is perfect for their project.

In this article, we will be comparing Grunt, Gulp and Webpack in relation to the following features:

Ease of Use
Popularity
Reliability
Extensibility

Grunt and Gulp are task runners, while Webpack is a module bundler. Task runners are basically used to automate tasks in a development process. Some of these tasks include compressing JS files, compiling Sass files, watching out for file changes, minifying files and auto-prefixing. Module bundlers take several modules of the application that each have dependencies and bundle them into static assets. Module bundlers perform the task of task runners, and take several steps further. Hopefully at the end of the article, it will be clearer what build tool to use for your project.

Ease of Use
The more complex a tool is, the more difficult it is to use it. Being task runners, Gulp and Grunt offer features like code minification, CSS preprocessing, unit testing and a list of others.

Compared to Grunt or Webpack, Gulp is a lot easier to use. It is possible to start up a project using Gulp in a few minutes. Grunt, however, has less configuration hassle than Webpack.

To set Gulp up, the following steps are taken


Check that node, npm, and npx are installed
Install Gulp CLI using npm install --global gulp-cli
Create a package.json file for devDependencies
Install gulp package into the devDependencies using npm install --save-dev gulp@next
Create a gulp.js file where the tasks will be written function defaultTask(cb) { // place code for your default task here cb(); } exports.default = defaultTask

In order to use Grunt, you have to do the following:


Update your npm [ Node.js package manager] using npm update -g npm
Install Grunt CLI [Command line interface] using npm install -g grunt-cli
Setup your package.json file where your devDependencies are listed
Create a Gruntfile.js file where the tasks are written. The tasks are always written within this block: module.exports = function(grunt) { // Grunt tasks go here };

The processes for setting up Gulp and Grunt look similar. However, Gulp is more expressive; it allows you to write code that clearly states its function. Gulp is based on Node streams, allowing for pipelining. Grunt, on the other hand, relies on data configuring files where every source and destination file must be declared. As the project gets larger, Grunt becomes even more cumbersome to manage.

If, for instance you need to compile Sass, minify the resultant CSS file and Uglify your JS file using Grunt, the Gruntfile.js file will look like this


module.exports = function (grunt) {
    grunt.initConfig({
        sass: {
            dist: {
                files: {
                    'dist-grunt/css/style.css': 'assets/scss/style.scss'
                }
            }
        },
        cssmin: {
            dist: {
                files: {
                    'dist-grunt/css/styles.min.css': ['dist-grunt/css/styles.css']
                }
            }
        },
        uglify: {
            dist: {
                files: {
                    'dist-grunt/js/scripts.min.js': ['dist-grunt/js/scripts.js']
                }
            }
        },
        grunt.loadNpmTasks('grunt-sass');
        grunt.loadNpmTasks('grunt-contrib-cssmin');
        grunt.loadNpmTasks('grunt-contrib-uglify');
        grunt.registerTask('default', ['sass', 'cssmin', 'uglify']);
};

On the other hand, if the same process is to be done with Gulp, the gulp.js will look like this:


var gulp = require('gulp');
var sass = require('gulp-sass');
var cleanCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');

gulp.task('sass', function () {
    return gulp.src('assets/scss/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('dist-gulp/css'));
});

gulp.task('css', ['sass'], function () {
    return gulp.src([
            'dist-gulp/css/style.css',
            'assets/css/test.css'
        ])
        .pipe(cleanCSS())
        .pipe(gulp.dest('dist-gulp/css'));
});
gulp.task('default', ['css']);

Gulp and Grunt are both easier to set up when compared to Webpack.

In order to use Webpack, you have to do the following:


Install Nodejs and Npm
In your terminal, create a Webpack folder using mkdir folder-name
Intialise the Nodejs project using npm init -y
Create the following files touch index.html webpack.config.js index.js
Install Webpack and Webpack dev server using npm i --save-dev webpack@latest webpack-dev-server@latest
Install Webpack globally npm i -g webpack@latest
Write Webpack’s configuration in the webpack.config.js file.

const webpack = require('webpack');

let config = { entry: './index.js', output: { filename: 'output.js' } } module.exports = config;

The set up above is the basic start off for Webpack. Webpack has various useful features and can do so much more. The plethora of possibilities uncovered with Webpack might justify its complexities. It takes more time to understand how Webpack works; it can also get cumbersome when managing several configurations for multiple environments.

Popularity
The metrics for measuring the popularity of each of these tools differ. Grunt has been in existence long before Gulp, and many developers have used it extensively. However, according to the State of JavaScript 2018 survey, Webpack ranks higher than Gulp and Grunt.

There is a growing interest in learning to use Webpack; this is justifiable seeing the wide range of possibilities made available through its use.

Also using Github stats, Webpack, Gulp and Grunt have above 41,000, 30,000 and 11,000 stars respectively.

Reliability
Gulp, Grunt and Webpack have a large dependence on plugins. All three tools have large communities where issues could be addressed and fixed. For Gulp and Grunt, there is a high dependence on the plugin author to maintain the plugins, and sometimes, there is little or no documentation to fall back on when an issue is being faced. The result is usually to wait for these authors to make updates or resolve to fix them yourself. However, most Webpack plugins are maintained by Webpack’s core team while the rest are managed by Webpack’s community; and this makes it more reliable.

Extensibility
Gulp, Grunt and Webpack have been improved upon over the past years. They all have more room for newer and even more efficient and standardised plugins.

Some Grunt users are beginning to adopt Gulp because it allows piping, and that explains why Grunt is said to soon have an update that also allows piping. Piping allows gulp users to attach the output of a task to another dependency, making the code shorter and to work faster.

Presently, Gulp seems more extensible than Grunt because when the project scales, it is not so difficult to manage the gulp file. Gulp uses plugins that each perform a single task as opposed to Grunt where one plugin can perform multiple tasks.

Webpack can be extended even beyond its core functionality with the aid of plugins and loaders, the only issue being the complications that may arise while configuring it. However, since Webpack bundles all the modules that have dependencies, it is not so painstaking to work on even large Webpack projects.

At the time of writing, Grunt has 6,459 plugins in its plugin registry, while Gulp has 3,672 plugins listed on its own plugin registry. With so many plugins for both, you are likely to find a plugin to do whatever you need. Webpack has its own set of official plugins listed here, and a list of other third-party plugins that could be found at awesome-webpack.

Conclusion
Webpack is similar to a combination of Gulp/Grunt and Browserify. Webpack is somewhat necessary for projects that have intentions of scaling. That is why it is often used with React or Angular.

Grunt and Gulp could be used when the project is moderate, or fairly large. If you are really comfortable writing JS functions, then Gulp is advisable, but, if you will rather write task configurations, then Grunt will suffice.

It is important to know that every one of the tools discussed here is created to solve specific problems, and hence, the choice of tool should be based on preference as well as the nature of the pending problem to be solved.

Webpack vs Gulp vs Grunt: What are the differences?

Gulp and Grunt are JavaScript task runners, while Webpack is a bundler. Gulp and Grunt are more direct competitors to one another than either is to Webpack, though the three tools can serve many of the same functions.

Webpack has been seeing more use in place of Gulp and Grunt; it appears in more overall stacks than either of the two task runners, and is mentioned in nearly twice the number of job posts. It’s also far more popular on GitHub.

What are some alternatives to Grunt, gulp, and Webpack?

npm
npm is the command-line interface to the npm ecosystem. It is battle-tested, surprisingly flexible, and used by hundreds of thousands of JavaScript developers every day.
Yarn
Yarn caches every package it downloads so it never needs to again. It also parallelizes operations to maximize resource utilization so install times are faster than ever.
Gradle
Gradle is a build tool with a focus on build automation and support for multi-language development. If you are building, testing, publishing, and deploying software on any platform, Gradle offers a flexible model that can support the entire development lifecycle from compiling and packaging code to publishing web sites.
Apache Maven
Maven allows a project to build using its project object model (POM) and a set of plugins that are shared by all projects using Maven, providing a uniform build system. Once you familiarize yourself with how one Maven project builds you automatically know how all Maven projects build saving you immense amounts of time when trying to navigate many projects.
Bower
Bower is a package manager for the web. It offers a generic, unopinionated solution to the problem of front-end package management, while exposing the package dependency model via an API that can be consumed by a more opinionated build stack. There are no system wide dependencies, no dependencies are shared between different apps, and the dependency tree is flat.

Basic

There are several other options you can use with the Babel CLI. But, as you can probably imagine, figuring out how to configure them, on top of whatever other tools you’re using, can become unwieldly.

For us, running babel –watch is enough. But for any nontrivial project, you’ll have a number of build steps: Transpiling, minifiying, organizing your outputs for distribution, determining what needs to be updated, optimizing images . . . It’s as much of a nightmare as it sounds like.

If you use the CLI directly, you’d have to run through each step manually, or script the process yourself. That second option is definitely viable, but it can be:

  • Brittle
  • Difficult for teams; and
  • Unportable across platforms.

This is where build tools come in. Grunt and Gulp are popular systems that streamline orchestration of the build process. Bundlers and module loaders like Webpack, JSPM, and Browserify — which you can read about in another article of mine — take care of transpilation alongside all of the other magic they work.

I’ve used Browserify to transpile tests written in CoffeeScript to plain JS; delete the output after the tests run; and bundle my code with Babel if they all pass; and put everything in the right place, with source maps, along the way. That’s all possible using the command-line tools directly, but it’s brittle.

Reference

Reference

Build Tools

Leave a Reply

Your email address will not be published. Required fields are marked *