Vinyl
一种虚拟文件格式。当通过 src()
读取文件时,会生成一个 Vinyl 对象来表示该文件 - 包括路径、内容和其他元数据。
Vinyl 对象可以使用插件应用转换。它们也可以使用 dest()
持久化到文件系统。
当创建自己的 Vinyl 对象(而不是用 src()
生成)时,请使用外部的 vinyl
模块,如下面的使用示例所示。
用法
const Vinyl = require('vinyl');
const file = new Vinyl({
cwd: '/',
base: '/test/',
path: '/test/file.js',
contents: new Buffer('var x = 123')
});
file.relative === 'file.js';
file.dirname === '/test';
file.dirname = '/specs';
file.path === '/specs/file.js';
file.basename === 'file.js';
file.basename = 'file.txt';
file.path === '/specs/file.txt';
file.stem === 'file';
file.stem = 'foo';
file.path === '/specs/foo.txt';
file.extname === '.txt';
file.extname = '.js';
file.path === '/specs/foo.js';
签名
new Vinyl([options])
参数
参数 | 类型 | 说明 |
---|---|---|
options | object | 详见下方选项。 |
返回值
Vinyl 类的一个实例,表示单个虚拟文件,详见下方Vinyl 实例。
错误
当任何传入的选项不符合实例属性定义时(例如,如果 path
被设置为一个数字),将按表中定义抛出错误。
选项
名称 | 类型 | 默认值 | 说明 |
---|---|---|---|
cwd | string | process.cwd() | 从中导出相对路径的目录。将被规范化并移除尾部分隔符。 |
base | string | 用于计算 relative 实例属性。如果未设置,则回退到 cwd 的值。通常设置为 glob 基础路径。将被规范化并移除尾部分隔符。 | |
path | string | 完整的绝对文件路径。将被规范化并移除尾部分隔符。 | |
history | array | [ ] | 一个路径数组,用于预先填充 Vinyl 实例的 history 。通常来自从先前的 Vinyl 对象派生出新的 Vinyl 对象。如果同时传递了 path 和 history ,则 path 会被附加到 history 上。每个项目都将被规范化并移除尾部分隔符。 |
stat | object | fs.Stats 的一个实例,通常是对文件调用 fs.stat() 的结果。用于确定 Vinyl 对象表示的是目录还是符号链接。 | |
contents | ReadableStream Buffer null | null | 文件的内容。如果 contents 是 ReadableStream,它将被包装在一个 cloneable-readable 流中。 |
options
上的任何其他属性将直接分配给 Vinyl 实例。
const Vinyl = require('vinyl');
const file = new Vinyl({ foo: 'bar' });
file.foo === 'bar';