cusejuice
cusejuice

Reputation: 10691

Run uglify-js in gulp task without gulp-uglify plugin

I have a restriction of not using the gulp-uglify task. I want to create a gulp task that runs the following uglify-js command:

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

Is there a way to do that without using the gulp-uglify task?

Upvotes: 0

Views: 280

Answers (1)

Sven Schoenung
Sven Schoenung

Reputation: 30574

You can use the Node.js-builtin child_process.spawn() to execute the command directly:

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();
  });
});

Upvotes: 1

Related Questions