如何在 Gulp 中将变量从一个管道传送到另一个管道

How to carry a variable from one pipe to another in Gulp

gulp.task('jade:prod:DE_EN', function () {
return gulp.src('./views/*.jade')
    .pipe(tap(function(file) {
        console.log(path.basename(file.path))
    }))
    .pipe(jade({
        locals: {
            env: 'production',
            texts: texts.EN,
            config: texts.EN['config']
        }
    }))
    .pipe(gulp.dest('./public/de/en'));
});

console.log(path.basename(file.path)) returns 通过流的 .jade 文件。

我想知道如何获取该变量并将其作为本地人之一传递到玉管中。这样我就可以在编译时在 Jade 中使用它了。

任何额外的 links/references/documentation 解释管道/流如何工作将不胜感激。

从一个 Transform (the thing you pass to pipe()) to the next is to attach it to the vinyl-fs file object that is moving through the stream. The gulp-data 插件获取数据的唯一方法让您做到这一点:

gulp.task('jade:prod:DE_EN', function () {
  return gulp.src('./views/*.jade')
    .pipe(data(function(file) {
       return { 
         myBasename: path.basename(file.path),
         env: 'production',
         texts: texts.EN,
         config: texts.EN['config']
       };
    }))
   .pipe(jade())
   .pipe(gulp.dest('./public/de/en'));
});

现在您可以在您的 Jade 模板中访问 myBasename 本地:

doctype html
html(lang="en")
  head
    title=Test
  body
    h1 #{myBasename}