历史驱动力
常用工具
QQ邮箱实践经验
// 插件框架
module.exports = function(grunt){
grunt.registerMultiTask('test', 'test for create a grunt plugin', function(){
var options = this.options();
// 输入的文件对象
// files(inputs)
this.files.forEach(function(file){
// 读取文件
var srcContent = grunt.file.read(file.src);
// build函数表示具体的任务执行
var destContent = build(source);
// 写回文件
grunt.file.write(file.dest, dest)
});
});
};
module.exports = function(grunt){
// init grunt task config
grunt.initConfig({
coffee : {
target : {
files : [{
expand : true,
cwd : 'scripts',
src : '**/*.coffee',
dest : 'build/coffee',
ext : '.js'
}]
}
},
concat : {
target : {
src : ['<%= coffee.target.files.dest %>/*.js'],
dest : 'build/scripts/all.js'
}
},
uglify : {
target : {
files : {
'build/dist/all.min.js' : ['<%= concat.target.dest %>']
}
}
},
htmlmin : {
target : {
files : [{
expand : true,
cwd : 'tpl',
src : '**/*.html',
dest : 'build/tpl',
ext : '*.html'
}]
}
},
watch : {
scripts : {
files : ['scripts/**/*.js'],
tasks : ['scripts']
},
html : {
files : ['tpl/**/*.html'],
tasks : ['htmlmin']
}
}
});
// load npm task
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-htmlmin');
// register task alias
grunt.registerTask('scripts', ['coffee', 'concat', 'uglify']);
grunt.registerTask('debug', ['htmlmin', 'scripts', 'watch']);
grunt.registerTask('release', ['htmlmin', 'scripts']);
};
var es = require('event-stream');
module.exports = function (opts) {
// task config
opts = opts || {};
// through streams
// file(inputs)
return es.map(function (file, cb) {
//what task do?
// transport data to next task(ouputs)
cb(null, file);
});
};
// https://github.com/wearefractal/vinyl
var File = require('vinyl');
var htmlTpl = new File({
cwd: "/",
base: "/test/",
path: "/test/item.html"
contents: new Buffer("<div></div>")
});
// https://github.com/jonschlinkert/gulp-htmlmin
var es = require('event-stream');
var htmlmin = require('html-minifier');
var gutil = require('gulp-util');
module.exports = function (opts) {
'use strict';
opts = opts || {
showStack: false
};
return es.map(function (file, cb) {
try {
file.contents = new Buffer(htmlmin.minify(String(file.contents), opts));
} catch (err) {
return cb(new gutil.PluginError('gulp-htmlmin', err, opts));
}
cb(null, file);
});
};
// gulp example config
var gulp = require('gulp');
var htmlmin = require('gulp-htmlmin');
var coffee = require('gulp-coffee');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
gulp.task('html', function(){
return gulp.src('tpl/**/*.html').pipe(htmlmin()).pipe(gulp.dest('build/tpl'));
});
gulp.task('scripts', function(){
return gulp.src('scripts/**/*.coffee')
.pipe(coffee())
.pipe(uglify())
.pipe(concat('all.min.js'))
.pipe(gulp.dest('build/scripts'));
});
gulp.task('watch', function(){
gulp.watch('tpl/**/*.html', ['html']);
gulp.watch('scripts/**/*.coffee', ['scripts']);
});
// task alias
gulp.task('debug', ['watch']);
gulp.task('release', ['scripts', 'html']);