gulp @ 4系列/并行任务多次运行

时间:2018-07-25 13:24:48

标签: gulp gulp-zip

在我的gulpfile中使用以下代码:

var gulp = require('gulp');
var pkg = require('./package.json');
var zip = require('gulp-zip');

var zipDist = () =>
  gulp
    .src('dist/**/*')
    .pipe(zip(`${pkg.name.replace('@relax/', '')}.dist.zip`))
    .pipe(gulp.dest('./'));

var zipFonts = () =>
  gulp
    .src('dist/fonts/*')
    .pipe(zip(`${pkg.name.replace('@relax/', '')}.fonts.dist.zip`))
    .pipe(gulp.dest('./'));

var zipIcons = () =>
  gulp
    .src(['dist/icons/*', 'dist/loaders/*', 'dist/sprites/*'])
    .pipe(zip(`${pkg.name.replace('@relax/', '')}.icons.dist.zip`))
    .pipe(gulp.dest('./'));

var zip = gulp.series(zipDist, zipFonts, zipIcons);

gulp.task('zip', gulp.series(zipDist, gulp.parallel(zipFonts, zipIcons)));

并运行gulp:

$ gulp zip

我得到这个结果:

$ gulp zip                                                                                            
[21:41:45] Using gulpfile ~/project/packages/core/gulpfile.js
[21:41:45] Starting 'zip'...
[21:41:45] Starting 'zipDist'...
[21:41:45] Finished 'zipDist' after 201 ms
[21:41:45] Starting 'zipFonts'...
[21:41:45] Starting 'zipDist'...
[21:41:45] 'zipFonts' errored after 1.48 ms
[21:41:45] TypeError: Cannot read property 'on' of undefined
    at DestroyableTransform.Readable.pipe (/home/alexander/project/packages/core/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:564:8)
    at zipFonts (/home/alexander/project/packages/core/gulpfile.js:181:6)
    at bound (domain.js:301:14)
    at runBound (domain.js:314:12)
    at asyncRunner (/home/alexander/project/packages/core/node_modules/async-done/index.js:55:18)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)
[21:41:45] 'zip' errored after 204 ms
[21:41:45] The following tasks did not complete: zipDist
[21:41:45] Did you forget to signal async completion?

版本:

$ gulp -v
[15:22:45] CLI version 2.0.1
[15:22:45] Local version 4.0.0

zipDist为什么要运行两次?如果我这样做:

gulp.task('zip', zipDist);

它只运行一次,但是我还需要完成其他两项任务。

编辑:

我现在添加了一个更完整的输出,并抛出一个错误。起初我认为这无关紧要。我删除了有问题的代码,日志显示一切正常。看来,如果在任务中引发错误,gulp想要重试吗?这是设计使然吗?

对于那些感兴趣的人,我在此处提交了有关该gulp-zip错误的问题:https://github.com/sindresorhus/gulp-zip/issues/105

1 个答案:

答案 0 :(得分:0)

我开始将var转换为const和尤里卡!令人反感的代码是:

var zip = require('gulp-zip');
...
var zipDist = () => gulp.src(..).pipe(zip(...))
...
var zip = gulp.series(zipDist, ...);

zip被声明了两次。 facepalm