G
G
genrich_pauls2020-12-14 22:10:14
gulp.js
genrich_pauls, 2020-12-14 22:10:14

How to change the syntax of gulpfile.js galp 3 to galp 4?

const gulp = require('gulp');
const html = require('gulp-html');
const webpackStream = require('webpack-stream');
const gulpAutoprefix = require('gulp-autoprefixer');
const gulpSass = require('gulp-sass');
const gulpWatch = require('gulp-watch');
const gulpSourceMap = require('gulp-sourcemaps');
const browserSync = require('browser-sync').create();
const gulpStylelint = require('gulp-stylelint');
const image = require('gulp-image');
const historyFallback = require('connect-history-api-fallback');
const webpackConfig = require('./webpack.config.js');

gulp.task('stylesheets', () => gulp.src('./src/scss/**/*.scss')
    .pipe(gulpStylelint({
        failAfterError: false,
        reporters: [
            {
                formatter: 'string',
                console: true,
                fix: true,
            },
        ],
    }))
    .pipe(gulpSourceMap.init())
    .pipe(gulpSass({
        outputStyle: 'compressed',
    })).on('error', gulpSass.logError)
    .pipe(gulpAutoprefix({
        browsers: ['last 2 versions'],
    }))
    .pipe(gulpSourceMap.write())
    .pipe(gulp.dest('./dev/css/'))
    .pipe(browserSync.stream()));

gulp.task('stylesheetsProduction', () => gulp.src('./src/scss/**/*.scss')
    .pipe(gulpStylelint({
        failAfterError: true,
        reporters: [
            {
                formatter: 'string',
                console: true,
                fix: true,
            },
        ],
    }))
    .pipe(gulpSass({
        outputStyle: 'compressed',
    })).on('error', gulpSass.logError)
    .pipe(gulpAutoprefix({
        browsers: ['last 2 versions'],
    }))
    .pipe(gulp.dest('./build/css/')));

gulp.task('js', () => gulp.src('./src/js/main.js')
    .pipe(webpackStream(webpackConfig))
    .pipe(gulp.dest('./dev/js'))
    .pipe(browserSync.stream()));

gulp.task('jsProduction', () => {
    webpackConfig.watch = false;

    gulp.src('./src/js/main.js')
        .pipe(webpackStream(webpackConfig))
        .pipe(gulp.dest('./build/js'));
});

gulp.task('image', () => gulp.src('./src/images/*')
    .pipe(image())
    .pipe(gulp.dest('./dev/images'))
    .pipe(browserSync.stream()));

gulp.task('imageProduction', () => gulp.src('./src/images/*')
    .pipe(image())
    .pipe(gulp.dest('./build/images')));

gulp.task('html', () => gulp.src('./src/template/**/*.html')
    .pipe(html())
    .pipe(gulp.dest('./dev'))
    .pipe(browserSync.stream()));

gulp.task('htmlProduction', () => gulp.src('./src/template/**/*.html')
    .pipe(html())
    .pipe(gulp.dest('./build')));

gulp.task('watch', () => {
    gulpWatch(['./src/scss/**/*.scss'], () => {
        gulp.start('stylesheets');
    });
    gulpWatch(['./src/images/**/*'], () => {
        gulp.start('image');
    });
    gulpWatch(['./src/template/**/*.html'], () => {
        gulp.start('html');
    });
});



gulp.task('server', gulp.series(function () {
    browser.init({
        server: {
            baseDir: './dev',
            middleware: [
                historyFallback(),
            ],
        },
        open: true,
        port: 8080,
    });
}));



gulp.task('server', gulp.series('build', function () {
    browser.init({
        server: {
            baseDir: './dev',
            middleware: [
                historyFallback(),
            ],
        },
        open: true,
        port: 8080,
    });
}));


gulp.task('development', [
    'stylesheets',
    'js',
    'image',
    'html',
    'watch',
    'server',
]);

gulp.task('production', [
    'stylesheetsProduction',
    'jsProduction',
    'imageProduction',
    'htmlProduction',
]);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuriy Vorobyov, 2020-12-14
@YuriyVorobyov1333

See here and here

