从任务内部运行任务。 (看)

时间:2016-09-14 22:07:10

标签: gulp

关于这个gulp任务。
我怎么能这样做,以便每当这个任务运行时(因为它使用watch),那么另一个任务也会运行,让我们调用其他任务脚本2.

gulp.task('scripts', function () {
return gulp.src(src + '/**/*.js', {read: false}) // no need of reading file because browserify does
    .pipe(plumber())
    .pipe(watch(src + '/**/*.js')) // WATCH
    // transform file objects using gulp-tap plugin
    .pipe(tap(function (file) {
        // replace file contents with browserify's bundle stream
        file.contents = browserify(file.path, {debug: true}).transform('babelify', {presets: ['es2015']}).bundle();
    }))
    // transform streaming contents into buffer contents (because gulp-sourcemaps does not support streaming contents)
    .pipe(buffer())
    .pipe(uglify())
    .pipe(plumber.stop())
    .pipe(gulp.dest(dest));
});

1 个答案:

答案 0 :(得分:0)

您似乎必须使用gulp-watch?您可以做的一件事是使用gulp的内置watch代替,设置为在监视文件中检测到更改时运行两个任务。在以下示例中,您将运行gulp watcher

gulp.task('scripts2', function() {
    console.log('hello world')
});

// I've left 'scripts' identical to what you gave us, except for dropping the watch pipe
//
gulp.task('scripts', function() {
    return gulp.src(src + '/**/*.js', { read: false }) // no need of reading file because browserify does
        .pipe(plumber())
        // transform file objects using gulp-tap plugin
        .pipe(tap(function(file) {
            // replace file contents with browserify's bundle stream
            file.contents = browserify(file.path, { debug: true }).transform('babelify', { presets: ['es2015'] }).bundle();
        }))
        // transform streaming contents into buffer contents (because gulp-sourcemaps does not support streaming contents)
        .pipe(buffer())
        .pipe(uglify())
        .pipe(plumber.stop())
        .pipe(gulp.dest(dest));
});

gulp.task('watcher', function() {
    gulp.watch(src + '/**/*.js', ['scripts','scripts2'])
});