Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support array of conditions #37

Open
terinjokes opened this issue Aug 18, 2014 · 17 comments
Open

Support array of conditions #37

terinjokes opened this issue Aug 18, 2014 · 17 comments

Comments

@terinjokes
Copy link

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
Copy link

gevgeny commented Oct 15, 2014

+1 for the feature

@nilssolanki
Copy link

+1

@robrich
Copy link
Owner

robrich commented Sep 29, 2015

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

@robrich robrich changed the title Support multiple solutions Support array of conditions Sep 29, 2015
@danimalweb
Copy link

+1

@mix3d
Copy link

mix3d commented Nov 30, 2015

plus one more here!

@mix3d
Copy link

mix3d commented Nov 30, 2015

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
Copy link
Owner

robrich commented Dec 1, 2015

... 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
Copy link
Author

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
#37 (comment).

@mix3d
Copy link

mix3d commented Dec 7, 2015

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
Copy link

Trost123 commented Nov 13, 2016

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
Copy link
Owner

robrich commented Nov 15, 2016

@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
Copy link

Trost123 commented Nov 15, 2016

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
Copy link
Owner

robrich commented Nov 15, 2016

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

@Trost123
Copy link

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

@robrich
Copy link
Owner

robrich commented Nov 15, 2016

:D Problem solved.

@Trost123
Copy link

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
Copy link

gchamon commented Feb 10, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants