robrich / gulp-if

Conditionally run a task
MIT License
655 stars 26 forks source link

how to pass variable to gulp-if streams #30

Closed cooolbasha closed 10 years ago

cooolbasha commented 10 years ago

I am using gulp-if to check on condition to execute a task. but that task takes a variable. how do I pass the variable since it is async. Here is some code.

gulp = require 'gulp'
watch =  require 'gulp-watch'
minify = require 'gulp-minify-css'
rename =  require 'gulp-rename'
gulpif = require 'gulp-if'
concat      = require 'gulp-continuous-concat'

path = 
    customCSS       : 'public/stylesheets/custom/*.css'
    commonCSS       : 'client/common-css/*.css'
    stylesheetCSS   : ['public/stylesheets/*.css','!public/stylesheets/*.min.css']
    styleSheets     : 'public/stylesheets/'

gulp.task 'default', ->
    stylesheets = 
        customCSS : 'custom.min.css'
        commonCSS : 'common.min.css'
        stylesheetCSS: false

    for stylesheet,filename of stylesheets
        console.log stylesheet, filename
        do ->
            gulp.src path[stylesheet]
                .pipe watch()
                .pipe minify()
                .pipe gulpif Boolean(filename),concat(filename),rename suffix: '.min'
                .pipe gulp.dest path.styleSheets
    return      
robrich commented 10 years ago

gulp-if is just a function that takes 2 (optionally 3) parameters: the condition, the truthy stream, and the falsey stream. Yours is coffeescript, but I'll reply in JavaScript just so the parenthesis are more obvious.

// something about for stylesheet and filename
var laz = lazypipe()
    .pipe(concat(filename))
    .pipe(rename({suffix:'.min'});

gulp.src(stylesheet)
    .pipe(watch())
    .pipe(minify())
    .pipe(gulpif(Boolean(filename), laz())

lazypipe is from https://www.npmjs.org/package/lazypipe

cooolbasha commented 10 years ago

I guess my question was not right..I wanted to call either concat or rename based on the boolean return ..but I was debugging the gulp file..it was calling concat even before the gulpif. it is not the problem of passing parameter...

cooolbasha commented 10 years ago

tried to simplfy the code.

    gulp.src path.stylesheetCSS
                .pipe gulpif true,rename(suffix: '.min'),concat(filename)
                .pipe gulp.dest path.styleSheets

still the concat is called even though the true is passed...I put a debugger on concat..and it tries to stop there

robrich commented 10 years ago

Ah, I see I misunderstood your intent. If condition is true, rename. If it's false, concat. Then you have the call setup correctly. Because the condition could be dependent on properties of the file, both pipes are initialized, but as it runs through each file, it determines if the vinyl file relates to the truthy or falsey condition. In your case, nothing goes to concat, but concat is still initialized.