Gulp-uglify wrap js

I have a gulp task that looks 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

All of the above works fine. But then I discovered that it gulp-uglify

seems to have a function of its own wrap

. How do I use a function gulp-uglify

to wrap a script?

+3


source to share


1 answer


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

Slightly long answer: there is a plugin called gulp-uglifyjs

that allows you to do everything the original can do uglify

(including wrapping and the like). However, it is blacklisted by the Gulp community as a duplicate and not "in the Gulp way".

The code will 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 wrapper made by Uglify is also very stubborn, which means you can't add anything yourself and have to stick with its module wrapper.

So, as said: Your setup is actually the one that needs :-)

Btw: For performance reasons, I would uglify()

only call it once

-1


source