Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'));
});
like image 312
Oscar Avatar asked Jul 17 '14 13:07

Oscar


2 Answers

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'));
});
like image 165
Patrik Oldsberg Avatar answered Sep 23 '22 10:09

Patrik Oldsberg


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
like image 35
Marlon Bernardes Avatar answered Sep 21 '22 10:09

Marlon Bernardes