-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
256 lines (231 loc) · 5.7 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* eslint-disable import/no-unresolved */
/* eslint-disable import/no-extraneous-dependencies */
import gulp from 'gulp';
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
import stylelint from '@ronilaukkarinen/gulp-stylelint';
import pug from 'gulp-pug';
import pugLinter from 'gulp-pug-linter';
import htmlMin from 'gulp-html-minifier-terser';
import imageMin from 'gulp-imagemin';
import svgMin from 'gulp-svgmin';
import webp from 'gulp-webp';
import svgSprite from 'gulp-svg-sprite';
import { deleteAsync } from 'del';
import browserSync from 'browser-sync';
import postcss from 'gulp-postcss';
import csso from 'postcss-csso';
import autoprefixer from 'autoprefixer';
import eslint from 'gulp-eslint-new';
import terser from 'gulp-terser';
import rename from 'gulp-rename';
import sourcemaps from 'gulp-sourcemaps';
const browser = browserSync.create();
const sass = gulpSass(dartSass);
// CSS
function lintSass() {
return gulp.src('src/scss/**/*.scss')
.pipe(stylelint({
configFile: './.stylelintrc.cjs',
failAfterError: true,
reporters: [
{ formatter: 'string', console: true },
],
fix: true,
}));
}
function compileScss() {
return gulp.src('src/scss/**/*.scss', { sourcemaps: true })
.pipe(sass({
outputStyle: 'compressed', // compressed | expanded
}))
.pipe(rename((path) => ({ ...path, extname: '.min.css' })))
.pipe(gulp.dest('build/css/', { sourcemaps: '.' }))
.pipe(browser.stream());
}
function postCSS() {
return gulp.src('build/css/*.css')
.pipe(postcss([
autoprefixer(),
csso(),
]))
.pipe(gulp.dest('build/css'))
.pipe(browser.stream());
}
// HTML
function lintPug() {
return gulp.src('src/pug/**/*.pug')
.pipe(pugLinter({
reporter: 'puglint-stylish',
failAfterError: true,
}));
}
function compilePug() {
return gulp.src('src/pug/pages/*.pug')
.pipe(pug({
pretty: false, // true | false
doctype: 'html',
}))
.pipe(gulp.dest('build/'))
.pipe(browser.stream());
}
function optimizeHTML() {
return gulp.src('build/*.html')
.pipe(htmlMin({ collapseWhitespace: true }))
.pipe(gulp.dest('build'));
}
// Javascript
function lintJS() {
return gulp.src('src/js/**/*.js')
.pipe(eslint({
fix: true,
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
}
function optimizeJS() {
return gulp.src('src/js/**/*.js')
.pipe(sourcemaps.init())
.pipe(terser())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('build/js'))
.pipe(browser.stream());
}
// Images
function optimizeImages() {
return gulp.src(['src/img/**/*.{png,jpg}', 'src/apple-touch-icon.png'], {
since: gulp.lastRun(imageMin),
base: 'src',
})
.pipe(imageMin())
.pipe(gulp.dest('build'))
.pipe(browser.stream());
}
async function optimizeSVG() {
gulp.src([
'src/img/**/*.svg',
'!src/img/icons/*.svg',
])
.pipe(svgMin())
.pipe(gulp.dest('build/img'));
}
function createWebp() {
return gulp.src(['src/img/**/*.{png,jpg}'], {
base: 'src',
})
.pipe(webp({ quality: 90 }))
.pipe(gulp.dest('build'));
}
// SVG
function makeSprite() {
return gulp.src([
'src/img/icons/*.svg',
])
.pipe(svgMin())
.pipe(svgSprite({
mode: {
symbol: {
dest: '',
sprite: 'sprite.svg',
inline: true,
},
},
}))
.pipe(gulp.dest('build/img/icons'));
}
// Copy resources
function copyImages() {
return gulp.src([
'src/img/**/*.{png,jpg,svg}',
'!src/img/icons/*.svg',
])
.pipe(gulp.dest('build/img'));
}
function copyFonts() {
return gulp.src('src/fonts/**/*')
.pipe(gulp.dest('build/fonts/'))
.pipe(browser.stream());
}
function copyMisc() {
return gulp.src([
'src/favicon.ico',
'src/site.webmanifest',
'src/apple-touch-icon.png',
])
.pipe(gulp.dest('build/'))
.pipe(browser.stream());
}
function copyVendor() {
return gulp.src(['src/vendor/**/*.min.{css,js,mjs,mjs.map}'])
.pipe(gulp.dest('build/vendor/'))
.pipe(browser.stream());
}
// Clean
function clean() {
return deleteAsync(['build']);
}
// Server
function browsersync() {
browser.init({
server: {
baseDir: 'build',
},
// cors: true,
notify: false,
open: false,
});
}
function watcher(cb) {
gulp.watch('src/pug/**/*.pug', gulp.series(lintPug, compilePug));
gulp.watch('src/scss/**/*.scss', gulp.series(lintSass, compileScss));
gulp.watch([
'src/img/**/*.{png,jpg,svg}',
'!src/img/icons/*.svg',
'src/apple-touch-icon.png',
], copyImages);
gulp.watch(['src/favicon.ico', 'src/manifest.webmanifest'], copyMisc);
gulp.watch(['src/vendor/**/*.min.{css,js,mjs}'], copyVendor);
gulp.watch('src/fonts/**/*.{woff,woff2}', copyFonts);
gulp.watch('src/js/**/*.js', gulp.series(lintJS, optimizeJS));
cb();
}
// ---------------------------------------------------------------------
// | Tasks |
// ---------------------------------------------------------------------
// Server
export const server = gulp.series(browsersync);
// Build
export const build = gulp.series(
clean,
gulp.parallel(
gulp.series(lintPug, compilePug, optimizeHTML),
gulp.series(lintSass, compileScss, postCSS),
gulp.series(lintJS, optimizeJS),
copyFonts,
copyMisc,
copyVendor,
optimizeImages,
optimizeSVG,
createWebp,
makeSprite,
),
);
// Default
export default gulp.series(
clean,
gulp.parallel(
gulp.series(lintPug, compilePug),
gulp.series(lintSass, compileScss),
gulp.series(lintJS, optimizeJS),
copyImages,
copyFonts,
copyVendor,
copyMisc,
createWebp,
makeSprite,
),
gulp.series(
watcher,
browsersync,
),
);