0

我有简单的“watchFiles”任务,在文件更改后使用任务“重新加载”。此“重新加载”任务位于单独的文件中。当我不希望在同一个文件中执行此任务时,如何将此任务加载到我的“watchFiles”任务中?

// watchFiles task
gulp.task(watchFiles);

function watchFiles(cb) {
    watch(src + '**/*.html', series('reload'));
    cb();
}

// reload task in another file
gulp.task(reload);

function reload(cb) {
   browserSync.reload();
   cb();
}

// run from gulpfile
exports.watch = series('watchFiles');
4

1 回答 1

0

我会这样做:

// watchFiles task file = watchfiles_task_file.js
const { reload } = require('./reload_task_file');
const { series, watch } = require('gulp');
const src = './';

function watchFiles(cb) {
    watch(src + '**/*.html', series(reload));
    cb();
}
exports.watchFiles = watchFiles;
// end watchfiles_task_file.js

// reload task in another file = reload_task_file.js
const browserSync = require('browser-sync').create();
function reload(cb) {
   browserSync.reload();
   cb();
}
exports.reload = reload;
// end reload_task_file.js

// gulpfile.js
const { watchFiles } = require('./watchfiles_task_file');
const { series } = require('gulp');

exports.watch = series(watchFiles);
于 2020-06-17T13:28:55.067 回答