-
Notifications
You must be signed in to change notification settings - Fork 68
/
gulpfile.js
1461 lines (1210 loc) · 45 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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const gulp = require('gulp'),
path = require("path"),
sass = require('gulp-sass')(require('node-sass')),
cleanCss = require('gulp-clean-css'),
base64 = require('gulp-base64-v2'),
rename = require('gulp-rename'),
ngConstant = require('gulp-ng-constant'),
fs = require("fs"),
header = require('gulp-header'),
footer = require('gulp-footer'),
removeCode = require('gulp-remove-code'),
removeHtml = require('gulp-html-remove'),
templateCache = require('gulp-angular-templatecache'),
ngTranslate = require('gulp-angular-translate'),
ngAnnotate = require('gulp-ng-annotate'),
zip = require('gulp-zip'),
del = require('del'),
debug = require('gulp-debug'),
useref = require('gulp-useref'),
filter = require('gulp-filter'),
uglify = require('gulp-uglify-es').default,
sourcemaps = require('gulp-sourcemaps'),
lazypipe = require('lazypipe'),
csso = require('gulp-csso'),
replace = require('gulp-replace'),
clean = require('gulp-clean'),
htmlmin = require('gulp-htmlmin'),
jshint = require('gulp-jshint'),
markdown = require('gulp-markdown'),
merge = require('merge2'),
log = require('fancy-log'),
colors = require('ansi-colors'),
{argv} = require('yargs'),
sriHash = require('gulp-sri-hash'),
sort = require('gulp-sort'),
map = require('map-stream');
// Workaround because @ioni/v1-toolkit use gulp v3.9.2 instead of gulp v4
let jsonlint;
try {
jsonlint = require('@prantlf/gulp-jsonlint');
} catch(e) {
log(colors.red("Cannot load 'gulp-jsonlint'. Retrying using project path"), e);
}
const paths = {
license_md: ['./www/license/*.md'],
sass: ['./scss/ionic.app.scss'],
config: ['./app/config.json'],
templatecache: ['./www/templates/**/*.html'],
ng_translate: ['./www/i18n/locale-*.json'],
ng_annotate: ['./www/js/**/*.js', '!./www/js/vendor/*.js'],
// plugins:
leafletSass: ['./scss/leaflet.app.scss'],
css_plugin: ['./www/plugins/*/css/**/*.css'],
templatecache_plugin: ['./www/plugins/*/templates/**/*.html'],
ng_translate_plugin: ['./www/plugins/*/i18n/locale-*.json'],
ng_annotate_plugin: ['./www/plugins/*/**/*.js', '!./www/plugins/*/js/vendor/*.js']
};
const uglifyBaseOptions = {
toplevel: true,
warnings: true,
mangle: {
reserved: ['qrcode', 'Base58']
},
compress: {
global_defs: {
"@console.log": "alert"
},
passes: 2
},
output: {
beautify: false,
preamble: "/* minified */",
max_line_len: 120000
}
};
const cleanCssOptions = {
specialComments: 0 // new name of 'keepSpecialComments', since 4.0
}
const debugBaseOptions = {
title: 'Processing',
minimal: true,
showFiles: argv.debug || false,
showCount: argv.debug || false,
logger: m => log(colors.grey(m))
};
function appAndPluginWatch(done) {
log(colors.green('Watching source files...'));
// Licenses
gulp.watch(paths.license_md, () => appLicense());
// App
gulp.watch(paths.sass, () => appSass());
gulp.watch(paths.templatecache, () => appNgTemplate());
gulp.watch(paths.ng_annotate, (event) => appNgAnnotate(event));
gulp.watch(paths.ng_translate, () => appNgTranslate());
// Plugins
gulp.watch(paths.templatecache_plugin, () => pluginNgTemplate());
gulp.watch(paths.ng_annotate_plugin, (event) => pluginNgAnnotate(event));
gulp.watch(paths.ng_translate_plugin, () => pluginNgTranslate());
gulp.watch(paths.css_plugin.concat(paths.leafletSass), () => pluginSass());
if (done) done();
}
function appAndPluginClean() {
return del([
'./www/dist',
'./www/css/ionic.app*.css',
'./www/css/leaflet.app*.css'
]);
}
/**
* Generate App CSS (using SASS)
* @returns {*}
*/
function appSass() {
log(colors.green('Building App Sass...'));
// Default App style
return gulp.src('./scss/ionic.app.scss')
.pipe(sass()).on('error', sass.logError)
.pipe(base64({
baseDir: "./www/css",
extensions: ['svg', 'png', 'gif', /\.jpg#datauri$/i],
maxImageSize: 14 * 1024
}))
.pipe(gulp.dest('./www/css/'))
.pipe(cleanCss(cleanCssOptions))
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest('./www/css/'));
}
function appConfig() {
const allConfig = JSON.parse(fs.readFileSync('./app/config.json', 'utf8'));
// Determine which environment to use when building config.
const env = argv.env || 'default';
const config = allConfig[env];
if (!config) {
throw new Error(colors.red("=> Could not load `" + env + "` environment!"));
}
log(colors.green("Building App config at `www/js/config.js` for `" + env + "` environment..."));
const project = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
config['version'] = project.version;
config['build'] = (new Date()).toJSON();
config['newIssueUrl'] = project.bugs.new;
return ngConstant({
name: 'cesium.config',
constants: {"csConfig": config},
stream: true,
dest: 'config.js'
})
// Add a warning header
.pipe(header("/******\n* !! WARNING: This is a generated file !!\n*\n* PLEASE DO NOT MODIFY DIRECTLY\n*\n* => Changes should be done on file 'app/config.json'.\n******/\n\n"))
// Writes into file www/js/config.js
.pipe(rename('config.js'))
.pipe(gulp.dest('www/js'));
}
function appConfigTest() {
const allConfig = JSON.parse(fs.readFileSync('./app/config.json', 'utf8'));
// Determine which environment to use when building config.
const env = 'g1-test';
const config = allConfig[env];
if (!config) {
throw new Error(colors.red("=> Could not load `" + env + "` environment!"));
}
log(colors.green("Building App test config at `www/js/config-test.js` for `" + env + "` environment..."));
const project = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
config['version'] = project.version;
config['build'] = (new Date()).toJSON();
config['newIssueUrl'] = project.bugs.new;
return ngConstant({
name: 'cesium.config',
constants: {"csConfig": config},
stream: true,
dest: 'config-test.js'
})
// Add a warning header
.pipe(header("/******\n* !! WARNING: This is a generated file !!\n*\n* PLEASE DO NOT MODIFY DIRECTLY\n*\n* => Changes should be done on file 'app/config.json'.\n******/\n\n"))
// Writes into file www/js/config-test.js
.pipe(rename('config-test.js'))
.pipe(gulp.dest('www/js'));
}
function appAndPluginLint() {
log(colors.green('Linting JS files...'));
// Copy Js (and remove unused code)
return gulp.src(paths.ng_annotate.concat(paths.ng_annotate_plugin))
.pipe(debug({...debugBaseOptions, title: 'Linting'}))
.pipe(jshint())
.pipe(jshint.reporter(require('jshint-stylish')))
.pipe(map( (file, cb) => {
if (!file.jshint.success) {
console.error('jshint failed');
process.exit(1);
}
cb();
}));
}
function appNgTemplate() {
log(colors.green('Building template file...'));
return gulp.src(paths.templatecache)
.pipe(sort())
.pipe(templateCache({
standalone: true,
module: "cesium.templates",
root: "templates/"
}))
.pipe(gulp.dest('./www/dist/dist_js/app'));
}
function appNgAnnotate(event) {
// If watch, apply only on changes files
if (event && event.type === 'changed' && event.path && event.path.indexOf('/www/js/') !== -1) {
let path = event.path.substring(event.path.indexOf('/www/js') + 7);
path = path.substring(0, path.lastIndexOf('/'));
return gulp.src(event.path)
.pipe(ngAnnotate({single_quotes: true}))
.pipe(gulp.dest('./www/dist/dist_js/app' + path));
}
log(colors.green('Building JS files...'));
return gulp.src(paths.ng_annotate)
.pipe(ngAnnotate({single_quotes: true}))
.pipe(gulp.dest('./www/dist/dist_js/app'));
}
function appNgTranslate() {
log(colors.green('Building translation file...'));
return gulp.src('www/i18n/locale-*.json')
.pipe(jsonlint())
.pipe(jsonlint.reporter())
.pipe(sort())
.pipe(ngTranslate({standalone:true, module: 'cesium.translations'}))
.pipe(gulp.dest('www/dist/dist_js/app'));
}
function appLicense() {
log(colors.green('Building License files...'));
return merge(
// Copy license into HTML
gulp.src(paths.license_md)
.pipe(markdown())
.pipe(header('<html><header><meta charset="utf-8"></header><body>'))
.pipe(footer('</body></html>'))
.pipe(gulp.dest('www/license')),
// Copy license into txt
gulp.src(paths.license_md)
.pipe(header('\ufeff')) // Need BOM character for UTF-8 files
.pipe(rename({ extname: '.txt' }))
.pipe(gulp.dest('www/license'))
);
}
/* -- Plugins -- */
function pluginNgTemplate() {
log(colors.green('Building Plugins template file...'));
return gulp.src(paths.templatecache_plugin)
.pipe(sort())
.pipe(templateCache({
standalone: true,
module: "cesium.plugins.templates",
root: "plugins/"
}))
.pipe(gulp.dest('./www/dist/dist_js/plugins'));
}
function pluginNgAnnotate(event) {
// If watch, apply only on changes files
if (event && event.type === 'changed' && event.path && event.path.indexOf('/www/js/') !== -1) {
let path = event.path.substring(event.path.indexOf('/www/js') + 7);
path = path.substring(0, path.lastIndexOf('/'));
return gulp.src(event.path)
.pipe(ngAnnotate({single_quotes: true}))
.pipe(gulp.dest('./www/dist/dist_js/app' + path));
}
log(colors.green('Building Plugins JS file...'));
return gulp.src(paths.ng_annotate_plugin)
.pipe(ngAnnotate({single_quotes: true}))
.pipe(gulp.dest('./www/dist/dist_js/plugins'));
}
function pluginNgTranslate() {
log(colors.green('Building Plugins translation file...'));
return gulp.src(paths.ng_translate_plugin)
.pipe(jsonlint())
.pipe(jsonlint.reporter())
.pipe(sort())
.pipe(ngTranslate({standalone:true, module: 'cesium.plugins.translations'}))
.pipe(gulp.dest('www/dist/dist_js/plugins'));
}
function pluginLeafletImages(dest) {
dest = dest || './www/img/';
// Leaflet images
return gulp.src(['scss/leaflet/images/*.*',
'www/lib/leaflet/dist/images/*.*',
'www/lib/leaflet-search/images/*.*',
'!www/lib/leaflet-search/images/back.png',
'!www/lib/leaflet-search/images/leaflet-search.jpg',
'www/lib/leaflet.awesome-markers/dist/images/*.*'],
{read: false, allowEmpty: true}
)
.pipe(gulp.dest(dest));
}
function pluginSass() {
log(colors.green('Building Plugins Sass...'));
return merge(
// Copy plugins CSS
gulp.src(paths.css_plugin)
.pipe(gulp.dest('www/dist/dist_css/plugins')),
// Leaflet images
pluginLeafletImages('./www/img/'),
// Leaflet App style
gulp.src('./scss/leaflet.app.scss')
.pipe(sass()).on('error', sass.logError)
// Fix bad images path
.pipe(replace("url('../images/", "url('../img/"))
.pipe(replace("url(\"../images/", "url(\"../img/"))
.pipe(replace("url('images/", "url('../img/"))
.pipe(replace("url(\"images/", "url(\"../img/"))
.pipe(replace("url(images/", "url(../img/"))
.pipe(base64({
baseDir: "./www/css/",
extensions: ['png', 'gif', /\.jpg#datauri$/i],
maxImageSize: 14 * 1024,
deleteAfterEncoding: false
}))
.pipe(gulp.dest('./www/css/'))
.pipe(cleanCss(cleanCssOptions))
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest('./www/css/'))
);
}
/* --------------------------------------------------------------------------
-- Build the web (ZIP) artifact
--------------------------------------------------------------------------*/
function webClean() {
return del([
'./dist/web/www'
]);
}
function webCopyFiles() {
log(colors.green('Preparing dist/web files...'));
let htmlminOptions = {removeComments: true, collapseWhitespace: true};
const debugOptions = { ...debugBaseOptions, title: 'Copying' };
var targetPath = './dist/web/www';
return merge(
// Copy Js (and remove unused code)
gulp.src('./www/js/**/*.js')
.pipe(debug(debugOptions))
.pipe(removeCode({"no-device": true}))
.pipe(jshint())
.pipe(gulp.dest(targetPath + '/js')),
// Copy HTML templates (and remove unused code)
gulp.src('./www/templates/**/*.html')
.pipe(removeCode({"no-device": true}))
.pipe(removeHtml('.hidden-no-device'))
.pipe(removeHtml('[remove-if][remove-if="no-device"]'))
.pipe(htmlmin(htmlminOptions))
.pipe(gulp.dest(targetPath + '/templates')),
// Copy index.html (and remove unused code)
gulp.src('./www/index.html')
.pipe(removeCode({'no-device': true}))
.pipe(removeHtml('.hidden-no-device'))
.pipe(removeHtml('[remove-if][remove-if="no-device"]'))
.pipe(htmlmin(/*no options, to keep comments*/))
.pipe(gulp.dest(targetPath)),
// Copy API index.html
gulp.src('./www/api/index.html')
.pipe(removeCode({'no-device': true}))
.pipe(removeHtml('.hidden-no-device'))
.pipe(removeHtml('[remove-if][remove-if="no-device"]'))
.pipe(htmlmin())
.pipe(gulp.dest(targetPath + '/api')),
// Copy config-test.js
gulp.src('./www/js/config*.js')
.pipe(debug(debugOptions))
.pipe(gulp.dest(targetPath)),
// Copy fonts
gulp.src('./www/fonts/**/*.*')
.pipe(debug(debugOptions))
.pipe(gulp.dest(targetPath + '/fonts')),
// Copy CSS
gulp.src('./www/css/**/*.*')
.pipe(debug(debugOptions))
.pipe(gulp.dest(targetPath + '/css')),
// Copy i18n
gulp.src('./www/i18n/locale-*.json')
.pipe(jsonlint())
.pipe(jsonlint.reporter())
.pipe(sort())
.pipe(ngTranslate({standalone:true, module: 'cesium.translations'}))
.pipe(debug(debugOptions))
.pipe(gulp.dest(targetPath + '/js')),
// Copy img
gulp.src('./www/img/**/*.*')
.pipe(debug(debugOptions))
.pipe(gulp.dest(targetPath + '/img')),
// Copy manifest.json
gulp.src('./www/manifest.json')
.pipe(debug(debugOptions))
.pipe(gulp.dest(targetPath)),
// Copy lib (JS, CSS and fonts)
gulp.src(['./www/lib/**/*.js', './www/lib/**/*.css', './www/lib/**/fonts/**/*.*'])
.pipe(debug(debugOptions))
.pipe(gulp.dest(targetPath + '/lib')),
// Copy license into HTML
gulp.src('./www/license/*.md')
.pipe(markdown())
.pipe(header('<html><header><meta charset="utf-8"></header><body>'))
.pipe(footer('</body></html>'))
.pipe(gulp.dest(targetPath + '/license')),
// Copy license into txt
gulp.src('./www/license/*.md')
.pipe(header('\ufeff')) // Need BOM character for UTF-8 files
.pipe(rename({ extname: '.txt' }))
.pipe(gulp.dest(targetPath + '/license'))
);
}
function webNgTemplate() {
var targetPath = './dist/web/www';
return gulp.src(targetPath + '/templates/**/*.html')
.pipe(sort())
.pipe(templateCache({
standalone: true,
module: "cesium.templates",
root: "templates/"
}))
.pipe(gulp.dest(targetPath + '/dist/dist_js/app'));
}
function webAppNgAnnotate() {
var targetPath = './dist/web/www';
var jsFilter = filter(["**/*.js", "!**/vendor/*"]);
return gulp.src(targetPath + '/js/**/*.js')
.pipe(jsFilter)
.pipe(ngAnnotate({single_quotes: true}))
.pipe(gulp.dest(targetPath + '/dist/dist_js/app'));
}
function webPluginCopyFiles() {
const targetPath = './dist/web/www';
return merge(
// Copy Js (and remove unused code)
gulp.src('./www/plugins/**/*.js')
.pipe(removeCode({"no-device": true}))
.pipe(jshint())
.pipe(gulp.dest(targetPath + '/plugins')),
// Copy HTML templates (and remove unused code)
gulp.src('./www/plugins/**/*.html')
.pipe(removeCode({"no-device": true}))
.pipe(removeHtml('.hidden-no-device'))
.pipe(removeHtml('[remove-if][remove-if="no-device"]'))
.pipe(htmlmin())
.pipe(gulp.dest(targetPath + '/plugins')),
// Transform i18n into JS
gulp.src(paths.ng_translate_plugin)
.pipe(jsonlint())
.pipe(jsonlint.reporter())
.pipe(sort())
.pipe(ngTranslate({standalone:true, module: 'cesium.plugins.translations'}))
.pipe(gulp.dest(targetPath + '/dist/dist_js/plugins')),
// Copy plugin CSS
gulp.src(paths.css_plugin)
.pipe(gulp.dest(targetPath + '/dist/dist_css/plugins')),
// Copy Leaflet images
pluginLeafletImages(targetPath + '/img'),
// Copy Leaflet CSS
gulp.src('./www/css/**/leaflet.*')
.pipe(gulp.dest(targetPath + '/css'))
);
}
function webPluginNgTemplate() {
var targetPath = './dist/web/www';
return gulp.src(targetPath + '/plugins/**/*.html')
.pipe(sort())
.pipe(templateCache({
standalone: true,
module: "cesium.plugins.templates",
root: "plugins/"
}))
.pipe(gulp.dest(targetPath + '/dist/dist_js/plugins'));
}
function webPluginNgAnnotate() {
var targetPath = './dist/web/www';
return gulp.src(targetPath + '/plugins/**/*.js')
.pipe(ngAnnotate({single_quotes: true}))
.pipe(gulp.dest(targetPath + '/dist/dist_js/plugins'));
}
function webUglify() {
const targetPath = './dist/web/www';
const enableUglify = argv.release || argv.uglify || false;
const version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
if (enableUglify) {
log(colors.green('Minify JS and CSS files...'));
const indexFilter = filter('**/index.html', {restore: true});
const jsFilter = filter(["**/*.js", '!**/config.js', '!**/config-test.js'], {restore: true});
const cssFilter = filter("**/*.css", {restore: true});
// Process index.html
return gulp.src(targetPath + '/index.html')
.pipe(useref({}, lazypipe().pipe(sourcemaps.init, { loadMaps: true }))) // Concatenate with gulp-useref
// Process JS
.pipe(jsFilter)
.pipe(uglify(uglifyBaseOptions)) // Minify javascript files
.pipe(jsFilter.restore)
// Process CSS
.pipe(cssFilter)
.pipe(csso()) // Minify any CSS sources
.pipe(cssFilter.restore)
// Add version to file path
.pipe(indexFilter)
.pipe(replace(/"(dist_js\/[a-zA-Z0-9]+).js"/g, '"$1.js?v=' + version + '"'))
.pipe(replace(/"(dist_css\/[a-zA-Z0-9]+).css"/g, '"$1.css?v=' + version + '"'))
.pipe(indexFilter.restore)
.pipe(sourcemaps.write('maps'))
.pipe(gulp.dest(targetPath));
}
else {
return Promise.resolve();
}
}
function webIntegrity() {
const targetPath = './dist/web/www';
const enableIntegrity = argv.release || false;
if (enableIntegrity) {
log(colors.green('Create index.integrity.html... '));
// Process index.html
return gulp.src(targetPath + '/index.html', {base: targetPath})
// Add an integrity hash
.pipe(sriHash())
.pipe(rename({ extname: '.integrity.html' }))
.pipe(gulp.dest(targetPath));
}
else {
return Promise.resolve();
}
}
function webApiUglify() {
const targetPath = './dist/web/www';
const version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
const jsFilter = filter(["**/*.js", '!**/config.js', '!**/config-test.js'], {restore: true});
const cssFilter = filter("**/*.css", {restore: true});
const indexFilter = filter('**/index.html', {restore: true});
// Skip if not required
const enableUglify = argv.release || argv.uglify || false;
if (enableUglify) {
log(colors.green('API: Minify JS and CSS files...'));
// Process api/index.html
return gulp.src(targetPath + '/*/index.html')
.pipe(useref({}, lazypipe().pipe(sourcemaps.init, { loadMaps: true }))) // Concatenate with gulp-useref
// Process JS
.pipe(jsFilter)
.pipe(uglify(uglifyBaseOptions)) // Minify any javascript sources
.pipe(jsFilter.restore)
// Process CSS
.pipe(cssFilter)
.pipe(csso()) // Minify any CSS sources
.pipe(cssFilter.restore)
.pipe(indexFilter)
// Add version to files path
.pipe(replace(/"(dist_js\/[a-zA-Z0-9-.]+).js"/g, '"$1.js?v=' + version + '"'))
.pipe(replace(/"(dist_css\/[a-zA-Z0-9-.]+).css"/g, '"$1.css?v=' + version + '"'))
.pipe(replace("dist_js", "../dist_js"))
.pipe(replace("dist_css", "../dist_css"))
.pipe(replace("config.js", "../config.js"))
.pipe(replace("config-test.js", "../config-test.js"))
.pipe(indexFilter.restore)
.pipe(sourcemaps.write('maps'))
.pipe(gulp.dest(targetPath));
}
else {
log(colors.red('API: Skipping minify JS and CSS files') + colors.grey(' (missing options --release or --uglify)'));
return gulp.src(targetPath + '/*/index.html')
.pipe(useref()) // Concatenate with gulp-useref
.pipe(indexFilter)
.pipe(replace("dist_js", "../dist_js"))
.pipe(replace("dist_css", "../dist_css"))
.pipe(replace("config.js", "../config.js"))
.pipe(replace("config-test.js", "../config-test.js"))
.pipe(indexFilter.restore)
.pipe(gulp.dest(targetPath));
}
}
function webCleanUnusedFiles(done) {
log(colors.green('Clean unused files...'));
const targetPath = './dist/web/www';
const enableUglify = argv.release || argv.uglify || false;
const cleanSources = enableUglify;
const debugOptions = {...debugBaseOptions,
title: 'Deleting',
showCount: !argv.debug
};
if (cleanSources) {
return merge(
// Clean core JS
gulp.src(targetPath + '/js/**/*.js', {read: false})
.pipe(debug(debugOptions))
.pipe(clean()),
// Clean core CSS
gulp.src(targetPath + '/css/**/*.css', {read: false})
.pipe(debug(debugOptions))
.pipe(clean()),
// Clean plugins JS + CSS
gulp.src(targetPath + '/plugins/**/*.js', {read: false})
.pipe(debug(debugOptions))
.pipe(clean()),
gulp.src(targetPath + '/plugins/**/*.css', {read: false})
.pipe(debug(debugOptions))
.pipe(clean()),
// Unused maps/config.js.map
gulp.src(targetPath + '/maps/config.js.map', {read: false, allowEmpty: true})
.pipe(debug(debugOptions))
.pipe(clean()),
gulp.src(targetPath + '/maps/config-test.js.map', {read: false, allowEmpty: true})
.pipe(debug(debugOptions))
.pipe(clean())
)
.on('end', done);
}
if (done) done();
}
function webCleanUnusedDirectories() {
log(colors.green('Clean unused directories...'));
const enableUglify = argv.release || argv.uglify || false;
const debugOptions = { ...debugBaseOptions,
title: 'Deleting',
showCount: !argv.debug
};
// Clean dir
const wwwPath = './dist/web/www';
let patterns = [
wwwPath + '/templates',
wwwPath + '/plugins'
];
if (enableUglify) {
patterns = patterns.concat([
wwwPath + '/js',
wwwPath + '/css',
wwwPath + '/dist',
wwwPath + '/lib/*',
// Keep IonIcons font
'!' + wwwPath + '/lib/ionic',
wwwPath + '/lib/ionic/*',
'!' + wwwPath + '/lib/ionic/fonts',
// Keep RobotoDraft font
'!' + wwwPath + '/lib/robotodraft',
wwwPath + '/lib/robotodraft/*',
'!' + wwwPath + '/lib/robotodraft/fonts'
]);
}
else {
patterns = patterns.concat([
wwwPath + '/js/*',
'!' + wwwPath + '/js/vendor',
wwwPath + '/dist_css',
wwwPath + '/dist_js'
]);
}
return gulp.src(patterns, {read: false, allowEmpty: true})
.pipe(debug(debugOptions))
.pipe(clean());
}
function webZip() {
const tmpPath = './dist/web/www';
const version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
const txtFilter = filter(["**/*.txt"], { restore: true });
return gulp.src(tmpPath + '/**/*.*')
// Process TXT files: Add the UTF-8 BOM character
.pipe(txtFilter)
.pipe(header('\ufeff'))
.pipe(txtFilter.restore)
.pipe(zip('cesium-v'+version+'-web.zip'))
.pipe(gulp.dest('./dist/web/build'));
}
function webExtClean() {
return del([
'./dist/web/ext'
]);
}
function webExtCopyFiles() {
const wwwPath = './dist/web/www';
const resourcesPath = './resources/web-ext';
log(colors.green('Copy web extension files...'));
const version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
const manifestFilter = filter(["**/manifest.json"], { restore: true });
const txtFilter = filter(["**/*.txt"], { restore: true });
// Copy files
return gulp.src([
wwwPath + '/**/*',
// Skip API files
'!' + wwwPath + '/api',
'!' + wwwPath + '/dist_js/*-api.js',
'!' + wwwPath + '/dist_css/*-api.css',
'!' + wwwPath + '/maps/dist_js/*-api.js.map',
'!' + wwwPath + '/maps/dist_css/*-api.css.map',
// Skip web manifest
'!' + wwwPath + '/manifest.json',
// Add specific resource (and overwrite the default 'manifest.json')
resourcesPath + '/**/*.*'
])
// Process TXT files: Add the UTF-8 BOM character
.pipe(txtFilter)
.pipe(header('\ufeff'))
.pipe(txtFilter.restore)
// Replace version in 'manifest.json' file
.pipe(manifestFilter)
.pipe(replace(/\"version\": \"[^\"]*\"/, '"version": "' + version + '"'))
.pipe(manifestFilter.restore)
.pipe(gulp.dest('./dist/web/ext'));
}
function webExtensionZip() {
const srcPath = './dist/web/ext';
const distPath = './dist/web/build';
const version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
return gulp.src(srcPath + '/**/*.*')
.pipe(zip('cesium-v'+version+'-extension.zip'))
.pipe(gulp.dest(distPath));
}
function webBuildSuccess(done) {
var version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
log(colors.green("Web artifact created at: 'dist/web/build/cesium-v" + version + "-web.zip'"));
if (done) done();
}
function webExtBuildSuccess(done) {
var version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
log(colors.green("Web extension artifact created at: 'dist/web/build/cesium-v" + version + "-extension.zip'"));
if (done) done();
}
function cdvAddPlatformToBodyTag() {
log(colors.green('Add platform CSS class to <body>... '));
const projectRoot = argv.root || '.';
const platform = argv.platform || 'android';
let wwwPath;
if (platform === 'android') {
wwwPath = path.join(projectRoot, 'platforms', platform, 'app','src','main','assets','www');
} else {
wwwPath = path.join(projectRoot, 'platforms', platform, 'www');
}
const indexPath = path.join(wwwPath, 'index.html');
// no opening body tag, something's wrong
if (!fs.existsSync(indexPath)) throw new Error('Unable to find the file ' + indexPath +'!');
// add the platform class to the body tag
try {
const platformClass = 'platform-' + platform;
const cordovaClass = 'platform-cordova platform-webview';
let html = fs.readFileSync(indexPath, 'utf8');
// get the body tag
let matches = html && html.match(/<body[^>/]+>/gi)
const bodyTag = matches && matches[0];
// no opening body tag, something's wrong
if (!bodyTag) throw new Error('No <body> element found in file ' + indexPath);
if (bodyTag.indexOf(platformClass) > -1) return; // already added
let newBodyTag = '' + bodyTag;
matches = bodyTag.match(/ class=["|'](.*?)["|']/gi);
const classAttr = matches && matches[0];
if (classAttr) {
// body tag has existing class attribute, add the classname
let endingQuote = classAttr.substring(classAttr.length - 1);
let newClassAttr = classAttr.substring(0, classAttr.length - 1);
newClassAttr += ' ' + platformClass + ' ' + cordovaClass + endingQuote;
newBodyTag = newBodyTag.replace(classAttr, newClassAttr);
} else {
// add class attribute to the body tag
newBodyTag = newBodyTag.replace('>', ' class="' + platformClass + ' ' + cordovaClass + '">');
}
html = html.replace(bodyTag, newBodyTag);
fs.writeFileSync(indexPath, html, 'utf8');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
}
function cdvNgAnnotate() {
log(colors.green('Building JS files... '));
const projectRoot = argv.root || '.';
const platform = argv.platform || 'android';
let wwwPath;
if (platform === 'android') {
wwwPath = path.join(projectRoot, 'platforms', platform, 'app','src','main','assets','www');
} else {
wwwPath = path.join(projectRoot, 'platforms', platform, 'www');
}
const jsFilter = filter(["**/*.js", "!**/vendor/*"]);
return merge(
// Ng annotate app JS file
gulp.src(wwwPath + '/js/**/*.js')
.pipe(jsFilter)
.pipe(ngAnnotate({single_quotes: true}))
.pipe(gulp.dest(wwwPath + '/dist/dist_js/app')),
// Ng annotate app JS file
gulp.src(wwwPath + '/plugins/**/*.js')
.pipe(ngAnnotate({single_quotes: true}))
.pipe(gulp.dest(wwwPath + '/dist/dist_js/plugins'))
);
}
function cdvRemoveCode() {
log(colors.green('Removing code... '));
const projectRoot = argv.root || '.';
const platform = argv.platform || 'android';
let wwwPath;
if (platform === 'android') {
wwwPath = path.join(projectRoot, 'platforms', platform, 'app','src','main','assets','www');
} else {
wwwPath = path.join(projectRoot, 'platforms', platform, 'www');
}
const appJsPath = [path.join(wwwPath, 'js', '**', '*.js'),
// Exclude vendor libs
"!" + path.join(wwwPath, 'js', 'vendor', '**', '*.js')];
const pluginPath = path.join(wwwPath, 'plugins', '*');
const pluginJsPath = path.join(pluginPath, '**', '*.js');
// Compute options {device-<platform>: true}
let removeCodeOptions = {};
removeCodeOptions[platform] = true; // = {<platform>: true}
const htmlminOptions = {removeComments: true, collapseWhitespace: true};
const debugOptions = {...debugBaseOptions,
showCount: false
};
// Do not remove desktop code for iOS and macOS (support for tablets and desktop macs)
if (platform !== 'ios' && platform !== 'osx') {
// Removing unused code for device...
return merge(
// Remove unused HTML tags
gulp.src(path.join(wwwPath, 'templates', '**', '*.html'))
.pipe(debug(debugOptions))
.pipe(removeCode({device: true}))
.pipe(removeCode(removeCodeOptions))
.pipe(removeHtml('.hidden-xs.hidden-sm'))
.pipe(removeHtml('.hidden-device'))
.pipe(removeHtml('[remove-if][remove-if="device"]'))
.pipe(htmlmin(htmlminOptions))
.pipe(gulp.dest(wwwPath + '/templates')),
gulp.src(path.join(pluginPath, '**', '*.html'))
.pipe(debug(debugOptions))
.pipe(removeCode({device: true}))
.pipe(removeCode(removeCodeOptions))
.pipe(removeHtml('.hidden-xs.hidden-sm'))
.pipe(removeHtml('.hidden-device'))
.pipe(removeHtml('[remove-if][remove-if="device"]'))
.pipe(htmlmin(htmlminOptions))
.pipe(gulp.dest(pluginPath)),
gulp.src(path.join(wwwPath, 'index.html'))
.pipe(debug(debugOptions))