gulp-if
gulp-if copied to clipboard
[ENHANCEMENT] Gulp-If should support lazy evaluation
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.
I like this idea, but so far we've been unable to find a syntax that doesn't look conveluted. Lazypipe's syntax is just weird. Do you have a proposal for how this should look?
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