CodeAbode/gulpfile.js

123 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-06-02 20:32:07 +02:00
// 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());
});
2024-06-02 21:35:24 +02:00
// Fonts
gulp.task("fonts", function () {
return gulp
.src(["./fonts/**"], { removeBOM: false })
.pipe(gulp.dest("./dist/fonts"))
.pipe(connect.reload());
});
2024-06-03 23:45:58 +02:00
// Other useless content
gulp.task("other", function () {
return gulp
.src(["./other/**"], { removeBOM: false })
.pipe(gulp.dest("./dist/other"))
.pipe(connect.reload());
});
2024-06-02 20:32:07 +02:00
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
2024-06-02 21:35:24 +02:00
.src(
["dist/css", "dist/js", "dist/images", "dist/index.html", "dist/fonts"],
{
read: false,
allowEmpty: true,
}
)
2024-06-02 20:32:07 +02:00
.pipe(clean());
});
// Default task
gulp.task(
"default",
gulp.series("clean", async function minifyAll() {
2024-06-02 21:35:24 +02:00
return gulp.parallel("styles", "scripts", "images", "html", "fonts")();
2024-06-02 20:32:07 +02:00
})
);
// 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"));
2024-06-02 21:35:24 +02:00
gulp.watch("./fonts/**/*", gulp.series("fonts"));
2024-06-02 20:32:07 +02:00
});
gulp.task("dev", gulp.series("default", gulp.parallel("connect", "watch")));