监视文件#

¥Watching Files

watch() API 使用文件系统监视器将 globs 连接到 tasks。它监视与 glob 匹配的文件的更改,并在发生更改时执行任务。如果任务没有触发 异步完成 信号,则它将永远不会运行第二次。

¥The watch() API connects globs to tasks using a file system watcher. It watches for changes to files that match the globs and executes the task when a change occurs. If the task doesn't signal Async Completion, it will never be run a second time.

此 API 提供基于最常用默认值的内置延迟和排队。

¥This API provides built-in delay and queueing based on most-common-use defaults.

const { watch, series } = require('gulp');
function clean(cb) {
// body omitted
cb();
}
function javascript(cb) {
// body omitted
cb();
}
function css(cb) {
// body omitted
cb();
}
exports.default = function() {
// You can use a single task
watch('src/*.css', css);
// Or a composed task
watch('src/*.js', series(clean, javascript));
};

警告:避免同步#

¥Warning: avoid synchronous

观察者的任务不能同步,就像注册到任务系统中的任务一样。如果传递同步任务,则无法确定完成情况,并且任务不会再次运行 - 假设它仍在运行。

¥A watcher's task cannot be synchronous, like tasks registered into the task system. If you pass a sync task, the completion can't be determined and the task won't run again - it is assumed to still be running.

没有提供错误或警告消息,因为文件监视器使你的 Node 进程保持运行。由于进程不会退出,因此无法确定任务是否已完成或只是花费了非常非常长的时间来运行。

¥There is no error or warning message provided because the file watcher keeps your Node process running. Since the process doesn't exit, it cannot be determined whether the task is done or just taking a really, really long time to run.

监视的事件#

¥Watched events

默认情况下,每当创建、更改或删除文件时,观察程序都会执行任务。如果需要使用不同的事件,可以在调用 watch() 时使用 events 选项。可用的事件有 'add''addDir''change''unlink''unlinkDir''ready''error'。此外,'all' 可用,它代表除 'ready''error' 之外的所有事件。

¥By default, the watcher executes tasks whenever a file is created, changed, or deleted. If you need to use different events, you can use the events option when calling watch(). The available events are 'add', 'addDir', 'change', 'unlink', 'unlinkDir', 'ready', 'error'. Additionally 'all' is available, which represents all events other than 'ready' and 'error'.

const { watch } = require('gulp');
exports.default = function() {
// All events will be watched
watch('src/*.js', { events: 'all' }, function(cb) {
// body omitted
cb();
});
};

初始执行#

¥Initial execution

调用 watch() 后,任务将不会执行,而是等待第一个文件更改。

¥Upon calling watch(), the tasks won't be executed, instead they'll wait for the first file change.

要在第一次文件更改之前执行任务,请将 ignoreInitial 选项设置为 false

¥To execute tasks before the first file change, set the ignoreInitial option to false.

const { watch } = require('gulp');
exports.default = function() {
// The task will be executed upon startup
watch('src/*.js', { ignoreInitial: false }, function(cb) {
// body omitted
cb();
});
};

排队#

¥Queueing

每个 watch() 保证其当前正在运行的任务不会再次并发执行。当观察程序任务运行时发生文件更改时,另一个执行将排队等待该任务完成时运行。一次只能排队一个运行。

¥Each watch() guarantees that its currently running task won't execute again concurrently. When a file change is made while a watcher task is running, another execution will queue up to run when the task finishes. Only one run can be queued up at a time.

要禁用队列,请将 queue 选项设置为 false

¥To disable queueing, set the queue option to false.

const { watch } = require('gulp');
exports.default = function() {
// The task will be run (concurrently) for every change made
watch('src/*.js', { queue: false }, function(cb) {
// body omitted
cb();
});
};

延迟#

¥Delay

文件更改后,观察程序任务将在 200 毫秒的延迟过后才会运行。这是为了避免在同时更改许多文件时过早开始任务 - 就像查找和替换一样。

¥Upon file change, a watcher task won't run until a 200ms delay has elapsed. This is to avoid starting a task too early when many files are being changed at once - like find-and-replace.

要调整延迟持续时间,请将 delay 选项设置为正整数。

¥To adjust the delay duration, set the delay option to a positive integer.

const { watch } = require('gulp');
exports.default = function() {
// The task won't be run until 500ms have elapsed since the first change
watch('src/*.js', { delay: 500 }, function(cb) {
// body omitted
cb();
});
};

使用观察者实例#

¥Using the watcher instance

你可能不会使用此功能,但如果你需要完全控制已更改的文件 - 例如访问路径或元数据 - 使用从 watch() 返回的 chokidar 实例。

¥You likely won't use this feature, but if you need full control over changed files - like access to paths or metadata - use the chokidar instance returned from watch().

当心:返回的 chokidar 实例没有队列、延迟或异步完成功能。

¥Be careful: The returned chokidar instance doesn't have queueing, delay, or async completion features.

可选依赖#

¥Optional dependency

Gulp 有一个名为 fsevents 的可选依赖,它是 Mac 特定的文件监视器。如果你看到 fsevents 的安装警告 - “npm 警告可选跳过可选依赖:事件事件” - 这不是问题。如果跳过 fsevents 安装,将使用后备观察程序,并且 gulpfile 中发生的任何错误都与此警告无关。

¥Gulp has an optional dependency called fsevents, which is a Mac-specific file watcher. If you see an installation warning for fsevents - "npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents" - it is not an issue. If fsevents installation is skipped, a fallback watcher will be used and any errors occurring in your gulpfile aren't related to this warning.