2016-08-24 57 views
0

我想要的是來縮小我在我的index.html所有的JS然後刪除所有console.logs如何運行使用一飲而盡

我嘗試了兩種選擇醜化然後帶調試在一個任務:

我試圖MERGE但只有醜化執行

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

我試圖返回兩個,而是隻執行醜化:

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

您的變量的名字是'_uglify'但你通過'uglify'成'合併()'?改變它能解決什麼問題嗎? –

+0

我改變了它,錯誤消失了。但是,控制檯日誌不會被刪除 –

回答

1

我假設app/assets/js/scripts.js是由gulp-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/')) 
}); 
+0

這應該被接受爲問題的寫回答。 –