CodeAbode/gulpfile.js

102 lines
2.3 KiB
JavaScript

// based on https://gist.github.com/torgeir/8507130
// Load plugins
let gulp = require("gulp"),
autoprefixer = require("gulp-autoprefixer"),
csso = require("gulp-csso"),
uglify = require("gulp-uglify"),
htmlmin = require("gulp-html-minimizer"),
clean = require("gulp-clean"),
purgecss = require("gulp-purgecss"),
connect = require("gulp-connect");
// Styles
gulp.task("styles", function () {
return gulp
.src("./css/*.css")
.pipe(
purgecss({
content: ["index.html", "js/*.js"],
})
)
.pipe(autoprefixer())
.pipe(csso())
.pipe(gulp.dest("./dist/css"))
.pipe(connect.reload());
});
// Scripts
gulp.task("scripts", function () {
return gulp
.src("./js/*.js")
.pipe(uglify())
.pipe(gulp.dest("./dist/js"))
.pipe(connect.reload());
});
// Images
gulp.task("images", function () {
return gulp
.src(["./images/**"], { removeBOM: false })
.pipe(gulp.dest("./dist/images"))
.pipe(connect.reload());
});
gulp.task("html", function () {
return gulp
.src("./index.html")
.pipe(
htmlmin({
collapseWhitespace: true,
removeComments: true,
minifyCSS: true,
minifyJS: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeOptionalTags: true,
removeStyleLinkTypeAttributes: true,
removeScriptTypeAttributes: true,
})
)
.pipe(gulp.dest("./dist"))
.pipe(connect.reload());
});
// Clean
gulp.task("clean", function () {
return gulp
.src(["dist/css", "dist/js", "dist/images", "dist/index.html"], {
read: false,
allowEmpty: true,
})
.pipe(clean());
});
// Default task
gulp.task(
"default",
gulp.series("clean", async function minifyAll() {
return gulp.parallel("styles", "scripts", "images", "html")();
})
);
// Connect server
gulp.task("connect", function () {
// Listen on port 8080
connect.server({
root: "dist",
port: 8080,
livereload: true,
});
});
// Watch
gulp.task("watch", function () {
gulp.watch("./css/*.css", gulp.series("styles"));
gulp.watch("./js/*.js", gulp.series("scripts"));
gulp.watch("./images/**/*", gulp.series("images"));
gulp.watch("./index.html", gulp.series("html"));
});
gulp.task("dev", gulp.series("default", gulp.parallel("connect", "watch")));