LoginSignup
4
4

More than 5 years have passed since last update.

Gruntでcontrib-watchからeste-watchに乗り換える

Posted at

今、開発ではGruntを使用してコンパイルなどを行っているのですが、
最近どうしてもwatchタスクが重くなっているため、噂のeste-watchにのりかえました

インストール

$ npm install grunt-este-watch --save-dev

今までのGruntfile

Gruntfile.coffee
module.export = (grunt)->
  require("load-grunt-task") grunt

  grunt.initConfig
    coffee:
      app:
        options:
          bare: yes
        files: [
          expand: yes
          cwd: "app/scripts"
          src: "**/*.coffee"
          dist: "app/scripts"
          ext: ".js"
        ]
    watch:
      options:
        spawn: no
      coffee:
        files: ["app/scripts"]
        tasks: ["coffee"]

grunt-contrib-watchは監視対象を決め、その対象が変更されれば指定したタスクが実行される仕組みになっています

書き直したGruntfile

Gruntfile.coffee
...
  grunt.initConfig
    ...
    esteWatch:
      options:
        dirs: ["app/scripts/**"]
        livereload:
          enabled: no
      coffee: (file_path)->
        grunt.config ["coffee", "app", "files"], [
            src: file_path
            dest: file_path.replace ".coffee", ".js"
        ]
        return ["coffee"]

今回ではCoffeeScriptをコンパイルするついでに、コンパイルの対象を変更があったファイルのみに変更するようにしてみました

contrib-watchとは違い、ウォッチ対象のファイルパスを指定し、
拡張子ごとに関数を作り、その関数内で返したタスクが実行されます
それぞれのファイルパスが引数として渡されるので、
かなり柔軟なタスク構成をすることが出来ます

ちなみに、全てのファイルを指定する場合は、

Gruntfile.cofee
...
  ...
    "*": (file_path)->
      # iroiro...

みたいな感じで書くことが出来ます

este-watchは待機時間中はCPU使用率がほぼゼロですし、何より軽いので非常に開発が捗ります

4
4
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
4
4