Black Lives Matter

タスクの作成

タスクはGruntの肝です。`jshint`や`nodeunit`のような、最も頻繁に行う処理のことです。Gruntを実行するたびに、実行する1つ以上のタスクを指定します。これにより、Gruntに何を実行させるかを指示します。

タスクを指定しない場合、"default"という名前のタスクが定義されていれば、そのタスクが(当然ながら)デフォルトで実行されます。

エイリアスタスク

タスクリストを指定すると、新しいタスクは1つ以上の他のタスクのエイリアスになります。「エイリアスタスク」が実行されるたびに、`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はGrunt設定で同じ名前のプロパティを探します。マルチタスクには、任意の名前の「ターゲット」を使用して定義された複数の設定を持つことができます。

`grunt concat:foo`や`grunt concat:bar`のように、タスクとターゲットの両方を指定すると、指定されたターゲットの設定だけが処理されます。一方、`grunt concat`を実行すると、すべてのターゲットが順番に処理されます。タスク名がgrunt.task.renameTaskで変更されている場合、Gruntは設定オブジェクト内で新しいタスク名を持つプロパティを探します。

コントリビュートタスクのほとんど(grunt-contrib-jshintプラグインのjshintタスクgrunt-contrib-concatプラグインのconcatタスクなど)はマルチタスクです。

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

指定された設定では、このマルチタスクの例は、Gruntが`grunt log:foo`で実行された場合は`foo: 1,2,3`をログ出力し、`grunt log:bar`で実行された場合は`bar: hello world`をログ出力します。しかし、Gruntが`grunt 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);
});

「基本」タスク

基本タスクを実行すると、Gruntは設定や環境を見ません。指定されたタスク関数を単に実行し、コロンで区切られた引数を関数引数として渡します。

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

このタスクの例では、Gruntが`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.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);
});

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

`--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() {
  // 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.
//   ***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 "foo" task.', function() {
  // Fail task if "meta.name" config prop 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 will only log if meta.name is defined in the config.');
});

タスクは設定プロパティにアクセスできます。

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

詳細な例については、コントリビュートタスクをご覧ください。

CLIオプション/環境

環境変数にアクセスするには、`process.env`を使用します。

CLIの使用ページで、使用可能なコマンドラインオプションの詳細をご覧ください。

非同期タスクが完了しないのはなぜですか?

これは、Gruntにタスクが非同期であることを伝えるthis.asyncメソッドを呼び出すのを忘れていることが原因である可能性が高いです。簡潔にするため、Gruntは同期コーディングスタイルを使用していますが、タスク本体内で`this.async()`を呼び出すことで非同期に切り替えることができます。

`done()`関数に`false`を渡すと、タスクが失敗したことをGruntに伝えます。

例:

grunt.registerTask('asyncme', 'My asynchronous task.', function() {
  var done = this.async();
  doSomethingAsync(done);
});

参考資料

タスクを作成するための追加の参考資料が必要な場合は、APIドキュメントをご覧ください。