Oscar
Oscar

Reputation: 105

gulp minifies already minified file

I try to minify separate .js-files with gulp. Like:

file_one.js --> file_one.min.js
file_two.js --> file_two.min.js

It works the first time I execute gulp. But if I run it a second time it looks like this:

file_one.js 
file_one.min.js
file_one.min.min.js
file_two.js
file_two.min.js
file_two.min.min.js

And it repeats that pattern every timme i execute gulp. How can I stop it from minify already minified js.

I use the following code:

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

Upvotes: 7

Views: 4260

Answers (3)

Bipin Shilpakar
Bipin Shilpakar

Reputation: 188

You are having problem with duplicate so you need to delete the destination file first to re-run the file. See Example:

//clean
gulp.task('clean', function(){
    return del(['dist']);
});

    //Default task
    gulp.task('default',['clean'], function(){
        gulp.start('usemin','imagemin','copy','views');
    });

Upvotes: 0

Marlon Bernardes
Marlon Bernardes

Reputation: 13843

This is happening because you are saving the minified files in the same directory of your non minified files. The first part of your stream (gulp.src) reads all files inside your dest folder. Since you are saving your minified files to the very same folder, it is minifying them again on the second time it is run. the You have some options:

  1. Change the output folder to something else (for instance gulp.dest('build'))
  2. Change gulp.src to match only the non-minified files

Upvotes: 8

Patrik Oldsberg
Patrik Oldsberg

Reputation: 1550

You can put the minified files in a different directory, or you can exclude them in the gulp.srclike this:

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

Upvotes: 26

Related Questions