什么是 gulp 任务异步 运行 提示?

What are gulp task async run hints?

我有两个任务。

Sass:

gulp.task('sass', function() {
    gulp.src('src/sass/**.{scss, sass}')
        .pipe(sourcemaps.init())
            .pipe(sass({outputStyle: 'compressed'}))
            .on('error', handleErrors)
            .pipe(autoprefixer())
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('dist/css'))
        .pipe(browserSync.reload({stream: true}))
});

和CSS优化任务:

gulp.task('cssopt', ['sass'], function() {
    gulp.src('dist/css/style.css')
        .pipe(uncss({html: ['http://example.dev//']}))
        .pipe(cmq({log: true}))
        .pipe(shrink())
        .pipe(rename({suffix: '.opt'}))
        .pipe(gulp.dest('dist/css'))
});

cssopt 任务依赖于 sass 任务,因为它使用 sass 生成的 style.css 文件。但它不会工作,它找不到 style.css 文件,因为它不存在。如果我先 运行 sass,然后 cssopt 起作用,因为 style.css 在那里。

来自文档(强调我的):

An array of tasks to be executed and completed before your task will run.

文档还说:

Note: Are your tasks running before the dependencies are complete? Make sure your dependency tasks are correctly using the async run hints: take in a callback or return a promise or event stream.

我不太明白:"async run hints"... wat...

我尝试 运行 clean 任务作为 deps sass 并且效果很好。


我可以将它们分解成一个任务,但是 cssopt 需要相当长的时间(uncss 当你给它一个 URL 时相当慢),我需要 style.opt.css 文件只用于生产。

我正在使用 gulp-sass 的最新版本 (1.3.3)。

"async run hints"... wat...

您需要提示 gulp 系统您的任务是async。您可以通过为您的任务函数提供回调参数,或通过 returning 事件流或承诺来做到这一点。这就是您的任务所缺少的,它们 return undefined 当前 gulp 希望它们立即完成。尝试

gulp.task('sass', function() {
    return gulp.src('src/sass/**.{scss, sass}').…