pump = require('pump')
https://github.com/terinjokes/gulp-uglify/blob/master/docs/why-use-pump/README.md#why-use-pumphtml
当使用来自Node.js的管道时,错误不会经过管道流向前传播,若是目标流关闭,源流也不会关闭。pump模块将这些问题规范化,并在回调中传递错误。node
pump可使咱们更容易找到代码出错位置。git
A common pattern in gulp files is to simply return a Node.js stream, and expect the gulp tool to handle errors.github
gulp文件中的一个常见模式是简单地返回一个Node.js流,并期待gulp工具能处理错误gulp
// example of a common gulpfile var gulp = require('gulp'); var uglify = require('gulp-uglify'); gulp.task('compress', function () { // returns a Node.js stream, but no handling of error messages return gulp.src('lib/*.js') .pipe(uglify()) .pipe(gulp.dest('dist')); });
There’s an error in one of the JavaScript files, but that error message is the opposite of helpful. You want to know what file and line contains the error. So what is this mess?api
这里有一个错误在某个JavaScript文件中,可是这些错误信息并无起到什么做用。咱们真正想要知道的是哪一个文件的哪一行出现了这些错误。app
When there’s an error in a stream, the Node.js stream fire the 'error' event, but if there’s no handler for this event, it instead goes to the defined uncaught exception handler. The default behavior of the uncaught exception handler is documented:ide
当在流中出现了错误时,将会触发'error'事件,可是若是这里的事件没有handler的话,它将会定义一个没有捕捉的异常handler,以下文档说明:函数
By default, Node.js handles such exceptions by printing the stack trace to stderr and exiting.默认状况下,Node.js经过将堆栈跟踪打印到stderr和退出来处理这些异常工具
Since allowing the errors to make it to the uncaught exception handler isn’t useful, we should handle the exceptions properly. Let’s give that a quick shot.
即然若是不处理异常将默认为uncaught exception handler(这是没有用的),那么咱们就应该恰当地对异常进行处理
var gulp = require('gulp'); var uglify = require('gulp-uglify'); gulp.task('compress', function () { return gulp.src('lib/*.js') .pipe(uglify()) .pipe(gulp.dest('dist')) .on('error', function(err) { //添加一个异常处理 console.error('Error in compress task', err.toString()); }); });
Unfortunately, Node.js stream’s pipe
function doesn’t forward errors through the chain, so this error handler only handles the errors given by gulp.dest
. Instead we need to handle errors for each stream.
可是这个异常处理是不会经过链来传递错误的,因此它只会处理来自 gulp.dest
的错误。那么若是咱们要处理每个流的错误的话,就要像下面同样,每一个流都进行错误处理
var gulp = require('gulp'); var uglify = require('gulp-uglify'); gulp.task('compress', function () { function createErrorHandler(name) { return function (err) { console.error('Error from ' + name + ' in compress task', err.toString()); }; } return gulp.src('lib/*.js') .on('error', createErrorHandler('gulp.src')) .pipe(uglify()) .on('error', createErrorHandler('uglify')) .pipe(gulp.dest('dist')) .on('error', createErrorHandler('gulp.dest')); });
This is a lot of complexity to add in each of your gulp tasks, and it’s easy to forget to do it. In addition, it’s still not perfect, as it doesn’t properly signal to gulp’s task system that the task has failed. We can fix this, and we can handle the other pesky issues with error propogations with streams, but it’s even more work!
这样就会变得很复杂,并且你也很容易就忘记这么作。因此咱们须要使用pump
The pump
module is a cheat code of sorts. It’s a wrapper around the pipe
functionality that handles these cases for you, so you can stop hacking on your gulpfiles, and get back to hacking new features into your app.
它是用来处理上面这些状况的pipe的包装器,这样您就能够中止对您的gulpfile文件进行黑客攻击,并从新对您的应用程序进行黑客攻击
var gulp = require('gulp'); var uglify = require('gulp-uglify'); var pump = require('pump'); gulp.task('compress', function (cb) { pump([ gulp.src('lib/*.js'), uglify(), gulp.dest('dist') ], cb //回调函数 ); });
The gulp task system provides a gulp task with a callback, which can signal successful task completion (being called with no arguments), or a task failure (being called with an Error argument). Fortunately, this is the exact same format pump
uses!
Now it’s very clear what plugin the error was from, what the error actually was, and from what file and line number.
这样咱们就可以看见是什么触发了error,在哪一个文件的哪一行
这只是一个简单的了解,若是想要更深刻的学习请自学https://github.com/pump-io/pump.io
以后若是看了这部分再写出来