在我的 Gulpfile 中,我遇到了一个问题,控制台中总是会出现错误:
[22:33:32] The following tasks did not complete: default, watch, watch:views, watch:styles, watch:scripts
[22:33:32] Did you forget to signal async completion?
但是,据我所知,我只是定义了一些监视任务,对此没有任何可疑之处。那么 gulp watch 任务的错误在哪里呢?
gulp.task('reload', (done) => {
browserSync.reload();
done();
});
gulp.task('watch:views', () => {
gulp.watch(getPath(paths.views.watch), gulp.series('lint', gulp.parallel('views', 'assets'), 'reload'));
});
gulp.task('watch:styles', () => {
gulp.watch(getPath(paths.styles.all), gulp.series('lint', gulp.parallel('styles', 'assets'), 'reload'));
});
gulp.task('watch:scripts', () => {
gulp.watch(getPath(paths.scripts.all), gulp.series('scripts', 'reload'));
});
gulp.task('watch', gulp.parallel('watch:views', 'watch:styles', 'watch:scripts'));
gulp.task('default', gulp.series(
'clean',
'lint',
gulp.parallel(['views', 'styles', 'scripts', 'assets']),
'serve',
'watch',
));
更新:资产、样式和脚本任务。
这些任务被定义为其他文件中的导出默认函数。在 gulpfile 中,它们将被加载并设置为任务。单独调用这些任务没有问题,它们会正确发出异步完成的信号。包含所有这些任务的默认任务运行良好。就在监视任务开始时它失败了。
gulp.assets.js
import gulp from 'gulp';
import { paths, getPath, getTargetPath } from './paths';
export default () => {
return gulp.src(getPath(paths.copy.src), {
base: getPath(paths.copy.base)
})
.pipe(gulp.dest(getTargetPath(paths.target.assets)));
}
gulp.styles.js
import gulp from 'gulp';
import sass from 'gulp-sass';
import rename from 'gulp-rename';
import cleanCSS from 'gulp-clean-css';
import gulpif from 'gulp-if';
import { paths, getPath, getTargetPath } from './paths';
import { isProd } from './env';
export default () => {
return gulp
.src(getPath(paths.styles.main))
.pipe(sass().on('error', sass.logError))
.pipe(gulpif(isProd(), cleanCSS()))
.pipe(rename({ dirname: '' }))
.pipe(gulp.dest(getTargetPath(paths.target.assets)));
};
gulp.scripts.js
import gulp from 'gulp';
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import babelify from 'babelify';
import rename from 'gulp-rename';
import gulpif from 'gulp-if';
import uglify from 'gulp-uglify-es';
import { paths, getPath, getTargetPath } from './paths';
import { isProd } from './env';
export default () => {
return browserify({
entries: getPath(paths.scripts.main),
debug: true,
extensions: ['.js', '.jsx']
})
.transform(babelify.configure({
presets: ["@babel/preset-env", "@babel/preset-react"]
}))
.bundle()
.pipe(source('main.js'))
.pipe(buffer())
.pipe(gulpif(isProd(), uglify()))
.pipe(rename({ dirname: '' }))
.pipe(gulp.dest(getTargetPath(paths.target.assets)));
}