3

I have a gulp task that look like this:

gulp.task('compressjs', function() {
    gulp.src('local/**/*.js')
    .pipe(sourcemaps.init())
    // purpose compress here is only to give better error report
    .pipe(uglify())
    .pipe(concat('all.min.js'))
    .pipe(wrap('(function(){"use strict"; <%= contents %>\n})();'))
    // compress here to get best final compression
    .pipe(uglify())
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('public/app'));
});

This gulp task uses:

  • gulp-uglify
  • gulp-wrap
  • gulp-concat
  • gulp-sourcemaps

Everything above is running properly. But then I found that gulp-uglify seems to come with it's own wrap function. How can I use gulp-uglify function to wrap the script?

user1995781
  • 19,085
  • 45
  • 135
  • 236

1 Answers1

-1

Short answer: You can't and you shouldn't. The Gulp philosophy is that one task should just do one thing. So your setup is actually perfectly fine. So stick with your configuration.

Slightly longer answer: There is a plugin called gulp-uglifyjs which enables you to do everything the original uglify can do (including wrapping and stuff like that). It is however blacklisted by the Gulp community as being a duplicate and not "in the way Gulp was meant to be".

The code would look like this:

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

gulp.task('scripts', function(){
   return gulp.src('./app/**/*.js')
       .pipe(uglify({
           wrap: true
        }))
        .pipe(gulp.dest('./dest'));
});

The wrapping done by Uglify is also very opinionated, which means that you can't add anything on your own and have to stick with it's module wrapper.

So, as said: Your setup is actually the one to go for ;-)

Btw.: For performance reasons, I would call uglify() just once

ddprrt
  • 7,424
  • 3
  • 29
  • 25