Grunt 入门指南3:创建Tasks

创建tasks

Tasks 是grunt最重要的组成部分.使用率最高,像 jshint 或 nodeunit. 每次Grunt执行,你可以告诉Grunt指定的一个或者多个tasks会运行.

如果你没有指定执行的task,但是有一个task的名字叫"default", 这个task会默认执行.

Alias Task 任务别名

新的task可以通过指定一个task列表来给一个或多个其他task做别名. 当这个"alias task"运行, 在taskList中指定的tasks将按顺序执行.taskList参数必须是包含tasks的数组.

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

这个例子中 alias task 定义了一个 “default” task,里头的“jshint”,“qunit”,“concat”,“uglify”将会在Grunt没有指定task时默认执行.

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

Task 也可以指定参数.下面的例子中, alias "dist" 将会执行"concat"和"min"task,每个都使用"dist"参数.

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

Muti Tasks 多任务

当一个multi task执行,Grunt会在Grunt配置寻找同名的属性. Muti task可以用任意属性名定义多个配置.

grunt concat:foo 或者 grunt concat:bar 这样的task和target 只在指定的target配置上处理,当执行grunt concat时会迭代所有的targets,对每一个进行处理. 注意如果一个task被grunt.renametask 方法重命名后,Grunt将会按新的名字来寻找配置对象。

很多tasks,包括 grunt-contrib-jshint 插件的 jshint task, concat task 和 grunt-contrib-concat 插件的 concat task 都是muti tasks.

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

使用grunt.registerMultiTask可以动态注册muti task.

提供特定的参数,这个例子中,如果Grunt通过grunt log:foo指定,muti task会显示log foo:1,2,3 如果Grunt通过grunt log:bar则只会log bar: helo world .如果Grunt通过grunt log执行,则会log foo:1,2,3 然后是 bar:hello world 然后是 baz:false.

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

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

 "Basic" Tasks 基础任务

当一个 basic task 执行的时候,Grunt不会去查看配置或者环境--而只是执行指定的task函数,并传递用冒号分割的参数。

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

本例中,如果Grunt 通过grunt foo:testing:123来运行,则task log foo, testing 123 ,如果task不带参数运行grunt foo 则logs foo, no args.

grunt.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);
  }
});

Custom tasks

如果你不用“multi task”结构,也可以使用cusom task

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

在一个task中,你可以运行其他的tasks

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

Tasks 可以是异步的

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 task...');
  // And some async stuff.
  setTimeout(function() {
    grunt.log.writeln('All done!');
    done();
  }, 1000);
});

Tasks 可以访问自己的名字和参数

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

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

Tasks 可以在任意错误log之后fail

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

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

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

当一个tasks报告fail,所有后续的tasks将被终止,除非使用指定了--force参数

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

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

Tasks可以依赖其他task的执行结果. 注意grunt.task.requires并不是执行其他task.而只是检查它执行的结果是否失败.

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

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

// Usage:
// grunt foo bar
//   doesn't log, because foo failed.
// grunt bar
//   doesn't log, because foo never ran.

task 可以在必须的配置属性不存在时fail

grunt.registerTask('foo', 'My "foo" task.', function() {
  // Fail task if "meta.name" config prop is missing.
  grunt.config.requires('meta.name');
  // Also fails if "meta.name" config prop is missing.
  grunt.config.requires(['meta', 'name']);
  // Log... conditionally.
  grunt.log.writeln('This will only log if meta.name is defined in the config.');
});

task 可以访问配置的所有属性

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

更多的例子可以参考 contrib tasks

CLI options/ environment

TODO

为什么我的异步task不成功?

遇到这种情况是因为你忘记调用 this.async 方法来告诉Grunt这是一个异步task.基于简洁的目地,Grunt使用了同步的代码风格,你可以在task里面通过调用this.async()切换到异步模式.

注意,通过传递false参数给done()函数来告诉Grunt这个任务是fail的.

例子:

grunt.registerTask('asyncme', 'My asynchronous task.', function() {
  var done = this.async();
  doSomethingAsync(done);
});
原文地址:https://www.cnblogs.com/zhepama/p/3081077.html