S
Sergey delphinpro, 2020-12-14
@delphinpro

Something like this
const gulp = require('gulp');
const gulpHtml = require('gulp-html');
const webpackStream = require('webpack-stream');
const gulpAutoprefix = require('gulp-autoprefixer');
const gulpSass = require('gulp-sass');
const gulpWatch = require('gulp-watch');
const gulpSourceMap = require('gulp-sourcemaps');
const browserSync = require('browser-sync').create();
const gulpStylelint = require('gulp-stylelint');
const gulpImage = require('gulp-image');
const historyFallback = require('connect-history-api-fallback');
const webpackConfig = require('./webpack.config.js');

export const stylesheets = () => {
  return gulp.src('./src/scss/**/*.scss')
    .pipe(gulpStylelint({
      failAfterError: false,
      reporters     : [
        {
          formatter: 'string',
          console  : true,
          fix      : true,
        },
      ],
    }))
    .pipe(gulpSourceMap.init())
    .pipe(gulpSass({
      outputStyle: 'compressed',
    })).on('error', gulpSass.logError)
    .pipe(gulpAutoprefix({
      browsers: ['last 2 versions'],
    }))
    .pipe(gulpSourceMap.write())
    .pipe(gulp.dest('./dev/css/'))
    .pipe(browserSync.stream());
};

export const stylesheetsProduction = () => {
  return gulp.src('./src/scss/**/*.scss')
    .pipe(gulpStylelint({
      failAfterError: true,
      reporters     : [
        {
          formatter: 'string',
          console  : true,
          fix      : true,
        },
      ],
    }))
    .pipe(gulpSass({
      outputStyle: 'compressed',
    })).on('error', gulpSass.logError)
    .pipe(gulpAutoprefix({
      browsers: ['last 2 versions'],
    }))
    .pipe(gulp.dest('./build/css/'));
};

export const js = () => {
  return gulp.src('./src/js/main.js')
    .pipe(webpackStream(webpackConfig))
    .pipe(gulp.dest('./dev/js'))
    .pipe(browserSync.stream());
};

export const jsProduction = () => {
  webpackConfig.watch = false;

  gulp.src('./src/js/main.js')
    .pipe(webpackStream(webpackConfig))
    .pipe(gulp.dest('./build/js'));
};

export const image = () => {
  return gulp.src('./src/images/*')
    .pipe(gulpImage())
    .pipe(gulp.dest('./dev/images'))
    .pipe(browserSync.stream());
};

export const imageProduction = () => {
  return gulp.src('./src/images/*')
    .pipe(gulpImage())
    .pipe(gulp.dest('./build/images'));
};

export const html = () => {
  return gulp.src('./src/template/**/*.html')
    .pipe(gulpHtml())
    .pipe(gulp.dest('./dev'))
    .pipe(browserSync.stream());
};

export const htmlProduction = () => {
  return gulp.src('./src/template/**/*.html')
    .pipe(gulpHtml())
    .pipe(gulp.dest('./build'));
};

const watch = () => {
  gulpWatch(['./src/scss/**/*.scss'], () => {
    gulp.start('stylesheets');
  });
  gulpWatch(['./src/images/**/*'], () => {
    gulp.start('image');
  });
  gulpWatch(['./src/template/**/*.html'], () => {
    gulp.start('html');
  });
};

const server = () => {
  return gulp.series(function () {
    browser.init({
      server: {
        baseDir   : './dev',
        middleware: [
          historyFallback(),
        ],
      },
      open  : true,
      port  : 8080,
    });
  });
};

// ХЗ что это такое....
// gulp.task('server', gulp.series('build', function () {
//   browser.init({
//     server: {
//       baseDir   : './dev',
//       middleware: [
//         historyFallback(),
//       ],
//     },
//     open  : true,
//     port  : 8080,
//   });
// }));

export const development = gulp.series(
  gulp.parallel(
    stylesheets,
    js,
    image,
    html,
  ),
  gulp.parallel(
    watch,
    server,
  ),
);

export const production = gulp.parallel(
  stylesheetsProduction,
  jsProduction,
  imageProduction,
  htmlProduction,
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question