-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
686 lines (618 loc) · 27.2 KB
/
Gruntfile.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
(function () {
'use strict';
module.exports = function(grunt) {
require('time-grunt')(grunt);
grunt.initConfig();
(function initGruntConfig(){
// Custom project config
// Values are accessible through:
// - Grunt templates, eg: '<%= projectConfig.liveReloadPath %>'
// - Grunt API, eg: grunt.config.get('projectConfig').liveReloadPath
grunt.config.set('projectConfig', grunt.file.readJSON('./app.config.json'));
// Environment choice is passed through CLI (`grunt --env=production`).
// Default environment is 'dev', which should be the first of definied environments
grunt.config.set('envId', grunt.option('env') || Object.keys(grunt.config.get('projectConfig').envs)[0]);
grunt.log.writeln('Environment id: ', grunt.config.get('envId'));
// 'currEnvConfig' - helper var for better readibility in tasks
grunt.config.set('currEnvConfig', grunt.config.get('projectConfig').envs[grunt.config.get('envId')]);
// Temporary file path that html2js will output to
grunt.config.set('projectConfig.compiledTemplatesPath', grunt.config.get('currEnvConfig').destDir + '/' + grunt.config.get('projectConfig').tempDir + '/compiledTemplates.js');
// Build markup of Angular module with config vars passed per environment from Gruntfile.js
// Markup will be injected into index.html
(function buildAngularConfig() {
var constantName = 'ENV';
var configValues = {
name: 'development',
apiEndpoint: grunt.config.get('currEnvConfig').apiEndpoint
};
grunt.config.set(
'angularConfigMarkup',
'<script>angular.module("config", []).constant("' + constantName + '", ' + JSON.stringify(configValues) + ');</script>'
);
})();
(function generateJsMarkup() {
var JSheadOutputList = [],
JSbodyOutputList = [];
if(grunt.config.get('envId') === 'dev') {
JSheadOutputList = [
'head'
];
JSbodyOutputList = [
'liveReload',
'body',
'templatesPlaceholder',
'angularConfig'
];
}
else if(grunt.config.get('envId') === 'local' || grunt.config.get('envId') === 'staging' || grunt.config.get('envId') === 'production') {
JSheadOutputList = [
'headJsMin'
];
JSbodyOutputList = [
'bodyJsMin'
];
}
else {
grunt.log.error('refreshBodyEndJsMarkup() failed to recognize envId: ' + envId);
}
grunt.config.set('JSheadMarkup', generateListOfJS(JSheadOutputList, {returnAsMarkup: true}));
grunt.config.set('JSbodyMarkup', generateListOfJS(JSbodyOutputList, {returnAsMarkup: true}));
})();
})();
// Helper funtion which generates list of JS scripts
// based on javascripts.config.json and blocks of inline code defined inside this function.
// Example: generateListOfJS(['compiledTemplates', 'body'], {returnAsMarkup: true});
function generateListOfJS(typesFilter, options) {
var scriptsConfig = grunt.file.readJSON(grunt.config.get('projectConfig').scriptsConfigPath);
var appScriptsBasePath = '';
var compiledTemplates = {type: 'path', value: grunt.config.get('projectConfig').compiledTemplatesPath};
var scriptsCategorized = {};
var inlineCodeBlocks = {};
var outputScripts = [];
options = options || {};
// Adding base path is required when list is returned for a task (eg. uglify) rather than for index.html
if(typeof options.globalBasePath === 'string'){
appScriptsBasePath = options.globalBasePath + '/';
// note: compiled templates are not subject to prefixing with base path
}
typesFilter = typesFilter || [];
// Build lists, append base path
// Vendor
scriptsCategorized.vendor = (function() {
var fromHead = getScripts(scriptsConfig.head, true);
var fromBody = getScripts(scriptsConfig.body, true);
return fromHead.concat(fromBody);
})();
// Compiled templates (html2js)
scriptsCategorized.compiledTemplates = [compiledTemplates];
// JS code in head section
scriptsCategorized.head = getScripts(scriptsConfig.head);
// JS code in body section
scriptsCategorized.body = getScripts(scriptsConfig.body);
// Angular config module with constants
scriptsCategorized.config = {
type: 'path',
value: '<%= currEnvConfig.destDir %>/<%= projectConfig.tempDir %>/config.js'
};
// Generate inline code blocks
inlineCodeBlocks.angularConfig = {type: 'code', value: grunt.config.get('angularConfigMarkup')};
inlineCodeBlocks.templatesPlaceholder = {type: 'code', value: '<script>angular.module(\'templates-dist\', []);</script>'};
inlineCodeBlocks.liveReload = {type: 'code', value: '<script src="<%= projectConfig.liveReloadPath %>"></script>'};
inlineCodeBlocks.headJsMin = {type: 'code', value: '<script src="js/initial.min.js"></script>'};
inlineCodeBlocks.bodyJsMin = {type: 'code', value: '<script src="js/main.min.js"></script>'};
// Types should be implicitely given
if (!typesFilter.length) {
grunt.log.warn('typesFilter in generateListOfJS() is empty. Returning an empty list.');
}
// Build list of scripts in the order given by filter
else {
typesFilter.forEach(function(type){
if(scriptsCategorized.hasOwnProperty(type)) {
outputScripts = outputScripts.concat(scriptsCategorized[type]);
}
else if(inlineCodeBlocks.hasOwnProperty(type)){
outputScripts = outputScripts.concat(inlineCodeBlocks[type]);
}
else {
grunt.log.error('generateListOfJS() is skipping scripts of "' + type +'" type, which was not recognized.');
}
});
}
return formatOutput(outputScripts);
// Convert to string of <script> tags when necessary
function formatOutput(items) {
// Convert each item in array to its output format
items = items.map(function(item){
if (item.type === 'path') {
var src = item.value.substring(0, 3) === 'src' ? item.value.substring(4, item.value.length) : item.value;
return options.returnAsMarkup === true ? '<script src="' + src + '"></script>' : item.value;
}
else if (item.type === 'code'){
return item.value;
}
grunt.log.error('formatOutput() failed to parse item: ' + JSON.stringify(item));
return '';
});
// Convert array to its output format
if (options.returnAsMarkup === true) {
items = items.join('\r\n');
}
return items;
}
function getScripts(files, vendorOnly) {
var output = [];
if (vendorOnly) {
files.map(function(filePath) {
if (filePath.substring(0, 7) === 'vendor/') {
output.push({
type: 'path',
value: filePath
});
}
});
} else {
files.map(function(filePath) {
output.push({
type: 'path',
value: filePath
});
});
}
return output;
}
}
////////////////////////////////////////////////////////////////////////////
// Tasks definitions
////////////////////////////////////////////////////////////////////////////
// JS linting
// https://www.npmjs.com/package/grunt-contrib-jshint
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.config('jshint', {
options: {
jshintrc: true
},
gruntfile: {
src: ['Gruntfile.js']
},
app: {
src: [
'<%= projectConfig.srcDir %>/js/**/*.js'
]
},
unittests: {
src: [
//'<%= projectConfig.srcDir %>/test/**/*.js' // TODO uncomment when jshint errors are fixed in the boilerplate
]
}
});
// Running unit tests via Karma runner
// https://www.npmjs.com/package/grunt-karma
grunt.loadNpmTasks('grunt-karma');
grunt.config('karma', {
options: {
configFile: 'karma.conf.js',
files: (function getKarmaFilesList() {
var karmaFiles = generateListOfJS(['body']);
// Add angular-mocks.js for Karma directly after angular.js (which should be the first item)
karmaFiles.splice(1, 0, ['vendor/angular-mocks/angular-mocks.js']);
return karmaFiles.concat([
'<%= projectConfig.srcDir %>/test/**/*.js'
]);
})()
},
unit: {
port: 9019,
background: true
},
continuous: {
singleRun: true
}
});
// Sass file compilation and compression
// https://www.npmjs.com/package/grunt-sass
// https://github.com/sass/node-sass#options
grunt.loadNpmTasks('grunt-sass');
grunt.config('sass', {
options: {
precision: 2,
sourceMap: true,
outFile: '<%= currEnvConfig.destDir %>/css'
},
dev: {
options: {
outputStyle: 'nested'
},
files: [{
expand: true,
cwd: '<%= projectConfig.srcDir %>/scss',
src: ['*.scss'],
dest: '<%= currEnvConfig.destDir %>/css',
ext: '.css'
}]
},
nondev: {
options: {
outputStyle: 'compressed'
},
files: [{
expand: true,
cwd: '<%= projectConfig.srcDir %>/scss',
src: ['*.scss'],
dest: '<%= currEnvConfig.destDir %>/css',
ext: '.css'
}]
}
});
// Adding autoprefixes to CSS files
// https://www.npmjs.com/package/grunt-autoprefixer
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.config('autoprefixer', {
options: {
browsers: ['last 2 versions'],
map: true
},
dist: {
src: '<%= currEnvConfig.destDir %>/css/*.css'
}
});
// Deleting outdated files
// https://www.npmjs.com/package/grunt-contrib-clean
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.config('clean', {
all: [
'<%= currEnvConfig.destDir %>/**/*'
],
js: [
'<%= currEnvConfig.destDir %>/js'
],
css: [
'<%= currEnvConfig.destDir %>/css'
],
destTemp: [
'<%= currEnvConfig.destDir %>/<%= projectConfig.tempDir %>'
],
initialFiles: [
'<%= currEnvConfig.destDir %>/js/**/initial.js'
]
});
// Replace '@@JSbody' tag with list of JS dependencies generated by generateListOfJS()
// https://www.npmjs.com/package/grunt-replace
grunt.loadNpmTasks('grunt-replace');
grunt.config('replace', {
dist: {
options: {
patterns: (function() {
var replacements = [];
var jsHead = generateListOfJS(['head'], {globalBasePath: 'src'});
replacements.push({
match: 'JShead',
replacement: jsHead.length > 0 ? '<%= JSheadMarkup %>\r\n' : ''
});
replacements.push({
match: 'JSbody',
replacement: '<%= JSbodyMarkup %>\r\n'
});
replacements.push({
match: 'metaNoIndex',
replacement: (grunt.config.get('envId') === 'dev' || grunt.config.get('envId') === 'local' || grunt.config.get('envId') === 'staging') ? '<meta name="robots" content="noindex,nofollow">' : ''
});
return replacements;
})()
},
files: [{
expand: true,
flatten: true,
src: ['<%= projectConfig.srcDir %>/*.html'],
dest: '<%= currEnvConfig.destDir %>'
}]
}
});
// https://www.npmjs.com/package/grunt-html2js
grunt.loadNpmTasks('grunt-html2js');
grunt.config('html2js', {
options: {
useStrict: true
},
dist: {
src: ['<%= projectConfig.srcDir %>/templates/**/*.tpl.html'],
dest: '<%= projectConfig.compiledTemplatesPath %>'
}
});
// JS file obfuscation
// https://www.npmjs.com/package/grunt-contrib-uglify
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.config('uglify', {
options: {
mangle: {
except: ['angular']
},
screwIE8: true,
sourceMap: true
},
jsHead: {
src: generateListOfJS(['head'], {globalBasePath: 'src'}),
dest: '<%= currEnvConfig.destDir %>/js/initial.min.js'
},
jsBody: {
src: generateListOfJS(['body', 'compiledTemplates', 'config'], {globalBasePath: 'src'}),
dest: '<%= currEnvConfig.destDir %>/js/main.min.js'
}
});
// Constants
// https://www.npmjs.com/package/grunt-ng-constant
grunt.loadNpmTasks('grunt-ng-constant');
grunt.config('ngconstant', {
// Options for all targets
options: {
space: ' ',
wrap: '"use strict";\n\n {%= __ngModule %}',
name: 'config'
},
// Environment targets
dist: {
options: {
dest: '<%= currEnvConfig.destDir %>/<%= projectConfig.tempDir %>/config.js'
},
constants: {
ENV: grunt.config.get('currEnvConfig')
}
}
});
// Copy files from source dir to build dir. Used when no JS uglyfying is applied.
// https://www.npmjs.com/package/grunt-contrib-copy
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.config('copy', {
js: {
files: [{
expand: true,
cwd: '<%= projectConfig.srcDir %>/js',
src: ['**'],
dest: '<%= currEnvConfig.destDir %>/js'
}]
},
templates: {
files: [{
expand: true,
cwd: '<%= projectConfig.srcDir %>/templates',
src: ['**'],
dest: '<%= currEnvConfig.destDir %>/templates'
}]
},
vendor: {
files: [{
expand: true,
src: generateListOfJS(['vendor']),
dest: '<%= currEnvConfig.destDir %>'
}]
},
img: {
files: [{
expand: true,
cwd: '<%= projectConfig.srcDir %>/img',
src: ['**/*'],
dest: '<%= currEnvConfig.destDir %>/img'
}]
},
fonts: {
files: [{
expand: true,
cwd: '<%= projectConfig.srcDir %>/fonts',
src: ['**/*'],
dest: '<%= currEnvConfig.destDir %>/fonts'
}]
},
other: {
files: [{
expand: true,
cwd: '<%= projectConfig.srcDir %>',
src: '<%= projectConfig.customAssetsToCopy %>',
dest: '<%= currEnvConfig.destDir %>'
}]
},
srcForMaps: {
files: [{
expand: true,
cwd: '<%= projectConfig.srcDir %>',
src: ['scss/**/*', 'js/**/*'],
dest: '<%= currEnvConfig.destDir %>/src'
}]
}
});
// Automatically run tasks when watched files are being modified
// https://www.npmjs.com/package/grunt-contrib-watch
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-newer');
grunt.config('watch', {
options: {
spawn: false,
livereload: true,
atBegin: false
},
fileDeleted: {
files: ['<%= projectConfig.srcDir %>/**/*'],
tasks: ['fileDeletedWarning'],
options: {
event: ['deleted']
}
},
sass: {
files: ['<%= projectConfig.srcDir %>/scss/**/*.scss'],
tasks: (function(){
if(grunt.config.get('envId') === 'dev'){
return [
'sass:dev',
'autoprefixer',
'copy:srcForMaps'
];
}
else {
return [
'sass:nondev',
'autoprefixer'
];
}
})()
},
html: {
files: ['<%= projectConfig.srcDir %>/*.html'],
tasks: ['replace'],
options: {
event: ['changed', 'added']
}
},
js: {
files: ['<%= projectConfig.srcDir %>/js/**/*.js'],
tasks: (function(){
if(grunt.config.get('envId') === 'dev'){
return [
'newer:jshint:app', 'karma:unit:run',
'copy:js'
];
}
else {
return [
'newer:jshint:app', 'karma:unit:run',
'html2js',
'uglify',
'clean:destTemp'
];
}
})(),
options: {
event: ['changed']
}
},
templates: {
files: ['<%= projectConfig.srcDir %>/templates/**/*.tpl.html'],
tasks: (function(){
if(grunt.config.get('envId') === 'dev'){
return [
'newer:copy:templates'
];
}
else {
return [
'html2js',
'uglify',
'clean:destTemp'
];
}
})(),
options: {
event: ['added', 'changed', 'deleted']
}
},
unittests: {
files: ['<%= projectConfig.srcDir %>/test/**/*.js'],
tasks: ['newer:jshint:unittests', 'karma:unit:run'],
options: {
event: ['changed', 'added']
}
},
img: {
files: ['<%= projectConfig.srcDir %>/img/**/*'],
tasks: ['newer:copy:img'],
options: {
event: ['changed', 'added']
}
},
fonts: {
files: ['<%= projectConfig.srcDir %>/fonts/**/*'],
tasks: ['newer:copy:fonts'],
options: {
event: ['changed', 'added']
}
},
other: {
files: grunt.config.get('projectConfig').customAssetsToCopy.map(function(item){
return grunt.config.get('projectConfig').srcDir + '/' + item;
}),
tasks: ['newer:copy:other'],
options: {
event: ['changed', 'added']
}
},
scriptsConfig: {
files: [grunt.config.get('projectConfig').scriptsConfigPath],
tasks: ['askForRestart'],
options: {
event: ['changed']
}
},
gruntfile: {
files: 'Gruntfile.js',
tasks: ['jshint:gruntfile', 'askForRestart']
}
});
////////////////////////////////////////////////////////////////////////////
// Custom Tasks definitions
////////////////////////////////////////////////////////////////////////////
// Displaying log message.
// Although watch sees changes in Gruntfile.js and runs jshint the new config is not automatically applied.
grunt.registerTask('askForRestart', function () {
grunt.log.error('#############################################################################');
grunt.log.error('Detected changes require performing a full rebuild.');
grunt.log.error('Press \'ctrl-c\' keys and run Grunt again.');
grunt.log.error('#############################################################################');
});
// Displaying log message.
grunt.registerTask('fileDeletedWarning', function (action, filepath, target) {
grunt.log.warn('#############################################################################');
grunt.log.warn('A file was deleted. Deleting files are mostly not supported.');
grunt.log.warn('Restart Grunt to make sure that the file was deleted in build directory.');
grunt.log.warn('#############################################################################');
});
// Just prettyfying console output
grunt.event.on('watch', function(action, filepath, target) {
grunt.log.subhead('===============================================================================\nRunning watch:' + target);
});
// When no CLI options are passed, the 'build' action is called
grunt.registerTask('default', function () {
grunt.task.run('build');
});
// The main, default build task. Target environment is definied through CLI: 'grunt --env=staging'
grunt.registerTask('build', function() {
var tasks = [];
grunt.log.subhead('#############################################################################');
grunt.log.subhead(' Assets files to edit are located at: /' + grunt.config.get('projectConfig').srcDir);
grunt.log.subhead('#############################################################################');
if(grunt.config.get('envId') === 'dev') {
tasks = [
'jshint', 'karma', // initial validation
'clean:all', // Deleting old content in dest directory
'sass:dev', 'autoprefixer', // CSS processing
'replace', // Replacing @@ tags in .html files (embedding JS scripts etc.)
'copy:vendor', 'copy:js', 'copy:templates', // copying JS files "as is"
'copy:img', 'copy:fonts', 'copy:other', // copying assets files "as is"
'copy:srcForMaps', // to enable preview of source JS and SCSS files in Chrome
'clean:destTemp', // deleting temporary files required for build process only (eg. compiledTemplates.js from html2js task)
'watch'
];
}
else if(grunt.config.get('envId') === 'local' || grunt.config.get('envId') === 'staging' || grunt.config.get('envId') === 'production') {
tasks = [
'jshint', 'karma', // initial validation
'clean:all', // Deleting old content in dest directory
'sass:nondev', 'autoprefixer', // CSS processing
'replace', // Replacing @@ tags in .html files
'html2js', // converting templates from '.tpl.html' files to a single Angular's module
'ngconstant', // Create a temporary file with angular constants
'uglify', // concatenating, minifying, mangling all JS files
'copy:img', 'copy:fonts', 'copy:other', // copying assets "as is"
'clean:destTemp' // deleting temporary files required for build process only
];
}
grunt.task.run(tasks);
});
// Creating new HTML templates in new folders was not watched by Grunt
// Following code will create initial template as far as new folder is created
grunt.event.on('watch', function(action, path) {
var folder = path.substring(0, 7).replace(/\\/g,"/");
var isFolderInTemplates = (path.slice(-3) !== '.js' && path.slice(-5) !== '.html' && folder === grunt.config.get('projectConfig').srcDir + '/tem') ? true : false;
if (isFolderInTemplates) {
grunt.file.write(path + '/initial.tpl.html', '');
grunt.file.write(grunt.config.get('projectConfig').srcDir + '/js/app.js', grunt.file.read(grunt.config.get('projectConfig').srcDir + '/js/app.js')); // Save app.js file so Grunt is looking at changes
}
});
};
}());