LoginSignup
31
29

More than 5 years have passed since last update.

grunt-contrib-watchで変更されたファイルを取得する方法

Last updated at Posted at 2013-04-30

grunt.event.on('watch', fn)で「watch」イベントを監視し、変更したファイルを取得することができる。

イベントハンドラでは変更ファイルのパスが取得できるので、
それを使用してconfigを書き換えて、変更ファイルのみにタスクを実行させる、ということができる。
※jshintの他にもsassやcoffeescript等のコンパイルに利用できそう。

grunt-contrib-watchのconfigでnospawn: trueにしないとイベントハンドラによるconfig書き換えが反映されない。

grunt.initConfig({
  watch: {
    scripts: {
      files: ['lib/*.js'],
      tasks: ['jshint'],
    },
    options: {
      nospawn: true
    }
  },
  jshint: {
    all: ['lib/*.js'],
  },
});

grunt.event.on('watch', function(action, filepath) {
  grunt.config(['jshint', 'all'], [filepath]);
});
31
29
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
31
29