-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
438 lines (381 loc) · 11.1 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
const gulp = require('gulp');
//Styles, scripts and optimisation there of
const sassProcessor = require('gulp-sass')(require('sass'));
const sourcemaps = require('gulp-sourcemaps');
const terser = require('gulp-terser');
const babel = require('gulp-babel');
const concat = require('gulp-concat');
const postcss = require('gulp-postcss');
const postcssCustomProperties = require('postcss-custom-properties');
const postcssclean = require('postcss-clean');
//Images
const responsive = require('gulp-responsive');
//Build tools
const data = require('gulp-data');
const nunjucks = require('nunjucks');
const markdown = require('nunjucks-markdown');
const marked = require('marked');
const gulpnunjucks = require('gulp-nunjucks');
const banner = require('gulp-banner');
const htmlmin = require('gulp-htmlmin');
const dateFilter = require('nunjucks-date-filter');
//System and Utilities
const del = require('del');
const path = require('path');
const plumber = require('gulp-plumber');
const rename = require('gulp-rename');
const bump = require('gulp-bump');
const merge = require('merge-stream');
const mergeJson = require('gulp-merge-json');
const shell = require('gulp-shell');
const exec = require('child_process').exec;
const browserSync = require('browser-sync').create();
const log = require('fancy-log');
const colors = require('ansi-colors');
//Ger package vars
const pkg = require('./package.json');
// Variables
// -----------------
const dir = {
dist: './docs/',
src: './src/',
styles: './assets/styles/',
scripts: './assets/scripts/'
};
// Banner to be injected into production build CSS file
const comment = '/*\n' +
' * Automatically Generated - DO NOT EDIT \n' +
' * Generated on <%= new Date().toISOString().substr(0, 19) %> \n' +
' * <%= pkg.name %> <%= pkg.version %>\n' +
' * <%= pkg.description %>\n' +
' * <%= pkg.homepage %>\n' +
' *\n' +
' * Copyright <%= new Date().getFullYear() %>, <%= pkg.author %>\n' +
' * Released under the <%= pkg.license %> license.\n' +
'*/\n\n';
// Development Tasks
// -----------------
//Nunjucks
// Markdown vars
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader(dir.src));
markdown.register(env, marked);
//Add global context {{ getContext() | dump| safe }}
env.addGlobal('getContext', function() {
return this.ctx;
})
// Get version from package.json
env.addGlobal('pkgVersion', function (str) {
var cbVersion = pkg.version;
return cbVersion;
});
//Add date - {{ creation_date | date("YYYY") }}
env.addFilter('date', dateFilter);
//Nunjucks
gulp.task('nunjucks', () => {
return gulp
.src(path.join(dir.src, '/*.html'))
//Get some data
.pipe(data(function(file) {
//Set up a global variable site - e.g. site.name
//Redfined in layout.njk {% set global = data.site[0] %} - allows for global.name, get the first item in the object e.g. [0]
return { 'data': require('./data/data.json') }
}))
.pipe(gulpnunjucks.compile("", {env: env}))
.pipe(gulp.dest(dir.dist))
});
// Sass: compile sass to css
//===========================================
gulp.task('sass', () => {
return gulp
.src(path.join(dir.styles, '*.scss'))
.pipe(plumber(function(error) {
// Output an error message
log(colors.bold.red('Error (' + error.plugin + '): ' + error.message));
// emit the end event, to properly end the task
this.emit('end');
})
)
.pipe(sourcemaps.init())
.pipe(sassProcessor())
.pipe(sourcemaps.write())
.pipe(gulp.dest(path.join(dir.src, 'css'))) // Outputs it in the css folder
.pipe(browserSync.stream()); // reload
});
// Build the production CSS
gulp.task('sass-build', () => {
return gulp
.src(path.join(dir.styles, '*.scss'))
.pipe(plumber(function(error) {
// Output an error message
log(colors.bold.red('Error (' + error.plugin + '): ' + error.message));
// emit the end event, to properly end the task
this.emit('end');
})
)
.pipe(sassProcessor())
//Polyfill for object fit
.pipe(postcss([require('postcss-object-fit-images')]))
//Polyfill for css vars
.pipe(postcss([postcssCustomProperties()]))
//Minify
.pipe(postcss([postcssclean()]))
.pipe(gulp.dest(path.join(dir.src, 'css')))
});
// Watchers
gulp.task('watch', () => {
gulp.watch(path.join(dir.styles, '**/*.scss'), gulp.series('sass'));
gulp.watch(path.join(dir.scripts, '**/*.js'), gulp.series('scripts'));
gulp.watch(path.join(dir.src, '**/*.+(html|njk|md)'), gulp.series('nunjucks'));
gulp.watch(path.join(dir.dist, '*.+(html|njk|md)')).on('change', browserSync.reload);
});
// Scripts
gulp.task('scripts', () => {
return gulp
.src([
'assets/vendor/photoswipe.min.js', //Galery plugin - https://photoswipe.com/documentation/getting-started.html
'assets/vendor/photoswipe-ui-default.min.js',
'assets/vendor/validate.js', //Validation plugin - https://github.com/cferdinandi/validate
'assets/vendor/validate.polyfills.min.js', //Validation plugin - polyfill
'assets/scripts/scripts.js'
])
.pipe(concat('app.js'))
.pipe(gulp.dest(path.join(dir.dist, 'scripts')))
});
// Scripts
gulp.task('babel', () => {
return gulp.src([
'assets/scripts/scripts.js'
])
.pipe(babel({
presets: ['@babel/env'],
plugins: ['@babel/plugin-transform-template-literals']
}))
.pipe(gulp.dest('assets/scripts/babel/'))
});
// Build the production Scripts
gulp.task('scripts-build', () => {
return gulp
.src([
'assets/vendor/photoswipe.min.js', //Galery plugin - https://photoswipe.com/documentation/getting-started.html
'assets/vendor/photoswipe-ui-default.min.js',
'assets/vendor/validate.js', //Validation plugin - https://github.com/cferdinandi/validate
'assets/vendor/validate.polyfills.min.js', //Validation plugin - polyfill
'assets/scripts/babel/scripts.js'
])
.pipe(concat('app.js'))
.pipe(terser()) //accepts ES6 template literals
.pipe(gulp.dest(path.join(dir.dist, 'scripts')))
});
// Cleaning
gulp.task('clean', () => del([ dir.dist ]) );
// Images
gulp.task('images', () => {
return gulp
.src('assets/images/**/*.+(png|jpg|jpeg|gif|svg|json|ico|json)')
.pipe(gulp.dest(path.join(dir.dist, 'images')))
});
// Favicon
gulp.task('favicon', () => {
return gulp
.src('assets/images/fav/favicon.png')
.pipe(
responsive(
{
// Resize all JPG images to three different sizes: 180, and 512 pixels
'**/*.png': [
{
width: 180,
rename: { suffix: '-180x180' }
},
{
width: 192,
rename: { suffix: '-192x192' }
},
{
width: 512,
rename: { suffix: '-512x512' }
}
]
},
{
// Global configuration for all images
// Use progressive (interlace) scan for JPEG and PNG output
progressive: true,
// Strip all metadata
withMetadata: false
}
)
)
.pipe(gulp.dest('assets/images/fav'))
});
gulp.task('responsive', () => {
return gulp
// assets/images/**/*.jpg
// **/*.jpg
.src('assets/images/blocks/**/*.jpg')
.pipe(
responsive(
{
// Resize all JPG images to three different sizes: 200, 500, and 630 pixels
'**/*.jpg': [
{
width: 640,
rename: { suffix: '-640w' }
},
{
width: 1920,
rename: { suffix: '-1920w' }
},
{
// Compress, strip metadata, and rename original image
rename: { suffix: '-original' }
}
]
},
{
// Global configuration for all images
// The output quality for JPEG, WebP and TIFF output formats
quality: 70,
// Use progressive (interlace) scan for JPEG and PNG output
progressive: true,
// Strip all metadata
withMetadata: false
}
)
)
.pipe(gulp.dest(path.join(dir.dist, 'images/blocks/')))
});
// Copying fonts
gulp.task('fonts', () => {
return gulp.src('assets/fonts/**/*')
.pipe(gulp.dest(path.join(dir.dist, 'fonts')))
});
// Banner
gulp.task('banner', () => {
return gulp
.src(path.join(dir.src, 'css/main.css'))
.pipe(banner(comment, {
pkg: pkg
}))
.pipe(gulp.dest(path.join(dir.dist, 'css')));
});
// Minify HTML
gulp.task('htmlminify', () => {
return gulp
.src(path.join(dir.dist, '*.html'))
.pipe(htmlmin({
collapseWhitespace: true,
preserveLineBreaks: true,
removeComments: true
}))
.pipe(gulp.dest(dir.dist));
});
// Versioning
gulp.task('bump', () => {
return gulp
.src('./package.json')
.pipe(bump({key: 'version', type:'minor'}))
.pipe(gulp.dest('./'));
});
// Service worker with versioning
gulp.task('serviceworker', () => {
return gulp
.src(path.join(dir.src, 'sw.njk.js'))
.pipe(gulpnunjucks.compile("", {env: env}))
.pipe(rename('sw.js'))
.pipe(gulp.dest(dir.dist));
});
// Moving files
gulp.task('move-files', () => {
let readme = gulp.src(['README.md'])
.pipe(gulp.dest(path.join(dir.src, 'markdown')));
let cname = gulp.src(['CNAME'])
.pipe(gulp.dest(dir.dist));
let scripts = gulp.src(['assets/scripts/gallery.js', 'assets/vendor/js.cookie.js'])
.pipe(gulp.dest(path.join(dir.dist, 'scripts')));
return merge(readme, cname, scripts);
});
// Static Server + watching scss/html files
gulp.task('serve', () => {
browserSync.init({
server: dir.dist
});
});
//Get data from google sheets
// JSON
gulp.task('parsedata', function (cb) {
//Clean the folder
del('./json/**')
//Run the command
exec('ruby scripts/update_data.rb', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
})
// JSON combine
gulp.task('combine', () => {
return gulp
.src('./json/*.json')
.pipe(mergeJson({
fileName: 'data.json',
edit: (json, file) => {
// Extract the filename and strip the extension
var filename = path.basename(file.path),
primaryKey = filename.replace(path.extname(filename), '');
// Set the filename as the primary key for our JSON data
var data = {};
data[primaryKey] = json;
return data;
}
}))
.pipe(gulp.dest('./data/'))
})
// Testing
//===========================================
gulp.task('tests', shell.task('$(npm bin)/cypress run'))
//Parallel tasks
gulp.task('compile', gulp.parallel(
'sass',
'scripts',
'serve',
'watch'
))
gulp.task('data', gulp.series(
'parsedata',
'combine'
))
gulp.task('imageassets', gulp.series(
'favicon',
'images'
))
gulp.task('build', gulp.parallel(
'sass-build',
'scripts-build',
'fonts',
'imageassets',
'responsive'
))
gulp.task('tidy', gulp.parallel(
'bump',
'serviceworker',
'banner',
'move-files',
'htmlminify'
))
const dev = gulp.series(
'nunjucks',
'compile'
);
const build = gulp.series(
'clean',
'babel',
'data',
'nunjucks',
'build',
'tidy'
);
//gulp dev
exports.default = dev;
//gulp build
exports.prod = build;