Web Building - Grunt

Grunt

About

grunt Grunt is a task runner that permits to perform build operations.

Installation

Global

npm update -g npm
  • Install the Grunt CLI. It will run the version of Grunt required by a Gruntfile
npm install -g grunt-cli

Project

See also the grunt-init

Package.json

{
  "name": "my-project-name",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-jshint": "~0.10.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-uglify": "~0.5.0"
  }
}

or

npm install grunt --save-dev
npm install grunt-contrib-jshint --save-dev
npm install grunt-contrib-uglify --save-dev
...

Gruntfile

This file is named Gruntfile.js or Gruntfile.coffee and is used to configure or define tasks and load Grunt plugins.

Plugins:

  • config: Most Grunt tasks rely on configuration data defined in an object passed to the grunt.initConfig method.
  • must be specified in package.json as a dependency.
module.exports = function(grunt) {
   "use strict";
   
  // Do grunt-related things in here
  
  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/<%= pkg.name %>.js',
        dest: 'build/<%= pkg.name %>.min.js'
      }
    }
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');

  // Default task(s).
  // It will run the uglify task
  // same as
  // grunt uglify 
  // or 
  // grunt default
  grunt.registerTask('default', ['uglify']);

  // A very basic custom default task.
  grunt.registerTask('default', 'Log some stuff.', function() {
    grunt.log.write('Logging some stuff...').ok();
  });
  
};

Custom project-specific tasks don't need to be defined in the Gruntfile; they may be defined in external .js files and loaded via the grunt.loadTasks method.

Task

grunt –help command will list all available tasks.

Documentation / Reference





Discover More
Card Puncher Data Processing
Code Shipping - Build

Build tool are tools that takes your sources: compile it into a target format such as a compiled file for a language that is not interpreted an HTML or Word document for a markdown document ...
Javascript - Transpiler (Transpiling)

A transpiler permits to take an language source and to transform it in a language target. The language source and target may be the same but with two differents version. compiling A transpiler permits...
Web - Build Operations / Build Pipeline

This page is the build operation for the web. A build pipeline will: transform (compile) bundle and optimize web code html, css (less, ...) Javascript (Javascript Module, React Jsx,...
Web Node Build - Task runner orchestrator

A Task runner (orchestrator) is a build tool that permits to run all kind of task (generally build operations) in serie or parallel. A task runner works on a stream of file selected from the file system...



Share this page:
Follow us:
Task Runner