Grunt scripts
What is Grunt?
Scripts for frequents tasks such as minify, compile and test.
Install via npm
1 | npm install -D grunt |
Configuration
via Gruntfile.js file in the actual directory, or the nearest directory if there is none.
The console for grunt is Grunt-cli and it can be installed with npm:
1
npm install –D -g grunt-cli
Parameters for “Grunt-cli”:
-
--gruntfile
: specify path for Gruntfile.js -
--force
: forces to continue after errors -
--tasks
: extra dir to scan for finding new taks --npm
: scans the new grunt plugins installed with npm to search for new tasks-
--verbose
: show more information about the program execution
-
Tasks
- A task is a set of commands which will be executed by grunt: if there is none, we will launch ‘default’.
- Tasks should be installed via npm before being registered
- The tasks configurations are set in
grunt.initConfig()
. Object keys are the tasks names (see line 40) - Tasks are loaded in the scheduler following the pattern (see also line 44):
grunt.registerTask(taskName, [description, ] tasklist);
- Multitasks are supported :
grunt.registerMultiTask(taskName, [description, ] taskFunction)
1 | module.exports = function(grunt){ |
- We can call subtasks of a multitask directly
1
2grunt concat //executes a multitask
grunt concat:foo //executes the foo sutask, which is part of concat - Tasks can call asynchronous functions.
1
2
3
4
5grunt.registerTask('asyncme', "My asynchronous task", function(){
// done will be executed once the task is finished
var done = this.async();
doSomethingAsync(done);
}
Mapping files
Parameters:
src
: source pathdest
: destination pathfilter
: file system native functions, to filter the files to load (e.g. supports the**
from the relative paths)nonull
: if true, it will include files which wont pass the filterdot
: add files whose name starts with the chracter ‘.’matchbase
: if it’s defined, the files will be searched starting on this direxpand
: processes a dynamically the src & dest mapping
Different file formats to define a task:
- Compact
1
2
3
4
5
6grunt.initConfig({
jshint: {
foo: {
src: ['src/aa.js', 'src/aaa.js']
}
}, - Object
1
2
3
4
5
6
7
8grunt.initConfig({
concat: {
foo: {
files: {
'dest/a.js': ['src/aa.js', 'src/aaa.js'],
'dest/a1.js': ['src/aa1.js', 'src/aaa1.js']}
},
}, - Array
1
2
3
4
5
6
7
8grunt.initConfig({
concat: {
foo: {
files: [
{src: ['src/aa.js', 'src/aaa.js'], dest: 'dest/a.js'},
{src: ['src/aa1.js', 'src/aaa1.js'], dest: 'dest/a1.js'}
],
},
- Compact
Dynamic mapping: we can even combine normal and dynamic mapping: we need to set the
expand
value to truecwd
: ruta del directorio donde se cargan los archivossrc
: regex to load filesdest
: destination to save the fileext
: reeplaces the file extension of the generated filesextDot
: points out where is the file extensionflatten
: deletes all the filepaths from the generated filesrename
: describes a function which returns a String with the new filesrc
anddest
1
2
3
4
5
6
7
8
9
10files: [
{
expand: true,
cwd: 'lib/',
src: ['**/*.js'],
dest: 'build/',
ext: '.min.js',
extDot: 'first'
},
],
Example
Step by step
- If we need to load any external JSON property we can get its content into a var via
1
grunt.file.readJSON('package.json')
- We can call any grunt property directly via
1
grunt.config.requires('meta.name')
- There are Grunt templates so we can use tags
<%= pkg.myvar %>
- To load Grunt tasks from npm we can use
loadNpmTasks(‘myTaskName’)
oloadTasks(‘myTaskName)
- Once loaded, we can register the defined task vis
grunt.registerTask()
- We export the grunt mocule with all the configuration:
module.exports = function(grunt) {…}
In a project
- Gruntfile.js should be at the root folder
- On node_modules we can see al the Grunt-related modules, which start with
grunt-*
- We can also this dependencies in package.json on devDependences
- To implement Grunt in any project with the
grunt-init
module we can create basic templates to start with. Don’t forget to configure its Gruntfile.js
So if we have:
1 | module.exports = function(){ |
And run grunt myTest
with the following Gruntfile.js
1 | module.exports = function(grunt){ |
We get as an output js-min/myTest.min.js
1 | module.exports = function(){ |
Other examples of defined task
1 | grunt.registerTask('install', [], installTaks); |