-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
148 lines (122 loc) · 3.58 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import path from "path";
import gulp from "gulp";
import rename from "gulp-rename";
import clean from "gulp-clean";
import concat from "gulp-concat";
import minify from "gulp-minify";
import include from "gulp-include";
import typograf from "gulp-typograf";
import htmlmin from "gulp-htmlmin";
import importCss from "gulp-import-css";
import imagemin from "gulp-imagemin";
import imageResize from "gulp-image-resize";
import webp from "gulp-webp";
import avif from "gulp-avif";
import webserver from "gulp-webserver";
function html() {
const nonBreakingHyphen = "‑";
const typografRules = [
{
name: "common/other/nonBreakingHyphen",
handler: (text) => text.replace(/\-/g, nonBreakingHyphen),
},
{
name: "common/other/typographicalEmoticon",
handler: (text) =>
text
.replace(/\:\ \–\)/g, ":–)")
.replace(/\:\ \–\(/g, ":–(")
.replace(/\;\ \–\)/g, ";–)"),
},
];
return gulp
.src("./src/*.html")
.pipe(include())
.on("error", console.log)
.pipe(
typograf({
locale: ["ru", "en-US"],
enableRule: ["ru/optalign/*"],
disableRule: ["ru/nbsp/afterNumberSign"],
rules: typografRules,
})
)
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest("./build/"));
}
function css() {
return gulp
.src("./src/css/style.css")
.pipe(importCss())
.pipe(gulp.dest("./build/css/"));
}
function js() {
const internal = ["./src/js/**/*.js"];
const external = ["node_modules/ilyabirman-likely/release/likely.min.js"];
return gulp
.src([...internal, ...external])
.pipe(concat("scripts.js"))
.pipe(minify())
.pipe(gulp.dest("./build/js/"));
}
function resize(done) {
const source = "./src/static/img/resize/**/*.{jpg,png}";
const target = "./src/static/img/tmp/";
const x1 = () =>
gulp
.src(source)
.pipe(imageResize({ width: 320 }))
.pipe(gulp.dest(target));
const x2 = () =>
gulp
.src(source)
.pipe(imageResize({ width: 640 }))
.pipe(rename((path) => (path.basename += "@2x")))
.pipe(gulp.dest(target));
return gulp.parallel(x1, x2)(done);
}
function images(done) {
const minifiable = [
"./src/static/img/*.{jpg,png,svg}",
"./src/static/img/tmp/*.{jpg,png}",
];
const convertible = [
"./src/static/img/*.{jpg,png}",
"./src/static/img/tmp/*.{jpg,png}",
];
const target = "./build/img/";
const minify = () =>
gulp.src(minifiable).pipe(imagemin()).pipe(gulp.dest(target));
const toWebp = () =>
gulp.src(convertible).pipe(webp()).pipe(gulp.dest(target));
const toAvif = () =>
gulp.src(convertible).pipe(avif()).pipe(gulp.dest(target));
return gulp.series(resize, gulp.parallel(minify, toWebp, toAvif))(done);
}
function meta(done) {
const txt = () => gulp.src("./src/static/*.txt").pipe(gulp.dest("./build/"));
const icons = () =>
gulp.src("./src/static/meta/*").pipe(gulp.dest("./build/meta/"));
return gulp.parallel(icons, txt)(done);
}
function cleanup() {
return gulp
.src("./src/static/img/tmp", { read: false, allowEmpty: true })
.pipe(clean());
}
function server() {
return gulp.src("./build/").pipe(
webserver({
livereload: { enable: true },
open: "http://localhost:8001/",
port: 8001,
})
);
}
gulp.task("watch", function () {
gulp.watch("./src/**/*.html", html);
gulp.watch("./src/css/*.css", css);
gulp.watch("./src/js/**/*.js", js);
});
gulp.task("default", gulp.series(html, css, js, images, server, "watch"));
gulp.task("build", gulp.series(html, css, js, images, meta, cleanup));