1

刚开始学习 gulp 并遵循本教程系列:https ://www.youtube.com/watch?v=oRoy1fJbMls&list=PLriKzYyLb28lp0z-OMB5EYh0OHaKe91RV

它在 gulp 3 上完美运行,但在将 npm 更新到当前版本后它崩溃了,我尝试将我的 gulpfile.js 从版本 3 转换为 4,并且在运行 gulp 命令后出现此错误: 以下任务未完成:默认, 您是否忘记发出异步完成信号?我该如何解决这个问题?

这是我的 gulpfile:

const gulp = require('gulp');
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const uglify = require('gulp-uglify');
const autoprefixer = require('gulp-autoprefixer');
const sourcemaps = require('gulp-sourcemaps');
const browserify = require('browserify');
const babelify = require('babelify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');

let styleSource = 'src/scss/style.scss';
let styleDestination = './build/css/';
let styleWatch = 'src/scss/**/*.scss';

let jsSource = 'main.js';
let jsFolder = 'src/js/';
let jsDestination = './build/js/';
let jsWatch = 'src/js/**/*.js';
let jsFILES = [jsSource];    
let htmlWatch = '**/*.html';

/* Converting Sass to CSS */
gulp.task('styles',function(){
    return gulp.src(styleSource)
        .pipe(sourcemaps.init())
        .pipe(sass({
            errorLogToConsole: true,
            outputStyle: 'compressed'
        }))
        .on('error', console.error.bind(console))
        .pipe(autoprefixer({
            browsers: ['last 2 versions'],
            cascade: false
        }))
        .pipe(rename({suffix:'.min'}))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(styleDestination));
});

/* Converting ES6 to Vanilla JS */
gulp.task('js',function(){
    return jsFILES.map(function(entry){
        return browserify({
            entries: [`${jsFolder}${entry}`]
        })
        .transform(babelify, {presets:['env']})
        .bundle()
        .pipe(source(entry))
        .pipe( rename({extname:'.min.js'}) )
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true})) 
        .pipe(uglify())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(jsDestination))
    });

})

// default task to run all tasks
const compile = gulp.parallel(['styles','js']);
compile.description = 'Compile all styles and js files';

gulp.task('default', compile);

// watch default
const watch = gulp.series('default', function(){ // ,'browser-sync'
// keep running, watching and triggering gulp
    gulp.watch(styleWatch, gulp.parallel('styles')); //, reload
    gulp.watch(jsWatch, gulp.parallel('js')); //, reload
    gulp.watch(htmlWatch);
});
watch.description = 'watch all changes in every files and folders';
gulp.task('watch', watch);

这是我运行 gulp 后的错误: 在此处输入图像描述

4

1 回答 1

3

您的js任务返回一个数组,gulp 不明白这一点。一个任务可以返回一个流或一个承诺——或者它必须调用回调参数来表示完成,如下所示:

gulp.task('js', function(cb) {
    // Your js task code here
    cb();    
})

另外,阅读最新的gulp4 文档

于 2018-10-12T16:45:19.513 回答