Search code examples
npmgulppostcss

Gulp v4 watch task



I just upgraded to Gulp v4 and was wondering why my gulpfile isn't working anymore.
I tried the new code of the new documentation but it didn't worked out as planned because I'm using "pure" postcss.
So I googled for my issue and found this question: Gulp error: watch task has to be a function
However, this also wasnt a solution for my problem, although I get the same error message Error: watching src/styles/**/*.scss: watch task has to be a function

I currently have this code

var gulp = require('gulp');
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');

gulp.task('default', function () {
    gulp.watch('src/styles/**/*.scss', ['styles']);
});

gulp.task('styles', function () {
    var processors = [
        autoprefixer({
            browsers: ['last 3 versions', 'ie > 9']
        }),
        cssnano()
    ];
    gulp.src('src/styles/main.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(postcss(processors))
        .pipe(gulp.dest('dist/files/styles/'));
});

and when I change
gulp.watch('src/styles/**/*.scss', ['styles']);
to
gulp.watch('src/styles/**/*.scss', gulp.series('styles'));
it just gives me a Starting 'default'... and after changing a file Starting 'styles'...
with Gulp 3.9 it was

Starting 'default'...
Finished 'default' after 174 ms

and after changing a file

Starting 'styles'...
Finished 'styles' after 561 μs

I've now tried many different things but I just dont get it to work like it did before. I'm really thinking of switching over to webpack like the cool kids now. But Gulp always worked fine.

Can someone explain to me what I'm doing wrong?


Solution

  • After reading the documentation and, more importantly, understanding it, I was able to figure it out myself.

    const autoprefixer = require('autoprefixer');
    const babel = require('gulp-babel');
    const cssdeclarationsorter = require('css-declaration-sorter');
    const cssnano = require('cssnano');
    const gulp = require('gulp');
    const postcss = require('gulp-postcss');
    const rename = require('gulp-rename');
    const sass = require('gulp-sass');
    const uglify = require('gulp-uglify');
    
    const paths = {
        'styles': {
            'base': 'src/styles/',
            'src': 'src/styles/main.scss',
            'dest': 'dist/styles/',
            'watch': 'src/styles/**/*.scss',
        },
        'scripts': {
            'base': 'src/scripts/',
            'src': 'src/scripts/**/*.js',
            'dest': 'dist/scripts/',
            'watch': 'src/scripts/**/*.js',
        },
    };
    
    const styles = function () {
        const plugins = [
            autoprefixer(),
            cssdeclarationsorter(),
            cssnano(),
        ];
    
        return gulp.src(
            paths.styles.src,
            {
                'base': paths.styles.base,
            }
        ).
            pipe(sass().on(
                'error',
                sass.logError
            )).
            pipe(postcss(plugins)).
            pipe(rename({
                'basename': 'styles',
                'suffix': '.min',
            })).
            pipe(gulp.dest(paths.styles.dest));
    };
    
    const scripts = function () {
        return gulp.src(
            paths.scripts.src,
            {
                'base': paths.scripts.base,
            }
        ).
            pipe(babel()).
            pipe(uglify()).
            pipe(rename({
                'suffix': '.min',
            })).
            pipe(gulp.dest(paths.scripts.dest));
    };
    
    const watch = function () {
        gulp.watch(
            paths.scripts.watch,
            scripts
        );
        gulp.watch(
            paths.styles.watch,
            styles
        );
    };
    
    const build = gulp.parallel(
        styles,
        scripts,
        gulp.series(watch)
    );
    
    exports.default = build;
    

    And for module

    import autoprefixer from 'autoprefixer';
    import babel from 'gulp-babel';
    import cssdeclarationsorter from 'css-declaration-sorter';
    import cssnano from 'cssnano';
    import gulp from 'gulp';
    import postcss from 'gulp-postcss';
    import rename from 'gulp-rename';
    import sass from 'gulp-sass';
    import uglify from 'gulp-uglify';
    
    const paths = {
        'styles': {
            'base': 'src/styles/',
            'src': 'src/styles/main.scss',
            'dest': 'dist/styles/',
            'watch': 'src/styles/**/*.scss',
        },
        'scripts': {
            'base': 'src/scripts/',
            'src': 'src/scripts/**/*.js',
            'dest': 'dist/scripts/',
            'watch': 'src/scripts/**/*.js',
        },
    };
    
    export const styles = function () {
        const plugins = [
            autoprefixer(),
            cssdeclarationsorter(),
            cssnano(),
        ];
    
        return gulp.src(
            paths.styles.src,
            {
                'base': paths.styles.base,
            }
        ).
            pipe(sass().on(
                'error',
                sass.logError
            )).
            pipe(postcss(plugins)).
            pipe(rename({
                'basename': 'styles',
                'suffix': '.min',
            })).
            pipe(gulp.dest(paths.styles.dest));
    };
    
    export const scripts = function () {
        return gulp.src(
            paths.scripts.src,
            {
                'base': paths.scripts.base,
            }
        ).
            pipe(babel()).
            pipe(uglify()).
            pipe(rename({
                'suffix': '.min',
            })).
            pipe(gulp.dest(paths.scripts.dest));
    };
    
    export const watch = function () {
        gulp.watch(
            paths.scripts.watch,
            scripts
        );
        gulp.watch(
            paths.styles.watch,
            styles
        );
    };
    
    const build = gulp.parallel(
        styles,
        scripts,
        gulp.series(watch)
    );
    
    export default build;