简体   繁体   中英

gulp-uglify minifying script that it is not supposed to

When I run gulp scripts , both all.js and all.min.js end up minified. Anybody know why this happens?

gulp.task("scripts", function() {

    var concatted = gulp.src(['js/first.js', 'js/second.js'])
                        .pipe(concat('all.js'));

    // output concatenated scripts to js/all.js
    concatted.pipe(gulp.dest('js'));

    // minify concatenated scripts and output to js/all.min.js
    concatted.pipe(uglify())
             .pipe(rename('all.min.js')
             .pipe(gulp.dest('js'))

});

The problem is here

// output concatenated scripts to js/all.js
concatted.pipe(gulp.dest('js'));

You aren't returning the modified stream into concatted .

You could change this to

// output concatenated scripts to js/all.js
concatted = concatted.pipe(gulp.dest('js'));

but this also works as expected

gulp.task("scripts", function() {
    return gulp.src(['js/first.js', 'js/second.js'])
        .pipe(concat('all.js'))
        .pipe(gulp.dest('js'))
        .pipe(uglify())
        .pipe(rename('all.min.js')
        .pipe(gulp.dest('js'));
});

What's happening is that you're concatenating the two scripts

var concatted = gulp.src(['js/first.js', 'js/second.js'])
                    .pipe(concat('all.js'));
                    // Both scripts are now in 'all.js'

Before minifying them

concatted.pipe(uglify())
         .pipe(rename('all.min.js')
         .pipe(gulp.dest('js'))

One possible solution could be:

gulp.src('js/first.js')
    .pipe(uglify())
    .pipe(rename('all.min.js'))
    .pipe(gulp.dest('js'));

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM