Grunt - 创建任务

在本章中,让我们了解创建任务.无论何时运行Grunt,都会指定一个或多个任务运行,通知Grunt您希望它执行什么操作.如果指定默认任务,则它将默认运行.

别名任务

每当任务列表如果指定,则新任务可以为一个或多个其他任务设置别名.运行别名将依次运行 taskList 中的每个指定任务. taskList 参数应该是一个任务数组,如下所示 :

grunt.registerTask(taskName, [description, ] taskList)	

例如,当您使用 jshint 定义 taskList 时, concat, uglify 任务并将 taskName 指定为默认,如果 Grunt,所有列出的任务将自动运行执行时没有指定任何任务.

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

您还可以指定任务参数,如下所示 :

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

在上面的任务中,别名 dist 同时运行 concat uglify 任务.

多任务

每当您运行多个任务时,Grunt会在Grunt配置中搜索同名属性.这些任务可以有多个配置,这些配置将使用任意命名的目标进行定义.

当您指定任务和目标时,只有指定的目标配置将是处理.

grunt concat:foo

以上命令将运行只有目标 foo .

当您只指定任务时,将处理所有目标.

grunt concat

上述命令将迭代 concat 任务的所有目标./p>

使用 grunt.task.renameTask 重命名任务时,Grunt会在配置对象中搜索具有 new 任务名称的属性.

grunt.initConfig({
   log: {
      foo: [1, 2, 3],
      bar: 'Welcome to IT屋',
      sap: true
   }
});

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

在上面的例子中,如果Grunt通过 grunt运行,多任务将记录 foo:1,2,3 log:foo 或者只要运行 grunt log:bar ,它就会记录 bar:欢迎来到it1352.它将记录 foo:1,2,3 然后 bar:欢迎来到it1352然后 sap:true 当Grunt以 grunt运行时log .

基本任务

每当您运行基本任务时,Grunt都不会搜索配置或环境.相反,它运行指定的任务函数,传递在函数参数中指定的任何以冒号分隔的参数.

grunt.registerTask(taskName, [description, ] taskFunction)	

在以下示例中,如果通过执行Grunt,任务会记录 foo,测试123 grunt foo:testing:123 命令.每当任务在没有参数的情况下运行 grunt foo 时,任务将记录foo,没有args .

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

自定义任务

如果您不想遵循多任务结构,可以定义你的自定义任务,如下所示 :

grunt.registerTask('default', 'My "default" task description.', function() {
  grunt.log.writeln('Currently running the "default" task.');
});

可以在另一个任务中运行任务,如下所示 :

grunt.registerTask('foo', 'My "foo" task.', function() {
   // Enqueue bar and baz tasks, to run after foo completes, in-order.
   grunt.task.run('bar', 'baz');
   // Or:
   grunt.task.run(['bar', 'baz']);
});

您还可以创建异步任务,如下所示 :

grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() {
   // Force task into async mode and grab a handle to the done() function.
   var done = this.async();
   // Run some sync stuff.
   grunt.log.writeln('Processing your task..');
   // Run some async stuff.
   setTimeout(function() {
      grunt.log.writeln('Finished!');
      done();
   }, 1000);
});

您可以创建可以访问其名称和参数的任务,如下所示 :

grunt.registerTask('foo', 'My task "foo" .', function(a, b) {
   grunt.log.writeln(this.name, a, b);
});

// Usage:
// grunt foo
//   logs: "foo", undefined, undefined
// grunt foo:bar
//   logs: "foo", "bar", undefined
// grunt foo:bar:baz
//   logs: "foo", "bar", "baz"

您可以创建任务,只要记录任何错误,任务就会失败,如下所示 :

grunt.registerTask('foo', 'My task "foo" .', function() {
   if (failureOfSomeKind) {
      grunt.log.error('This is an error message.');
   }

   // If this task had errors then fail by returning false
   if (ifErrors) { return false; }

   grunt.log.writeln('This is success message');
});

每当任务失败时,除非指定了  -  force ,否则每个后续任务都将被终止.

grunt.registerTask('foo', 'My task "foo" .', function() {
   // Fail synchronously.
   return false;
});

grunt.registerTask('bar', 'My task "bar" .', function() {
   var done = this.async();
   setTimeout(function() {
      // Fail asynchronously.
      done(false);
   }, 1000);
});

任务可以依赖于其他任务来成功执行.请记住, grunt.task.requires 实际上不会执行其他任务,而只会检查它是否已执行但未发生故障.

grunt.registerTask('foo', 'My task "foo" .', function() {
   return false;
});

grunt.registerTask('bar', 'My task "bar" .', function() {
   // Fail task if foo task failed or never ran.
   grunt.task.requires('foo');
   // This code executes if the foo task executed successfully.
   grunt.log.writeln('Hello, World.. Welcome to Tutorialspoint!..');
});

// Usage:
// grunt foo bar doesn't log, because foo failed to execute.
// **Note: This is an example of space-separated sequential commands,
// (similar to executing two lines of code: `grunt foo` then `grunt bar`)
// grunt bar doesn't log, because foo never ran.

当找不到所需的配置属性时,任务甚至会失败.

grunt.registerTask('foo', 'My task "foo" .', function() {
   // Fail task if meta.name config properties is missing
   // Format 1: String 
   grunt.config.requires('meta.name');
   // or Format 2: Array
   grunt.config.requires(['meta', 'name']);
   // Log... conditionally.
   grunt.log.writeln('This only log if meta.name is defined in the config.');
});

任务可以访问配置属性,如下所示 :

grunt.registerTask('foo', 'My task "foo" .', function() {
   // Log the value of the property. Returns null if the property is undefined.
   grunt.log.writeln('The meta.name property is: ' + grunt.config('meta.name'));
   // Also logs the value of the property. Returns null if the property is undefined.
   grunt.log.writeln('The meta.name property is: ' + grunt.config(['meta', 'name']));
});