E-Book, Englisch, 132 Seiten
Pillora / LLC Getting Started with Grunt: The JavaScript Task Runner
1. Auflage 2025
ISBN: 978-1-78398-063-5
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
If you know JavaScript you ought to know Grunt – the Task Runner for managing sophisticated web applications. From a basic understanding to constructing your own advanced Grunt tasks, this tutorial has it all covered.
E-Book, Englisch, 132 Seiten
ISBN: 978-1-78398-063-5
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
A step-by-step, practical tutorial to help you transform into a Grunt power-user. This book is for JavaScript developers who want to get to grips with GruntJS and use it to build and test their JavaScript applications. The only requirement for this book is a basic understanding of objects and functions in JavaScript.
Autoren/Hrsg.
Weitere Infos & Material
Real-world use cases
Hearing about the benefits of Grunt is all well and good, but what about actual use cases that the average web developer will face every day in the real world? In this section, we'll take an eagle-eye view of the most common use cases for Grunt.
These examples make use of configuration targets. Essentially, targets allow us to define multiple configurations for a task. We'll cover more on configuration targets in Chapter 2, .
Static analysis or Linting
In programming, the term linting is the process of finding bugs and/or style errors. Linting is more popular in dynamically typed languages as type errors may only be resolved at runtime. Douglas Crockford popularized JavaScript linting in 2011 with the release of his popular tool, JSLint.
JSLint is a JavaScript library, so it can be run in Node.js or in a browser. JSLint is a set of predetermined rules that enforce correct JavaScript coding practices. Some of these rules may be optionally turned on and off, however, many cannot be changed. A complete list of JSLint rules can be found at http://gswg.io#jslint-options.
This leads us to JSHint. Due to Douglas Crockford's coding style being too strict for some, has forked the JSLint project to create a similar, yet more lenient version, which he aptly named: JSHint.
I am a fan of and his book, (http://gswg.io#the-good-parts), but like Anton, I prefer a more merciful linter, so in this example below, we will use the Grunt plugin for JSHint: http://gswg.io#grunt-contrib-jshint.
In the preceding code, we first load the task. We then configure JSHint to run on the file itself, as well as all of the files in the directory and its subdirectories (which is in this case). We also set two JSHint options: , which ensures that curly braces are always used in , , and statements; and , which ensures that strict equality is always used.
JSHint has retained most of the optional rules from JSLint and it has also added many more. These rules can be found at: http://gswg.io#jshint-options.
Finally, we can run the task with , and we should see the following:
The result shows that JSHint found two warnings in the file on:
- Line 1, column 6—since we've enforced the use of strict equality, is not allowed, so it must be changed to .
- Line 1, column 14—since we've enforced the use of the curly braces, the statement body must explicitly use curly braces.
Once we've fixed these two issues as follows:
We can then re-run and we should see:
Notice that two files were reported to be lint free. The second file was the file, and if we review this file, we see it does not break either of the two rules we enabled.
In summary, JSHint is very useful as the first step of our Grunt build as it can help catch simple errors, such as unused variables or accidental assignments in statements. Also, by enforcing particular coding standards on the project's code base, it helps maintain code readability, as all code entering the shared repository will be normalized to a predetermined coding style.
Transcompilation
Transcompiling—also known as source-to-source compilation and often abbreviated to transpiling—is the process of converting the source code of one language to the source code of another. Within the web development community in recent years, there has been an increase in the use of transcompile languages such as Haml, Jade, Sass, LESS, Stylus, CoffeeScript, Dart, TypeScript, and more.
The idea of transcompiling has been around since the 1980s. A popular example was an original C++ compiler (Cfront) by Bjarne Stroustrup, which converted C++ (known as C with Classes at the time) to C.
CoffeeScript
CoffeeScript (http://gswg.io#coffeescript) is the most popular transpile language for JavaScript. It was released in 2009 by and is now the 10th most popular language on GitHub with 3 percent of the all code in public Git repositories. Due to this popularity, a particularly common use case for the modern web developer is to compile CoffeeScript to JavaScript. This can be easily achieved with the Grunt plugin http://gswg.io#grunt-contrib-coffee.
In the following example, we'll use the plugin to compile all of our CoffeeScript files:
Inside the configuration, the object has two properties; each of which defines a target. For instance, we might wish to have one target to compile the application source and another target to compile the unit test source. We'll cover more on tasks, multitasks, and targets in Chapter 2, .
In this case, the target will compile each file in the directory to a corresponding output file in the directory. We can execute this target explicitly with , which should produce the result:
Next, will compile and combine each of the files in the directory to a file in the directory called . We can execute this target with , which should produce the result:
Combining multiple files into one has advantages and disadvantages, which we shall review in the next section .
Jade
Jade (http://gswg.io#jade) compiles to HTML and, as with CoffeeScript to JavaScript, Jade has the semantics of HTML, though different syntax. , an extremely prolific open-source contributor, released Jade in July 2010. More information on the Grunt plugin for Jade can be found at http://gswg.io#grunt-contrib-jade.
We'll also notice the following example file is quite similar to the previous CoffeeScript example. As we will see with many Grunt plugins, both these examples define some kind of transform from one set of source files to another set of destination files:
In this example, will do a one-to-one compilation, where and will be compiled into and respectively. As we have set the task to be the task, we can run all of...




