grunt-contrib-coffeeを試す

CoffeeScript & Grunt インストール

すでに、nodeとnpmはインストール済みとします。

CoffeeScript インストール

$ npm install -g coffee-script

Grunt インストール

$ npm install -g grunt

GruntのCoffeeScriptプラグインをインストール

プロジェクトのディレクトリ直下にnode_modulesディレクトリを作っておきます。

$ mkdir node_modules

node_modulesディレクトリ以下にインストールされます。

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

GruntでCoffeeScriptを自動コンパイル

プロジェクト直下にgrunt.jsファイルを作成します。

grunt.js

module.exports = function(grunt) {
  grunt.initConfig({
    coffee: {
      compile: {
        files: {
          'js/*.js': ['coffee/*.coffee']
        },
        options: {
          bare: true
        }
      }
    },
    watch: {
      files: ['coffee/*.coffee'],
      tasks: 'coffee'
    }
  });

  grunt.loadNpmTasks('grunt-contrib-coffee');

  grunt.registerTask('default', 'coffee');
};

これでファイル変更を監視してCoffeeScriptをコンパイルしてくれます。

$ grunt watch

たとえば、coffee/app.coffeeを編集すると

Running "watch" task
Waiting...OK
>> File "coffee/app.coffee" changed.

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

Running "watch" task
Waiting...

js/app.jsを作ってくれます。

便利!

TitaniumとCoffeeScriptとGruntで快適開発環境を作る | masaplabs

folders