简体   繁体   中英

Set gulp tasks depending on NODE_ENV

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?

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.

Have your first gulp task run other gulp tasks based on the process.env.NODE_ENV value.

gulp.task('launcher', function(){
  switch (process.env.NODE_ENV){
    case 'development':
      // Run dev tasks from here
      break;
    case 'production':
      // Run prod tasks
      break;
  }
});

The other simple way could be

gulp.task('set-dev-env', function () {
    return process.env.NODE_ENV = 'development';
});

gulp.task('set-prod-env', function () {
    return process.env.NODE_ENV = 'production';
});

gulp.task('development', ['set-dev-env'], function () {
    // your code
});

gulp.task('production', ['set-prod-env'], function () {
    // your code
});

Run gulp production or gulp development .

You can also use gulp-mode plugin.

Usage:

    var gulp = require('gulp');
    var mode = require('gulp-mode')();
    var uglify = require('gulp-uglify');

    gulp.task('default', function() {
      gulp.src('src/*.js')
          .pipe(mode.production(uglify()))
          .pipe(gulp.dest('dist'));
    });

OR

    var isProduction = mode.production();
    if (isProduction) {
        console.log("Production mode");
    }

Start build as:

gulp build --production

For "npm run-script" use-cases:

package.json :-
    "scripts": {
        "devbld": "gulp build --development",
        "prodbld": "gulp build --production",
      }

    $ npm run devbld
    $ npm run prodbld
if (process.env.NODE_ENV === "production")
  // whatever

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM