-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
175 lines (154 loc) · 4.63 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const gulp = require("gulp");
const plumber = require("gulp-plumber");
const sourcemap = require("gulp-sourcemaps");
const sass = require("gulp-sass");
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
const server = require("browser-sync").create();
const csso = require("gulp-csso");
const rename = require("gulp-rename");
const imagemin = require("gulp-imagemin");
const webp = require("gulp-webp");
const svgstore = require("gulp-svgstore");
const del = require("del");
const webpackStream = require("webpack-stream");
const webpackConfig = require("./webpack.config.js");
const gcmq = require("gulp-group-css-media-queries");
const css = () => {
return gulp
.src("source/sass/style.scss")
.pipe(plumber())
.pipe(sourcemap.init())
.pipe(sass())
.pipe(
postcss([
autoprefixer({
grid: true,
}),
])
)
.pipe(gcmq()) // выключите, если в проект импортятся шрифты через ссылку на внешний источник
.pipe(gulp.dest("build/css"))
.pipe(csso())
.pipe(rename("style.min.css"))
.pipe(sourcemap.write("."))
.pipe(gulp.dest("build/css"))
.pipe(server.stream());
};
const js = () => {
return gulp
.src(["source/js/main.js"])
.pipe(webpackStream(webpackConfig))
.pipe(gulp.dest("build/js"));
};
const jsBuild = () => {
return gulp.src(["source/js/main.js"]).pipe(gulp.dest("build/js"));
};
const svgo = () => {
return gulp
.src("source/img/**/*.{svg}")
.pipe(
imagemin([
imagemin.svgo({
plugins: [
{ removeViewBox: false },
{ removeRasterImages: true },
{ removeUselessStrokeAndFill: false },
],
}),
])
)
.pipe(gulp.dest("source/img"));
};
const sprite = () => {
return gulp
.src("source/img/sprite/*.svg")
.pipe(svgstore({ inlineSvg: true }))
.pipe(rename("sprite_auto.svg"))
.pipe(gulp.dest("build/img"));
};
const copySvg = () => {
return gulp
.src("source/img/**/*.svg", { base: "source" })
.pipe(gulp.dest("build"));
};
const copyImages = () => {
return gulp
.src("source/img/**/*.{png,jpg,webp}", { base: "source" })
.pipe(gulp.dest("build"));
};
const copy = () => {
return gulp
.src(
[
"source/**.html",
"source/fonts/**",
"source/img/**",
"source/favicon/**",
],
{
base: "source",
}
)
.pipe(gulp.dest("build"));
};
const clean = () => {
return del("build");
};
const syncServer = () => {
server.init({
server: "build/",
index: "index.html",
notify: false,
open: true,
cors: true,
ui: false,
});
gulp.watch("source/**.html", gulp.series(copy, refresh));
gulp.watch("source/sass/**/*.{scss,sass}", gulp.series(css));
gulp.watch("source/js/**/*.{js,json}", gulp.series(js, refresh));
gulp.watch("source/data/**/*.{js,json}", gulp.series(copy, refresh));
gulp.watch("source/img/**/*.svg", gulp.series(copySvg, sprite, refresh));
gulp.watch(
"source/img/**/*.{png,jpg,webp}",
gulp.series(copyImages, refresh)
);
gulp.watch("source/favicon/**", gulp.series(copy, refresh));
gulp.watch("source/video/**", gulp.series(copy, refresh));
gulp.watch("source/downloads/**", gulp.series(copy, refresh));
gulp.watch("source/*.php", gulp.series(copy, refresh));
};
const refresh = (done) => {
server.reload();
done();
};
const build = gulp.series(clean, svgo, copy, css, sprite, js, jsBuild);
const start = gulp.series(build, syncServer);
// Optional tasks
//---------------------------------
// Используйте отличное от дефолтного значение root, если нужно обработать отдельную папку в img,
// а не все изображения в img во всех папках.
// root = '' - по дефолту webp добавляются и обналяются во всех папках в source/img/
// root = 'content/' - webp добавляются и обновляются только в source/img/content/
const createWebp = () => {
const root = "";
return gulp
.src(`source/img/${root}**/*.{png,jpg}`)
.pipe(webp({ quality: 90 }))
.pipe(gulp.dest(`source/img/${root}`));
};
const optimizeImages = () => {
return gulp
.src("build/img/**/*.{png,jpg}")
.pipe(
imagemin([
imagemin.optipng({ optimizationLevel: 3 }),
imagemin.mozjpeg({ quality: 75, progressive: true }),
])
)
.pipe(gulp.dest("build/img"));
};
exports.imagemin = optimizeImages;
exports.webp = createWebp;
exports.start = start;
exports.build = build;