6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gruntで独自のタスクを追加する方法

Last updated at Posted at 2014-06-21

http://gruntjs.com/creating-tasks#basic-tasks に書いてあった

gruntのタスク追加をする場合、大抵どこかで提供されているモジュールを使うが
どうしても独自の処理のタスクを追加したい場合は
自分で独自のタスクをgrunt.registerTask に設定することができる。

grunt.registerTask("タスク名", ["タスクの説明", ] "実行する関数")

使い方

Gruntfile.js
  grunt.registerTask('task1', 'A sample task1', function() {
    grunt.log.writeln(this.name +  ': running');
  });

実行すると

% grunt task1
Running "task1" task
task1: running # grunt.log.writeln() で出力される部分

非同期処理が中に入る場合は async()で、コールバック関数を取得する

Gruntfile.js
  grunt.registerTask('task2', 'A sample task2', function() {
    var self = this;
    var done = this.async();

    setTimeout(function() {
      grunt.log.writeln(self.name +  ': running');
      done();
    }, 1000);
  });

タスク内で使える関数は http://gruntjs.com/inside-tasks あたりに。
grunt.log などの関数は http://gruntjs.com/api/grunt あたりに書いてある。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?