Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

grunt.task


May 25, 2021 Grunt


Table of contents


grunt.task

Register, execute, and load external tasks.

See task lib source and task util lib source for more information.

The task API

Grunt exposes many task-specific properties and this this task function through this object while a task is in the process of executing. See the In-Depth Task Insider guide, where you can find a complete list of properties and methods.

Many properties and methods can be this through this object.

Note that any unicode snowman ☃ can also be accessed directly grunt object. See API Home for more information.

Create a task

grunt.task.registerTask

Register an "alias task" or a task function. This method supports two types:

Alias task

If a task list is specified, the newly registered task will be the alias (alias) for one or more of the tasks. W hen this Alias Task is executed, all taskList are executed in the order specified. taskList parameter must be an array of tasks.

grunt.task.registerTask(taskName, taskList)

The following example shows that defining a "default" task, "jshint," "qunit," "concat," and "uglify" tasks are automated when Grunt is executed and tasks are not specified by parameters:

task.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);

You can also specify the parameters of the task. In the following case, the alias "dist" performs both "concat" and "uglify" tasks, and both specify the "dist" parameter:

task.registerTask('dist', ['concat:dist', 'uglify:dist']);

The task function

If description and taskFunction the specified taskFunction whenever the task runs. description I n addition, when grunt --help the description specified description is displayed. T he properties and methods of a particular task are accessible inside the task this properties of this object. If the task function returns false the task fails.

Note that grunt.task.registerMultiTask which can be used to define a particular type of task, "composite tasks," will be introduced later.

grunt.task.registerTask(taskName, description, taskFunction)

In the following case, when Grunt runs grunt foo:testing:123 the log foo, testing 123 If you run this task without parameters such as grunt foo log output foo, no args

grunt.task.registerTask('foo', 'A sample task that logs stuff.', function(arg1, arg2) {
  if (arguments.length === 0) {
    grunt.log.writeln(this.name + ", no args");
  } else {
    grunt.log.writeln(this.name + ", " + arg1 + " " + arg2);
  }
});

See the Create Tasks documentation for more task implementation cases and alias tasks.

This method can also be called through grunt.registerTask.

grunt.task.registerMultiTask

Sign up for a "multi-task". A composite task is a sub-attribute (that is, a target) that is all named sub-attributes that are included in a target when it is not specified. In addition to the default properties and methods, you can also this to composite tasks through this object.

Most of the contrib tasks, including jshint, concat, and uglify, are composite tasks.

grunt.task.registerMultiTask(taskName, description, taskFunction)

Given the following configuration information, the following composite task outputs the log foo: 1,2,3 the grunt grunt log:foo is executed, and the log bar: hello world grunt log:bar If you just grunt log the log foo: 1,2,3 is output first, bar: hello world and finally baz: false

grunt.initConfig({
  log: {
    foo: [1, 2, 3],
    bar: 'hello world',
    baz: false
  }
});

grunt.task.registerMultiTask('log', 'Log stuff.', function() {
  grunt.log.writeln(this.target + ': ' + this.data);
});

See create a task document for more examples of composite tasks.

This method can also be called through grunt.registerMultiTask.

grunt.task.requires

Fail the task if some other task failed or never ran.

grunt.task.requires(taskName);

grunt.task.exists

Added in 0.4.5

Check with the name, if a task exists in the registered tasks. Return a boolean.

grunt.task.exists(name)

grunt.task.renameTask

Rename the task. This function is useful if you want to override the default behavior of a task and want to keep the original name.

Note that if a task has been renamed, this.name and this.nameArgs properties change accordingly.

grunt.task.renameTask(oldname, newname)

This method can also be called through grunt.renameTask.

Load externally defined tasks

For most projects, many tasks may be defined in the Gruntfile file. For large projects or situations where tasks need to be shared across multiple projects, tasks can be loaded from one or more external directories, or from grunt plug-ins installed at npm.

grunt.task.loadTasks

Load task-related files from the specified directory (note: relative to the directory where Gruntfile is located). This method can load task-related files from the local Grunt plug-in, simply specifying a plug-in directory that contains the "tasks" subdirect directory.

grunt.task.loadTasks(tasksPath)

This method can also be called through grunt.loadTasks.

grunt.task.loadNpmTasks

Load the task from the specified Grunt plug-in. T his plug-in must be installed locally via npm and is the relative path to the Gruntfile file. Grunt plug-ins can also be created from the grunt-init grunt plug-in grunt init:gruntplugin

grunt.task.loadNpmTasks(pluginName)

This method can also be called by grunt.loadNpmTasks.

Queue task

Grunt automatically queues and executes tasks specified on the command line, but some unique tasks can add additional tasks to the queue that need to be performed.

grunt.task.run

Put one or more tasks in the queue. taskList is executed as soon as the current task is completed, in its order in the queue. A task list can be an array of tasks or a single task.

grunt.task.run(taskList)

grunt.task.clearQueue

Completely empty the task queue. No tasks will be performed unless additional tasks are queued.

grunt.task.clearQueue()

grunt.task.normalizeMultiTaskFiles

Standardize the configuration object of the target into an array of src-dest file maps. This method is called internally by the this.files /grunt.task.current.files property of the composite task system.

grunt.task.normalizeMultiTaskFiles(data [, targetname])