gruntでcoffeescriptコンパイルの自動化

gruntでcoffeescriptコンパイルを自動化した。

【環境】
Mac OS 10.8.5

【手順】
1 : node.jsのインストール

node.jsを入れていなかったので、HomeBrewからインストール

$ brew update
$ brew install node
$ node -v
=>v0.10.22

2 : grunt-cliのインストール

npmからインストール
gオプションは付けたほうが良さそう
http://shimotori.github.io/blog-it/2013/09/16/1-npm-install-g-option/

$ npm install -g grunt-cli

3 : package.jsonの作成

プロジェクトフォルダを作っておき、移動してから以下コマンド

$ npm init

対話式に入力を求められるけれど、とりあえず全てEnter、
package.jsonが作成されるので不要な所を削除

[package.json]

{
  "name": "grunt",
  "version": "0.0.0"
}


4 : grunt、grunt-contrib-watch、grunt-contrib-coffeeのインストール

grunt本体とプラグイン2つをインストール
・grunt-contrib-watch(ファイル変更監視)
・grunt-contrib-coffee(coffeescriptコンパイル)

npm install --save-dev grunt
npm install --save-dev grunt-contrib-watch
npm install --save-dev grunt-contrib-coffee

gruntインストールの段階で、警告が出たけれど、スルーした

npm WARN install Refusing to install grunt as a dependency of itself

インストール後のpackage.jsonは以下のようになった

[package.json]

{
  "name": "grunt",
  "version": "0.0.0",
  "devDependencies": {
    "grunt": "~0.4.2",
    "grunt-contrib-watch": "~0.5.3",
    "grunt-contrib-coffee": "~0.7.0"
  }
}

5 : Gruntfileの作成

こちらのものを使わせてもらった(例1のもの)
http://tuki0918.hatenablog.com/entry/2013/06/12/164011

6 : 実行

gruntでタスクを実行する。

$ grunt
Running "watch" task
Waiting...OK

ターゲットを編集すると

>> File "coffee/hello.coffee" changed.

Running "coffee:compile" (coffee) task
File Resources/hello.js created.

Done, without errors.

という感じでコンパイルしてくれる。