【问题标题】:Returning a stream but still getting error "Did you forget async completion?" Gulp 4返回流但仍然出现错误“您忘记了异步完成吗?”吞咽 4
【发布时间】:2020-06-15 05:06:44
【问题描述】:

我正在gulp上建一个网站,这段代码sn-p基本上包含3个任务browser-syncjekyll-buildstylusbrowser-sync任务依赖jekyll-build

browser-sync 工作正常,但我返回流的stylus 函数给出了一个意外错误。

下面是我的代码 sn-p。 我在这里返回一个流,这是文档Async Completion 中提到的解决方案之一,但我仍然收到错误。

var gulp        = require('gulp'),
    plumber     = require('gulp-plumber'),
    browserSync = require('browser-sync'),
    stylus      = require('gulp-stylus'),
    uglify      = require('gulp-uglify'),
    concat      = require('gulp-concat'),
    jeet        = require('jeet'),
    rupture     = require('rupture'),
    koutoSwiss  = require('kouto-swiss'),
    prefixer    = require('autoprefixer-stylus'),
    imagemin    = require('gulp-imagemin'),
    cp          = require('child_process');

var messages = {
    jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};

var jekyllCommand = (/^win/.test(process.platform)) ? 'jekyll.bat' : 'jekyll';

/**
 * Build the Jekyll Site
 */
gulp.task('jekyll-build', function (done) {
    browserSync.notify(messages.jekyllBuild);
    return cp.spawn(jekyllCommand, ['build'], {stdio: 'inherit'})
        .on('close', done);
});


/**
 * Wait for jekyll-build, then launch the Server
 */
gulp.task('browser-sync', gulp.series('jekyll-build'), function() {
    browserSync({
        server: {
            baseDir: '_site'
        }
    });
    done();
});

/**
 * Stylus task
 */
gulp.task('stylus', function() {
    return gulp.src('src/styl/main.styl').pipe(plumber())
        .pipe(
            stylus({
            use: [koutoSwiss(), prefixer(), jeet(), rupture()],
            compress: true,
            })
        )
        .pipe(gulp.dest('_site/assets/css/'))
        .pipe(gulp.dest('assets/css'))
        .pipe(browserSync.stream());
});

错误如下所示

[23:28:52] Using gulpfile ~/berserker1.github.io/gulpfile.js
[23:28:52] Starting 'default'...
[23:28:52] Starting 'browser-sync'...
[23:28:52] Starting 'jekyll-build'...
Configuration file: /home/aaryan/berserker1.github.io/_config.yml
            Source: /home/aaryan/berserker1.github.io
       Destination: /home/aaryan/berserker1.github.io/_site
 Incremental build: disabled. Enable with --incremental
      Generating... 
                    done in 0.234 seconds.
 Auto-regeneration: disabled. Use --watch to enable.
[23:28:52] Finished 'jekyll-build' after 818 ms
[23:28:52] Finished 'browser-sync' after 819 ms
[23:28:52] Starting 'stylus'...
[23:28:53] The following tasks did not complete: default, stylus
[23:28:53] Did you forget to signal async completion?

【问题讨论】:

  • 请也包括你的stylus 任务?
  • 添加了代码,
  • stylus 中,您可以尝试gulp.task('stylus', function(cb) {,然后在最后一个pipe 之后调用cb()。并删除return,看看是否有另一种发送异步完成信号的方法有帮助。
  • 我试过了,发生的事情是所有任务都完成了,没有浏览器同步提供任何文件(意味着我的网站没有启动),我认为这是因为为了在这里使用浏览器同步,我必须return stream 而不是callback

标签: gulp stylus gulp-4


【解决方案1】:

这里有一个问题(是否与您的问题相同,我们会看看是否有帮助):

gulp.task('browser-sync', gulp.series('jekyll-build'), function() {
    browserSync({
        server: {
            baseDir: '_site'
        }
    });
    done();
});

我 gulp v4 task 只接受两个参数 - 上面的代码有三个。这应该是正确的:

gulp.task('browser-sync', gulp.series('jekyll-build', function(done) {
    browserSync({
        server: {
            baseDir: '_site'
        }
    });
    done();
}));

另外,我在第一行添加了done 参数。看看这是否有帮助。

【讨论】:

  • 我已将这些更改应用于我的代码,现在我认为它部分正确。我的意思是doc of browser-sync + gulp 已经老了,所以没有太多帮助。问题是当我做gulp.task('default', gulp.series('browser-sync', 'stylus') 一切正常并且我的网站加载但“手写笔”任务从未完成时,所以如果我在手写笔之后添加任何任务(例如task js),它将永远无法工作,作为解决我现在正在做这个gulp.task('default', gulp.series('browser-sync', gulp.parallel('stylus, 'js')
  • gulpv4 有这么多重大变化我不知道为什么,而且我正在做的解决方法实际上是一种解决方法或根据新版本接受的新方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多