-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
446 lines (410 loc) · 11.6 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
439
440
441
442
443
444
445
446
// Global Imports
const { src, dest, series, parallel, watch } = require('gulp');
const fs = require('fs');
// Gulp tools
const del = require('del');
const tap = require('gulp-tap');
const rev = require('gulp-rev');
const git = require('git-rev-sync')
const cache = require('gulp-cache');
const concat = require('gulp-concat');
const minify = require('gulp-minify');
const jsonminify = require('gulp-jsonminify');
const rename = require('gulp-rename');
const flatten = require('gulp-flatten');
const replace = require('gulp-replace');
const cleanCss = require('gulp-clean-css');
const nunjucks = require('gulp-nunjucks-render');
// Image optimization
const imagemin = require('gulp-imagemin');
const svgo = require('imagemin-svgo');
const webp = require('imagemin-webp');
const mozjpeg = require('imagemin-mozjpeg');
const pngquant = require('imagemin-pngquant');
const browserSync = require('browser-sync').create();
// BrowserSync instance creation
function serve(done) {
var browserSyncConfig = {
server: {
baseDir: "./dist/"
},
ui: false,
online: false,
open: false,
notify: false
}
if (fs.existsSync('./.vscode/browsersync-local.json')) {
console.log('Detected local Browsersync configuration.')
browserSyncConfig = JSON.parse(fs.readFileSync('./.vscode/browsersync-local.json'));
}
browserSync.init(browserSyncConfig);
done();
};
// DevServer asset watchdogs
const watchHTML = () => watch('./src/**/*.html', series(processTemplateDev, reload));
const watchCSS = () => watch('./src/**/*.css', series(packBundleCSS, packVendorCSS, packLocalCSS, processTemplateDev, reload));
const watchJS = () => watch('./src/**/*.js', series(packVendorJS, packBundleJS, packLocalJS, processTemplateDev, reload));
const watchImg = () => watch(['./src/**/*.jpg', './src/**/*.png'], series(optimizeImg, optimizeImgToWebp, processTemplateDev, reload));
function reload(done) {
browserSync.reload();
done();
}
function prepare() {
return del('./dist/');
}
function cleanCache() {
return cache.clearAll();
}
function processTemplateDev() {
delete require.cache[require.resolve('./dist/assets/rev-manifest.json')]
var bundleManifest = require('./dist/assets/rev-manifest.json');
return src([
'./src/index.html',
'./src/out/index.html',
'./src/development_vs_release/index.html',
'./src/project_billboard/index.html',
'./src/project_propaganda/index.html',
'./src/project_doomsday/index.html',
'./src/project_doomsday/2d-game/index.html',
'./src/project_graffiti/index.html',
'./src/archive/index.html',
'./src/contribute/index.html',
'./src/credits/index.html',
'./src/legal/*.html'
], {
base: './src/'
})
.pipe(nunjucks({
data: {
manifest: bundleManifest,
git: {
long: git.long(),
short: git.short(),
branch: git.branch(),
production: false
}
}
}))
.pipe(replace('{{stamp_title}}', 'Build ID: ' + git.short()))
.pipe(replace('{{stamp_text}}', 'DEV'))
.pipe(dest('./dist/'));
}
function processTemplateProd() {
delete require.cache[require.resolve('./dist/assets/rev-manifest.json')]
var bundleManifest = require('./dist/assets/rev-manifest.json');
return src([
'./src/index.html',
'./src/out/index.html',
'./src/development_vs_release/index.html',
'./src/project_billboard/index.html',
'./src/project_propaganda/index.html',
'./src/project_doomsday/index.html',
'./src/project_doomsday/2d-game/index.html',
'./src/project_graffiti/index.html',
'./src/archive/index.html',
'./src/contribute/index.html',
'./src/credits/index.html',
'./src/legal/*.html'
], {
base: './src/'
})
.pipe(nunjucks({
data: {
manifest: bundleManifest,
git: {
long: git.long(),
short: git.short(),
branch: git.branch(),
production: true
}
}
}))
.pipe(dest('./dist/'));
}
function packBundleJS() {
return src([
'./node_modules/jquery/dist/jquery.js',
'./node_modules/bootstrap/dist/js/bootstrap.bundle.min.js',
'./node_modules/js-cookie/src/js.cookie.js',
'./src/assets/js/util.js'
])
.pipe(concat('./dist/assets/vendor/bundles/baseBundle.js'))
.pipe(minify({
noSource: true,
ext: {
min: '.js'
}
}))
.pipe(rev())
.pipe(dest('./'))
.pipe(tap(function (file) {
file.base = file.base + '\\dist'
}))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true
}))
.pipe(dest('./'));
}
function packLocalJS() {
delete require.cache[require.resolve('./dist/assets/rev-manifest.json')]
var bundleManifest = require('./dist/assets/rev-manifest.json');
return src([
'./src/assets/js/*.js',
'!./src/assets/js/global.js'
], { base: 'src' })
.pipe(nunjucks({
data: {
manifest: bundleManifest,
git: {
long: git.long(),
short: git.short(),
production: false
}
},
ext: '.js'
}))
.pipe(minify({
noSource: true,
ext: {
min: '.js'
}
}))
.pipe(rev())
.pipe(dest('./dist'))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true,
}))
.pipe(dest('./'));
}
function packVendorJS() {
return src([
'./node_modules/comparison-slider/dist/comparison-slider.min.js',
'./node_modules/pagemap/dist/pagemap.min.js',
'./node_modules/image-picker/image-picker/image-picker.min.js',
'./node_modules/nouislider/distribute/nouislider.min.js',
'./node_modules/shaka-player/dist/shaka-player.compiled.js',
'./node_modules/wnumb/wNumb.min.js'
], { base: 'node_modules' })
.pipe(flatten({ includeParents: 1 }))
.pipe(minify({
noSource: true,
ext: {
min: '.js'
}
}))
.pipe(rev())
.pipe(dest('./dist/assets/vendor/'))
.pipe(tap(function (file) {
file.base = file.base.substring(0, file.base.length - 14);
}))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true
}))
.pipe(dest('./'))
}
function copyVendorDependencies() {
return src([
'./node_modules/wasm-imagemagick/dist/magick.wasm',
'./node_modules/wasm-imagemagick/dist/magickApi.js',
'./node_modules/wasm-imagemagick/dist/magick.js',
], { base: 'node_modules' })
.pipe(flatten({ includeParents: 1 }))
.pipe(dest('./dist/assets/vendor/'))
}
function packBundleCSS() {
delete require.cache[require.resolve('./dist/assets/rev-manifest.json')]
var bundleManifest = require('./dist/assets/rev-manifest.json');
return src([
'./node_modules/bootstrap/dist/css/bootstrap.min.css',
'./src/assets/css/global.css'
], { base: '/' })
.pipe(concat('./dist/assets/vendor/bundles/baseBundle.css'))
.pipe(nunjucks({
data: {
manifest: bundleManifest,
git: {
long: git.long(),
short: git.short(),
production: false
}
},
ext: '.css'
}))
// PurgeCSS has been disabled as it causes a whole bunch of problems and requires a humongous whitelist to work with 3rd-party frameworks
// Install gulp-purgecss to re-enable CSS purging
// .pipe(purgecss({
// content: ['./src/**/*.html', './src/**/*.js'],
// whitelist: [
// '.carousel-item-next',
// 'carousel-item-prev',
// 'carousel-item.active',
// 'close',
// 'alert-secondary',
// 'fade',
// 'show',
// 'alert-dismissible']
// }))
.pipe(cleanCss())
.pipe(rev())
.pipe(dest('./'))
.pipe(tap(function (file) {
file.base = file.base + '\\dist'
}))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true
}))
.pipe(dest('./'));
}
function packLocalCSS() {
return src([
'./src/assets/css/*.css',
'!./src/assets/css/global.css'
], { base: 'src' })
.pipe(cleanCss())
.pipe(rev())
.pipe(dest('./dist/'))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true,
}))
.pipe(dest('./'));
}
function packVendorCSS() {
return src([
'./node_modules/image-picker/image-picker/image-picker.css',
'./node_modules/@mdi/font/css/materialdesignicons.min.css',
'./node_modules/nouislider/distribute/nouislider.min.css'
], { base: 'node_modules' })
.pipe(flatten({ includeParents: 1 }))
.pipe(cleanCss())
.pipe(rev())
.pipe(dest('./dist/assets/vendor/'))
.pipe(tap(function (file) {
file.base = file.base.substring(0, file.base.length - 14);
}))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true
}))
.pipe(dest('./'))
}
function optimizeImg() {
console.log(`Caching optimized images in ${require('os').tmpdir()}`)
return src([
'./src/assets/media/image/**/*.jpg',
'./src/assets/media/image/**/*.png',
'./src/assets/media/image/**/*.svg'
], { base: 'src' })
.pipe(cache(imagemin([
pngquant({ quality: [0.65, 0.85] }),
mozjpeg({ quality: 85 }),
svgo()
],
{
verbose: true
})),
{
name: "jpgpng"
}
)
.pipe(rev())
.pipe(dest('./dist'))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true
}))
.pipe(dest('./'));
}
function optimizeImgToWebp() {
console.log(`Caching optimized images in ${require('os').tmpdir()}`)
return src([
'./src/assets/media/image/**/*.jpg',
'./src/assets/media/image/**/*.png'
], { base: 'src' })
.pipe(cache(imagemin(
[webp({quality: 90, alphaQuality: 100})],
{
verbose: true
}
),
{
name: "webp"
}
))
.pipe(rename({ extname: '.webp' }))
.pipe(rev())
.pipe(dest('./dist'))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true
}))
.pipe(dest('./'));
}
function copyFonts() {
return src('./node_modules/@mdi/font/fonts/*')
.pipe(dest('./dist/assets/vendor/fonts/'))
}
function copyStatic() {
return src([
'src/favicon.ico',
'src/icon-192.png',
'src/icon-512.png',
'src/icon.svg',
'src/apple-touch-icon.png',
'src/manifest.webmanifest',
'src/.well-known/*',
'src/project_doomsday/2d-game/ea.comm.swf'
], { base: 'src' })
.pipe(dest('dist/'));
}
function copyJson() {
return src([
'src/**/*.json'
], { base: 'src' })
.pipe(jsonminify())
.pipe(dest('dist/'));
}
function copyAV() {
return src(['./src/assets/media/audio/**/*', './src/assets/media/video/**/*'], {
base: 'src'
})
.pipe(rev())
.pipe(dest('./dist'))
.pipe(rev.manifest('./dist/assets/rev-manifest.json', {
merge: true
}))
.pipe(dest('./'));
}
// Task groups
// CopyAndPack is the main task, which calls all relevant sub-tasks to pack, optimize and copy resources.
// ProcessTemplate[dev/prod] is called externally, to determine the build environment.
const copyAssets = series(
copyJson,
copyStatic,
copyFonts,
copyAV,
);
const optimizeAssets = series(
optimizeImg,
optimizeImgToWebp
);
const packJS = series(
packVendorJS,
copyVendorDependencies,
packBundleJS,
packLocalJS
);
const packCSS = series(
packBundleCSS,
packVendorCSS,
packLocalCSS,
);
const copyAndPack = series(
copyAssets,
optimizeAssets,
packJS,
packCSS
);
// Exports
exports.default = series(prepare, copyAndPack);
exports.build = series(prepare, copyAndPack, processTemplateDev);
exports.buildProduction = series(prepare, cleanCache, copyAndPack, processTemplateProd)
exports.devServer = series(prepare, copyAndPack, processTemplateDev, serve, parallel(watchHTML, watchCSS, watchJS, watchImg));
exports.prepare = prepare;
exports.cleanCache = cleanCache;