-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
593 lines (505 loc) · 17.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// ----------------------------------------------------------------------
// ∴ Gulp Imports.
// ----------------------------------------------------------------------
const gulp = require('gulp');
const cssnano = require('gulp-cssnano');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const sourcemaps = require('gulp-sourcemaps');
const hash = require('gulp-hash-filename');
const clean = require('gulp-clean');
const runSequence = require('run-sequence');
const uglify = require('gulp-uglify');
const tsc = require('gulp-typescript');
const babel = require('gulp-babel');
const rollup = require('gulp-better-rollup');
const babel2 = require('rollup-plugin-babel');
const color = require('gulp-color');
const cmd = require('child_process').exec;
const devColor = "GREEN";
const prodColor = "CYAN";
// ----------------------------------------------------------------------
// ∴ Build configuration.
// ----------------------------------------------------------------------
// Create build configuration based on argument conditions.
// 1st parameter: 'Development Environment', can be set to 'production', or 'development'.
// 1st parameter: 'Use Angular Compiler', can be set to 'true', or 'false'.
// 1st parameter: 'Use Watch', can be set to 'true', or 'false'. This is always disabled in prod.
// This MUST be set under the 'config' flag of package.json located in the root directory of nFrame.
const config = {
environment: '',
useAngular: '',
watch: '',
useNgRecompiler: true
};
// ----------------------------------------------------------------------
// ∴ Path Variables.
// ----------------------------------------------------------------------
const cssDest = './nStatic/css';
cssBundleName = 'bundle.css';
jsDest = './nStatic/js';
jsBundleName = 'bundle.js';
cssBundleLocal = `${cssDest}/${cssBundleName}`;
jsBundleLocal = `${jsDest}/${jsBundleName}`;
sassSource = './webStyle/**/**/*.scss';
sassBase = './webStyle/main.scss';
tsSource = './webTS/**/*.ts';
jsSource = './webJS/**/*.js';
tsCompilerOuts = './webTS/_compiler/**/*.ts';
jsCompilerOuts = './webTS/_compiler/**/*.js';
compilerEs6Out = './webTS/_compiler/es6_out';
jsFolderCapture = './webTS/**/*.js';
es6TranspileCapture = './webTS/_compiler/es6_out/**/*.js';
jQSource = './node_modules/jquery/dist/jquery.min.js';
compilerEs5Out = './webTS/_compiler/es5_out';
mainEs5Capture = './webTS/_compiler/es5_out/main.js';
mainEs5JSCapture = './webTS/_compiler/es5_out/mainjs.js';
crossAppJsCapture = './webApp/src/**/**/*.js';
webAppDistOut = './webApp/dist/**/*.*';
preBundleJsAsset = `${jsDest}/main.js`;
preBundleVanillaJSAsset = `${jsDest}/mainjs.js`;
staticLocals = `${jsDest}/*.js`;
karma = './webApp/src/karma.conf.js';
// ----------------------------------------------------------------------
// ∴ Capture NPM build command and execute paths based on selection.
// ----------------------------------------------------------------------
gulp.task('development-true-false', function() {
config.environment = 'development';
config.useAngular = true;
config.watch = false;
developmentBuild();
})
gulp.task('development-false-false', function() {
config.environment = 'development';
config.useAngular = false;
config.watch = false;
developmentBuild();
})
gulp.task('development-true-true', function() {
config.environment = 'development';
config.useAngular = true;
config.watch = true;
console.log(color(`
---------------------------------------
∴ nFrame Development Build Initialised.
∴ Watching: Angular & SCSS Changes.
---------------------------------------`,
'YELLOW'));
gulp.start('build-angular-watch'),
gulp.watch(sassSource, ['sassDevBuild'])
})
gulp.task('development-false-true', function() {
config.environment = 'development';
config.useAngular = false;
config.watch = true;
console.log(color(`
---------------------------------------
∴ nFrame Development Build Initialised.
∴ Watching: TypeScript & SCSS Changes.
---------------------------------------`,
'YELLOW'));
gulp.watch(tsSource, ['buildDevTypeScript']),
gulp.watch(jsSource, ['buildDevTypeScript']),
gulp.watch(sassSource, ['sassDevBuild'])
})
gulp.task('production-true-false', function() {
config.environment = 'production';
config.useAngular = true;
config.watch = false;
productionBuild();
})
gulp.task('production-false-false', function() {
config.environment = 'production';
config.useAngular = false;
config.watch = false;
productionBuild();
})
gulp.task('production-true-true', function() {
console.log(color(`
---------------------------------------
∴ Watch cannot be initialised on production build.
---------------------------------------`
, 'RED'));
})
gulp.task('production-false-true', function() {
console.log(color(`
---------------------------------------
∴ Watch cannot be initialised on production build.
---------------------------------------`
, 'RED'));
})
// ----------------------------------------------------------------------
// ∴ Initialise compiler method path.
// ----------------------------------------------------------------------
const developmentBuild = function() {
let compilerType;
switch (config.useAngular) {
case true:
compilerType = color("Angular Compiler", 'RED');
console.log(color(`
---------------------------------------
∴ nFrame Development Build Initialised.
∴ Compiler type: ${compilerType}`,
devColor));
console.log(color(`
---------------------------------------
`, devColor));
gulp.start('buildDevAngular');
break;
case false:
compilerType = color("TypeScript Compiler", 'YELLOW');
console.log(color(`
---------------------------------------
∴ nFrame Development Build Initialised.
∴ Compiler type: ${compilerType}`,
devColor));
console.log(color(`
---------------------------------------
`, devColor));
gulp.start('buildDevTypeScript');
break;
}
}
const productionBuild = function() {
let compilerType;
switch (config.useAngular) {
case true:
compilerType = color("Angular Compiler", 'RED');
console.log(color(`
---------------------------------------
∴ nFrame Production Build Initialised.
∴ Compiler type: ${compilerType}`,
prodColor));
console.log(color(`
---------------------------------------
`, prodColor));
gulp.start('buildProdAngular');
break;
case false:
compilerType = color("TypeScript Compiler", 'YELLOW');
console.log(color(`
---------------------------------------
∴ nFrame Production Build Initialised.
∴ Compiler type: ${compilerType}`,
prodColor));
console.log(color(`
---------------------------------------
`, prodColor));
gulp.start('buildProdTypeScript');
break;
}
}
// ----------------------------------------------------------------------
// ∴ Build front-end for Development.
// ----------------------------------------------------------------------
// With Angular Compiler.
gulp.task('buildDevAngular', function (callback) {
switch (config.watch) {
case true:
runSequence(
'build-angular-watch',
'mark-angular-complete',
)
break;
case false:
runSequence(
'build-angular',
'sassDevBuild',
'mark-complete',
callback);
break;
}
})
// With TypeScript Compiler.
gulp.task('buildDevTypeScript', function (callback) {
switch (config.watch) {
case true:
runSequence(
'pre-clean',
'ts-to-es6',
'es6-to-es5',
'es5-concat',
'jquery-concat',
'post-clean-dev',
'mark-ts-complete',
callback);
break;
case false:
runSequence(
'pre-clean',
'ts-to-es6',
'es6-to-es5',
'es5-concat',
'jquery-concat',
'sassDevBuild',
'post-clean-dev',
'mark-complete',
callback);
break;
}
})
// ----------------------------------------------------------------------
// ∴ Build front-end for production.
// ----------------------------------------------------------------------
// With Angular Compiler.
gulp.task('buildProdAngular', function (callback) {
runSequence(
'pre-clean-prod-ng',
'build-angular-prod',
'concat-ng',
'sassProdBuild',
'mark-complete',
callback);
})
// With TypeScript Compiler.
gulp.task('buildProdTypeScript', function (callback) {
runSequence(
'pre-clean-prod',
'ts-to-es6-prod',
'es6-to-es5',
'es5-concat-prod',
'jquery-concat-compress',
'sassProdBuild',
'post-clean-prod',
'mark-complete',
callback);
})
// ----------------------------------------------------------------------
// ∴ Initialise either the Angular compiler, or the TypeScript compiler.
// ----------------------------------------------------------------------
gulp.task('build-angular', function(cb) {
console.log(color(`
∴ Building Angular in DEVELOPMENT Mode.
`, devColor))
cmd('ng build', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
})
gulp.task('build-angular-watch', function(cb) {
var internalConfig = config;
console.log(color(`
∴ Building Angular in DEVELOPMENT Mode.
`, devColor))
console.log(color(`
∴ WARNING: Angular will recompile SILENTLY in this state.
∴ This is due to the nature of reporting in the NGBuild Pipeline.
∴ For ore verbose ngRebuild reporting, set 'useNgRecompiler' on line 42 of this file to 'false'
∴ And instead run 'ng build --watch' in a seperate terminal window alongside 'npm run build'
∴ useNgRecompiler is currently set to ${internalConfig.useNgRecompiler}
`, 'YELLOW'))
if(internalConfig.useNgRecompiler === true) {
cmd('ng build --watch', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
}
})
gulp.task('build-angular-prod', function(cb) {
console.log(color(`
∴ Building Angular in PRODUCTION Mode.
`, prodColor));
cmd('ng build --prod --aot --output-hashing none', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
})
// ----------------------------------------------------------------------
// ∴ Initialise Typscript compiler, concatinate jQuery.
// ----------------------------------------------------------------------
gulp.task('pre-clean', function() {
console.log(color(`
∴ Cleaning up JS/TS folders
`, devColor));
gulp.src([staticLocals, tsCompilerOuts, jsCompilerOuts], {
read: false
})
.pipe(clean({force: true}));
})
gulp.task('ts-to-es6', function() {
console.log(color(`
∴ Running TypeScript Transpiler
`, devColor));
return gulp.src([tsSource])
.pipe(tsc({
typescript: require('typescript'),
target: 'ES6',
}))
.pipe(gulp.dest(compilerEs6Out));
});
gulp.task('pre-clean-prod', function() {
console.log(color(`
∴ Cleaning up JS/TS folders
`, prodColor));
gulp.src([staticLocals, tsCompilerOuts, jsCompilerOuts], {
read: false
})
.pipe(clean({force: true}));
})
gulp.task('pre-clean-prod-ng', function() {
console.log(color(`
∴ Cleaning up Angular folders
`, prodColor));
gulp.src([webAppDistOut, staticLocals, tsCompilerOuts, jsCompilerOuts], {
read: false
})
.pipe(clean({force: true}));
})
gulp.task('ts-to-es6-prod', function() {
console.log(color(`
∴ Running TypeScript Transpiler
`, prodColor));
return gulp.src([tsSource])
.pipe(tsc({
typescript: require('typescript'),
target: 'ES6',
}))
.pipe(gulp.dest(compilerEs6Out));
});
gulp.task('es6-to-es5', function() {
return gulp.src(
[
es6TranspileCapture,
jsSource
])
.pipe(babel({
presets: [
['@babel/env', {
modules: false
}]
]
}))
.pipe(gulp.dest(compilerEs5Out));
});
gulp.task('es5-concat', function() {
return gulp.src([mainEs5Capture, mainEs5JSCapture])
.pipe(sourcemaps.init())
.pipe(rollup({
plugins: [ babel2() ]
},
{
format: 'cjs',
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest(jsDest));
});
gulp.task('es5-concat-prod', function() {
return gulp.src([mainEs5Capture, mainEs5JSCapture])
.pipe(rollup({
plugins: [ babel2() ]
},
{
format: 'cjs',
}))
.pipe(gulp.dest(jsDest));
});
gulp.task('jquery-concat', function() {
return gulp.src([jQSource, preBundleJsAsset, preBundleVanillaJSAsset])
.pipe(concat(jsBundleName))
.pipe(gulp.dest(jsDest));
})
gulp.task('jquery-concat-compress', function() {
return gulp.src([jQSource, preBundleJsAsset, preBundleVanillaJSAsset])
.pipe(uglify())
.pipe(concat(jsBundleName))
.pipe(gulp.dest(jsDest));
})
gulp.task('post-clean-dev', function() {
console.log(color(`
∴ Cleaning Development environment.
`, devColor));
gulp.src([preBundleJsAsset, preBundleVanillaJSAsset, jsFolderCapture, crossAppJsCapture], {
read: false
})
.pipe(clean({force: true}));
})
gulp.task('post-clean-prod', function() {
console.log(color(`
∴ Cleaning Production environment.
`, prodColor));
gulp.src([preBundleJsAsset, preBundleVanillaJSAsset, jsFolderCapture, crossAppJsCapture, karma], {
read: false
})
.pipe(clean({force: true}));
})
gulp.task('concat-ng', function() {
console.log(color(`
∴ Packaging Angular Production bundle.
`, prodColor));
return gulp.src([
'./webApp/dist/webApp/es2015-polyfills.js',
'./webApp/dist/webApp/polyfills.js',
'./webApp/dist/webApp/main.js',
'./webApp/dist/webApp/runtime.js'
])
.pipe(uglify())
.pipe(concat(jsBundleName))
.pipe(gulp.dest(jsDest))
})
// ----------------------------------------------------------------------
// Initialise SCSS Transpilers, concatinate and distribute to Static.
// ----------------------------------------------------------------------
gulp.task('sassDevBuild', function () {
console.log(color(`
∴ Running SCSS -> CSS Development Transpiler
∴ Source Mapping ENABLED
`, devColor));
gulp.src([
sassBase
])
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(cssnano())
.pipe(concat(cssBundleName))
.pipe(sourcemaps.write())
.pipe(gulp.dest(cssDest));
if (config.watch === true) {
gulp.start('mark-scss-complete');
}
});
gulp.task('sassProdBuild', function () {
console.log(color(`
∴ Cleaning CSS folder
`, prodColor));
gulp.src(cssBundleLocal, {
read: false
})
.pipe(clean({force: true}));
console.log(color(`
∴ Running SCSS -> CSS Production Transpiler
∴ Source Mapping DISABLED
`, prodColor));
gulp.src([
sassBase
])
.pipe(sass())
.pipe(cssnano())
.pipe(concat(cssBundleName))
.pipe(gulp.dest(cssDest));
});
// ----------------------------------------------------------------------
// Mark Operation as Complete.
// ----------------------------------------------------------------------
gulp.task('mark-complete', function() {
console.log(color(`
∴ Build Complete ✓
`, 'BLUE'))
})
gulp.task('mark-ts-complete', function() {
console.log(color(`
∴ TypeScript Re-Transpile Complete ✓
`, 'YELLOW'))
})
gulp.task('mark-scss-complete', function() {
console.log(color(`
∴ SCSS Re-Transpile Complete ✓
`, 'YELLOW'))
})
gulp.task('mark-angular-complete', function() {
console.log(color(`
∴ Angular Re-Transpile Complete ✓
`, 'YELLOW'))
})