-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
571 lines (541 loc) · 16.9 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
const { src, dest, watch, series, parallel } = require('gulp');
// SASS vars
const sass = require('gulp-sass')(require('sass'));
const cleanCSS = require('gulp-clean-css');
const autoprefixer = require('gulp-autoprefixer');
// JS vars
const terser = require('gulp-terser');
const concat = require('gulp-concat');
const eslint = require('gulp-eslint');
// HTML vars
const htmlHint = require('gulp-htmlhint');
const htmlMin = require('gulp-htmlmin');
const ngHtml2Js = require('gulp-ng-html2js');
// Image vars
const imagemin = require('gulp-imagemin');
// Live-reload server vars
const browserSync = require('browser-sync').create();
const sourcemaps = require('gulp-sourcemaps');
// Other vars
const del = require('del');
const plumber = require('gulp-plumber');
const replace = require('gulp-replace');
const fs = require('fs');
var gulpCopy = require('gulp-copy');
const json = JSON.parse(fs.readFileSync('./package.json'));
let build = 'azurelive';
// Function that is ran when buildAll is called to determine buildEnv
// This matches the buildDirs in package.json
function determineBuild(done) {
switch (process.env.npm_config_build) {
case 'staging':
build = 'staging';
break;
case 'live':
build = 'live';
break;
case 'livetest':
build = 'livetest';
break;
case 'azure':
build = 'azure';
break;
case 'azurelive':
build = 'azurelive';
break;
default:
build = 'azurelive';
break;
}
done();
}
const paths = {
output: 'build/', // Default output location for code build
server: {
port: 8080,
baseDir: './',
index: 'build/index.html'
},
serverSwift: {
port: 8080,
baseDir: './',
index: 'build/index-swift.html'
},
serverOneapp: {
port: 8080,
baseDir: './',
index: 'build/index-oneapp.html'
},
styles: {
src: 'src/app/sass/wmn/*.scss', // src of styles
minifySrc: 'src/app/sass/wmn/wmn-ticketing.scss', // List of scss file(s) which should be processed, linted & minified
output: 'build/css/wmn' // output location of minified styles
},
stylesSwift: {
src: 'src/app/sass/swift/*.scss', // src of styles
minifySrc: 'src/app/sass/swift/swift-ticketing.scss', // List of scss file(s) which should be processed, linted & minified
output: 'build/css/swift' // output location of minified styles
},
ticketStyles: {
src: 'src/app/ticket/*.scss'
},
ticketsStyles: {
src: 'src/app/tickets/*.scss'
},
scripts: {
src: './src/**/*.js', // Src of JS files
// List of JS folders to concatenate, lint and minified to one file (DON'T LINT ASSETS AS IT WILL TAKE TOO LONG TO SCAN MINIFIED LIBS)
minifySrc: [
{ src: 'src/app/js/wmn/*.js', minName: 'wmn.app.min.js', lint: true },
{ src: 'src/app/js/swift/*.js', minName: 'swift.app.min.js', lint: true },
{
src: 'src/app/js/oneapp/*.js',
minName: 'oneapp.app.min.js',
lint: true
},
{ src: 'src/assets/**/*.js', minName: 'assets.min.js', lint: false },
{ src: 'src/app/services/*.js', minName: 'services.min.js', lint: true },
{ src: 'src/app/shared/*.js', minName: 'shared.min.js', lint: true },
{
src: 'src/app/controller/*.js',
minName: 'controller.min.js',
lint: true
},
{
src: 'src/app/directives/*.js',
minName: 'directives.min.js',
lint: true
}
],
output: 'build/js/' // Output location of minified JS files
},
templates: {
src: './src/app/**/views/wmn/*.html',
minName: 'partials.min.js'
},
templatesShared: {
src: './src/app/**/views/shared/*.html',
minName: 'shared.partials.min.js'
},
templatesSwiftShared: {
src: './src/app/**/views/shared/*.html',
minName: 'swift.shared.partials.min.js'
},
templatesOneappShared: {
src: './src/app/**/views/shared/*.html',
minName: 'oneapp.shared.partials.min.js'
},
templatesSwift: {
src: './src/app/**/views/swift/*.html',
minName: 'swift.partials.min.js'
},
templatesOneapp: {
src: './src/app/**/views/oneapp/*.html',
minName: 'oneapp.partials.min.js'
},
images: {
src: './src/assets/img/**/*',
dest: 'build/img/'
}
};
const getRoot = path => '../'.repeat(path.match(/\//gi).length); // Function which takes in a path and back counts slashes to get to baseRoot dir
// Clean the current build & _sourcemaps dir
function cleanBuild() {
return del([paths.output, '_sourcemaps']);
}
// Process, lint, and minify Sass files
function buildStyles() {
return src(paths.styles.minifySrc)
.pipe(
plumber({
errorHandler: function(error) {
console.log(error.message);
this.emit('end');
}
})
)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError)) // Compile Sass
.pipe(autoprefixer()) // Prefix css with older browser support
.pipe(cleanCSS({ level: 2 })) // Minify css
.pipe(sourcemaps.write(getRoot(paths.styles.output) + '_sourcemaps/'))
.pipe(replace('$*imgUrl', json.buildDirs[build].imgUrl))
.pipe(dest(paths.styles.output))
.pipe(browserSync.stream()); // Push new CSS to server without reload
}
// Process, lint, and minify Sass files for Swift
function buildSwiftStyles() {
console.log(json.buildDirs[build].images);
return src(paths.stylesSwift.minifySrc)
.pipe(
plumber({
errorHandler: function(error) {
console.log(error.message);
this.emit('end');
}
})
)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError)) // Compile Sass
.pipe(autoprefixer()) // Prefix css with older browser support
.pipe(cleanCSS({ level: 2 })) // Minify css
.pipe(sourcemaps.write(getRoot(paths.stylesSwift.output) + '_sourcemaps/'))
.pipe(replace('$*imgUrl', json.buildDirs[build].imgUrl))
.pipe(dest(paths.stylesSwift.output))
.pipe(browserSync.stream()); // Push new CSS to server without reload
}
// Minify, and concatenate scripts
function buildScripts(done) {
paths.scripts.minifySrc.map(jsFile => minifyJS(jsFile));
done();
}
// Placeholder function for buildScripts to loop through
function minifyJS(jsFile) {
return src(jsFile.src)
.pipe(
plumber({
errorHandler: function(error) {
console.log(error.message);
this.emit('end');
}
})
)
.pipe(sourcemaps.init())
.pipe(concat(jsFile.minName)) // concat all js files in folder
.pipe(terser())
.pipe(sourcemaps.write(getRoot(paths.scripts.output) + '_sourcemaps/'))
.pipe(plumber.stop())
.pipe(replace('$*api', json.buildDirs[build].api))
.pipe(replace('$*baseUrl', json.buildDirs[build].baseUrl))
.pipe(replace('$*baseUrlSwift', json.buildDirs[build].baseUrlSwift))
.pipe(replace('$*baseUrlOneapp', json.buildDirs[build].baseUrlOneapp))
.pipe(dest(paths.scripts.output)); // Spit out concat + minified file in ./build/
}
// Lint scripts/JS
function lintScripts() {
// Loop through each minifySrc and check if it is to be linted
const lintSrc = paths.scripts.minifySrc.map(jsFile =>
jsFile.lint ? jsFile.src : '!' + jsFile.src
);
return src(lintSrc)
.pipe(eslint({ configFile: '.eslintrc.json' })) // eslint() attaches the lint output to the "eslint" property of the file object so it can be used by other modules.
.pipe(eslint.format()); // eslint.format() outputs the lint results to the console.
// .pipe(eslint.failAfterError()); // Cause the stream to stop/fail before copying an invalid JS file to the output directory
}
// Lint Templates/HTML
function lintTemplates() {
return src(paths.templates.src)
.pipe(htmlHint('.htmlhintrc'))
.pipe(htmlHint.reporter());
}
function buildTemplates() {
return src(paths.templates.src)
.pipe(htmlMin({ collapseWhitespace: true, removeComments: true }))
.pipe(
ngHtml2Js({
moduleName: 'ticketingApp'
})
)
.pipe(concat(paths.templates.minName))
.pipe(terser())
.pipe(replace('$*baseUrl', json.buildDirs[build].baseUrl))
.pipe(replace('$*baseUrlSwift', json.buildDirs[build].baseUrlSwift))
.pipe(replace('$*baseUrlOneapp', json.buildDirs[build].baseUrlOneapp))
.pipe(replace('$*imgUrl', json.buildDirs[build].imgUrl))
.pipe(replace('$*paygLink', json.buildDirs[build].paygLink))
.pipe(dest('./build/js/'));
}
// Lint Shared Templates/HTML
function lintSharedTemplates() {
return src(paths.templatesShared.src)
.pipe(htmlHint('.htmlhintrc'))
.pipe(htmlHint.reporter());
}
function lintSharedSwiftTemplates() {
return src(paths.templatesSwiftShared.src)
.pipe(htmlHint('.htmlhintrc'))
.pipe(htmlHint.reporter());
}
function lintSharedOneappTemplates() {
return src(paths.templatesOneappShared.src)
.pipe(htmlHint('.htmlhintrc'))
.pipe(htmlHint.reporter());
}
function buildSharedTemplates() {
return src(paths.templatesShared.src)
.pipe(htmlMin({ collapseWhitespace: true, removeComments: true }))
.pipe(
ngHtml2Js({
moduleName: 'ticketingApp'
})
)
.pipe(concat(paths.templatesShared.minName))
.pipe(terser())
.pipe(replace('$*baseUrl', json.buildDirs[build].baseUrl))
.pipe(replace('$*baseUrlSwift', json.buildDirs[build].baseUrlSwift))
.pipe(replace('$*swiftHost', json.buildDirs[build].swiftHost))
.pipe(replace('$*baseUrlOneapp', json.buildDirs[build].baseUrlOneapp))
.pipe(replace('$*oneappHost', json.buildDirs[build].oneappHost))
.pipe(replace('$*imgUrl', json.buildDirs[build].imgUrl))
.pipe(replace('$*paygLink', json.buildDirs[build].paygLink))
.pipe(dest('./build/js/'));
}
function buildSharedSwiftTemplates() {
return src(paths.templatesSwiftShared.src)
.pipe(htmlMin({ collapseWhitespace: true, removeComments: true }))
.pipe(
ngHtml2Js({
moduleName: 'ticketingApp'
})
)
.pipe(concat(paths.templatesSwiftShared.minName))
.pipe(terser())
.pipe(replace('$*baseUrl', json.buildDirs[build].baseUrlSwift))
.pipe(replace('$*baseUrlSwift', json.buildDirs[build].baseUrlSwift))
.pipe(replace('$*swiftHost', json.buildDirs[build].swiftHost))
.pipe(replace('$*imgUrl', json.buildDirs[build].imgUrl))
.pipe(replace('$*paygLink', json.buildDirs[build].paygLink))
.pipe(dest('./build/js/'));
}
function buildSharedOneappTemplates() {
return src(paths.templatesOneappShared.src)
.pipe(htmlMin({ collapseWhitespace: true, removeComments: true }))
.pipe(
ngHtml2Js({
moduleName: 'ticketingApp'
})
)
.pipe(concat(paths.templatesOneappShared.minName))
.pipe(terser())
.pipe(replace('$*baseUrl', json.buildDirs[build].baseUrlOneapp))
.pipe(replace('$*baseUrlOneapp', json.buildDirs[build].baseUrlOneapp))
.pipe(replace('$*oneappHost', json.buildDirs[build].oneappHost))
.pipe(replace('$*imgUrl', json.buildDirs[build].imgUrl))
.pipe(replace('$*paygLink', json.buildDirs[build].paygLink))
.pipe(dest('./build/js/'));
}
// Lint Swift Templates/HTML
function lintSwiftTemplates() {
return src(paths.templatesShared.src)
.pipe(htmlHint('.htmlhintrc'))
.pipe(htmlHint.reporter());
}
function buildSwiftTemplates() {
return src(paths.templatesSwift.src)
.pipe(htmlMin({ collapseWhitespace: true, removeComments: true }))
.pipe(
ngHtml2Js({
moduleName: 'ticketingApp'
})
)
.pipe(concat(paths.templatesSwift.minName))
.pipe(terser())
.pipe(replace('$*baseUrl', json.buildDirs[build].baseUrlSwift))
.pipe(replace('$*imgUrl', json.buildDirs[build].imgUrl))
.pipe(replace('$*swiftGo', json.buildDirs[build].swiftGo))
.pipe(dest('./build/js/'));
}
// Lint Oneapp Templates/HTML
function lintOneappTemplates() {
return src(paths.templatesOneapp.src)
.pipe(htmlHint('.htmlhintrc'))
.pipe(htmlHint.reporter());
}
function buildOneappTemplates() {
return src(paths.templatesOneapp.src)
.pipe(htmlMin({ collapseWhitespace: true, removeComments: true }))
.pipe(
ngHtml2Js({
moduleName: 'ticketingApp'
})
)
.pipe(concat(paths.templatesOneapp.minName))
.pipe(terser())
.pipe(replace('$*baseUrl', json.buildDirs[build].baseUrlOneapp))
.pipe(replace('$*imgUrl', json.buildDirs[build].imgUrl))
.pipe(replace('$*swiftGo', json.buildDirs[build].swiftGo))
.pipe(dest('./build/js/'));
}
function moveMain() {
return src(['index.html']).pipe(gulpCopy('build', { prefix: 1 }));
}
function moveSwift() {
return src(['swift/index.html']).pipe(gulpCopy('build/swift', { prefix: 1 }));
}
function moveOneapp() {
return src(['oneapp/index.html']).pipe(gulpCopy('build/oneapp', { prefix: 1 }));
}
// Optimise images
function minImages() {
return src(paths.images.src)
.pipe(imagemin())
.pipe(dest(paths.images.dest));
}
// Default WMN Server
function server(done) {
browserSync.init({
server: {
baseDir: paths.server.baseDir
},
port: paths.server.port
});
done();
}
// Swift Server
function serverSwift(done) {
browserSync.init({
server: {
baseDir: paths.serverSwift.baseDir
},
port: paths.serverSwift.port,
index: paths.serverSwift.index
});
done();
}
// Oneapp Server
function serverOneapp(done) {
browserSync.init({
server: {
baseDir: paths.serverOneapp.baseDir
},
port: paths.serverOneapp.port,
index: paths.serverOneapp.index
});
done();
}
function reload(done) {
browserSync.reload();
done();
}
const buildAll = series(
determineBuild,
cleanBuild,
minImages,
buildScripts,
buildStyles,
buildSwiftStyles,
buildTemplates,
buildSharedTemplates,
buildSharedSwiftTemplates,
buildSharedOneappTemplates,
buildSwiftTemplates,
buildOneappTemplates,
lintScripts,
lintTemplates,
lintSharedTemplates,
lintSharedSwiftTemplates,
lintSharedOneappTemplates,
lintSwiftTemplates,
lintOneappTemplates,
moveMain,
moveSwift,
moveOneapp
);
// Watch files for changes
function watchFiles() {
// Lint, concat, minify JS then reload server
watch([paths.scripts.src], series(lintScripts, buildScripts, reload));
watch(
'./**/*.html',
series(
lintTemplates,
lintSharedTemplates,
lintSharedSwiftTemplates,
lintSharedOneappTemplates,
lintSwiftTemplates,
lintOneappTemplates,
buildTemplates,
buildSharedTemplates,
buildSharedSwiftTemplates,
buildSharedOneappTemplates,
buildSwiftTemplates,
buildOneappTemplates,
reload
)
); // Reload when html changes
watch(paths.images.src, minImages);
watch(paths.styles.src, buildStyles); // run buildStyles function on scss change(s)
watch(paths.stylesSwift.src, buildSwiftStyles); // run buildSwiftStyles function on scss change(s) - swift
watch(paths.ticketStyles.src, buildStyles); // update custom ticket product page scss
watch(paths.ticketStyles.src, buildSwiftStyles); // update custom ticket product page scss
watch(paths.ticketsStyles.src, buildStyles); // update custom ticket search scss
watch(paths.ticketsStyles.src, buildSwiftStyles); // update custom ticket search scss
watch(['./package.json', './gulpfile.js'], series(buildAll, reload));
}
// Default WMN
const dev = series(
lintScripts,
lintTemplates,
lintSharedTemplates,
lintSharedSwiftTemplates,
lintSharedOneappTemplates,
lintSwiftTemplates,
lintOneappTemplates,
parallel(
buildStyles,
buildSwiftStyles,
buildScripts,
buildTemplates,
buildSharedTemplates,
buildSharedSwiftTemplates,
buildSharedOneappTemplates,
buildSwiftTemplates,
buildOneappTemplates,
minImages,
moveMain,
moveSwift,
moveOneapp
),
parallel(watchFiles, server)
); // run buildStyles & minifyJS on start, series so () => run in an order and parallel so () => can run at same time
// Swift version
const devSwift = series(
lintScripts,
lintTemplates,
lintSharedTemplates,
lintSharedSwiftTemplates,
lintSwiftTemplates,
parallel(buildSwiftStyles, buildScripts, buildSwiftTemplates, minImages),
parallel(watchFiles, serverSwift)
);
// Oneapp version
const devOneapp = series(
lintScripts,
lintTemplates,
lintSharedTemplates,
lintSharedOneappTemplates,
lintOneappTemplates,
parallel(buildStyles, buildScripts, buildOneappTemplates, minImages),
parallel(watchFiles, serverOneapp)
);
// Export items to be used in terminal
exports.default = dev;
exports.defaultSwift = devSwift;
exports.defaultOneapp = devOneapp;
exports.lintScripts = lintScripts;
exports.lintTemplates = series(
lintTemplates,
lintSharedTemplates,
lintSharedSwiftTemplates,
lintSharedOneappTemplates,
lintSwiftTemplates,
lintOneappTemplates
);
exports.clean = cleanBuild;
exports.buildScripts = series(buildScripts, lintScripts);
exports.buildStyles = buildStyles;
exports.buildSwiftStyles = buildSwiftStyles;
exports.buildTemplates = series(
buildTemplates,
buildSharedTemplates,
buildSharedSwiftTemplates,
buildSharedOneappTemplates,
buildSwiftTemplates,
buildOneappTemplates,
lintTemplates
);
exports.moveMain = moveMain;
exports.moveSwift = moveSwift;
exports.moveOneapp = moveOneapp;
exports.minImages = minImages;
exports.buildAll = buildAll;