robrich / gulp-if

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

Support array of conditions #37

Open terinjokes opened 10 years ago

terinjokes commented 10 years ago

One currently has multiple options to support multiple conditions with gulp-if:

gif(PRODUCTION, gif('*.js', minify()));
gif(function(file) { return PRODUCTION && gulpmatch(file, '*.js'); }, minify());

But all of these (and more involving lazypipe), distract from the goal of this simple problem: matching a boolean condition with a glob condition.

I propose something similar to the following to allow multiple conditions without nesting gulp-if instances:

gif([PRODUCTION, '*.js'], minify());

I don't believe the above will work as is because it conflicts with the multiple glob support in gulp-match, but it's the general idea of what i've got.

gevgeny commented 10 years ago

+1 for the feature

nilssolanki commented 9 years ago

+1

robrich commented 9 years ago

I like the idea of an array of match conditions. Should we hard-code that all must match? Or any? (e.g. || vs &&)

danimalweb commented 8 years ago

+1

mix3d commented 8 years ago

plus one more here!

mix3d commented 8 years ago

I like the idea of an array of match conditions. Should we hard-code that all must match? Or any? (e.g. || vs &&)

@robrich hardcoding would be the "easy" way to go, but implementing one or the other would likely cause people to want the other one as well, and once they have both, then a way to specify chains of conditionals. If you give a mouse a cookie...

Without creating a complex builder system, you could probably do gif.and([conditional, ...], function) and gif.or([conditional, ...], function) for the && and ||, respectively, keeping the original syntax for backwards compatibility.

If we wanted to get wild, you might be able to go with an object structure, that allowed for multiple nested conditionals, something like the following:

var crazy = {
    AND: [ {
             OR:[ boolean_conditional, bool_function(), ETC ]
           },
             boolean_conditional,
             "string_matcher*.js"
         ]
}

gif(crazy, minify())

but that's crazy and needlessly complex.

robrich commented 8 years ago

... and this "give a mouse a cookie" is part of my hesitation here. It almost sounds like you should pass in a function, I hand off the arguments you need to run your crazy, and you hand me back a boolean answer ... which by the way is the work-around the original author uses. :D

I'm thinking if we take an opinion, && seems the most discoverable -- matches the multiple glob array metaphor.

terinjokes commented 8 years ago

For the record. I no longer need this. On Dec 1, 2015 7:27 AM, "Rob Richardson" notifications@github.com wrote:

... and this "give a mouse a cookie" is part of my hesitation here. It almost sounds like you should pass in a function, I hand off the arguments you need to run your crazy, and you hand me back a boolean answer ... which by the way is the work-around the original author uses. :D

I'm thinking if we take an opinion, && seems the most discoverable -- matches the multiple glob array metaphor.

— Reply to this email directly or view it on GitHub https://github.com/robrich/gulp-if/issues/37#issuecomment-161001149.

mix3d commented 8 years ago

Perhaps then the "easiest" solution then, would be to provide examples of multi-conditional external functions, added as a pattern in the README? If the file object is always available, it is less work for @robrich to "implement".

Otherwise, while I don't know enough about what is kosher in terms of the multi file glob pattern, but mixing booleans with strings in an array, and checking them internally, sounds like an anti-pattern. Similarly, using an array of strings, conditionals, and/or string arrays (for the glob array), leaves you with nested arrays, and the likelihood of people accidentally assuming the array is flat, even if that inherently gets you an "or" pattern with anything in a sub array (assuming the sub array is a glob array and not mixed.)

Quite the rabbit hole!

Trost123 commented 7 years ago

Bump. Currently doing ugly stuff like this:

var wa_mode = true //boolean switch

.pipe($.if(wa_mode,
      $.if('*.html',
        $.replace('rel="stylesheet" href="', 'rel="stylesheet" href="{$wa_theme_url}')
      )
    ))

Is there a better solution for this?

Also, it there a way to do this in one line? (match one glob OR the other)

    .pipe($.if('*.css', $.cached('CSSandJScache')))
    .pipe($.if('*.js', $.cached('CSSandJScache')))

I really miss the good old || and &&.

robrich commented 7 years ago

@Trost123: in your first example, you could replace it with a function. I think what you've got isn't bad though. In your second example, pass in this: /*\.(css|js)$/.

Trost123 commented 7 years ago

Thanks, that helps!

Could you also help me with function soultion? I'm trying to use what @terinjokes suggested: gif(function(file) { return PRODUCTION && gulpmatch(file, '*.js'); }, minify()); But I want to declare the function outside of task/pipe. What should I use in place of "file"? If I try to use it as-is, it tell me that file is undefined.

robrich commented 7 years ago

that's the correct way to do it. Do you have a gist or pastebin we can review?

Trost123 commented 7 years ago

Nevermind, I found the mistake (in my code).

robrich commented 7 years ago

:D Problem solved.

Trost123 commented 7 years ago

You might wanna fix your previous advice though: /.*\.(css|js)$/ (The dot before *) Spent around 20 min trying to figure our why it didn't work. On the bright side - you made me learn more about regexp ;)

gchamon commented 5 years ago

old issue, still open, my workaround on it is using vinyl directly:

  const matchesExtensionsAndConditional = (includes, excludes, conditional) =>
    (file) => (conditional
      && includes.some((include) => file.basename.endsWith(include))
      && excludes.every((exclude) => !file.basename.endsWith(exclude)));

  gulp.src('src/**/*')
    .pipe($gulp.if(
            matchesExtensionsAndConditional(['.js'], ['.min.js'], runUglify),
            uglify(uglifyOpts)))

can be extended to parse !.min.js, but I wanted to make it simple. Uses different syntax than glob, so might be a deal breaker for some. Works for me though