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

  • 本文关键字:gulpfile 另一个 运行 gulp
  • 更新时间 :
  • 英文 :


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

您可以使用 child_process.exec(...) 来运行这两个 gulp 任务,就像使用 CLI API 从控制台运行一样。有Gulp.run,但该函数已弃用,并将被删除。

此代码段将连续运行下面的两个 gulp 文件。

运行两个文件.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);
            }
        });
    }
});

古尔普文件.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']);

最新更新