2014/11/04

[node.js][grunt]複数フォルダを監視してcoffeescirptをコンパイル

前回、gruntを使って監視してcoffeescirptをコンパイルというエントリーを書きました。

で、それから複数フォルダを監視して、coffeescirptをコンパイルするにはどうすればいいか調査しました。

状況としては、testフォルダの中に、test1フォルダとtest2フォルダがあり、それぞれのフォルダの中に入っているcoffeescirptを監視し、変更があった場合、コンパイルするという流れです。

まずは、以下のようにGruntfile.coffeeを組みました。

module.exports = (grunt)->
   grunt.initConfig
       pkg : grunt.file.readJSON 'package.json'
       watch : 
         files : ["./test1/*.coffee","./test2/*.coffee"]
         tasks : "coffee"
       coffee : 
         compile : 
           files : [
             expand : true
             cwd : "./"
             src :  ['test1/*.coffee','test2/*.coffee']
             dest : "./src/"
             ext : ".js"
           ]
       directories:[]
   grunt.loadNpmTasks 'grunt-contrib-coffee'
   grunt.loadNpmTasks 'grunt-contrib-watch'
   grunt.registerTask 'default', ['watch']
   return
ただ、これだと、片方のプログラムに修正があった場合、両方フォルダの中にあるファイルを強制的にコンパイルするため、フォルダが増えた場合、時間がかかってしまう。

そこで、下記のように組み直した結果、独立してコンパイルすることができた。
module.exports = (grunt)->
    grunt.initConfig
        pkg : grunt.file.readJSON 'package.json'
        watch :
          coffee_test1:
            files : "test1/*.coffee"
            tasks : "coffee:test1"
          coffee_test2:
            files : "test2/*.coffee"
            tasks : "coffee:test2"
        coffee : 
          test1:
            #compile : 
              files : [
                expand : true
                cwd : "./test1"
                src :  "*.coffee"
                dest : "./test1/src/"
                ext : ".js"
              ]
          test2:
            #compile : 
              files : [
                expand : true
                cwd : "./test2"
                src :  '*.coffee'
                dest : "./test2/src/"
                ext : ".js"
              ]

    grunt.loadNpmTasks 'grunt-contrib-coffee'
    grunt.loadNpmTasks 'grunt-contrib-watch'
    grunt.registerTask 'default', ['watch']

    return
でも、これって、フォルダが増える度に、ツラツラ書かなくちゃーいけないのかなー。

めんどくせーなー。

参考
GruntでCoffeeScriptを自動コンパイルするコピペ

0 コメント:

コメントを投稿