繁体   English   中英

如何使用gulp在一个任务中运行uglify然后进行带调试

[英]How can I run uglify then strip-debug in one task using gulp

我想要的是缩小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'));
    });

我假设app/assets/js/scripts.jsgulp-useref生成的级联JavaScript文件。

在那种情况下,使用merge-stream将不起作用,因为当您尝试对其gulp.src()进行gulp.src()时, app/assets/js/scripts.js文件可能尚不存在。 相反,只需在您的第一个流中添加另一个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/'))
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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