【问题标题】:How to compose sequence of pipes for gulp?如何为 gulp 组成管道序列?
【发布时间】:2016-07-22 03:55:11
【问题描述】:

我的gulpfile.js 中有一个共同的模式:

var rev         = require('gulp-rev');
var buffer      = require('gulp-buffer');

gulp.src.some_stuff
  .pipe(anotherStuff)
  .pipe(buffer()) // this line & 4 lines down
  .pipe(rev())
  .pipe(gulp.dest(options.dest))
  .pipe(rev.manifest({ path: 'manifest.json', merge: true }))
  .pipe(gulp.dest(options.dest)) // to this
  .pipe(extrastuff)

我想编写这 5 行代码,以便在我的项目中通过几个 gulp 任务重用它们。我该怎么做?

我找到了multipipe 包,但它不支持将变量传递给新管道(你可以看到我需要在我的新管道中传递options.dest)。

【问题讨论】:

    标签: javascript node.js gulp pipe


    【解决方案1】:

    使用lazypipe:

    var lazypipe = require('lazypipe');
    
    function something(dest) {
      return (lazypipe()
       .pipe(buffer)
       .pipe(rev)
       .pipe(gulp.dest, dest)
       .pipe(rev.manifest, { path: 'manifest.json', merge: true })
       .pipe(gulp.dest, dest))();
    }
    
    gulp.src.some_stuff
      .pipe(anotherStuff)
      .pipe(something(options.dest))
      .pipe(extrastuff)
    

    【讨论】:

    • 不知何故我无法让它与参数一起工作......文档和讨论不匹配......(是否使用.pipe(foo).pipe(foo())github.com/OverZealous/lazypipe/issues/19
    • @Sandrooco 当您想将foo 添加到lazypipe 时,它是.pipe(foo)。当foolazypipe 本身时,它是.pipe(foo())
    • 那么我怎样才能将参数传递给lazypipe?
    • 看答案。 dest 是用于构造lazypipe 的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 2015-03-29
    • 2015-02-10
    • 1970-01-01
    • 2021-12-09
    • 2015-01-26
    相关资源
    最近更新 更多