- Grunt4の使い方
- 13.09.22 @masuilab
- Node.js製のタスクランナー
- ビルドやテストランの自動化
- Compile
- coffee, stylus, jade …
- Test
- mocha, jslint …
- Function
- concat, watch …
- and more…
assets/js/*.coffee
をpublic/js/*.js
へコンパイルしたい- コードに変更があったら自動でコンパイルしてほしい
- livereloadとかもしてくれたら開発強度高まる
- デプロイをかけたらmochaでテストを走らせてほしい
node
,npm
が必要です
- grunt-cliをインスコします
npm -g i grunt-cli
- grunt本体はプロジェクトへ個別にインストールします
Gruntfile.js
またはGruntfile.coffee
をルートディレクトリに作ります
coffeeでやります
module.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON 'package.json'
# grunt.loadNpmTasks ''
grunt.registerTask 'default', []
module.exports
: nodeのアレinitConfig
: ここにモジュールの設定を書きますloadNpmTasks
: npmのモジュールをロードしますregisterTask
: タスクのエイリアスを作ります、複数のタスクをまとめるなど
npm i grunt --save-dev
grunt [task]
task
を省略した場合はdefault
が実行されます
npm i grunt-contrib-coffee --save-dev
initConfig
に設定を追加します
grunt.initConfig
coffee:
compile:
files: [{
expand: yes # destが無い場合に mkdir -p
cwd: 'assets/'
src: [ '**/*.coffee' ] # パターン
dest: 'public/'
ext: '.js'
}]
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt coffee
initConfigの設定名がtask名になります
% npm i grunt-contrib-watch --save-dev
grunt.initConfig
watch:
coffee:
files: ['assets/**/*.coffee']
tasks: ['coffee']
grunt.loadNpmTasks 'grunt-contrib-watch'
% grunt watch
- contribをインストールして
- 設定を書き足して
- モジュールのローダーを書いて
- 実行