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

时间:2015-01-17 07:35:32

标签: gulp

我需要在'默认'之前运行另一个gulp文件。当前gulp文件中的任务。这种情况是否有任何gulp插件。

1 个答案:

答案 0 :(得分:10)

您可以使用child_process.exec(...)来运行两个gulp任务,就像使用CLI API从控制台一样。有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);
            }
        });
    }
});

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']);

另一件事与 - 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']);
相关问题