Skip to content

Instantly share code, notes, and snippets.

@bakso
Last active August 29, 2015 14:01
Show Gist options
  • Save bakso/7b839ad116a7e44b29cf to your computer and use it in GitHub Desktop.
Save bakso/7b839ad116a7e44b29cf to your computer and use it in GitHub Desktop.
'use strict';
var through = require('through2'), //开发gulp插件必备的模块
Buffer = require('buffer').Buffer; //处理二进制buffer的时候有用
module.exports = function(opt){
/*
* gulp会将你通过诸如src('*/**/.js')获取的文件对象,传递到你返回的下面这个流对象里
* file对象可用属性:file.path、file.contents等
*/
return through.obj(function(file, encoding, callback){
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isStream()) {
return callback(uglifyError('Streaming not supported'));
}
//这里就是你插件干活的地方了 通常是先把文件的bufer转成字符串 处理完之后再转回去
var str = String(file.contents);
//例如文本替换后再转成Buffer
file.contents = new Buffer(str.replace('abc', 123));
//把文件对象塞回去
this.push(file);
callback(); //干完活说一声
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment