Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 678 Bytes

pass-arguments-from-cli.md

File metadata and controls

30 lines (22 loc) · 678 Bytes

从命令行传递参数

// 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')
    .pipe(gulpif(options.env === 'production', uglify())) // 仅在生产环境时候进行压缩
    .pipe(gulp.dest('dist'));
});

然后,通过如下命令运行 gulp:

$ gulp scripts --env development