Grunt 创建任务
任务是Grunt的面包和奶油。就像你常用的工具,如: jshint 或 nodeunit。每当运行Grunt时, 你可以为其指定或多个任务, 这些任务用于告诉Grunt你想要它做什么事情。
如果你没有指定任务,并且你已经定义名为 "default" 的任务,那么该任务将会认被执行。
任务别名
如果指定了任务列表,新任务将是这或多个指定任务的别名。当运行此 "任务别名" 时,在taskList 中指定的每个任务都会按照其出现的顺序依次执行。taskList参数必须时任务数组。
grunt.registerTask(taskName, [description, ] taskList)
下面的任务别名案例中定义了 'default' 任务,如果运行Grunt时没有指定任何任务,它将执行'jshint'、'qunit'、'concat' 和 'uglify' 任务。
grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);
还可以给任务指定参数。在下面的案例中,别名 "dist" 将执行 "concat" 和 "uglify" 两个任务,并且它们都带有 "dist" 参数:
grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);
多任务
当运行多任务时,Grunt会从项目的配置对象中查找同名。多任务可以有多个配置,并且可以使用任意命名的'targets'。
同时指定像grunt concat:foo或者grunt concat:bar这样的任务和目标,在运行时Grunt只会处理指定目标的配置;然而如果运行grunt concat,将会遍历所有的目标, 并按任务指定的顺序处理每个目标。注意,如果任务已经使用grunt.task.renask过,Grunt将会在配置对象中查找新任务。
大部分的contrib任务(主要是指官方提供的任务),,以及都是多任务形式的。
grunt.registerMultiTask(taskName, [description, ] taskFunction)
对于指定的配置,这里有案例演示了如果通过grunt log:foo运行Grunt,它会foo: 1,2,3;如果通过grunt log:bar来运行Grunt, 它会bar: hello world。然而如果通过grunt log运行Grunt, 它会foo: 1,2,3,然后是bar: hello world,最后是baz: false(任务目标会按照指定的顺序进行处理)。
grunt.initCon({ log: { foo: [1, 2, 3], bar: 'hello world', baz: false } }); grunt.registerMultiTask('log', 'Log stuff.', function() { grunt.log.writeln(this.target + ': ' + this.data); });
Grunt 基本任务
当基本任务执行时,Grunt并不会检查配置和环境,Grunt 仅仅执行指定的任务,并传递任何使用冒号分割的参数作为的参数。
grunt.registerTask(taskName, [description, ] taskFunction)
下面的案例中,如果执行 grunt foo:testing:123,将日志 foo, testing 123。 如果执行这个任务时不传递参数,只是执行 grunt foo,那么将日志 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); } });
Grunt 任务
你可以和任务一起疯狂。如果你的任务并没有遵循 "多任务" 结构,那就使用任务。
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" finishes, 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 task...'); // And some async stuff. setTimeout(function() { grunt.log.writeln('All done!'); done(); }, 1000); });
任务也可以访问它们自身和参数。
grunt.registerTask('foo', 'My "foo" task.', function(a, b) { grunt.log.writeln(this.name, a, b); }); // : // grunt foo foo:bar // logs: "foo", undefined, undefined // logs: "foo", "bar", undefined // grunt foo:bar:baz // logs: "foo", "bar", "baz"
如果记录到任何,那么任务就会失败。
grunt.registerTask('foo', 'My "foo" task.', function() { if (failureOfSomeKind) { grunt.log.error('This is an error message.'); } // 如果这个任务出现则返回false if (ifErrors) { return false; } grunt.log.writeln('This is the success message'); });
当任务失败时,所有后续任务都将终止,除非指定 --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); });
任务还可以依赖于其他任务的成功执行。注意 grunt.task.requires 并不会真正的运行其他任务,它仅仅检查其它任务是否已经执行,并且没有失败。
grunt.registerTask('foo', 'My "foo" task.', function() { return false; }); grunt.registerTask('bar', 'My "bar" task.', function() { // 如果"foo"任务运行失败或者没有运行则任务失败。 grunt.task.requires('foo'); // 如果"foo"任务运行成功则执行这里的。 grunt.log.writeln('Hello, world.'); }); // : // grunt foo bar // 没有,因为"foo"失败。 // grunt bar // 没有,因为"foo"从未运行。
如果任务需要的配置不存在,其也可能失败。
grunt.registerTask('foo', 'My "foo" task.', function() { // Fail task if ".name" con prop is missing // Format 1: String grunt.con.requires('.name'); // or Format 2: Array grunt.con.requires(['', 'name']); // Log... conditionally. grunt.log.writeln('This will only log if .name is defined in the con.'); });
任务还可以访问配置。
grunt.registerTask('foo', 'My "foo" task.', function() { // 记录值,如果未定义(undefined)则返回null。 grunt.log.writeln('The .name property is: ' + grunt.con('.name')); // 同样的记录值,如果未定义(undefined)则返回null。 grunt.log.writeln('The .name property is: ' + grunt.con(['', 'name'])); });
在 中可以查看更多案例。
CLI 参数 / 环境
通过 process.env 来访问。
异步task没有完成的原因
如果发生这种情况,可能是由于你忘记 this.async 来告诉Grunt你的任务是异步的。为了简单起见,Grunt使用同步的编码风格,可以在task体中通过 this.async() 将其转换为异步的。
注意,传递 false 给 done() 就会告诉Grunt你的任务已经失败。
例如:
grunt.registerTask('asyncme', 'My asynchronous task.', function() { var done = this.async(); doSomethingAsync(done); });