const gulp = require('gulp');
const sass = require('gulp-sass');
const plumber = require('gulp-plumber');
const autoprefixer = require('gulp-autoprefixer');
const browserSync = require('browser-sync').create();
const sourceMaps = require('gulp-sourcemaps');
const imagemin = require("gulp-imagemin");
const imageminJpegRecompress = require('imagemin-jpeg-recompress');
const pngquant = require('imagemin-pngquant');
const run = require("run-sequence");
const del = require("del");
const svgSprite = require('gulp-svg-sprite');
const svgmin = require('gulp-svgmin');
const cheerio = require('gulp-cheerio');
const replace = require('gulp-replace');
gulp.task('sass', function(){
return gulp.src("app/scss/style.scss/")
.pipe(plumber())
.pipe(sourceMaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 version']
}))
.pipe(sourceMaps.write())
.pipe(gulp.dest("dist/css"))
.pipe(browserSync.reload({stream: true}));
});
gulp.task("html", function(){
return gulp.src("app/*.html")
.pipe(gulp.dest("dist"))
.pipe(browserSync.reload({stream: true}));
});
gulp.task("js", function(){
return gulp.src("app/js/**/*.js")
.pipe(gulp.dest("dist/js"))
.pipe(browserSync.reload({stream: true}));
});
gulp.task("css", function(){
return gulp.src("app/css/**/*.css")
.pipe(gulp.dest("dist/css"))
.pipe(browserSync.reload({stream: true}));
});
gulp.task("allimg", function(){
return gulp.src("app/img/**/*.{png, jpg}")
.pipe(gulp.dest("dist/img"))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('images', function(){
return gulp.src("dist/img/**/*.{png, jpg}")
.pipe(imagemin([
imagemin.jpegtran({progressive: true}),
imageminJpegRecompress({
loops: 5,
min: 65,
max: 70,
quality: 'medium'
}),
imagemin.optipng({optimizationLevel: 3}),
pngquant({quanty: "65-70", speed: 5})
]))
.pipe(gulp.dest("dist/img"));
});
gulp.task('svg', function(){
return gulp.src('app/img/**/*.svg')
.pipe(svgmin({
js2svg: {
pretty: true
}
}))
.pipe(cheerio({
run: function ($) {
$('[fill]').removeAttr('fill');
$('[stroke]').removeAttr('stroke');
$('[style]').removeAttr('style');
},
parserOptions: {xmlMode: true}
}))
.pipe(replace('>', '>'))
.pipe(svgSprite({
mode: {
symbol: {
sprite: "sprite.svg"
}
}
}))
.pipe(gulp.dest('dist/img'));
});
gulp.task("serve", function(){
browserSync.init({
server: "dist"
});
gulp.watch("app/scss/**/*.scss", ["sass"]);
gulp.watch("app/*.html", ["html"]);
gulp.watch("app/js/**/*.js", ["js"]);
gulp.watch("app/css/**/*.css", ["css"]);
gulp.watch("app/img/**/*.{png, jpg}", ["allimg"]);
gulp.watch("app/img/**/*.{svg}", ["svg"]);
});
gulp.task("copy", function(){
return gulp.src([
'app/img/**',
'app/js/**',
'app/css/**',
'app/*.html'
],{
base: 'app'
})
.pipe(gulp.dest("dist"));
});
gulp.task('clean', function(){
return del('dist');
});
gulp.task('dist', function(fn){
run(
'clean',
'copy',
'sass',
'images',
'svg',
fn
);
});