gulp


How to run some gulp task based on a conditional


Suppose I have this in my gulpfile:
gulp.task('foo', ...);
gulp.task('bar', function () {
if (something) {
// how do I run task 'foo' here?
}
});
You could make 'bar' a dependency of 'foo' and put the condition inside 'foo':
gulp.task('foo', function(){
if(something){...}
}, 'bar');
gulp.task('bar', function(){});
This way bar will always run before foo, and foo can choose if it is necessary to run its own logic.
Gulp v3
Use deprecated but still working gulp.run
gulp.task('foo', ...)
gulp.task('bar', function () {
if (something) {
gulp.run('foo')
}
})
Alternatively, use use any plugins that consume task names as arguments, like run-sequence for example (which you will probably need anyway for running tasks in a strict sequence). I call my tasks conditionally this way (Gulp v3):
gulp.task('bar', (callback) => {
if (something) {
runSequence('foo', callback)
} else {
runSequence('foo', 'anotherTask', callback)
}
})
Gulp v4
Your gulpfile, that is, gulpfile.babel.js for now, would set Gulp tasks as exported functions so you would call them directly:
export function foo () {
...
}
export function bar () {
if (something) {
foo()
}
}

Related Links

Proxy URL not working with BrowserSync Gulp task
the task can't be called by watch module.
I can't get the data-uri() function to work on gulp.js to base64 an image on mac
How to inject bower dependencies in karma.conf.js file with gulp?
Getting Libsass Gulp Susy to work together
Gulp compass without config.rb
gulp-webserver: Default to index.html if available, else show directories
gulp-uglify notify on failing build
Declaring optional dependencies
gulp html minification error
Can an IIFE be added to a browserify bundle?
Gulp: Error Messages and Notifications with Plumber
Gulp - gulp-load-plugins not working
How to configure Gulp task to copy bower resources
gulp-jscs doesn't output anything when used in gulp-watch pipeline
ES6 project does not compile because modules not found

Categories

HOME
repository
sip
qemu
blob
gaussian
livecharts
jquery-plugins
coordinates
yuv
aggregate-functions
mamp
tag-cloud
novacode-docx
summernote
android-wifi
descriptor
handle
scalatest
knapsack-problem
symlink
sqldependency
request-uri
drawrect
restler
mkdir
ponylang
hippocms
rythm
scip
susy
android-graphview
vpython
visio-2010
visualizer
petapoco
directinput
ownership
nimbus
bytearray
django-taggit
jde
oryx
std
nslocalizedstring
serial-communication
database-project
transport
jain-sip
om
firewire
nexusdb
jsonmodel
nsurlrequest
nest-initiative
scjp
dateadd
mixradio
complete
google-mirror-api
uikit-dynamics
junction-table
bho
zepto
linqdatasource
libx264
yii-components
sup
ruby-1.9
utm
capitalization
wdm
funambol
n2
datarelation
system-analysis
ihtmldocument2
scala-designer
scalable
paperless

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App