在上一篇文章我们对 Stream 的特性及其接口进行了介绍,gulp 之所以在性能上好于 grunt,主要是因为有了 Stream 助力来做数据的传输和处理。
那么我们不难猜想出,在 gulp 的任务中,gulp.src 接口将匹配到的文件转化为可读(或 Duplex/Transform)流,通过 .pipe 流经各插件进行处理,最终推送给 gulp.dest 所生成的可写(或 Duplex/Transform)流并生成文件。
本文将追踪 gulp(v4.0)的源码,对上述猜想进行验证。
为了分析源码,我们打开 gulp 仓库下的入口文件 index.js,可以很直观地发现,几个主要的 API 都是直接引用 vinyl-fs 模块上暴露的接口的:
var util = require('util'); var Undertaker = require('undertaker'); var vfs = require('vinyl-fs'); var watch = require('glob-watcher'); //略... Gulp.prototype.src = vfs.src; Gulp.prototype.dest = vfs.dest; Gulp.prototype.symlink = vfs.symlink; //略...
因此了解 vinyl-fs 模块的作用,便成为掌握 gulp 工作原理的关键之一。需要留意的是,当前 gulp4.0 所使用的 vinyl-fs 版本是 v2.0.0。
vinyl-fs 其实是在 vinyl 模块的基础上做了进一步的封装,在这里先对它们做个介绍:
一. Vinyl
Vinyl 可以看做一个文件描述器,通过它可以轻松构建单个文件的元数据(metadata object)描述对象。依旧是来个例子简洁明了:
//ch2-demom1 var Vinyl = require('vinyl'); var jsFile = new Vinyl({ cwd: '/', base: '/test/', path: '/test/file.js', contents: new Buffer('abc') }); var emptyFile = new Vinyl(); console.dir(jsFile); console.dir(emptyFile);
上述代码会打印两个File文件对象:
简而言之,Vinyl 可以创建一个文件描述对象,通过接口可以取得该文件所对应的数据(Buffer类型)、cwd路径、文件名等等:
//ch2-demo2 var Vinyl = require('vinyl'); var file = new Vinyl({ cwd: '/', base: '/test/', path: '/test/newFile.txt', contents: new Buffer('abc') }); console.log(file.contents.toString()); console.log('path is: ' + file.path); console.log('basename is: ' + file.basename); console.log('filename without suffix: ' + file.stem); console.log('file extname is: ' + file.extname);
打印结果:
更全面的 API 请参考官方描述文档,这里也对 vinyl 的源码贴上解析注释:
var path = require('path'); var clone = require('clone'); var cloneStats = require('clone-stats'); var cloneBuffer = require('./lib/cloneBuffer'); var isBuffer = require('./lib/isBuffer'); var isStream = require('./lib/isStream'); var isNull = require('./lib/isNull'); var inspectStream = require('./lib/inspectStream'); var Stream = require('stream'); var replaceExt = require('replace-ext'); //构造函数 function File(file) { if (!file) file = {}; //-------------配置项缺省设置 // history是一个数组,用于记录 path 的变化 var history = file.path ? [file.path] : file.history; this.history = history || []; this.cwd = file.cwd || process.cwd(); this.base = file.base || this.cwd; // 文件stat,它其实就是 require('fs').Stats 对象 this.stat = file.stat || null; // 文件内容(这里其实只允许格式为 stream 或 buffer 的传入) this.contents = file.contents || null; this._isVinyl = true; } //判断是否 this.contents 是否 Buffer 类型 File.prototype.isBuffer = function() { //直接用 require('buffer').Buffer.isBuffer(this.contents) 做判断 return isBuffer(this.contents); }; //判断是否 this.contents 是否 Stream 类型 File.prototype.isStream = function() { //使用 this.contents instanceof Stream 做判断 return isStream(this.contents); }; //判断是否 this.contents 是否 null 类型(例如当file为文件夹路径时) File.prototype.isNull = function() { return isNull(this.contents); }; //通过文件 stat 判断是否为文件夹 File.prototype.isDirectory = function() { return this.isNull() && this.stat && this.stat.isDirectory(); }; //克隆对象,opt.deep 决定是否深拷贝 File.prototype.clone = function(opt) { if (typeof opt === 'boolean') { opt = { deep: opt, contents: true }; } else if (!opt) { opt = { deep: true, contents: true }; } else { opt.deep = opt.deep === true; opt.contents = opt.contents !== false; } // 先克隆文件的 contents var contents; if (this.isStream()) { //文件内容为Stream //Stream.PassThrough 接口是 Transform 流的一个简单实现,将输入的字节简单地传递给输出 contents = this.contents.pipe(new Stream.PassThrough()); this.contents = this.contents.pipe(new Stream.PassThrough()); } else if (this.isBuffer()) { //文件内容为Buffer /** cloneBuffer 里是通过 * var buf = this.contents; * var out = new Buffer(buf.length); * buf.copy(out); * 的形式来克隆 Buffer **/ contents = opt.contents ? cloneBuffer(this.contents) : this.contents; } //克隆文件实例对象 var file = new File({ cwd: this.cwd, base: this.base, stat: (this.stat ? cloneStats(this.stat) : null), history: this.history.slice(), contents: contents }); // 克隆自定义属性 Object.keys(this).forEach(function(key) { // ignore built-in fields if (key === '_contents' || key === 'stat' || key === 'history' || key === 'path' || key === 'base' || key === 'cwd') { return; } file[key] = opt.deep ? clone(this[key], true) : this[key]; }, this); return file; }; /** * pipe原型接口定义 * 用于将 file.contents 写入流(即参数stream)中; * opt.end 用于决定是否关闭 stream */ File.prototype.pipe = function(stream, opt) { if (!opt) opt = {}; if (typeof opt.end === 'undefined') opt.end = true; if (this.isStream()) { return this.contents.pipe(stream, opt); } if (this.isBuffer()) { if (opt.end) { stream.end(this.contents); } else { stream.write(this.contents); } return stream; } // file.contents 为 Null 的情况不往stream注入内容 if (opt.end) stream.end(); return stream; }; /** * inspect原型接口定义 * 用于打印出一条与文件内容相关的字符串(常用于调试打印) * 该方法可忽略 */ File.prototype.inspect = function() { var inspect = []; // use relative path if possible var filePath = (this.base && this.path) ? this.relative : this.path; if (filePath) { inspect.push('"'+filePath+'"'); } if (this.isBuffer()) { inspect.push(this.contents.inspect()); } if (this.isStream()) { //inspectStream模块里有个有趣的写法——判断是否纯Stream对象,先判断是否Stream实例, //再判断 this.contents.constructor.name 是否等于'Stream' inspect.push(inspectStream(this.contents)); } return '<File '+inspect.join(' ')+'>'; }; /** * 静态方法,用于判断文件是否Vinyl对象 */ File.isVinyl = function(file) { return file && file._isVinyl === true; }; // 定义原型属性 .contents 的 get/set 方法 Object.defineProperty(File.prototype, 'contents', { get: function() { return this._contents; }, set: function(val) { //只允许写入类型为 Buffer/Stream/Null 的数据,不然报错 if (!isBuffer(val) && !isStream(val) && !isNull(val)) { throw new Error('File.contents can only be a Buffer, a Stream, or null.'); } this._contents = val; } }); // 定义原型属性 .relative 的 get/set 方法(该方法几乎不使用,可忽略) Object.defineProperty(File.prototype, 'relative', { get: function() { if (!this.base) throw new Error('No base specified! Can not get relative.'); if (!this.path) throw new Error('No path specified! Can not get relative.'); //返回 this.path 和 this.base 的相对路径 return path.relative(this.base, this.path); }, set: function() { //不允许手动设置 throw new Error('File.relative is generated from the base and path attributes. Do not modify it.'); } }); // 定义原型属性 .dirname 的 get/set 方法,用于获取/设置指定path文件的文件夹路径。 // 要求初始化时必须指定 path <或history> Object.defineProperty(File.prototype, 'dirname', { get: function() { if (!this.path) throw new Error('No path specified! Can not get dirname.'); return path.dirname(this.path); }, set: function(dirname) { if (!this.path) throw new Error('No path specified! Can not set dirname.'); this.path = path.join(dirname, path.basename(this.path)); } }); // 定义原型属性 .basename 的 get/set 方法,用于获取/设置指定path路径的最后一部分。 // 要求初始化时必须指定 path <或history> Object.defineProperty(File.prototype, 'basename', { get: function() { if (!this.path) throw new Error('No path specified! Can not get basename.'); return path.basename(this.path); }, set: function(basename) { if (!this.path) throw new Error('No path specified! Can not set basename.'); this.path = path.join(path.dirname(this.path), basename); } }); // 定义原型属性 .extname 的 get/set 方法,用于获取/设置指定path的文件扩展名。 // 要求初始化时必须指定 path <或history> Object.defineProperty(File.prototype, 'extname', { get: function() { if (!this.path) throw new Error('No path specified! Can not get extname.'); return path.extname(this.path); }, set: function(extname) { if (!this.path) throw new Error('No path specified! Can not set extname.'); this.path = replaceExt(this.path, extname); } }); // 定义原型属性 .path 的 get/set 方法,用于获取/设置指定path。 Object.defineProperty(File.prototype, 'path', { get: function() { //直接从history出栈 return this.history[this.history.length - 1]; }, set: function(path) { if (typeof path !== 'string') throw new Error('path should be string'); // 压入history栈中 if (path && path !== this.path) { this.history.push(path); } } }); module.exports = File;