在gulp任务中运行uglify-js而不使用gulp-uglify插件

时间:2016-04-04 22:23:48

标签: gulp uglifyjs

我限制不使用gulp-uglify任务。我想创建一个运行以下uglify-js命令的gulp任务:

./node_modules/.bin/uglifyjs ./public/dist/js/myfile.js -o ./public/dist/js/myfile.min.js --comments='/customComments/'

有没有办法在不使用gulp-uglify任务的情况下执行此操作?

1 个答案:

答案 0 :(得分:1)

您可以使用Node.js-builtin child_process.spawn()直接执行命令:

var gulp = require('gulp');
var spawn = require('child_process').spawn;

gulp.task('default', function(done) {
  var uglifyjs = spawn('./node_modules/.bin/uglifyjs', [
     './public/dist/js/myfile.js', 
     '-o', './public/dist/js/myfile.min.js', 
     "--comments='/customComments/'"
  ]);
  uglifyjs.stderr.on('data', function(data) {
    console.error(data.toString());
  });
  uglifyjs.on('close', function(code) {
    if (code !== 0) {
      done('uglifyjs exited with code ' + code);
      return;
    }
    done();
  });
});