admin管理员组

文章数量:1186099

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'));
});

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'));
});
Share Improve this question asked Jul 17, 2014 at 13:44 OscarOscar 1052 silver badges10 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 26

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'));
});

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

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');
    });

本文标签: javascriptgulp minifies already minified fileStack Overflow