Ankur Marwaha
Ankur Marwaha

Reputation: 1885

How grunt command works?

What command is actually executed? Like using npm start with following included in package.json

  "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "start": "node index.js"
  }

npm start runs node index.js behind the scenes,

Similarly, what does grunt run (behind the scenes)?

Upvotes: 0

Views: 361

Answers (2)

Matthew Daly
Matthew Daly

Reputation: 9476

When you call the Grunt task runner, it runs whatever Grunt plugins you have specified in your Gruntfile in the order you specify.

A Grunt plugin consists of a single task file. That file is essentially nothing but a Node.js script that carries out the task in question. It has access to the settings passed to the plugin and can use Grunt's file API to access the filesystem, but otherwise it's just a Node.js script.

It's not hard to write a Grunt plugin and if you're interested in learning more about Grunt it's a good way to become more familiar with it. I personally have written several static site generators as Grunt plugins and it was very useful. Grunt task file ie. gruntfile.js looks something like this,

module.exports = function(grunt) {

  // 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).
  grunt.registerTask('default', ['uglify']);

};

when you run command grunt uglify basically it runs the task defined under uglify.You can find more in their get started guide here

Upvotes: 2

Bamieh
Bamieh

Reputation: 10906

grunt command executes Gruntfile.js, it does this through node, however it passes grunt configurations and functions to the plugins used inside Gruntfile. thats why you write

module.exports = function(grunt) {
   // grunt parameter is passed from grunt-cli
   // which contains grunt functions and utilities
   // that you can use to configure the tasks etc.
});

Upvotes: 1

Related Questions