CLIから引数を渡す

npm install --save-dev gulp gulp-if gulp-uglify minimist
var gulp = require('gulp');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');

var minimist = require('minimist');

var knownOptions = {
  string: 'env',
  default: { env: process.env.NODE_ENV || 'production' }
};

var options = minimist(process.argv.slice(2), knownOptions);

gulp.task('scripts', function() {
  return gulp.src('**/*.js')
    // productionの場合のみminify
    .pipe(gulpif(options.env === 'production', uglify()))
    .pipe(gulp.dest('dist'));
});

gulpで次のように実行します。

$ gulp scripts --env development

 Back to top

© 2010 - 2017 STUDIO KINGDOM