-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
327 lines (303 loc) · 7.78 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const {
src,
dest,
series,
watch
} = require('gulp');
const autoprefixer = require('gulp-autoprefixer');
const cleanCSS = require('gulp-clean-css');
const del = require('del');
const browserSync = require('browser-sync').create();
const sass = require('sass');
const gulpSass = require('gulp-sass');
const svgSprite = require('gulp-svg-sprite');
const svgmin = require('gulp-svgmin');
const cheerio = require('gulp-cheerio');
const replace = require('gulp-replace');
const fileInclude = require('gulp-file-include');
const rev = require('gulp-rev');
const revRewrite = require('gulp-rev-rewrite');
const revDel = require('gulp-rev-delete-original');
const htmlmin = require('gulp-htmlmin');
const gulpif = require('gulp-if');
const notify = require('gulp-notify');
const image = require('gulp-imagemin');
const {
readFileSync
} = require('fs');
const typograf = require('gulp-typograf');
const webp = require('gulp-webp');
const mainSass = gulpSass(sass);
const webpackStream = require('webpack-stream');
const plumber = require('gulp-plumber');
const path = require('path');
const zip = require('gulp-zip');
const rootFolder = path.basename(path.resolve());
// paths
const srcFolder = './src';
const buildFolder = './app';
const paths = {
srcSvg: `${srcFolder}/img/svg/**.svg`,
srcImgFolder: `${srcFolder}/img`,
buildImgFolder: `${buildFolder}/img`,
srcScss: `${srcFolder}/scss/**/*.scss`,
buildCssFolder: `${buildFolder}/css`,
srcFullJs: `${srcFolder}/js/**/*.js`,
srcMainJs: `${srcFolder}/js/main.js`,
buildJsFolder: `${buildFolder}/js`,
srcPartialsFolder: `${srcFolder}/partials`,
resourcesFolder: `${srcFolder}/resources`,
};
let isProd = false; // dev by default
const clean = () => {
return del([buildFolder])
}
//svg sprite
const svgSprites = () => {
return src(paths.srcSvg)
.pipe(
svgmin({
js2svg: {
pretty: true,
},
})
)
.pipe(
cheerio({
run: function ($) {
$('[fill]').removeAttr('fill');
$('[stroke]').removeAttr('stroke');
$('[style]').removeAttr('style');
},
parserOptions: {
xmlMode: true
},
})
)
.pipe(replace('>', '>'))
.pipe(svgSprite({
mode: {
stack: {
sprite: "../sprite.svg"
}
},
}))
.pipe(dest(paths.buildImgFolder));
}
// scss styles
const styles = () => {
return src(paths.srcScss, { sourcemaps: !isProd })
.pipe(plumber(
notify.onError({
title: "SCSS",
message: "Error: <%= error.message %>"
})
))
.pipe(mainSass())
.pipe(autoprefixer({
cascade: false,
grid: true,
overrideBrowserslist: ["last 5 versions"]
}))
.pipe(gulpif(isProd, cleanCSS({
level: 2
})))
.pipe(dest(paths.buildCssFolder, { sourcemaps: '.' }))
.pipe(browserSync.stream());
};
// styles backend
const stylesBackend = () => {
return src(paths.srcScss)
.pipe(plumber(
notify.onError({
title: "SCSS",
message: "Error: <%= error.message %>"
})
))
.pipe(mainSass())
.pipe(autoprefixer({
cascade: false,
grid: true,
overrideBrowserslist: ["last 5 versions"]
}))
.pipe(dest(paths.buildCssFolder))
.pipe(browserSync.stream());
};
// scripts
const scripts = () => {
return src(paths.srcMainJs)
.pipe(plumber(
notify.onError({
title: "JS",
message: "Error: <%= error.message %>"
})
))
.pipe(webpackStream({
mode: isProd ? 'production' : 'development',
output: {
filename: 'main.js',
},
module: {
rules: [{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', {
targets: "defaults"
}]
]
}
}
}]
},
devtool: !isProd ? 'source-map' : false
}))
.on('error', function (err) {
console.error('WEBPACK ERROR', err);
this.emit('end');
})
.pipe(dest(paths.buildJsFolder))
.pipe(browserSync.stream());
}
// scripts backend
const scriptsBackend = () => {
return src(paths.srcMainJs)
.pipe(plumber(
notify.onError({
title: "JS",
message: "Error: <%= error.message %>"
})
))
.pipe(webpackStream({
mode: 'development',
output: {
filename: 'main.js',
},
module: {
rules: [{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', {
targets: "defaults"
}]
]
}
}
}]
},
devtool: false
}))
.on('error', function (err) {
console.error('WEBPACK ERROR', err);
this.emit('end');
})
.pipe(dest(paths.buildJsFolder))
.pipe(browserSync.stream());
}
const resources = () => {
return src(`${paths.resourcesFolder}/**`)
.pipe(dest(buildFolder))
}
const images = () => {
return src([`${paths.srcImgFolder}/**/**.{jpg,jpeg,png,svg}`])
.pipe(gulpif(isProd, image([
image.mozjpeg({
quality: 80,
progressive: true
}),
image.optipng({
optimizationLevel: 2
}),
])))
.pipe(dest(paths.buildImgFolder))
};
const webpImages = () => {
return src([`${paths.srcImgFolder}/**/**.{jpg,jpeg,png}`])
.pipe(webp())
.pipe(dest(paths.buildImgFolder))
};
const htmlInclude = () => {
return src([`${srcFolder}/*.html`])
.pipe(fileInclude({
prefix: '@',
basepath: '@file'
}))
.pipe(typograf({
locale: ['ru', 'en-US']
}))
.pipe(dest(buildFolder))
.pipe(browserSync.stream());
}
const watchFiles = () => {
browserSync.init({
server: {
baseDir: `${buildFolder}`
},
});
watch(paths.srcScss, styles);
watch(paths.srcFullJs, scripts);
watch(`${paths.srcPartialsFolder}/*.html`, htmlInclude);
watch(`${srcFolder}/*.html`, htmlInclude);
watch(`${paths.resourcesFolder}/**`, resources);
watch(`${paths.srcImgFolder}/**/**.{jpg,jpeg,png,svg}`, images);
watch(`${paths.srcImgFolder}/**/**.{jpg,jpeg,png}`, webpImages);
watch(paths.srcSvg, svgSprites);
}
const cache = () => {
return src(`${buildFolder}/**/*.{css,js,svg,png,jpg,jpeg,webp,woff2}`, {
base: buildFolder
})
.pipe(rev())
.pipe(revDel())
.pipe(dest(buildFolder))
.pipe(rev.manifest('rev.json'))
.pipe(dest(buildFolder));
};
const rewrite = () => {
const manifest = readFileSync('app/rev.json');
src(`${paths.buildCssFolder}/*.css`)
.pipe(revRewrite({
manifest
}))
.pipe(dest(paths.buildCssFolder));
return src(`${buildFolder}/**/*.html`)
.pipe(revRewrite({
manifest
}))
.pipe(dest(buildFolder));
}
const htmlMinify = () => {
return src(`${buildFolder}/**/*.html`)
.pipe(htmlmin({
collapseWhitespace: true
}))
.pipe(dest(buildFolder));
}
const zipFiles = (done) => {
del.sync([`${buildFolder}/*.zip`]);
return src(`${buildFolder}/**/*.*`, {})
.pipe(plumber(
notify.onError({
title: "ZIP",
message: "Error: <%= error.message %>"
})
))
.pipe(zip(`${rootFolder}.zip`))
.pipe(dest(buildFolder));
}
const toProd = (done) => {
isProd = true;
done();
};
exports.default = series(clean, htmlInclude, scripts, styles, resources, images, webpImages, svgSprites, watchFiles);
exports.backend = series(clean, htmlInclude, scriptsBackend, stylesBackend, resources, images, webpImages, svgSprites)
exports.build = series(toProd, clean, htmlInclude, scripts, styles, resources, images, webpImages, svgSprites, htmlMinify);
exports.cache = series(cache, rewrite);
exports.zip = zipFiles;