gulpgulp-uglifygulp-concatgulp-if

Skip uglifying already compressed files with Gulp


I have a situation where I was currently uglifying all JavaScript files even if they were already compressed, which obviously isn't a great idea.

My respective task was:

gulp.task('uglify', ['scripts'], function() {
    return gulp.src('./js/scripts.js')
        .pipe(rename({suffix: '.min'}))
        .pipe(uglify({
            mangle: false
        }))
        .pipe(gulp.dest('./js'));
});

scripts.js was a file that was previously concatenated together, but obviously if there was some already compressed files in there then they are going to get uglified again, which we are trying to avoid.

So I did a search around and have been looking at processing all the files again rather than working off of the unminified (the file that just contains concatenated files) version so that I have more control over each file.

I looked into using gulp-ignore and gulp-if and gulp-if seems to possibly be what I want; however I'm not 100% sure how streams operate in Gulp - when you pass it a list of files does it loop through the tasks for each file or just run once?

I'm curious because if it runs through for each file I was working on doing something like this:

gulp.task('uglify', ['scripts'], function() {
    return gulp.src(js_scripts)
        .pipe(gulpif(condition, uglify({mangle: false})))
        .pipe(concat('scripts.min.js'))
        .pipe(gulp.dest('./js'));
});

Where condition was something that checked to see if the file had .min in the filename and if so, don't uglify it.

So my question is - if it loops through and I can run the condition on each file, how can I go about running tests on the filename and if it doesn't loop through then how can I achieve what I am trying to do here? Am I going to be forced to create two separate tasks, one for unminified and one for .min files and then at the end join them together?


Solution

  • With the help of @lofihelsinki's answer and @mark's comment I was able to come up with a solution.

    Whilst these led me in the right direction, I still had to tweak some code through trial and error, so I was unable to accept the answer, but I wanted to credit them since they led me to the solution.

    The final solution was:

    gulp.task('uglify', ['scripts'], function() {
        return gulp.src(js_scripts)
            .pipe(gulpif('!**/*.min.js', uglify({mangle: false})))
            .pipe(concat('scripts.min.js'))
            .pipe(gulp.dest('./js'));
    });
    

    I tried the suggestion by @Mark but it seems that even though you have the files in the stream you still have to respect the folder location; so I had to change his suggestion of '!*.min.js' to '!**/*.min.js'; this will now only uglify scripts that do not contain .min.js in the filename and the ones that do will get passed through untouched.