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

[ENHANCEMENT] Gulp-If should support lazy evaluation #75

Open
smac89 opened this issue Jul 5, 2017 · 2 comments
Open

[ENHANCEMENT] Gulp-If should support lazy evaluation #75

smac89 opened this issue Jul 5, 2017 · 2 comments

Comments

@smac89
Copy link

smac89 commented Jul 5, 2017

At the moment, it requires one to pass in an already existing stream as parameter. It would be nice if instead, one can pass in a function which returns a stream.

The benefit of this is that the conditions are evaluated lazily rather than eagerly, thus making it possible for to pass in streams with side-effects, but only the side-effects of the chosen stream will occur and not the other.

I'm sure there are other benefits to having lazily evaluated operands, but that's all I can think of atm, because it relates to my use-case.

If this is something you would like, I would be glad to create a pull request once it's ready.

@robrich
Copy link
Owner

robrich commented Jul 5, 2017 via email

@smac89
Copy link
Author

smac89 commented Jul 15, 2017

I wasn't sure how to make the arguments lazily evaluated without resorting to using the same syntax as lazy pipe. This is the syntax I settled on. There are two:

gulp.task('default', () => {
    return gulpIf(true)
        .then(gulp.src, ['**/*.ts'])
        .otherwise(gulp.src, ['**/*.js'])
        .pipe(concat('files.txt'))
        .pipe(gulp.dest('dist'));
});

This first one uses the builder pattern to build the full stream

gulp.task('test1', () => {
    return gulp.src(['**/*', '!dist/**/*', '!dist/']).pipe(
        gulpIf({
            condition: (vf: Vinyl) => vf.path.endsWith('.js'),
            thenStream: concat('all.js')
        }).otherwise(concat, 'rest.txt'))
        .pipe(gulp.dest('dist'));
});

This one uses an options parameter to specify the condition, and branches. You can also specify the elseStream in there too, however this does not allow for specifying lazily evaluated streams, so you can break out of the options style and use the builder syntax to specify a lazily evaluated stream.


Here is an example that brings them all together including showing how to include minimatch options:

gulp.task('test3', () => {
    return gulp.src(['**/*', '!dist/**/*', '!dist/']).pipe(
        gulpIf({
            condition: (vf: Vinyl) => vf.path.endsWith('.js'),
            thenStream: gulpIf('INDEX.JS', {nocase: true}).then(concat, 'found.index.js')
        }).otherwise(concat, 'rest.txt'))
        .pipe(gulpIf('*.+(index.js|txt)').then(concat('index-and-nonjs.txt'))
            .otherwise(concat, 'something.txt'))
        .pipe(gulp.dest('dist'));
});

You can find the complete project at https://github.com/smac89/gulp-if. It is in typescript mind you.

After doing npm install, you can run the tasks by navigating to the test folder and run each test using gulp

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

2 participants