Grunt - 建立任務



本章我們學習建立任務。每次執行 Grunt 時,都會指定一個或多個任務來執行,通知 Grunt 你希望它做什麼。如果你指定了預設任務,那麼它將預設執行。

別名任務

每當指定任務列表時,一個或多個其他任務可以透過新任務建立別名。執行別名將依次執行taskList中指定的每個任務。taskList引數應為任務陣列,如下所示:

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

例如,當你定義一個包含jshintconcatuglify任務的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 會在 config 物件中搜索具有任務名稱的屬性。

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;如果透過grunt log:bar執行,則將記錄bar: Welcome to tutorialspoint。當 Grunt 作為grunt log執行時,它將依次記錄foo: 1,2,3,然後bar: Welcome to tutorialspoint,最後sap: true

基本任務

每當你執行基本任務時,Grunt 不會搜尋配置或環境。而是執行指定的任務函式,並將作為函式引數指定的任何冒號分隔的引數傳遞。

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

在下面的示例中,如果 Grunt 透過grunt foo:testing:123命令執行,則任務將記錄foo, testing 123。每當任務在沒有引數的情況下執行為grunt foo時,任務將log foo, no 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']));
});  
廣告