-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
196 lines (175 loc) · 4.97 KB
/
gulpfile.js
File metadata and controls
196 lines (175 loc) · 4.97 KB
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
import sync from 'browser-sync';
import del from 'del';
import gulp from 'gulp';
import fileInclude from 'gulp-file-include';
import gulpIf from 'gulp-if';
import plumber from 'gulp-plumber';
import removeHtml from 'gulp-remove-html';
import pug from 'gulp-pug';
import htmlMin from 'gulp-htmlmin';
import webpHtml from 'gulp-webp-html';
import fonter from 'gulp-fonter';
import ttf2woff2 from 'gulp-ttf2woff2';
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
import imagemin from 'gulp-imagemin';
import webp from 'gulp-webp';
import webpCss from 'gulp-webpcss';
import postCss from 'gulp-postcss';
import groupMedia from 'gulp-group-css-media-queries';
import pimport from 'postcss-import';
import autoprefixer from 'autoprefixer';
import minmax from 'postcss-media-minmax';
import csso from 'postcss-csso';
import terser from 'gulp-terser';
import babel from 'gulp-babel';
let isBuildFlag = false;
const sass = gulpSass(dartSass);
const projectFolder = 'prod';
const sourceFolder = 'src';
const path = {
build: {
html: projectFolder + '/',
css: projectFolder + '/styles/',
js: projectFolder + '/scripts/',
img: projectFolder + '/images/',
fonts: projectFolder + '/fonts/',
},
src: {
html: sourceFolder + '/*.html',
pug: sourceFolder + '/*.pug',
css: sourceFolder + '/styles/styles.{scss,css}',
js: sourceFolder + '/scripts/script.js',
img: sourceFolder + '/images/**/*.{jpg,jpeg,png,gif,ico,webp,svg}',
fonts: sourceFolder + '/fonts/*.{ttf,eot,otf,ttc,woff,woff2}',
},
watch: {
html: sourceFolder + '/**/*.html',
pug: sourceFolder + '/**/*.pug',
css: sourceFolder + '/styles/**/*.{scss,css}',
js: sourceFolder + '/scripts/**/*.js',
img: sourceFolder + '/images/**/*.{jpg,jpeg,png,gif,ico,webp,svg}',
},
clean: './' + projectFolder + '/'
};
export const browserSync = () => {
sync.init({
ui: false,
notify: false,
browser: 'chrome',
server: {
baseDir: './' + projectFolder + '/'
},
});
};
export const html = () => {
return gulp.src(path.src.pug)
.pipe(plumber())
.pipe(pug({
verbose: true,
}))
.pipe(webpHtml())
.pipe(gulp.dest(path.build.html))
.pipe(gulp.src(path.src.html))
.pipe(plumber())
.pipe(fileInclude())
.pipe(webpHtml())
.pipe(gulpIf(isBuildFlag, removeHtml()))
.pipe(gulpIf(isBuildFlag, htmlMin({
removeComments: true,
collapseWhitespace: true,
})))
.pipe(gulp.dest(path.build.html))
.pipe(sync.stream());
};
export const css = () => {
return gulp.src(path.src.css, { sourcemaps: !isBuildFlag })
.pipe(plumber())
.pipe(sass({
outputStyle: 'expanded'
}))
.pipe(webpCss({
webpClass: '.webp',
noWebpClass: '.no-webp',
}))
.pipe(postCss([
pimport,
autoprefixer,
minmax,
]))
.pipe(groupMedia())
.pipe(gulpIf(isBuildFlag, postCss([
csso,
])))
.pipe(gulp.dest(path.build.css, { sourcemaps: !isBuildFlag }))
.pipe(sync.stream());
};
export const js = () => {
return gulp.src(path.src.js, { sourcemaps: !isBuildFlag })
.pipe(plumber())
.pipe(fileInclude())
.pipe(gulpIf(isBuildFlag, babel({
presets: ['@babel/preset-env']
})))
.pipe(gulpIf(isBuildFlag, terser()))
.pipe(gulp.dest(path.build.js, { sourcemaps: !isBuildFlag }))
.pipe(sync.stream());
};
export const images = () => {
return gulp.src(path.src.img)
.pipe(plumber())
.pipe(webp())
.pipe(gulp.dest(path.build.img))
.pipe(gulp.src(path.src.img))
.pipe(gulpIf(isBuildFlag, imagemin({
progressive: true,
verbose: true,
optimizationLevel: 3
})))
.pipe(gulp.dest(path.build.img));
};
export const fonts = () => {
return gulp.src(path.src.fonts)
.pipe(plumber())
.pipe(fonter({
formats: ['ttf', 'eot', 'woff', 'svg']
}))
.pipe(gulp.dest(path.build.fonts))
.pipe(ttf2woff2())
.pipe(gulp.dest(path.build.fonts));
};
export const clean = () => {
return del(path.clean);
};
const setMode = (isBuild) => {
return cb => {
isBuildFlag = isBuild;
cb();
};
};
export const watchFiles = () => {
gulp.watch([path.watch.html], html);
gulp.watch([path.watch.pug], pug);
gulp.watch([path.watch.css], css);
gulp.watch([path.watch.js], js);
gulp.watch([path.watch.img], images);
};
const watch = gulp.parallel(
watchFiles,
browserSync);
const dev = gulp.parallel(
css,
html,
js,
images);
export const build = gulp.series(
clean,
setMode(true),
dev,
fonts,
browserSync);
export default gulp.series(
clean,
dev,
watch,
);