javascriptnode.jsgulpgulp-if

Multiple streams inside single gulp-if calling?


In the below code, I do plugins.if( !config.isDevelopmentMode && config.compileBladeTemplates, /* task */) check twice (plugins.if is gulp-if package). Can I refactor below code with one check? Not sure that it possible just with gulp-if, so it's allowed to use additional plugins.

const   gulp = require('gulp'),
        plugins = require('gulp-load-plugins')();

// ...
.pipe(plugins.if(
  !config.isDevelopmentMode && config.compileBladeTemplates,
  plugins.replace(
     /<!--\s+@blade\s+namedRoute\(\s*(.+?)\s*\)\s*-->\s*(?:\r\n|\r|\n)?\s*(<.+href=")(?:.+?)(".*\/?>)/g,
     `$2{{ route('$1') }}$3`
  )
))
.pipe(plugins.if(
  !config.isDevelopmentMode && config.compileBladeTemplates,
  plugins.replace(
     /<!--\s+@blade\s+(yield|include)\s*-->/g,
     ` @$1('$2')`
  )
))
// ...

Solution

  • If I understand what you are trying to do I think a lazypipe would work for you. See gulp-if with lazypipe.

    var replaceChannel = lazypipe()
          plugins.replace(
         /<!--\s+@blade\s+namedRoute\(\s*(.+?)\s*\)\s*-->\s*(?:\r\n|\r|\n)?\s*(<.+href=")(?:.+?)(".*\/?>)/g,
         `$2{{ route('$1') }}$3`
        )
        plugins.replace(
         /<!--\s+@blade\s+(yield|include)\s*-->/g,
         ` @$1('$2')`
       );
    

    and then in your task:

    .pipe(plugins.if(
      !config.isDevelopmentMode && config.compileBladeTemplates, replaceChannel()))