I am looking to include a file in gulp only if it exists, when I am compiling, for development. Currently I have the following:
gulp.task('compile:js:development', function() {
return gulp.src([
'src/js/**/*.js',
]).pipe(concat('dist.js'))
.pipe(gulp.dest('compiled/js/'))
});
I need to add another file to this array, but only if that file exists. I have seen gulp-if
But I don't think that has the capability I am looking for.
I would also like to warn the developer that this file doesn't exist, when compiling for development in the console.
Gulp is just a node application, so you can use any node functions in your gulpfile. You can easily check existence of a file using fs.exists()
gulp.task('compile:js:development', function() {
var fs = require('fs'),
files = ['src/js/**/*.js'],
extraFile = 'path/to/other/file';
if (fs.existsSync(extraFile)) {
files.push(extraFile);
} else {
console.log('FILE DOES NOT EXIST');
}
return gulp.src(files)
.pipe(concat('dist.js'))
.pipe(gulp.dest('compiled/js/'))
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With