Skip to main content

src()

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

**注:**BOMs(字节顺序标记)在 UTF-8 中没有任何作用,除非使用 removeBOM 选项禁用,否则 src() 将从读取的 UTF-8 文件中删除BOMs。

用法

const { src, dest } = require('gulp');

function copy() {
return src('input/*.js')
.pipe(dest('output/'));
}

exports.copy = copy;

函数原型

src(globs, [options])

参数

参数类型描述
globsstring
array
用于监视文件系统中匹配文件的 Globs
optionsobject在下面的 选项 中详细说明。

返回值

返回一个可以在管道的开始或中间使用的流,用于根据给定的 globs 添加文件。

可能出现的错误

globs 参数只能匹配一个文件(如 foo/bar.js)而且没有找到匹配时,会抛出一个错误,提示 "File not found with singular glob"。若要抑制此错误,请将 allowEmpty 选项设置为 true

当在 globs 中给出一个无效的 glob 时,抛出一个错误,并显示 "Invalid glob argument"。

选项

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

名称类型默认值描述
encodingstring
boolean
"utf8"When false, file contents are treated as binary. When a string, this is used as the text encoding.
bufferboolean
function
trueWhen true, file contents are buffered into memory. If false, the Vinyl object's contents property will be a paused stream. It may not be possible to buffer the contents of large files.
Note: Plugins may not implement support for streaming contents.
readboolean
function
trueIf false, files will be not be read and their Vinyl objects won't be writable to disk via .dest().
sincedate
timestamp
function
When set, only creates Vinyl objects for files modified since the specified time.
removeBOMboolean
function
trueWhen true, removes the BOM from UTF-8 encoded files. If false, ignores a BOM.
sourcemapsboolean
function
falseIf true, enables sourcemaps support on Vinyl objects created. Loads inline sourcemaps and resolves external sourcemap links.
resolveSymlinksboolean
function
trueWhen true, recursively resolves symbolic links to their targets. If false, preserves the symbolic links and sets the Vinyl object's symlink property to the original file's path.
cwdstringprocess.cwd()The directory that will be combined with any relative path to form an absolute path. Is ignored for absolute paths. Use to avoid combining globs with path.join().
This option is passed directly to glob-stream.
basestringExplicitly set the base property on created Vinyl objects. Detailed in API Concepts.
This option is passed directly to glob-stream.
cwdbasebooleanfalseIf true, cwd and base options should be aligned.
This option is passed directly to glob-stream.
rootstringThe root path that globs are resolved against.
This option is passed directly to glob-stream.
allowEmptybooleanfalseWhen false, globs which can only match one file (such as foo/bar.js) causes an error to be thrown if they don't find a match. If true, suppresses glob failures.
This option is passed directly to glob-stream.
uniqueBystring
function
'path'Remove duplicates from the stream by comparing the string property name or the result of the function.
Note: When using a function, the function receives the streamed data (objects containing cwd, base, path properties).
dotbooleanfalseIf true, compare globs against dot files, like .gitignore.
This option is passed directly to anymatch.
nouniquebooleanfalseWhen false, prevents duplicate files in the result set.
This option is passed directly to anymatch.
debugbooleanfalseIf true, debugging information will be logged to the command line.
This option is passed directly to anymatch.
nobracebooleanfalseIf true, avoids expanding brace sets - e.g. {a,b} or {1..3}.
This option is passed directly to anymatch.
noglobstarbooleanfalseIf true, treats double-star glob character as single-star glob character.
This option is passed directly to anymatch.
noextbooleanfalseIf true, avoids matching extglob patterns - e.g. +(ab).
This option is passed directly to anymatch.
nocasebooleanfalseIf true, performs a case-insensitive match.
Note: On case-insensitive file systems, non-magic patterns will match by default.
This option is passed directly to anymatch.
matchBasebooleanfalseIf true and globs don't contain any / characters, traverses all directories and matches that glob - e.g. *.js would be treated as equivalent to **/*.js.
This option is passed directly to anymatch.
ignorestring
array
Globs to exclude from matches. This option is combined with negated globs.
Note: These globs are always matched against dot files, regardless of any other settings.
This option is passed directly to anymatch.

资源映射(Sourcemaps)

对资源映射(Sourcemap )的支持已内置到 src()dest() 中了,但是默认情况下是禁用的。开启此功能能够生成内联或外联的资源映射(Sourcemap)。

内联资源映射(sourcemaps):

const { src, dest } = require('gulp');
const uglify = require('gulp-uglify');

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

外部资源映射:

const { src, dest } = require('gulp');
const uglify = require('gulp-uglify');

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