Grunt教程

Grunt 创建任务

在本章中,让我们学习 创建任务。每当您运行 Grunt 时,都会指定一个或多个任务来运行,以通知 Grunt 您希望它做什么。如果您指定 默认任务,那么它将默认运行。

别名任务

每当指定任务列表时,一个或多个其他任务可以被一个新任务命名。运行别名将依次运行 taskList 中的每个指定任务。 taskList 参数应该是一个任务数组,如下所示-
grunt.registerTask(taskName, [description, ] taskList)	
例如,当您使用 jshintconcat、uglify 任务定义 taskList 并指定 taskName 作为 default,如果 Grunt没有指定任何任务被执行,所有列出的任务将自动运行。
grunt.registerTask('default', ['jshint', 'concat', 'uglify']);
您还可以指定任务参数,如下所示-
grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);	
在上述任务中,别名 dist 运行 concatuglify 任务。

多任务

每当您运行多个任务时,Grunt 都会在 Grunt 配置中搜索同名的属性。这些任务可以有多个配置,将使用任意命名的 目标来定义。
当您同时指定任务和目标时,只会处理指定的目标配置。
grunt concat:foo
上面的命令只会运行目标 foo
当你只指定一个任务时,所有的目标都会被处理。
grunt concat
以上命令将遍历 concat任务的所有目标。
当您使用 grunt.task.renameTask 重命名任务时,Grunt 在配置对象中搜索具有 new 任务名称的属性。
grunt.initConfig({
   log: {
      foo: [1, 2, 3],
      bar: 'Welcome to tutorialspoint',
      sap: true
   }
});
grunt.registerMultiTask('log', 'Log stuff.', function() {
   grunt.log.writeln(this.target + ': ' + this.data);
});
在上面的例子中,如果 Grunt 通过 grunt log:foo 运行,多任务将记录 foo: 1,2,3 或者它会记录 bar :每当运行 grunt log:bar 时,欢迎来到 tutorialspoint。它将记录 foo: 1,2,3 然后 bar: Welcome to tutorialspoint 然后 sap: true 当 Grunt 作为 grunt 运行时日志.

基本任务

无论何时运行基本任务,Grunt 都不会搜索配置或环境。相反,它运行指定的任务函数,传递任何以冒号分隔的参数作为函数参数。
grunt.registerTask(taskName, [description, ] taskFunction)	
在以下示例中,如果通过 grunt foo:testing:123 命令执行 Grunt,任务会记录 foo, testing 123。每当任务在没有参数的情况下以 grunt foo 运行时,任务将 记录 foo,无参数
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']));
});  
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4