Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

javascript - How to compose sequence of pipes for gulp?

I have a common pattern in my 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)

I want to compose these 5 lines to reuse them in my project over a couple of gulp tasks. How can I do that?

I found multipipe package but it doesn't support passing variables to new pipes (you can see I need to pass options.dest in my new pipe).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...