gulp生成TypeError: Streams属性必须是函数类型



我得到了一个错误TypeError: The "streams[stream.length - 1]" property must be of type function. Received an instance of Pumpify,而试图使用gulp最小化javascript包,

Using gulpfile MY_PROJECT_PATHgulpfile.js
Starting 'compress'...
'compress' errored after 21 ms
TypeError: The "streams[stream.length - 1]" property must be of type function. Received an instance of Pumpify
at popCallback (MY_PROJECT_PATHnode_modulesreadable-streamlibinternalstreamspipeline.js:59:3)
at pipeline (MY_PROJECT_PATHnode_modulesreadable-streamlibinternalstreamspipeline.js:134:37

这是我在[gulpfile.js]中的代码

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var pipeline = require('readable-stream').pipeline;
gulp.task('compress', function () {
return pipeline(
gulp.src('DIR_NAME/*.js'),
uglify(),
gulp.dest('DIR_NAME/dist')
);
});

包。在调试

时,我试图安装[pipeline, read -stream, pumpify]
{
"devDependencies": {
"gulp": "^4.0.2",
"gulp-uglify": "^3.0.2",
"pipeline": "^0.1.3",
"pumpify": "^2.0.1",
"readable-stream": "^4.3.0"
}
}

解决方案1streamreadable_streampipeline函数需要一个回调作为最后一个参数。

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var pipeline = require('readable-stream').pipeline;
gulp.task('compress', function (cb) {
return pipeline(
gulp.src('DIR_NAME/*.js'),
uglify(),
gulp.dest('DIR_NAME/dist'),
cb
);
});

解决方案2

stream/promises公开了一个基于承诺的pipeline版本,它不使用回调:

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var pipeline = require('stream/promises').pipeline;
gulp.task('compress', async function () {
await pipeline(
gulp.src('DIR_NAME/*.js'),
uglify(),
gulp.dest('DIR_NAME/dist')
);
});

3

解决方案然后是传统的管道蒸汽方式,用.pipe()方法。

var gulp = require('gulp');
var uglify = require('gulp-uglify');
gulp.task('compress', function () {
return gulp.src('DIR_NAME/*.js')
.pipe(uglify())
.pipe(gulp.dest('DIR_NAME/dist'));
});

相关内容

  • 没有找到相关文章

最新更新