src()#

创建用于从文件系统读取 Vinyl 对象的流。

¥Creates a stream for reading Vinyl objects from the file system.

注意:BOM(字节顺序标记)在 UTF-8 中没有任何用途,并且将从 src() 读取的 UTF-8 文件中删除,除非使用 removeBOM 选项禁用。

¥Note: BOMs (byte order marks) have no purpose in UTF-8 and will be removed from UTF-8 files read by src(), unless disabled using the removeBOM option.

用法#

¥Usage

const { src, dest } = require('gulp');
function copy() {
return src('input/*.js')
.pipe(dest('output/'));
}
exports.copy = copy;

签名#

¥Signature

src(globs, [options])

参数#

¥Parameters

参数typenote
globs字符串
数组
通配符 在文件系统上监视。
optionsobject详细见下文 选项。

返回#

¥Returns

可以在管道的开头或中间使用的流,以根据给定的 glob 添加文件。

¥A stream that can be used at the beginning or in the middle of a pipeline to add files based on the given globs.

错误#

¥Errors

当 globs 参数只能匹配一个文件(例如 foo/bar.js)且未找到匹配项时,会抛出错误并显示消息 "未找到带有单一 glob 的文件"。要抑制此错误,请将 allowEmpty 选项设置为 true。

¥When the globs argument can only match one file (such as foo/bar.js) and no match is found, throws an error with the message, "File not found with singular glob". To suppress this error, set the allowEmpty option to true.

当 globs 中给出无效的 glob 时,会抛出错误并显示消息 "无效的全局参数"。

¥When an invalid glob is given in globs, throws an error with the message, "Invalid glob argument".

选项#

¥Options

对于接受函数的选项,将使用每个 Vinyl 对象调用传递的函数,并且必须返回另一个列出类型的值。

¥For options that accept a function, the passed function will be called with each Vinyl object and must return a value of another listed type.

nametypedefaultnote
encoding字符串
布尔值
"utf8"当为 false 时,文件内容被视为二进制。如果是字符串,则将其用作文本编码。
buffer布尔
函数
true当为 true 时,文件内容将缓冲到内存中。如果为 false,则 Vinyl 对象的 contents 属性将是暂停流。可能无法缓冲大文件的内容。
注意:插件可能无法实现对流内容的支持。
read布尔
函数
true如果为 false,则不会读取文件,并且它们的 Vinyl 对象将无法通过 .dest() 写入磁盘。
since日期
时间戳
函数
设置后,仅为自指定时间以来修改的文件创建 Vinyl 对象。
删除 BOM布尔
函数
true如果为 true,则从 UTF-8 编码文件中删除 BOM。如果为 false,则忽略 BOM。
sourcemaps布尔
函数
false如果为 true,则对创建的 Vinyl 对象启用 sourcemaps 支持。加载内联源映射并解析外部源映射链接。
resolveSymlinks布尔
函数
true如果为 true,则递归解析到其目标的符号链接。如果为 false,则保留符号链接并将 Vinyl 对象的 symlink 属性设置为原始文件的路径。
cwdstringprocess.cwd()将与任何相对路径组合形成绝对路径的目录。绝对路径被忽略。用于避免将 globs 与 path.join() 组合。
此选项直接传递给 glob-stream。
basestring在创建的 Vinyl 对象上显式设置 base 属性。详见 API 概念。
该选项直接传递给 glob-stream。
cwdbasebooleanfalse如果为 true,则 cwd 和 base 选项应该对齐。
此选项直接传递给 glob-stream。
rootstringglobs 解析的根路径。
此选项直接传递给 glob-stream。
allowEmptybooleanfalse如果为 false,则 globs 只能匹配一个文件(例如 foo/bar.js),如果找不到匹配项,则会引发错误。如果为 true,则抑制 glob 失败。
此选项直接传递给 glob-stream。
uniqueBy字符串
函数
'path'通过比较字符串属性名称或函数结果来从流中删除重复项。
注意:使用函数时,函数接收流数据(包含 cwd、base、path 属性的对象)。
dotbooleanfalse如果为 true,则将 glob 与点文件进行比较,例如 .gitignore。
此选项直接传递给 anymatch。
nouniquebooleanfalse当为 false 时,防止结果集中出现重复文件。
此选项直接传递给 anymatch。
debugbooleanfalse如果为 true,调试信息将记录到命令行。
此选项直接传递给 anymatch。
nobracebooleanfalse如果为 true,则避免扩展大括号集 - 例如 {a,b} 或 {1..3}。
该选项直接传递给 anymatch。
noglobstarbooleanfalse如果为 true,则将双星 glob 字符视为单星 glob 字符。
此选项直接传递给 anymatch。
noextbooleanfalse如果为 true,则避免匹配 extglob 模式 - 例如 +(ab).
该选项直接传递给 anymatch。
nocasebooleanfalse如果为 true,则执行不区分大小写的匹配。
注意:在不区分大小写的文件系统上,默认情况下将匹配非魔法模式。
此选项直接传递给 anymatch。
matchBasebooleanfalse如果 true 并且 glob 不包含任何 / 字符,则遍历所有目录并匹配该 glob - 例如 *.js 将被视为等同于 **/*.js。
此选项直接传递给 anymatch。
ignore字符串
数组
要从匹配中排除的 Glob。该选项与否定的 globs 结合使用。
注意:无论任何其他设置如何,这些 glob 始终与点文件匹配。
此选项直接传递给 anymatch。

源映射#

¥Sourcemaps

Sourcemap 支持直接内置于 src() 和 dest() 中,但默认情况下处于禁用状态。使其能够生成内联或外部源映射。

¥Sourcemap support is built directly into src() and dest(), but is disabled by default. Enable it to produce inline or external sourcemaps.

内联源映射:

¥Inline sourcemaps:

const { src, dest } = require('gulp');
const uglify = require('gulp-uglify');
src('input/**/*.js', { sourcemaps: true })
.pipe(uglify())
.pipe(dest('output/', { sourcemaps: true }));

外部源映射:

¥External sourcemaps:

const { src, dest } = require('gulp');
const uglify = require('gulp-uglify');
src('input/**/*.js', { sourcemaps: true })
.pipe(uglify())
.pipe(dest('output/', { sourcemaps: '.' }));