0

使用 Gulp,您可以使用每种事件类型的watch().on()方法来访问触发事件的路径:

const { watch } = require('gulp');

watch('*.js').on('change', (path, stats) => {
  console.log(`File ${path} was changed`);
});

watch('*.js').on('add', (path, stats) => {
  console.log(`File ${path} was added`);
});

您还可以使用eventsoption监视多种事件类型,但您无法访问触发事件的路径:

watch('*.js', { events: ['change', 'add'] }, cb => {
  // body omitted
  cb();
});

在监视多种事件类型时,有没有办法从单个匿名处理程序访问触发事件的路径?

4

1 回答 1

3

您可以监视所有事件,并根据需要稍后在回调中过滤掉这些事件。

const gulp = require('gulp');

const watcher = gulp.watch(['*.js']);

gulp.task('default', function() {
  watcher.on('all', function(event, path, stats) {
    console.log(`File ${path} was ${event}`);
    console.log(stats);
    // filter some things using the event param if needed
  });
});
于 2021-03-02T11:27:03.233 回答