Grunt-creating-tasks

提供:Dev Guides
移動先:案内検索

Grunt-タスクの作成

この章では、_タスクの作成_について学びましょう。 Gruntを実行するたびに、Gruntに実行したいことを通知する1つ以上のタスクを実行するように指定します。 _default task_を指定すると、デフォルトで実行されます。

エイリアスタスク

タスクのリストを指定するたびに、1つ以上の他のタスクに新しいタスクのエイリアスを作成できます。 エイリアスを実行すると、_taskList_で指定されたすべてのタスクが順番に実行されます。 _taskList_引数は、以下に示すようにタスクの配列でなければなりません-

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

たとえば、jshint _、 concat、_、および_uglify_タスクで_taskList_を定義し、_taskName_を_default_として指定すると、_Grunt_がタスクを指定せずに実行された場合、リストされたすべてのタスクが自動的に実行されます。

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

また、以下に示すようにタスクの引数を指定することができます-

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

上記のタスクでは、エイリアス_dist_は_concat_と_uglify_の両方のタスクを実行します。

マルチタスク

複数のタスクを実行するたびに、GruntはGrunt設定で同じ名前のプロパティを検索します。 これらのタスクには、_targets_という任意の名前を使用して定義される複数の構成を含めることができます。

タスクとターゲットの両方を指定すると、指定されたターゲット構成のみが処理されます。

grunt concat:foo

上記のコマンドは、ターゲット_foo_のみを実行します。

タスクのみを指定すると、すべてのターゲットが処理されます。

grunt concat

上記のコマンドは、_concat_タスクのすべてのターゲットを反復処理します。

_grunt.task.renameTask_を使用してタスクの名前を変更すると、_new_タスク名のプロパティがGruntによって設定オブジェクトで検索されます。

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

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

上記の例では、Gruntが_grunt log:foo_を介して実行された場合、マルチタスクは_foo:1,2,3_をログに記録します。 Gruntが_grunt log_として実行されている場合、foo:1,2,3、次に_bar:finddevguides_、次に_sap:true_のログが記録されます。

基本的なタスク

基本タスクを実行するたびに、Gruntは構成または環境を検索しません。 代わりに、指定されたタスク関数を実行し、で指定されたコロン区切りの引数を関数の引数として渡します。

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

次の例では、タスクは_fooをログに記録し、Gruntが_grunt foo:testing:123_コマンドで実行された場合に123_をテストします。 _grunt foo_のような引数なしでタスクが実行されるたびに、タスクはargsなしでfooを_logします。

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

カスタムタスク

_multi task_構造に従わない場合は、以下に示すようにカスタムタスクを定義できます-

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 finddevguides!..');
});

//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']));
});