如何在当前gulpfile中运行另一个gulpfile

zhe*_*ong 9 gulp

我需要在当前gulp文件中的'default'任务之前运行另一个gulp文件.这种情况有没有gulp插件.

MLM*_*MLM 10

您可以使用child_process.exec(...)CLI API从控制台运行两个gulp任务.有Gulp.run,但该功能已取消,将被删除向前发展.

此代码段将连续运行以下两个gulp文件.

运行两一饮而尽,files.js

跑步: node run-two-gulp-files.js

./gulpfile.js 依赖于取决于 ./other-thing-with-gulpfile/gulpfile.js

var exec = require('child_process').exec;

// Run the dependency gulp file first
exec('gulp --gulpfile ./other-thing-with-gulpfile/gulpfile.js', function(error, stdout, stderr) {
    console.log('other-thing-with-gulpfile/gulpfile.js:');
    console.log(stdout);
    if(error) {
        console.log(error, stderr);
    }
    else {

        // Run the main gulp file after the other one finished
        exec('gulp --gulpfile ./gulpfile.js', function(error, stdout, stderr) {
            console.log('gulpfile.js:');
            console.log(stdout);
            if(error) {
                console.log(error, stderr);
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

gulpfile.js

var gulp = require('gulp');
var replace = require('gulp-replace');

gulp.task('file1-txt', function() {
    return gulp.src('file1.txt')
        .pipe(replace(/foo/g, 'bar'))
        .pipe(gulp.dest('dest'));
});

gulp.task('default', ['file1-txt']);
Run Code Online (Sandbox Code Playgroud)

其他-的事情 - - gulpfile/gulpfile.js

var gulp = require('gulp');
var replace = require('gulp-replace');

gulp.task('file2-txt', function() {
    return gulp.src('file2.txt')
        .pipe(replace(/baz/g, 'qux'))
        .pipe(gulp.dest('../dest'));
});

gulp.task('default', ['file2-txt']);
Run Code Online (Sandbox Code Playgroud)