如何将gulp任务从文件加载到另一个任务

时间:2019-08-23 12:53:02

标签: gulp-4

我只是执行了“ 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');

1 个答案:

答案 0 :(得分: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);