Skip to content

Instantly share code, notes, and snippets.

@fnobi
Created April 16, 2013 14:48
Show Gist options
  • Save fnobi/5396523 to your computer and use it in GitHub Desktop.
Save fnobi/5396523 to your computer and use it in GitHub Desktop.
grunt-pluginの作り方と解剖 ref: http://qiita.com/items/5590e7e92b4f2bd81d04
var grunt = require('grunt');
grunt.registerTask('sample', 'sample task', function () {
console.log('this is sample task!');
});
% grunt sample
this is sample task!

var fs = require('fs');
module.exports = function (grunt) {
grunt.registerTask('sample', 'sample task', function () {
// 環境名の取得
var target = this.target;
// 非同期タスクにする(doneにcallback関数が入る)
var done = grunt.async();
// configから"filename"を取得
var filename = grunt.config('sample')[target].filename;
fs.readFile(filename, 'utf8', function (err, data) {
// ファイルをreadするのを待って、出力したら終了
console.log(data);
done();
});
});
};
% grunt simple:dev
hogehoge
% grunt simple:prod
moge
% grunt simple
hogehoge
moge
var grant = require('grunt');
module.exports = function () {
grunt.initConfig({
sample: {
dev: { // 開発環境
hoge: 'hogehoge'
},
prod: { // 本番環境
hoge: 'moge'
}
}
});
grunt.loadNpmTask('grunt-sample');
};
{
"name": "grunt-sample",
"version": "0.0.1",
"description": "sample grunt plugin",
"keywords": [
"gruntplugin"
],
"devDependencies": {
"grunt": "~0.4.1"
}
}
module.exports = function (grunt) {
grunt.registerTask('sample', 'sample task', function () {
console.log('this is sample task!');
});
};
module.exports = function (grunt) {
grunt.registerMultiTask('sample', 'sample task', function () {
// 環境名の取得
var target = this.target;
// configから"hoge"を取得
var hoge = grunt.config('sample')[target].hoge;
console.log(hoge);
});
};
grunt-sample
├── package.json
└── tasks
└── sample.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment