gulp-uglify 缩小脚本,它不应该

gulp-uglify minifying script that it is not supposed to

当我 运行 gulp scripts 时,all.js 和 all.min.js 都缩小了。有人知道为什么会这样吗?

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

});

实际情况是您将两个脚本串联在一起

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

缩小它们之前

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

一个可能的解决方案是:

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

希望对您有所帮助。

问题就在这里

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

您没有将修改后的流返回到 concatted

您可以将其更改为

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

但这也符合预期

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