【问题标题】:How can I run uglify then strip-debug in one task using gulp如何使用 gulp 在一项任务中运行 uglify 然后剥离调试
【发布时间】:2016-08-24 00:35:18
【问题描述】:

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

【问题讨论】:

  • 您的变量名称是_uglify,但您将uglify 传递给merge()?改变它会解决什么问题吗?
  • 我改了,错误消失了。但是,控制台日志不会被删除

标签: javascript gulp gulp-uglify


【解决方案1】:

我假设app/assets/js/scripts.js 是由gulp-useref 生成的串联JavaScript 文件。

在这种情况下,使用merge-stream 将不起作用,因为当您尝试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/'))
});

【讨论】:

  • 这应该被接受为问题的书面答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多