フォルダをひとつ作成し、そのフォルダに移動する
mkdir tutorial
cd tutorial
npm init
を実行する
npm init
package.json
(JavaScriptライブラリを管理するための設定ファイル) が作成される以下のコマンドを実行し、Grunt をこのフォルダにインストールする
npm install grunt --save-dev
以下のフォルダとファイルが作成されていることを確認する
tutorial/
- node_modules/ ... インストールした JavaScript ライブラリが格納される
- package.json
作成したフォルダに Gruntfile.js
という名前のファイルを作成し、
以下のように記述する
module.exports = function(grunt) {
grunt.registerTask('hello', function(){
console.log('Hello, world!');
});
};
grunt.registerTask
で実行するタスクを登録できるgrunt
コマンドでタスクを実行する
grunt hello
bye
タスクを追加する
module.exports = function(grunt) {
grunt.registerTask('hello', function(){
console.log('Hello, world!');
});
grunt.registerTask('bye', function(){
console.log('Bye, world!');
});
};
追加したタスクを実行する
grunt bye
hellobye
タスクを追加する
module.exports = function(grunt) {
grunt.registerTask('hello', function(){
console.log('Hello, world!');
});
grunt.registerTask('bye', function(){
console.log('Bye, world!');
});
grunt.registerTask('hellobye', ['hello', 'bye']);
};
grunt.registerTask
の第2引数にタスク名の配列を指定することで
複数タスクを一括実行するタスクを作成できる追加したタスクを実行する
grunt hellobye
default
タスクを追加する
module.exports = function(grunt) {
grunt.registerTask('hello', function(){
console.log('Hello, world!');
});
grunt.registerTask('bye', function(){
console.log('Bye, world!');
});
grunt.registerTask('hellobye', ['hello', 'bye']);
grunt.registerTask('default', ['hello']);
};
デフォルトタスクを実行する
grunt
grunt
コマンドを実行した場合、
default
という名前で登録されたタスクが実行されるgrunt.initConfig
に message
を定義して、
タスクの中で利用する
module.exports = function(grunt) {
grunt.initConfig({
message: 'Hello, hello, world!'
});
grunt.registerTask('hello', function(){
console.log(grunt.config('message'));
});
};
grunt.initConfig
には Gruntの設定をオブジェクトとして記述するタスクを実行する
grunt hello
grunt-contrib-concat を例にして、プラグインを利用してみます。
(*) grunt-contrib-concat は複数のファイルをひとつに結合するためのプラグインです。
フォルダをひとつ作成し、そのフォルダに移動する
mkdir plugins
cd plugins
npm init
を実行する
npm init
package.json
(JavaScriptライブラリを管理するための設定ファイル) が作成される以下のコマンドを実行し、Grunt をこのフォルダにインストールする
npm install grunt --save-dev
以下のように grunt-contrib-concat をインストールする
npm install grunt-contrib-concat --save-dev
作成したフォルダに Gruntfile.js
という名前のファイルを作成し、
以下のように記述する
module.exports = function(grunt) {
grunt.initConfig({
concat: {
target1: {
src: ['src/a.js', 'src/b.js'],
dest: 'dist/built.js'
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
};
grunt.loadNpmTasks
でプラグインのタスク(concat)を登録するgrunt.initConfig
にプラグインタスク名(concat)と同じキーを追加し、
その値としてプラグイン設定を記述する今回の定義では src
フォルダにある a.js
, b.js
という二つのファイルを結合して
dist
フォルダに built.js
というファイルを作成するので、実行の前に src
フォルダを作成し、
その中に a.js
, b.js
という二つのファイルを作成する(内容は何でもよい)
src/
- a.js
- b.js
grunt コマンドでタスクを実行する
grunt concat
dist
フォルダが作成され、その中に built.js
というファイルができているはずなので、
テキストエディタで開き、内容を確認する
Gruntfile.js の grunt.initConfig
に以下のように options
の定義を追加する
module.exports = function(grunt) {
grunt.initConfig({
concat: {
options: {
banner: '/*! Generated: <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
target1: {
src: ['src/a.js', 'src/b.js'],
dest: 'dist/built.js'
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
};
options
にはプラグインのオプションを指定できるconcat
の banner
オプションには、結合したファイルの先頭に挿入する文字列を指定できるGruntfile.js の grunt.initConfig
を以下のように記述する
module.exports = function(grunt) {
grunt.initConfig({
concat: {
target1: {
src: ['src/a.js', 'src/b.js'],
dest: 'dist/built.js'
},
target2: {
src: ['src/*.txt'],
dest: 'dist/built.txt'
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
};
concat
の中に target1
と target2
を追加したdist
フォルダのファイルを削除して、以下のコマンドを実行するgrunt concat:target2
build.txt
が作成されるgrunt タスク:ターゲット
とすれば指定したターゲットのみ実行されるdist
フォルダのファイルを削除して、以下のコマンドを実行するgrunt concat
build.js
と build.txt
が作成されるgrunt タスク
とした場合、全ターゲットが実行されるgrunt-contrib-concat と grunt-contrib-uglify を組み合わせて利用してみます。
(*) grunt-contrib-uglify は UglifyJS を使って JavaScript を圧縮するプラグインです。
以下のように grunt-contrib-concat
と grunt-contrib-uglify
をインストールする
npm install grunt-contrib-concat --save-dev
npm install grunt-contrib-uglify --save-dev
grunt-contrib-concat
をインストール済みのフォルダ上で実行する場合は、
grunt-contrib-uglify
のインストールのみでOKmodule.exports = function(grunt) {
grunt.initConfig({
concat: {
target1: {
src: ['src/a.js', 'src/b.js'],
dest: 'dist/built.js'
}
},
uglify: {
target1: {
files: {
'dist/built.min.js': ['<%= concat.target1.dest %>']
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
};
grunt.loadNpmTasks
でプラグインのタスク(uglify)を登録するgrunt.initConfig
に uglify
タスク設定を追加concat.target1.dest
で定義されている 'dist/built.js' を対象にして
圧縮を行い、'dist/built.min.js' を作成するという定義になっている以下のようなファイルを作成する
src/a.js
function f1(param1) {
console.log(param1);
}
src/b.js
function f2(param1) {
console.log(param1);
}
以下のコマンドを実行する
grunt concat uglify
dist/built.min.js
が作成されていることを確認する。毎回、grunt concat uglify
を入力するのは面倒だったり、
concat の実行を忘れたりしがちなので、
通常こういうケースでは、あらかじめ別タスクとして定義します。
Gruntfile.js に以下のように記述する
module.exports = function(grunt) {
...(省略)...
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('default', ['concat', 'uglify']);
};
実行する
grunt