gulp-if icon indicating copy to clipboard operation
gulp-if copied to clipboard

Support array of conditions

Open terinjokes opened this issue 10 years ago • 17 comments

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.

terinjokes avatar Aug 18 '14 23:08 terinjokes

+1 for the feature

gevgeny avatar Oct 15 '14 08:10 gevgeny

+1

nilssolanki avatar Apr 21 '15 08:04 nilssolanki

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

robrich avatar Sep 29 '15 20:09 robrich

+1

danimalweb avatar Nov 17 '15 23:11 danimalweb

plus one more here!

mix3d avatar Nov 30 '15 20:11 mix3d

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.

mix3d avatar Nov 30 '15 21:11 mix3d

... 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.

robrich avatar Dec 01 '15 15:12 robrich

For the record. I no longer need this. On Dec 1, 2015 7:27 AM, "Rob Richardson" [email protected] 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.

terinjokes avatar Dec 01 '15 15:12 terinjokes

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!

mix3d avatar Dec 07 '15 16:12 mix3d

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 &&.

Trost123 avatar Nov 13 '16 17:11 Trost123

@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)$/.

robrich avatar Nov 15 '16 02:11 robrich

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.

Trost123 avatar Nov 15 '16 09:11 Trost123

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

robrich avatar Nov 15 '16 15:11 robrich

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

Trost123 avatar Nov 15 '16 15:11 Trost123

:D Problem solved.

robrich avatar Nov 15 '16 15:11 robrich

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

Trost123 avatar Nov 15 '16 16:11 Trost123

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

gchamon avatar Feb 10 '19 14:02 gchamon