Gulp can be configured to handle multiple environments by using the ‘
gulp-environments
’ plugin. This allows you to define tasks specific to each environment like development, testing, and production.
Firstly, install the plugin via
npm: npm install --save-dev gulp-environments
.
Then, in your Gulpfile.js, require the module and create environment variables:
var gulp = require('gulp');
var environments = require('gulp-environments');
var development = environments.development;
var production = environments.production;
Next, use these variables within your tasks to specify different actions for each environment. For example, minifying files only in production:
gulp.task('scripts', function() {
gulp.src('src/js/*.js')
.pipe(production(uglify()))
.pipe(gulp.dest('dist/js'));
});
To switch between environments, set the
NODE_ENV
variable before running Gulp:
NODE_ENV=production gulp scripts
.