How to create repeating pipe in gulp?

Koustuv Sinha

I have a gulp task to inject several dependencies in index.html as this :

return gulp.src(config.serve.html)
          .pipe($.inject(gulp.src(prependSource(config.inject.source.indexes)), {
            read: false,
            starttag: config.inject.indexes,
            addRootSlash: false,
            addPrefix: prefix,
            ignorePath: ignore
          }))
          .pipe($.inject(gulp.src(managerList), {
            read: false,
            starttag: config.inject.managers,
            addRootSlash: false,
            addPrefix: prefix,
            ignorePath: ignore
          }))
          .pipe($.inject(gulp.src(prependSource(config.inject.source.directives)), {
            read: false,
            starttag: config.inject.directives,
            addRootSlash: false,
            addPrefix: prefix,
            ignorePath: ignore
          }))

          .pipe($.inject(gulp.src(prependSource(config.inject.source.routes)), {
            read: false,
            starttag: config.inject.routes,
            addRootSlash: false,
            addPrefix: prefix,
            ignorePath: ignore
          }))
          .pipe($.inject(gulp.src(prependSource(config.inject.source.css)), {
            read: false,
            starttag: config.inject.css,
            addRootSlash: false,
            addPrefix: prefix,
            ignorePath: ignore
          }))


          .pipe($.size())
          .pipe(gulp.dest(dest));
};

As you can see, all the pipes are in repeating in some fashion (except the managerList). So what I want is to consolidate the repeating pipes into one single pipe, with the help of an array of injects. How to achieve the same?

Akshay Dhalwala

gulp.src returns a stream (as does each .pipe). If you think about what each .pipe call is doing, you can simply turn it into a for loop. Something like this:

config = { /* your config object */ };

var stream = gulp.src(/* source glob */);
for (var i = 0; i < injects.length; i++) {
  stream = stream.pipe($.inject(gulp.src(prependSource(config.inject.source[i])), config.inject.starttag[i]));
}

stream.pipe($.size())
      .pipe(gulp.dest(dest));

return stream;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

gulp - how to pipe tasks

From Dev

gulp - how to pipe tasks

From Dev

gulp pipe(gulp.dest()) does not create any results

From Dev

How to pipe YAML into EJS in a Gulp stream?

From Dev

How to pipe YAML into EJS in a Gulp stream?

From Dev

gulp.on('data') How to pass data to next pipe

From Dev

How can I pass gulp.watch() to a pipe?

From Dev

How can I write a simple gulp pipe function?

From Dev

How to set gulp.dest() in same directory as pipe inputs?

From Dev

How to create an array of non repeating random numbers

From Dev

how to create a dataframe by repeating series multiple times?

From Dev

Gulp pipe minimal function?

From Dev

Gulp condition inside pipe

From Dev

How to create war files from gulp

From Dev

How to create multiple files in one gulp task

From Dev

How should I create a complete build with Gulp?

From Dev

How to create parameterized and reusable gulp tasks

From Dev

Gulp pipe to separate destination folders

From Dev

Gulp / Unhandled stream error in pipe

From Dev

Gulp plumber cannot pipe to undefined

From Dev

gulp - make gulp.pipe(gulp.dest) sync

From Dev

How can I create vertically repeating horizontal dashed lines in css

From Dev

Polymer: How do you create repeating elements dynamic?

From Dev

how can i create repeating section in sharepoint site?

From Dev

How to create repeating rsync that runs on one password input?

From Dev

How to create a file from terminal repeating a set of words infinitely?

From Dev

How can I create a List of random int numbers without repeating?

From Dev

How do you resolve references to gulp.src().pipe() in webstorm 10

From Dev

How to create and call a pipe from the component in Angular 2?

Related Related

HotTag

Archive