使用文件#

¥Working with Files

xx1 和 xx2 方法由 gulp 公开以与计算机上的文件进行交互。

¥The src() and dest() methods are exposed by gulp to interact with files on your computer.

src() 被给予 glob 以从文件系统读取并生成 Node 流。它找到所有匹配的文件并将它们读入内存以通过流。

¥src() is given a glob to read from the file system and produces a Node stream. It locates all matching files and reads them into memory to pass through the stream.

src() 生成的流应从任务返回以指示异步完成,如 创建任务 中所述。

¥The stream produced by src() should be returned from a task to signal async completion, as mentioned in Creating Tasks.

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

流的主要 API 是用于链接 Transform 或 Writable 流的 .pipe() 方法。

¥The main API of a stream is the .pipe() method for chaining Transform or Writable streams.

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

dest() 一个输出目录字符串,并且还生成一个 Node 流,该 Node 流 通常用作终止符流。当它接收到通过管道传递的文件时,它将内容和其他详细信息写入文件系统的给定目录。symlink() 方法也可用,其操作类似于 dest(),但创建链接而不是文件(有关详细信息,请参阅 symlink())。

¥dest() is given an output directory string and also produces a Node stream which is generally used as a terminator stream. When it receives a file passed through the pipeline, it writes the contents and other details out to the filesystem at a given directory. The symlink() method is also available and operates like dest(), but creates links instead of files (see symlink() for details).

大多数情况下,插件将使用 .pipe() 方法放置在 src()dest() 之间,并将转换流中的文件。

¥Most often plugins will be placed between src() and dest() using the .pipe() method and will transform the files within the stream.

将文件添加到流中#

¥Adding files to the stream

src() 还可以放置在管道中间,以根据给定的 glob 将文件添加到流中。附加文件仅可用于流中稍后的转换。如果是 通配符重叠,将再次添加文件。

¥src() can also be placed in the middle of a pipeline to add files to the stream based on the given globs. The additional files will only be available to transformations later in the stream. If globs overlap, the files will be added again.

这对于在将纯 JavaScript 文件添加到管道并混淆所有内容之前转译某些文件非常有用。

¥This can be useful for transpiling some files before adding plain JavaScript files to the pipeline and uglifying everything.

const { src, dest } = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
exports.default = function() {
return src('src/*.js')
.pipe(babel())
.pipe(src('vendor/*.js'))
.pipe(uglify())
.pipe(dest('output/'));
}

分相输出#

¥Output in phases

dest() 可以用在管道中间,将中间状态写入文件系统。接收到文件时,当前状态将写入文件系统,更新路径以表示输出文件的新位置,然后该文件继续沿着管道向下传输。

¥dest() can be used in the middle of a pipeline to write intermediate states to the filesystem. When a file is received, the current state is written out to the filesystem, the path is updated to represent the new location of the output file, then that file continues down the pipeline.

此功能对于使用同一管道创建未缩小和缩小的文件非常有用。

¥This feature can be useful to create unminified and minified files with the same pipeline.

const { src, dest } = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
exports.default = function() {
return src('src/*.js')
.pipe(babel())
.pipe(src('vendor/*.js'))
.pipe(dest('output/'))
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(dest('output/'));
}

模式:流式、缓冲式和空式#

¥Modes: streaming, buffered, and empty

src() 可以在三种模式下运行:缓冲、流式传输和清空。这些是通过 src() 上的 bufferread options 配置的。

¥src() can operate in three modes: buffering, streaming, and empty. These are configured with the buffer and read options on src().

  • 缓冲模式是默认模式,并将文件内容加载到内存中。插件通常在缓冲模式下运行,许多插件不支持流模式。

    ¥Buffering mode is the default and loads the file contents into memory. Plugins usually operate in buffering mode and many don't support streaming mode.

  • 流模式的存在主要是为了操作内存无法容纳的大文件,例如巨大的图片或电影。内容以小块的形式从文件系统流式传输,而不是一次全部加载。如果你需要使用流模式,请寻找支持它的插件或编写自己的插件。

    ¥Streaming mode exists mainly to operate on large files that can't fit in memory, like giant images or movies. The contents are streamed from the filesystem in small chunks instead of loaded all at once. If you need to use streaming mode, look for a plugin that supports it or write your own.

  • 空模式不包含任何内容,并且在仅处理文件元数据时很有用。

    ¥Empty mode contains no contents and is useful when only working with file metadata.