简体   繁体   English

gulp 4.0:连续运行任务

[英]gulp 4.0: run tasks in series

I'm trying to run a set of tasks in series, that is one after the other in Gulp 4.0 however when I add my 3rd task my gulp code breaks. 我试图按顺序运行一组任务,这在Gulp 4.0中是一个接一个的,但是当我添加第三个任务时,我的gulp代码就会中断。

    gulp.task('concat-js', (done) => {
      gulp.src([  
        'app1.js',
        'app2.js',
      ])
      .pipe(concat("app.js")) 
      .pipe(gulp.dest('build'))
      done();
    });

    gulp.task('concat-css', (done) => {
      gulp.src([
        '.styles1.css',
        '.style2.css'
      ])
      .pipe(concat("app.css"))
      .pipe(gulp.dest('build'))
      done();  
    });

    gulp.task('minify-js', (done) => {
      gulp.src('./build/app.js')
      .pipe(uglify())
      .pipe(gulp.dest('./build/'))
      done();
    })

    //this works & creates /build with the app.js & app.css files
    gulp.task('build-production-static-resource', gulp.series('concat-js', 'concat-css',, (done) => {
      done();
    }));

If I delete the build folder to start all over & try adding another task (minfy-js), my third task fails & the build folder also doesn't get created. 如果我删除构建文件夹以重新开始并尝试添加另一个任务(minfy-js),则我的第三个任务将失败,并且也不会创建构建文件夹。

    //File not found with singular glob: /Users/user/myapp/build/app.js
    gulp.task('build-production-static-resource', gulp.series('concat-js', 'concat-css', 'minify-js', (done) => {
      done();
    }));

The way you're signalling async completion of your tasks is wrong. 信号异步完成任务的方式是错误的。 Read this answer for an overview of the different ways in which you can signal async completion in gulp. 阅读此答案以概述在gulp中发信号异步完成的不同方式。

Basically all the streams that you create via gulp.src() are asynchronous. 基本上,您通过gulp.src()创建的所有流都是异步的。 That means you create the stream and your code immediately returns. 这意味着您创建了流,并且代码立即返回。 However the actual processing of the stream starts only after you exit the task function. 但是,仅退出任务功能之后才开始对流进行实际处理。 In order for gulp to know that you're using a stream and that it has to wait for the stream to finish processing, you need to return the stream from your task. 为了使gulp知道您正在使用流,并且它必须等待流完成处理,您需要从任务中return该流。

You're doing something else entirely. 您正在完全做其他事情。 You're invoking a callback function done which is another way to signal asynchronous task completion to gulp. 您正在调用已done的回调函数done这是向gulp发出异步任务完成信号的另一种方法。 However that's the completely wrong way in this case, because when you invoke the callback function done the stream you created hasn't even started to process. 然而这是在这种情况下,完全走错了路,因为当你调用回调函数done创建甚至还没有开始处理流。

That means gulp thinks your concat-js task has finished, although the code inside your task hasn't even really started running. 这意味着gulp认为concat-js任务已经完成,尽管任务中的代码甚至还没有真正开始运行。 So when the minify-js task runs your ./build/app.js file hasn't been created yet. 因此,当./build/app.js minify-js任务运行时,尚未创建./build/app.js文件。 Boom. 繁荣。 Error. 错误。

To fix this always return the streams from your tasks: 要解决此问题,请始终从任务中return流:

gulp.task('concat-js', () => {
  return gulp.src([  
      'app1.js',
      'app2.js',
    ])
    .pipe(concat("app.js")) 
    .pipe(gulp.dest('build'))
});

gulp.task('concat-css', () => {
  return gulp.src([
      '.styles1.css',
      '.style2.css'
    ])
    .pipe(concat("app.css"))
    .pipe(gulp.dest('build'))
});

gulp.task('minify-js', () => {
  return gulp.src('./build/app.js')
    .pipe(uglify())
    .pipe(gulp.dest('./build/'))
})

gulp.task('build-production-static-resource', gulp.series(
  'concat-js', 'concat-css', 'minify-js'
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM