Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  151] [ 6]  / answers: 1 / hits: 22442  / 9 Years ago, tue, march 24, 2015, 12:00:00

Is there a way to specify a gulp task depending on the NODE_ENV that is set?



For example in my package.json file, I have something like:



scripts: {
start: gulp
}


And I have multiple gulp tasks



gulp.task('development', function () {
// run dev related tasks like watch
});

gulp.task('production', function () {
// run prod related tasks
});


If I set NODE_ENV=production npm start, can I specify to only run gulp production? Or is there a better way to do this?


More From » node.js

 Answers
76

Using a single ternary in your default gulp task, you can have something like:



gulp.task('default',
[process.env.NODE_ENV === 'production' ? 'production' : 'development']
);


You will then be able to keep the single gulp command in your package.json and using this like you said:



NODE_ENV=production npm start


Any other value of your NODE_ENV variable will launch the development task.






You could of course do an advanced usage using an object allowing for multiple tasks and avoiding if trees hell:



var tasks = {
development: 'development',
production: ['git', 'build', 'publish'],
preprod: ['build:preprod', 'publish:preprod'],
...
}

gulp.task('default', tasks[process.env.NODE_ENV] || 'fallback')


Keep in mind that when giving an array of tasks, they will be run in parallel.


[#67336] Friday, March 20, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janettejordynm

Total Points: 550
Total Questions: 94
Total Answers: 98

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
janettejordynm questions
Tue, Nov 24, 20, 00:00, 4 Years ago
Sat, May 23, 20, 00:00, 4 Years ago
Mon, Apr 6, 20, 00:00, 4 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
;