Gulp4 - tasks did not complete and forget to signal async completion

4.9k views Asked by At

Just started learning gulp and followed this tutorial series:https://www.youtube.com/watch?v=oRoy1fJbMls&list=PLriKzYyLb28lp0z-OMB5EYh0OHaKe91RV

It works perfectly on gulp 3 but after updating npm to the current version it broke down and i tried converting my gulpfile.js from version 3 to 4, and after running the gulp command i have this error: The following tasks did not complete: default, Did you forget to signal async completion? How do i solve this?

Here's my 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);

And here's the error after i run gulp: enter image description here

1

There are 1 answers

0
Kamil Dzielinski On

Your js task returns an array, gulp doesn't understand that. A task can return a stream or a promise - or it must call the callback parameter to signal completion, like so:

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

Also, read the latest gulp4 documentation.