-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
120 lines (106 loc) · 3.27 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
import bemValidator from 'gulp-html-bem-validator';
import browsersync from 'browser-sync';
import clean from 'gulp-clean';
import del from 'del';
import gulp from 'gulp';
import imagemin from 'gulp-imagemin';
import mozjpeg from 'imagemin-mozjpeg';
import pngquant from 'imagemin-pngquant';
import postcss from 'gulp-postcss';
import posthtml from 'gulp-posthtml';
import rename from 'gulp-rename';
import stackSprite from 'gulp-svg-sprite';
import svgo from 'imagemin-svgo';
import svgoConfig from './svgo.config.js';
import vinylNamed from 'vinyl-named';
import webp from 'gulp-webp';
import webpack from 'webpack';
import webpackConfig from './webpack.config.js';
import webpackStream from 'webpack-stream';
const { src, dest, watch, series, parallel } = gulp;
const server = browsersync.create();
const IS_DEV = process.env.NODE_ENV === 'development';
const Path = {
Build: {
CSS: ['source/styles/*.css'],
JS: ['source/scripts/*.js']
},
DIST: 'public',
Watch: {
CSS: 'source/**/*.css',
HTML: 'source/**/*.njk',
ICONS: 'place/icons/**/*.{svg,png}',
IMG: 'place/images/**/*.{svg,png,jpg}',
IMG_DEST: 'public/images/**/*.{png,jpg}',
JS: ['*.{js,cjs}', 'source/**/*.{js,cjs}'],
SPRITE: 'source/icons/*.svg'
}
};
if (!IS_DEV) {
Path.Build.JS.push('!source/scripts/dev.js');
}
const buildHTML = () => src('source/layouts/pages/**/*.njk')
.pipe(posthtml())
.pipe(bemValidator())
.pipe(rename({ extname: '.html' }))
.pipe(dest(Path.DIST));
const buildCSS = () => src(Path.Build.CSS)
.pipe(postcss())
.pipe(rename({ suffix: '.min' }))
.pipe(dest(`${Path.DIST}/styles`));
const buildJS = () => src(Path.Build.JS)
.pipe(vinylNamed())
.pipe(webpackStream(webpackConfig, webpack))
.pipe(dest(`${Path.DIST}/scripts`));
const saveImages = () => src(Path.Watch.IMG)
.pipe(imagemin([
svgo(svgoConfig),
mozjpeg({
progressive: true,
quality: 75
}),
pngquant()
]))
.pipe(clean())
.pipe(dest(`${Path.DIST}/images`));
const createWebp = () => src(Path.Watch.IMG_DEST)
.pipe(webp({ quality: 80 }))
.pipe(dest(`${Path.DIST}/images`));
const optimizeIcons = () => src(Path.Watch.ICONS)
.pipe(imagemin([
svgo(svgoConfig),
pngquant()
]))
.pipe(clean())
.pipe(dest('source/icons'));
const buildSprite = () => src(Path.Watch.SPRITE)
.pipe(stackSprite({ mode: { stack: true } }))
.pipe(rename('sprite.min.svg'))
.pipe(dest(`${Path.DIST}/images`));
const reload = (done) => {
server.reload();
done();
};
const startServer = () => {
server.init({
cors: true,
open: false,
server: Path.DIST,
ui: false
});
watch([Path.Watch.HTML, 'source/layouts/**/*.js'], series(buildHTML, reload));
watch('source/{components,scripts,lib}/**/*.{js,cjs}', series(buildJS, reload));
watch(Path.Watch.CSS, series(buildCSS, reload));
watch(Path.Watch.IMG, saveImages);
watch(Path.Watch.IMG_DEST, series(createWebp, reload));
watch(Path.Watch.ICONS, series(optimizeIcons, buildCSS, reload));
watch(Path.Watch.SPRITE, series(buildSprite, reload));
};
const cleanDest = () => del([
`${Path.DIST}/**/*.{html,css,js,webp}`,
`${Path.DIST}/images/sprite.min.svg`
]);
const prepare = parallel(cleanDest, saveImages, optimizeIcons);
const compile = parallel(buildHTML, buildCSS, buildJS, createWebp, buildSprite);
export const build = series(prepare, compile);
export default series(build, startServer);