gulp源码解析(三)—— 任务管理
注意attention: 转载学习来源https://www.cnblogs.com/vajoy/p/6359950.html
gulp插件
gulp-rename
'use strict';
var Stream = require('stream');
var Path = require('path');
function gulpRename(obj, options) {
options = options || {};
var stream = new Stream.Transform({objectMode: true});
function parsePath(path) {
var extname = options.multiExt ? Path.basename(path).slice(Path.basename(path).indexOf('.')) : Path.extname(path);
return {
dirname: Path.dirname(path),
basename: Path.basename(path, extname),
extname: extname
};
}
stream._transform = function (originalFile, unused, callback) {
var file = originalFile.clone({contents: false});
var parsedPath = parsePath(file.relative);
var path;
var type = typeof obj;
if (type === 'string' && obj !== '') {
path = obj;
} else if (type === 'function') {
obj(parsedPath, file);
path = Path.join(parsedPath.dirname, parsedPath.basename + parsedPath.extname);
} else if (type === 'object' && obj !== undefined && obj !== null) {
var dirname = 'dirname' in obj ? obj.dirname : parsedPath.dirname,
prefix = obj.prefix || '',
suffix = obj.suffix || '',
basename = 'basename' in obj ? obj.basename : parsedPath.basename,
extname = 'extname' in obj ? obj.extname : parsedPath.extname;
path = Path.join(dirname, prefix + basename + suffix + extname);
} else {
callback(new Error('Unsupported renaming parameter type supplied'), undefined);
return;
}
file.path = Path.join(file.base, path);
// Rename sourcemap if present
if (file.sourceMap) {
file.sourceMap.file = file.relative;
}
callback(null, file);
};
return stream;
}
module.exports = gulpRename;
gulp-file
var Vinyl = require('vinyl')
, through = require('through2');
/**
* Create vinyl file from string or buffer and add to gulp stream.
*
* @param {String} name
* @param {String|Buffer} source
* @param {Object=} options
* @param {Boolean=false} options.src
* @return {stream.Transform}
* @api public
*/
module.exports = function(fileArray, source, options) {
if (fileArray instanceof Array) {
options = source;
} else {
fileArray = [{
name: fileArray,
source: source
}];
}
var vinylFiles = fileArray.map(function(file) {
return new Vinyl({
cwd: "",
base: undefined,
path: file.name,
contents: ((file.source instanceof Buffer) ? file.source : new Buffer(file.source))
});
});
var stream = through.obj(function(file, enc, callback) {
this.push(file);
return callback();
});
vinylFiles.forEach(function(vinylFile) {
stream.write(vinylFile);
});
if (options && options.src) {
stream.end();
}
return stream;
};
gulp-replace
'use strict';
var Transform = require('readable-stream/transform');
var rs = require('replacestream');
var istextorbinary = require('istextorbinary');
module.exports = function(search, _replacement, options) {
if (!options) {
options = {};
}
if (options.skipBinary === undefined) {
options.skipBinary = true;
}
return new Transform({
objectMode: true,
transform: function(file, enc, callback) {
if (file.isNull()) {
return callback(null, file);
}
var replacement = _replacement;
if (typeof _replacement === 'function') {
// Pass the vinyl file object as this.file
replacement = _replacement.bind({ file: file });
}
function doReplace() {
if (file.isStream()) {
file.contents = file.contents.pipe(rs(search, replacement));
return callback(null, file);
}
if (file.isBuffer()) {
if (search instanceof RegExp) {
file.contents = new Buffer(String(file.contents).replace(search, replacement));
}
else {
var chunks = String(file.contents).split(search);
var result;
if (typeof replacement === 'function') {
// Start with the first chunk already in the result
// Replacements will be added thereafter
// This is done to avoid checking the value of i in the loop
result = [ chunks[0] ];
// The replacement function should be called once for each match
for (var i = 1; i < chunks.length; i++) {
// Add the replacement value
result.push(replacement(search));
// Add the next chunk
result.push(chunks[i]);
}
result = result.join('');
}
else {
result = chunks.join(replacement);
}
file.contents = new Buffer(result);
}
return callback(null, file);
}
callback(null, file);
}
if (options && options.skipBinary) {
istextorbinary.isText(file.path, file.contents, function(err, result) {
if (err) {
return callback(err, file);
}
if (!result) {
callback(null, file);
} else {
doReplace();
}
});
return;
}
doReplace();
}
});
};