我对Gulp
及其库非常陌生。我刚刚在StackOverflow上看到过一些有关在gulpfile.js中使用JavaScript编写管道函数的文章。我尝试通过编写源自WriteStream
的非常短的管道函数来进行实验:
var gulp = require('gulp');
var connect = require('gulp-connect');
var PATHS = {
src: 'src/**/*.ts',
html: 'src/**/*.html',
css: 'src/**/*.css'
};
var stream = require('stream');
//var map = require('map-stream');
var writable = new stream.Writable({
write: function(chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
gulp.task('test', function() {
var test = gulp.src(PATHS.src);
gulp.src(PATHS.src)
.pipe(writable);
});
它给了我以下错误,但我不明白:
(node:5396) ExperimentalWarning: The http2 module is an experimental API.
[13:28:25] Starting 'test'...
[13:28:25] Finished 'test' after 7.12 ms
events.js:183
throw er; // Unhandled 'error' event
^
TypeError: Invalid non-string/buffer chunk
at validChunk (_stream_writable.js:254:10)
at Writable.write (_stream_writable.js:288:21)
at write (C:\AngularIBought\angular2byexample-base\trainer\node_modules\vinyl-fs\node_modules\readable-stream\lib\_stream_readable.js:623:24)
at flow (C:\AngularIBought\angular2byexample-base\trainer\node_modules\vinyl-fs\node_modules\readable-stream\lib\_stream_readable.js:632:7)
at DestroyableTransform.pipeOnReadable (C:\AngularIBought\angular2byexample-base\trainer\node_modules\vinyl-fs\node_modules\readable-stream\lib\_stream_readable.js:664:5)
at emitNone (events.js:106:13)
at DestroyableTransform.emit (events.js:208:7)
at emitReadable_ (C:\AngularIBought\angular2byexample-base\trainer\node_modules\vinyl-fs\node_modules\readable-stream\lib\_stream_readable.js:448:10)
at emitReadable (C:\AngularIBought\angular2byexample-base\trainer\node_modules\vinyl-fs\node_modules\readable-stream\lib\_stream_readable.js:444:5)
at readableAddChunk (C:\AngularIBought\angular2byexample-base\trainer\node_modules\vinyl-fs\node_modules\readable-stream\lib\_stream_readable.js:187:9)
我的代码有什么问题? 问候, 贾努斯
答案 0 :(得分:-1)
问题出在writable
函数上,而不一定是点子函数。一个pip函数,但以下是管道函数在Real World中的用法:
// Compress and minify images to reduce their file size
gulp.task('images', function() {
var imgSrc = './src/images/**/*',
imgDst = './images';
return gulp.src(imgSrc)
.pipe(plumber({
errorHandler: onError
}))
.pipe(changed(imgDst))
.pipe(imagemin())
.pipe(gulp.dest(imgDst))
.pipe(notify({ message: 'Images task complete' }));
});
如果您提供更多信息,例如writable
函数的功能,我可以帮助您重新编写它。