-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
421 lines (311 loc) · 12.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* Const variables for required modiles */
const autoprefixer = require('gulp-autoprefixer'),
babel = require('gulp-babel'),
browserSync = require('browser-sync'),
changed = require('gulp-changed'),
cheerio = require('gulp-cheerio'),
cleaner = require('gulp-clean'),
concat = require('gulp-concat'),
cssParser = require('css'),
cssnano = require('gulp-cssnano'),
data = require('gulp-data'),
del = require('del'),
footer = require('gulp-footer'),
fs = require('fs'),
fsExtra = require('fs-extra'),
gulp = require('gulp'),
header = require('gulp-header'),
htmlMin = require('gulp-htmlmin'),
inject = require('gulp-inject'),
merge = require('merge-stream'),
plumber = require('gulp-plumber'),
prettyHtml = require('gulp-pretty-html'),
readYaml = require('read-yaml'),
removeEmptyLines = require('gulp-remove-empty-lines'),
rename = require('gulp-rename'),
replace = require('gulp-replace'),
sass = require('gulp-sass'),
sassGlob = require('gulp-sass-glob'),
sourcemaps = require('gulp-sourcemaps'),
svgmin = require('gulp-svgmin'),
svgSprite = require('gulp-svg-sprite'),
template = require('gulp-template'),
twig = require('gulp-twig'),
uglify = require('gulp-uglify');
/* Variable for config */
var configGlobal = './config-default.yml';
if (fs.existsSync('./config.yml')) {
configGlobal = './config.yml';
}
/* Define all paths */
const paths = {
src: {
__core: 'src/__core/',
assets: 'src/assets/',
components: 'src/components/',
includes: 'src/includes/',
layouts: 'src/layouts/',
pages: 'src/pages/',
},
build: 'dist/',
};
/* BrowserSync Init and Reload */
function browserSyncInit(done) {
browserSync.init({
server: {
baseDir: 'src/',
index: 'src-pages.html',
},
notify: false,
port: 3000,
});
done();
}
function browserSyncReload(done) {
browserSync.reload();
done();
}
/* Separate server run for build. No reloads and other tasks */
function runbuild(done) {
browserSync.init({
server: {
baseDir: paths.build,
index: 'src-pages.html',
},
notify: false,
port: 3000,
});
done();
}
/* Generate layout helpers template */
function layoutHelpers() {
const layoutHelpersTwig = gulp
.src(`${paths.src.__core}__layout-helpers/layout-helpers.twig`)
.pipe(data(() => readYaml.sync(configGlobal)))
.pipe(twig())
.pipe(gulp.dest(`${paths.src.__core}__layout-helpers/`));
const layoutHelpersCss = gulp
.src(`${paths.src.__core}__layout-helpers/layout-helpers.scss`)
.pipe(plumber({ handleError(err) { console.log(err); this.emit('end'); } }))
.pipe(sassGlob())
.pipe(sass({ errLogToConsole: true }))
.pipe(autoprefixer(['last 10 versions', '> 1%', 'IE 11'], { cascade: true }))
.pipe(gulp.dest(`${paths.src.assets}css/`));
return merge(layoutHelpersTwig, layoutHelpersCss);
}
/* Compiling HTML */
/* Delete all generated files */
function htmlClean() {
return gulp
.src([`src/*.html`], { read: false, force: true })
.pipe(cleaner());
}
/* Compile all pages/*.twig independentely on cache */
function htmlCompileAll() {
return gulp
.src([`${paths.src.pages}*.twig`])
.pipe(plumber({ handleError(err) { console.log(err); this.emit('end'); } }))
.pipe(data(() => readYaml.sync(configGlobal)))
.pipe(twig({ base: 'src/' }))
.pipe(gulp.dest('src'));
}
/* Compile all pages/*.twig independentely on cache */
function htmlCompileChanged() {
return gulp
.src([`${paths.src.pages}*.twig`])
.pipe(changed('src', {extension: '.html'}))
.pipe(plumber({ handleError(err) { console.log(err); this.emit('end'); } }))
.pipe(data(() => readYaml.sync(configGlobal)))
.pipe(twig({ base: 'src/' }))
.pipe(gulp.dest('src'));
}
/* Put links for all generated html */
function htmlLinks() {
return gulp
.src(`${paths.src.__core}src-pages.html`)
.pipe(plumber({ handleError(err) { console.log(err); this.emit('end'); } }))
.pipe(inject(
gulp.src(['src/*.html', '!src/src-pages.html'], { read: false }), {
transform(filepath) {
filepath = filepath.split('/src/').join('');
if (filepath.slice(-5) === '.html') {
return `<li><a href="${filepath}">${filepath}</a></li>`;
}
return inject.transform.apply(inject.transform);
},
},
))
.pipe(prettyHtml({ indent_size: 4, end_with_newline: true }))
.pipe(gulp.dest('src'));
}
/* Initial compiling on watch: clear src/*.html, compile all templates, insert links into src-pages.html */
const html = gulp.series(htmlClean, htmlCompileAll, htmlLinks);
/* On any change in {components|includes|layouts} recompile all templates, but don't update links — pages/.*twig still same */
const htmlInserts = gulp.series(htmlCompileAll);
/* On any change of pages/*.twig, recompilem them smart using gulp-changed */
const htmlPages = gulp.series(htmlCompileChanged, htmlLinks);
/* Compiling CSS on 'watch' or 'build' into src/assets/css/** */
function cssCore() {
return gulp
//.src([`${paths.src.__core}__core-scss/*.scss`, `!${paths.src.__core}__core-scss/custom.scss`])
.src([`${paths.src.assets}scss/style.scss`])
.pipe(plumber({ handleError(err) { console.log(err); this.emit('end'); } }))
.pipe(sassGlob())
.pipe(sass({ errLogToConsole: true, noCache: true }))
.pipe(autoprefixer(['last 10 versions', '> 1%', 'IE 11'], { cascade: true }))
//.pipe(cssnano({ autoprefixer: false, zindex: false, reduceIdents: false, colormin: false, discardUnused: false }))
.pipe(gulp.dest(`${paths.src.assets}css/`));
}
/* Compiling JS */
/* Compiling core scripts once before 'watch' or 'build' into src/assets/js/** */
function jsCore() {
return gulp
.src([`${paths.src.__core}__core-js/*.js`])
.pipe(babel())
.pipe(gulp.dest(`${paths.src.assets}js/`));
}
/* Compiling custom scripts ON 'watch' or 'build' into src/assets/js/** */
function jsCustom() {
/* Compiling components.min.js from separate js-files */
const jsComponentsBundle = gulp
.src([`${paths.src.components}**/*.js`, `!${paths.src.components}**/%*.js`])
.pipe(sourcemaps.init({largeFile: true}))
.pipe(concat('components.js'))
.pipe(header('window.addEventListener(\'DOMContentLoaded\', function() {'))
.pipe(footer('});'))
.pipe(babel())
.pipe(sourcemaps.write('/maps'))
.pipe(gulp.dest(`${paths.src.assets}js/`));
/* Get separate js-files, marked with % */
const jsComponentsSeparate = gulp
.src([`${paths.src.components}**/%*.js`])
.pipe(rename(function(path) {
path.dirname = 'components/';
path.basename = path.basename.replace('%', '');
path.basename += ".min";
}))
.pipe(babel())
.pipe(gulp.dest(`${paths.src.assets}js/`));
return merge(jsComponentsBundle, jsComponentsSeparate);
}
/* Creating SVG-sprite from all .svg files in src/assets/img/icons/src */
function svgIconsSprite() {
return gulp
.src([`${paths.src.assets}img/icons/src/*.svg`])
.pipe(plumber({ handleError(err) { console.log(err); this.emit('end'); } }))
.pipe(svgmin({ js2svg: { pretty: true } }))
.pipe(cheerio({
run($) {
$('[fill]').removeAttr('fill');
$('[stroke]').removeAttr('stroke');
$('[style]').removeAttr('style');
$('style').remove();
},
parserOptions: { xmlMode: true },
}))
.pipe(replace('>', '>'))
.pipe(svgSprite({ mode: { symbol: { sprite: '../sprite.svg' } } }))
.pipe(gulp.dest([`${paths.src.assets}img/icons/`]));
}
/* Clean build folder before build task */
function clean() {
return del(`${paths.build}**/*.*`);
}
/* Tasks for build */
function buildHtml() {
/* Update css and js import right into src/*.html */
return gulp
.src(['src/*.html'])
.pipe(htmlMin({ collapseWhitespace: true }))
.pipe(replace(
/<!-- inject:css -->(.*)(.css">)<!-- endinject -->/g,
'<link rel="stylesheet" href="assets/css/libs.min.css">\r\n<link rel="stylesheet" href="assets/css/style.min.css">',
))
.pipe(replace(
/<!-- inject:js -->(.*)<!-- endinject -->/g,
'<script src="assets/js/libs.min.js"></script>\r\n<script src="assets/js/common.min.js"></script>\r\n<script src="assets/js/components.min.js"></script>',
))
.pipe(prettyHtml({ indent_size: 4, end_with_newline: true }))
.pipe(gulp.dest(paths.build));
}
function buildCss() {
/* Compile all css libs into one */
const buildCssLibs = gulp
.src([`${paths.src.assets}css/libs/**/*.css`])
.pipe(concat('libs.min.css'))
.pipe(cssnano({ autoprefixer: false, zindex: false, reduceIdents: false, discardUnused: false }))
.pipe(gulp.dest(`${paths.build}assets/css/`));
/* Build color-vars.min.css and common.min.css into one */
const buildCssCommon = gulp
//.src([`${paths.src.assets}css/common.css`, `${paths.src.assets}css/custom.css`])
.src(`${paths.src.assets}css/style.css`)
//.pipe(concat('common.min.css'))
.pipe(rename({ suffix: '.min' }))
.pipe(cssnano({ autoprefixer: false, zindex: false, reduceIdents: false, discardUnused: false }))
.pipe(gulp.dest(`${paths.build}assets/css/`));
/* Move separate css files but layout-helpers */
const buildCssSeparate = gulp
//.src([`${paths.src.assets}css/**/*.css`, `!${paths.src.assets}css/libs/*.css`, `!${paths.src.assets}css/common.css`, `!${paths.src.assets}css/custom.css`, `!${paths.src.assets}css/layout-helpers.css`])
.src([`${paths.src.assets}css/**/*.css`, `!${paths.src.assets}css/libs/*.css`, `!${paths.src.assets}css/style.css`, `!${paths.src.assets}css/layout-helpers.css`])
.pipe(rename({ suffix: '.min' }))
.pipe(cssnano({ autoprefixer: false, zindex: false, reduceIdents: false, discardUnused: false }))
.pipe(gulp.dest(`${paths.build}assets/css/`));
return merge(buildCssLibs, buildCssCommon, buildCssSeparate);
}
function buildFonts() {
/* Moving fonts */
return gulp
.src([`${paths.src.assets}fonts/**/*.*`])
.pipe(gulp.dest(`${paths.build}assets/fonts/`));
}
function buildImg() {
/* Moving images */
return gulp
.src([`${paths.src.assets}img/**/*.*`])
.pipe(gulp.dest(`${paths.build}assets/img/`));
}
function buildJs() {
/* Compile all js libs into one */
const buildJsLibs = gulp
.src([`${paths.src.__core}__core-js/__libs/*.js`, `${paths.src.assets}js/libs/**/*.js`])
.pipe(concat('libs.min.js'))
.pipe(gulp.dest(`${paths.build}assets/js/`));
/* Compile common.js */
const buildJsCommon = gulp
.src([`${paths.src.__core}__core-js/common.js`, `${paths.src.assets}js/components.js`], { allowEmpty: true })
.pipe(rename({ suffix: '.min' }))
.pipe(babel())
.pipe(uglify())
.pipe(gulp.dest(`${paths.build}assets/js/`));
/* Compile separate components (everything but libs) */
const buildJsComponents = gulp
.src([`${paths.src.assets}js/components/*.js`])
.pipe(babel())
.pipe(uglify())
.pipe(gulp.dest(`${paths.build}assets/js/components`));
return merge(buildJsLibs, buildJsCommon, buildJsComponents);
}
/* Preparing whole src folder before watch or build */
//const srcPrepare = gulp.series(layoutHelpers, cssCore, cssCustom, jsCore, jsCustom, html, svgIconsSprite);
const srcPrepare = gulp.series(layoutHelpers, cssCore, jsCore, jsCustom, html, svgIconsSprite);
/* Watcher */
function watchFiles() {
/* Watch twig inserts to recompile all the pages */
gulp.watch(['src/{components,includes,layouts}/**/*.twig'], gulp.series(htmlInserts, browserSyncReload));
/* Watch pages only to recompile only updated */
gulp.watch(['src/pages/**/*.twig'], gulp.series(htmlPages, browserSyncReload));
/* Watch custom Sass */
gulp.watch(['src/{components,layouts,pages}/**/*.scss', 'src/assets/scss/*.scss'], gulp.series(cssCore, browserSyncReload));
/* Watch custom JS */
gulp.watch([`${paths.src.components}**/*.js`], gulp.series(jsCustom, browserSyncReload));
/* Watch SVG-icons to recompile sprite */
gulp.watch([`${paths.src.assets}img/icons/src/*.svg`], gulp.series(svgIconsSprite, browserSyncReload));
}
/* Watcher and builder series */
const watch = gulp.series(srcPrepare, gulp.parallel(watchFiles, browserSyncInit));
const build = gulp.series(clean, srcPrepare, gulp.parallel(buildHtml, buildCss, buildFonts, buildImg, buildJs));
/* Available tasks from command line */
exports.default = watch;
exports.watch = watch;
exports.build = build;
exports.runbuild = runbuild;