May 25, 2021 Grunt
1. Methods/attributes that are available within all tasks
2. Methods/attributes that are available internally in the multitasth form
When a task is executed, Grunt exposes
this
properties and methods to this task function through this object.
The object will also be
grunt.task.current
in
templates,
for
this.name
property can
grunt.task.current.name
the template.
If a task is asynchronous, this method must be called to notify Grunt. T
his method returns a "done" function that should be called after the task has been executed.
false
Error
objects can be passed to the done function to notify Grunt that this task has failed to execute.
If
this.async
is not called, this task is executed synchronously.
// Tell Grunt this task is asynchronous.
var done = this.async();
// Your async code.
setTimeout(function() {
// Let's simulate an error, sometimes.
var success = Math.random() > 0.5;
// All done!
done(success);
}, 1000);
If a task depends on the successful execution of another (or some) task, this method can be used to force Grunt to exit if the task it depends on does not run or fails to run. As an argument to this method, the task list it depends on can be an array of multiple task names or a single task name.
Note that this method does not actually run tasks in the specified task list, it only notifies the system that the current task has failed when the tasks in the task list do not run successfully.
this.requires(tasksList)
This method can specify that one or more strings or arrays of configuration properties are required. If one or more of the required configuration properties is missing, the system is notified that the current task has failed.
this.requiresConfig(prop [, prop [, ...]])
Check out the grunt.config documentation to learn more about configuring properties.
This method is an alias of the grunt.config.requiress method.
The name of the current task is the
grunt.registerTask
For example, if you run a task called "sample" as
grunt sample
or
grunt sample:foo
this.name
is
"sample"
Note that if you rename a task with grunt.task.renameTask, this property will also change to a new name.
The name of the current task, including any parameters or tags specified on the command line that are split with commas.
For example, if you run a task called "sample" as
grunt sample:foo
this.nameArgs
is
"sample:foo"
Note that if a task has been renamed using the grunt.task.renameTask method, this property also points to the corresponding new name.
An array of parameters passed to the current task.
For example, if you run a task called "sample" as
grunt sample:foo:bar
this.args
task function is
["foo", "bar"]
Note that in multitaste, the current target (name) is
this.args
array.
An object generated from parameters passed to the current task.
For example, if you run a task called "sample" in the form of
grunt sample:foo:bar
this.flags
task function is
{foo: true, bar: true}
Note that in multitasth, the task target name is not set to a tag.
The number of times the grunt.log.error method was called during the current task execution. If there is an error message output while the task is running, it can be used to cause the task to fail.
Returns an options object.
defaultsObj
is an optional argument whose properties are
options
of
options
object;
this.options([defaultsObj])
Here's an example of how the
this.options
task:
var options = this.options({
enabled: false,
});
doSomething(options.enabled);
In the Configuration Tasks guide, there is an example of how to specify options from the perspective of a user's task.
In a multitaser form, this property contains the name of the target that is currently being traversed.
For example, if a multitaser named "sample" has configuration data such as
{sample: {foo: "bar"}}
when the task is run as
grunt sample:foo
this.target
is
"foo"
In a multitask format, files specified using Grunt-supported file formats and options, wildcard mode, or dynamic mapping are automatically standardized to a unique format: the file array format.
This means that tasks do not need to contain a large number of templates for explicit processing of custom file formats, wildcard formats, source file-to-target file mapping, or filtering output files or directories. b21> files according to the instructions in the Configuration Tasks guide, and Grunt automatically handles all task details.
The this.files
属性永远都是一个数组。你的任务应该利用数组中每个对象的
with
和
src and dest
属性遍历
object in
数组。在你的任务关注每个目标文件的多个源文件的情况下,
on multiple source files for each target file.
Note that files that may not exist are also included in the value of
src
so you should check for the existence of the source file before using it.
The following example shows how a simple "concat" task uses
this.files
property:
this.files.forEach(function(file) {
var contents = file.src.filter(function(filepath) {
// Remove nonexistent files (it's up to you to filter or warn here).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
}).map(function(filepath) {
// Read and return the file's source.
return grunt.file.read(filepath);
}).join('\n');
// Write joined contents to destination filepath.
grunt.file.write(file.dest, contents);
// Print a success message.
grunt.log.writeln('File "' + file.dest + '" created.');
});
If you also need to use the properties of the original file object, you can get
orig
of each individual file object, but no use cases have been found to access the original properties.
In multitask, files specified
in
src
through any file format are grouped into an array.
If your task is "read-only" and you don't care about the target file path, you can use this array
this.files
Here's an example of how a simple "lint" task uses
this.filesSrc
property:
// Lint specified files.
var files = this.filesSrc;
var errorCount = 0;
files.forEach(function(filepath) {
if (!lint(grunt.file.read(filepath))) {
errorCount++;
}
});
// Fail task if errors were logged.
if (errorCount > 0) { return false; }
// Otherwise, print a success message.
grunt.log.ok('Files lint free: ' + files.length);
In the multitasunded form, this is the actual data stored in the Grunt configuration object for a given target.
For example, if a multitaser named "sample" has configuration data such as
{sample: {foo: "bar"}}
when the task is run as
grunt sample:foo
this.data
"bar"
this.options
this.files
this.filesSrc
recommended
this.data
because their values are standardized.