如何使用gulp在一个任务中运行uglify然后strip-debug

时间:2016-08-24 00:35:19

标签: javascript gulp gulp-uglify

我想要的是缩小我的index.html中的所有js然后删除所有 console.logs

我尝试了两个选项:

我试过合并,但只执行了uglify

// Command: gulp useref
gulp.task('useref', function(){
    var _uglify = gulp.src('app/index.html') // .src is the function that is very similar to locating or searching on that file or folder
    .pipe(useref())
    // Minifies only if it's a Javascript file
    .pipe(gulpIf('*.js', uglify()))
    // Minifies only if it's a CSS file
    .pipe(gulpIf('*.css', cssnano()))
    .pipe(gulp.dest('app/')) // .dest is the location where it will produce the output
    // set to app/, so it will automatically change the index and there's no need to move files 

    var _strip_debug = gulp.src('app/assets/js/scripts.js')
    .pipe(stripDebug())
    .pipe(gulp.dest('app/assets/js'));

    return merge(_uglify, _strip_debug);
});

我尝试返回两个,但只执行了uglify:

    gulp.task('useref', function(){
        return gulp.src('app/index.html') // .src is the function that is very similar to locating or searching on that file or folder
        .pipe(useref())
        // Minifies only if it's a Javascript file
        .pipe(gulpIf('*.js', uglify()))
        // Minifies only if it's a CSS file
        .pipe(gulpIf('*.css', cssnano()))
        .pipe(gulp.dest('app/')) // .dest is the location where it will produce the output
        // set to app/, so it will automatically change the index and there's no need to move files 

        return gulp.src('app/assets/js/scripts.js')
        .pipe(stripDebug())
        .pipe(gulp.dest('app/assets/js'));
    });

1 个答案:

答案 0 :(得分:1)

我认为app/assets/js/scripts.jsgulp-useref生成的串联JavaScript文件。

在这种情况下使用merge-stream将不起作用,因为当您尝试app/assets/js/scripts.js时,gulp.src()文件可能尚不存在。而只需在您的第一个流中添加另一个gulpIf阶段:

gulp.task('useref', function(){
   return gulp.src('app/index.html')
     .pipe(useref())
     .pipe(gulpIf('*.js', stripDebug()))
     .pipe(gulpIf('*.js', uglify()))
     .pipe(gulpIf('*.css', cssnano()))
     .pipe(gulp.dest('app/'))
});