var
gulp = require("gulp"),
del = require("del"),
webp = require("gulp-webp"),
minify = require("gulp-csso"),
sass = require("gulp-sass"),
rename = require("gulp-rename"),
plumber = require("gulp-plumber"),
postcss = require("gulp-postcss"),
csscomb = require("gulp-csscomb"),
autoprefixer = require("autoprefixer"),
posthtml = require("gulp-posthtml"),
imagemin = require("gulp-imagemin"),
svgstore = require("gulp-svgstore"),
server = require("browser-sync").create();
gulp.task("style", function() {
gulp.src("src/sass/style.scss")
.pipe(plumber())
.pipe(sass().on("error", sass.logError))
.pipe(postcss([autoprefixer()]))
.pipe(csscomb())
.pipe(gulp.dest("build/css"))
.pipe(minify())
.pipe(rename("style.min.css"))
.pipe(gulp.dest("build/css"))
.pipe(server.stream());
});
gulp.task("sprite", function() {
return gulp.src("src/img/svg/icon-*.svg")
.pipe(svgstore({
inlineSvg: true
}))
.pipe(rename("sprite.svg"))
.pipe(gulp.dest("build/img"));
});
gulp.task("html", function() {
return gulp.src("src/*.html")
.pipe(posthtml([
include()
]))
.pipe(gulp.dest("build"));
});
gulp.task("images", function() {
return gulp.src("src/img/**/*.{png,jpg,svg}")
.pipe(imagemin([imagemin.optipng({ optimizationLevel: 3 }),
imagemin.jpegtran({ progressive: true }),
imagemin.svgo()
]))
.pipe(gulp.dest("src/img"))
});
gulp.task("webp", function() {
return gulp.src("src/img/**/*.{png,jpg}")
.pipe(webp({ quality: 90 }))
.pipe(gulp.dest("src/img"))
});
gulp.task("clean", function() {
return del("build");
});
gulp.task("copy", function() {
return gulp.src([
"src/fonts/**/*.{woff,woff2}",
"src/img/**",
"src/js/**"
], {
base: "src"
})
.pipe(gulp.dest("build"));
});
gulp.task("serve", function() {
server.init({
server: "build/"
});
gulp.watch("src/sass/**/*.scss", ["style"]);
gulp.watch("src/*.html", ["html"]);
gulp.watch("src/js/*.js").on("change", server.reload);
});
gulp.task("build", function(done) {
run(
"clean",
"copy",
"style",
"sprite",
"html",
done
);
});