gulpgulp-uglifygulp-concat

GULP: How to combine files AFTER uglify


Scenario I need to grab all *.js files in a folder, combine and uglify them. This is what I achieve with:

gulp.task('default', function(){
    return gulp.src('resources_folder/*.js')
        .pipe(plumber({
            errorHandler: function (error) {
                console.log(error.message);
                this.emit('end');
            }}))
        .pipe(concat('output.js'))
        .pipe(babel())
        .pipe(rename({suffix: '.min'}))
        .pipe(uglify())
        .pipe(gulp.dest('js/'))
});

But I'm struggling with two things:

  1. AFTER the uglify I would like to concat the result with an other (already minified file). I don't wish to uglify this file again - I want it just to combine with the result.

  2. How do I concat the results with a "new line" or a comment please?

What I want to achieve: I have my source fiels in "res" and Im using an external small library (minifeid already) which I would like to include into my output file.

Something like:


Solution

  • You will need two plugins:

    gulp-footer and gulp-add-src

     var footer = require('gulp-footer');
     var addsrc = require('gulp-add-src');
    

    So after your uglify pipe put something like:

    .pipe(uglify())
    .pipe(footer('\n// my comment\n'))
    .pipe(addsrc.append('your resource library to append'))
    .pipe(concat('new file name here'))
    .pipe(gulp.dest('js'))
    

    Look at the gulp-footer documentation to see different ways to build up the comment you wish to insert. You can, for example, read it from a file or variable.