Задать вопрос
  • Как поправить синтаксис кода gulp3 под gulp 4, чтобы все работало?

    1. Где gulp.src(...) надо return gulp.src(...)
    2. Для
    gulp.task('webserver', function () {
        browserSync(config);
    });
    надо сделать так
    gulp.task('webserver', function (cb) {
        browserSync(config);
        cb();
    });


    Пример

    const gulp = require('gulp');
    const pipeIf = require('gulp-if');
    const order = require('gulp-order');
    const nunjucks = require('gulp-nunjucks');
    const pretty = require('gulp-pretty-html');
    const minify = require('gulp-htmlmin');
    const replace = require('gulp-replace');
    const sass = require('gulp-sass');
    const sourcemaps = require('gulp-sourcemaps');
    const autoprefixer = require('gulp-autoprefixer');
    const clean = require('gulp-clean-css');
    const babel = require('gulp-babel');
    const concat = require('gulp-concat');
    const uglify = require('gulp-uglify');
    const image = require('gulp-image');
    const clear = require('gulp-clean');
    const browser = require('browser-sync').create();
    const { argv } = require('yargs');
    
    sass.compiler = require('node-sass');
    
    gulp.task('server', (callback) => {
    	browser.init({
    		server: {
    			baseDir: 'Build'
    		},
    		ui: {
    			port: 8080
    		},
    		notify: false
    	});
    
    	callback();
    });
    
    gulp.task('reload', (callback) => {
    	browser.reload();
    
    	callback();
    });
    
    gulp.task('clear', () => {
    	return gulp.src('Build', { allowEmpty: true })
    		.pipe(clear({
    			force: true
    		}));
    });
    
    gulp.task('build:html', (callback) => {
    	return gulp.src('Sources/**/*.html')
    		.pipe(nunjucks.compile())
    		.pipe(replace(/(\r\n|\r|\n)(\s+)?(\r\n|\r|\n)/gm, '\n'))
    		.pipe(pipeIf(argv.development, pretty()))
    		.pipe(pipeIf(argv.production, minify({ collapseWhitespace: true })))
    		.pipe(gulp.dest(`Build/`));
    });
    
    gulp.task('build:sass', () => {
    	return gulp.src(['Sources/Styles/*.sass', 'Sources/Styles/*.scss'])
    		.pipe(pipeIf(argv.development, sourcemaps.init()))
    		.pipe(sass())
    		.pipe(autoprefixer({
    			cascade: false
    		}))
    		.pipe(pipeIf(argv.production, clean()))
    		.pipe(pipeIf(argv.development, sourcemaps.write('.')))
    		.pipe(gulp.dest('Build/Styles/'));
    });
    
    gulp.task('build:js', () => {
    	return gulp.src('Sources/Scripts/**/*.js')
    		.pipe(pipeIf(argv.development, sourcemaps.init()))
    		.pipe(order([
    			// Order for JS files
    			'*.js'
    		]))
    		.pipe(concat('main.js'))
    		.pipe(babel({
    			presets: ['@babel/env']
    		}))
    		.pipe(pipeIf(argv.production, uglify()))
    		.pipe(pipeIf(argv.development, sourcemaps.write('.')))
    		.pipe(gulp.dest('Build/Scripts/'));
    });
    
    gulp.task('build:images', () => {
    	return gulp.src(['Sources/Images/**/*.png', 'Sources/Images/**/*.jpg', 'Sources/Images/**/*.svg'])
    		.pipe(pipeIf(argv.production, image()))
    		.pipe(gulp.dest('Build/Images/'));
    });
    
    gulp.task('watch:html', () => {
    	gulp.watch(['Sources/**/*.html', 'Sources/**/*.njk'], gulp.series('build:html', 'reload'));
    });
    
    gulp.task('watch:sass', () => {
    	gulp.watch(['Sources/Styles/**/*.sass', 'Sources/Styles/**/*.scss'], gulp.series('build:sass', 'reload'));
    });
    
    gulp.task('watch:js', () => {
    	gulp.watch('Sources/Scripts/**/*.js', gulp.series('build:js', 'reload'));
    });
    
    gulp.task('watch:images', () => {
    	gulp.watch(['Sources/Images/**/*.png', 'Sources/Images/**/*.jpg', 'Sources/Images/**/*.svg'], gulp.series('build:images'));
    });
    
    gulp.task('build', gulp.parallel('build:html', 'build:sass', 'build:js', 'build:images'));
    
    gulp.task('watch', gulp.parallel('watch:html', 'watch:sass', 'watch:js', 'watch:images'));
    
    gulp.task('default', gulp.series('clear', 'build', 'server', 'watch'));

    Ответ написан
    2 комментария