1

例如,假设我有这个听众:

gulp.watch('../templates/**/*.tpl', gulp.series('template'));

和相关的任务:

var template = gulp.task('template', function(done) {
    console.log(filename);
    done();
});

是否可以获取触发监视事件的当前 .tpl 文件的文件名,如果可以,如何获取?

4

1 回答 1

1

来自gulp.watch 文档

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

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

watcher.on('all', function(path, stats) {
 console.log(`File ${path} was changed`);
});

您可以'path'像这样使用该信息:

function scripts(myPath) {
  console.log(`in scripts, path = ${myPath}`);

  return gulp.src(myPath)
    .pipe(gulp.dest('pathTest'));
};

function watch(done) {

  const watcher = gulp.watch(["../templates/**/*.tpl"]);

  watcher.on('change', function (path, stats) {
    scripts(path);
  });

  done();
}

exports.default = gulp.series(watch);

所以像上面的例子一样重新排列你的代码:

const watcher = gulp.watch('../templates/**/*.tpl');
watcher.on('change', function (path,stats) {
  template(path);
};

function template(filename) {
    console.log(filename);
    return gulp.src…...
});
于 2019-02-14T01:55:04.927 回答