-
Notifications
You must be signed in to change notification settings - Fork 7
/
Gruntfile.js
644 lines (608 loc) · 24.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
/**
*
* @Author - Eugene Mutai
* @Twitter - kn9ts
* @Email - [email protected]
*
* Date: 06/04/14
* Time: 8:12 PM
* Description: The default grunt setup
* Licensed under MIT (https://github.com/jheneknights/knightstart/blob/master/docs/LICENSE-MIT)
*/
// Being a frontend developer
// if you haven’t yet added Grunt.js to your workflow,
// then you are seriously missing something.
'use strict';
var LIVERELOAD_PORT = 35729;
// # Globbing Tut
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
module.exports = function(grunt) {
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// load all grunt tasks matching the `grunt-*` pattern
// This replaces the tiresome task of including all grunt-plugins as you would have done below
// These plugins provide necessary tasks. -- loading grunt plugins
// Equivalent to:
// require('load-grunt-tasks')(grunt, {pattern: 'grunt-*'});
// require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); //using matchdep
// OR:
// -------------------------------------------------------------
// grunt.loadNpmTasks('grunt-contrib-concat');
// grunt.loadNpmTasks('grunt-contrib-cssmin');
// grunt.loadNpmTasks('grunt-contrib-csslint');
// grunt.loadNpmTasks('grunt-contrib-jade');
// grunt.loadNpmTasks('grunt-contrib-uglify');
// grunt.loadNpmTasks('grunt-contrib-jade');
// grunt.loadNpmTasks('grunt-contrib-nodeunit');
// grunt.loadNpmTasks('grunt-contrib-jshint');
// grunt.loadNpmTasks('grunt-contrib-watch');
require('load-grunt-tasks')(grunt);
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
app: {
file_name: 'index',
file_being_editted: '<%= app.file_name %>.jade', // Set the current file that you are editing
// Incase if dev is using html, make the html be copied
// to .tmp/ folder as task related to html can access it from here
related_html_file: '<%= app.file_name %>.html',
flatten_test_path: false, // Set to "true" to export to silverstripe as theme
dist_folder: "dist",
dev_folder: "app",
test_folder: "test",
banner: "/*!\n *\n * @Author - Eugene Mutai\n * @Twitter - JheneKnights\n * @Email - [email protected]\n *\n * Date: <%= grunt.template.today('dd/mm/yyyy') %>\n * Time: <%= grunt.template.today('hh:mm') %>\n * Description: <%= pkg.description %>\n * Licensed under <%= pkg.license.type %> ( <%= pkg.license.url %>)\n * Ayyee, Ayyee! Always happy to help. :) \n */\n"
},
// Empties folders to start fresh
clean: {
dist: {
// Using this:
// http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
files: [{
dot: true,
// dont delete the git folder
src: ['.tmp', '<%= app.dist_folder %>', '!<%= app.dist_folder %>/.git*']
}]
},
test: ['.tmp', '<%= app.test_folder %>'],
htmt_from_root: ['./*.html']
},
copy: {
components_to_dist: {
files: [{
expand: true,
// dot: true,
cwd: '.tmp/',
dest: '<%= app.dist_folder %>/',
src: ['**/*.{css,js}'] // 'js/*', 'css/*'
}, {
expand: true,
// dot: true,
cwd: './app/',
dest: '<%= app.dist_folder %>/',
src: ['*.{htm,html}', '!<%= app.related_html_file %>']
}]
},
app_assets_to_dist: {
files: [{
expand: true,
cwd: '<%= app.dev_folder %>/',
dest: '<%= app.dist_folder %>/',
src: ['{,*/}*.{ico,png,txt,htaccess,md,markdown}', 'images/**/*.*', 'fonts/**/*.*']
}]
},
html_to_test: {
files: [{
expand: true,
// dot: true,
cwd: './app/',
dest: '<%= app.test_folder %>/',
src: ['*.{htm,html}']
}]
},
scripts_to_test: {
files: [{
expand: true,
// cwd: 'app/',
dest: '<%= app.test_folder %>/',
// flatten: '<%= app.flatten_test_path =>',
src: '<%= dom_munger.data.jsRefs %>'
}]
},
css_to_test: {
files: [{
expand: true,
// dot: true,
dest: '<%= app.test_folder %>/',
// flatten: '<%= app.flatten_test_path =>',
src: '<%= dom_munger.data.cssRefs %>'
}]
},
app_assets_to_test: {
files: [{
expand: true,
cwd: 'app/',
dest: '<%= app.test_folder %>/app/',
// flatten: '<%= app.flatten_test_path =>',
src: ['images/**/*.*', 'fonts/**/*.*']
}]
},
// Adding the copying of font-awesome fonts
// Pre-destribution deprecated
test_bower_asset: {
files: [{
expand: true,
// dot: true,
cwd: 'bower_assets/', // "app/" folder
dest: '<%= app.test_folder %>/bower_assets/',
src: ['**/*.{css,js,eot,ttf,svg,woff,woff2}']
}]
},
dist_bower_asset: {
files: [{
expand: true,
// dot: true,
cwd: 'bower_assets/', // "app/" folder
dest: '<%= app.dist_folder %>/bower_assets/',
src: ['**/*.{css,js,eot,ttf,svg,woff,woff2}']
}]
},
others: {
files: [{
expand: true,
dest: '<%= app.dist_folder %>/',
src: ['.htaccess', 'docs/BSD-License', 'docs/LICENSE-MIT', 'README.md']
}]
},
to_be_concatenated: {
files: [{
expand: true,
// dot: true,
cwd: '.tmp/',
dest: '<%= app.test_folder %>/',
src: ['{,*/}*.{css,js}']
}]
},
// If user is using HTML and not Jade in <%= app.dev_folder %> for development
app_html_to_root: {
files: [{
expand: true,
cwd: '<%= app.dev_folder %>/',
dest: 'html',
src: ['{,*/}*.{htm,html}']
}]
}
},
// Reads the related html file
// Then can manipulate it using DOM queries
// Can also be used to extract all related files for concatination and minification
dom_munger: {
test: {
options: {
read: [{
selector: 'link[rel="stylesheet"]',
attribute: 'href',
writeto: 'cssRefs',
isPath: true
}, {
selector: 'script[type="text/javascript"]',
attribute: 'src',
writeto: 'jsRefs',
isPath: true
}],
},
// read from source <%= app.related_html_file %>
src: 'app/<%= app.related_html_file %>',
// update the dist/<%= app.related_html_file %> (the src <%= app.related_html_file %> is copied there)
dest: 'test/<%= app.related_html_file %>'
},
dist: {
options: {
read: [{
selector: 'link[rel="stylesheet"]',
attribute: 'href',
writeto: 'cssRefs',
isPath: true
}, {
selector: 'script[type="text/javascript"]',
attribute: 'src',
writeto: 'jsRefs',
isPath: true
}],
remove: ['link[rel="stylesheet"]', 'script[type="text/javascript"]'],
append: {
selector: 'head',
html: '<link href="css/app.all.min.css" rel="stylesheet">'
},
prepend: {
selector: 'application-engine',
html: '<script src="js/app.all.min.js"></script>'
}
},
src: 'app/<%= app.related_html_file %>',
dest: 'dist/<%= app.related_html_file %>'
}
},
// Concat is a pretty simple task that does exactly what you think it does.
// concatenate files.
concat: {
// Join all the app's css, in this case bootstrap and your css
options: {
separator: '\n\n',
stripBanners: true
},
app_css: {
src: '<%= dom_munger.data.cssRefs %>',
dest: '.tmp/css/app.all.css'
},
// for custom CSS files compilation
// after customizing the individual css files in bootstrap
bootstrap_css: {
src: ['bower_assets/bootstrap/test/*.css'],
dest: 'app/css/bootstrap.css'
},
//Application's javascript dependencies in js/ folder
app_scripts: {
options: {
separator: ';\n\n',
banner: '<%= app.banner %>'
},
src: '<%= dom_munger.data.jsRefs %>',
dest: '.tmp/js/app.all.js'
}
},
// By default, your `index.html`'s <!-- Usemin block --> will take care of minification.
// These next options are pre-configured if you do not wish to use the Usemin blocks.
// Minify the CSS concentanated -- application.css
cssmin: {
options: {
banner: '<%= app.banner %>\n\n/* My minified css file */',
keepSpecialComments: '1', // '*' or 1 or 0
report: 'min' // 'gzip'
},
dist_css: {
src: '.tmp/css/app.all.css', //'<%= dom_munger.data.cssRefs %>'
dest: '.tmp/css/app.all.min.css'
},
bootstrap_css: {
options: {
banner: false
},
src: 'app/css/bootstrap.css',
dest: 'app/css/bootstrap.min.css'
}
},
csslint: {
options: {
csslintrc: '.csslintrc'
},
strict: {
options: {
import: 2
},
src: ['app/css/**/*.css']
},
lax: {
options: {
import: false
},
src: ['app/css/**/*.css']
}
},
// Your CSS has now been combined into a single file and compressed.
// Lets compress your JavaScript code now by adding a new task called uglify.
// Uglify does what cssmin does to css, only with javascript.
uglify: {
app_scripts: {
options: {
report: "min", //"gzip",
// sourceMap: true,
// sourceMapURL: true,
preserveComments: "some" //"all"
},
files: [{
// expand: true,
src: '.tmp/js/app.all.js',
dest: ".tmp/js/app.all.min.js"
}]
}
},
// Compile bootstrap LESS files to CSS. -- /test folder
// Use this if you are customizing the bootstrap css using less
// https://www.npmjs.org/package/grunt-contrib-less
less: {
// compile Bootstrap
bootstrap: {
options: {
report: 'min',
sourceMap: false,
// @path - Specifies directories to scan for @import directives when parsing.
// Default value is the directory of the source, which is probably what you want
paths: ['assets/bootstrap/less']
},
files: [{
// expand: true,
cwd: 'assets/bootstrap/less/',
src: '*.less',
dest: 'assets/bootstrap/test',
ext: '.css'
}]
},
// Your custom LESS script will compiled by this
for_app: {
options: {
report: 'min',
sourceMap: false,
paths: ['app/less'],
plugins: [
new require('less-plugin-autoprefix')({
browsers: ["last 5 versions"]
})
// new require('less-plugin-clean-css')(cleanCssOptions)
],
banner: "/**\n * CSS STYLING FILE\n * Created by Eugene Mutai\n * Date: <%= grunt.template.today('dd/mm/yyyy') %>\n * Time: <%= grunt.template.today('hh:mm') %>\n * Description: Custom CSS of the application\n * @import url(http://fonts.googleapis.com/css?family=Raleway:300,400,500);\n */\n\n\n"
},
files: [{
expand: true,
cwd: 'app/less/',
src: '*.less',
dest: 'app/css/',
ext: '.css'
}]
}
},
// Add vendor prefixed styles to the CSS
autoprefixer: {
options: {
browsers: ['last 5 version']
},
default: {
files: [{
expand: true,
cwd: '.tmp/css/',
src: '{,*/}*.css',
dest: '.tmp/css/'
}]
}
},
// minify HTML file
htmlmin: {
dist: {
options: {
collapseBooleanAttributes: true,
collapseWhitespace: true,
removeAttributeQuotes: false,
removeComments: true, // Should remove comments from the DIST file [clean markup]
removeCommentsFromCDATA: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
useShortDoctype: true
},
files: [{
expand: true,
cwd: '<%= app.dist_folder %>/',
src: ['{,*/}*.html'],
dest: '<%= app.dist_folder %>/',
// ext: '.min.html',
// extDot: 'last'
}]
}
},
// Compile Jade templates.
// STOP WRITTING TIRESOME HTML, you do get tired opening and closing tags dont you?!
// http://jade-lang.com
jade: {
compile: {
options: {
pretty: true
},
files: [{
expand: true,
cwd: 'app/jade/',
// src: '{,*/}*.jade',
src: '*.jade',
dest: 'app/',
ext: '.html'
// The path to compile to and the path to where to get the jade files
// In this case the in root
// "index.html": ['app/index.jade']
}]
// files: {"index.html": ['app/index.jade'] }
}
},
jshint: {
options: {
jshintrc: '.jshintrc',
// For pretty reports in the CLI :)
reporter: require('jshint-stylish')
},
gruntfile: {
src: 'Gruntfile.js'
},
app_scripts: {
src: 'app/js/{,*/}*.js'
},
afterconcat: ['Gruntfile.js', 'app/js/{,*/}*.js']
},
// The command should start server and the should open http://localhost:1500/ in your browser.
// You can also try changing less / html file, the changes should be reflected on browser without refreshing the page.
// livereload and starting a development server.
connect: {
options: {
port: 1515,
livereload: 35729,
// Change this to '0.0.0.0' to access the server from outside
hostname: 'localhost'
},
livereload: {
options: {
open: true,
base: ['<%= app.test_folder %>'],
livereload: true
}
},
dist: {
options: {
open: true,
port: 1500,
base: '<%= app.dist_folder %>',
livereload: false
}
}
},
// grunt-open will open your browser at the project's URL
// https://www.npmjs.org/package/grunt-open
// open: {
// server: {
// path: 'http://localhost:1515/tellme/index.html'
// }
// },
// Wouldn't it be awesome if grunt did all of this automatically every time we changed a file?
// Watch is a grunt plugin written to do just that.
// Run predefined tasks whenever watched file patterns are added, changed or deleted.
// Grouped the files to run specific tasks that are unique to each;
watch: {
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'test/{,*/}*.{htm,html}',
'test/**/*.{css,less}',
'test/**/*.{png,jpg,jpeg,gif,webp,mp3,m4a,mp4,ogg}',
'test/**/*.{eot,otf,svg,tff,woff,woff2}'
]
},
less: {
files: ["app/less/{,*/}*.less"],
tasks: ['less:for_app']
},
css: {
files: ["app/**/*.css"],
tasks: ['dom_munger:test', 'concat:app_css', 'copy:css_to_test']
},
scripts: {
files: ['app/{,*/}*.js', 'js/{,*/}*.js'],
tasks: ['dom_munger:test', 'jshint:app_scripts', 'copy:scripts_to_test']
},
jade: {
files: ['app/{,*/}*.jade'],
// if you do not want to concatinate them replace 'copy:dist' with 'copy:test_unpackaged'
tasks: ['jade', 'copy:html_to_test']
},
gruntfile: {
files: ['Gruntfile.js'],
tasks: ['test']
},
// For Html files
html: {
files: ['app/{,*/}*.{htm,html}', '*.html'],
tasks: ['test']
}
},
// To prefix the CSS file with the app's signature
css_selectors: {
options: {
mutations: [{
prefix: '.kn9t'
}]
},
prefix: {
src: 'app/css/application.css',
dest: 'app/css/prefixed-application.css'
}
}
});
// Default task to run.
grunt.registerTask('start', [
'jshint:gruntfile',
'clean', // clean all the files and folders [.tmp, dist and test]
'jade', // IMPORTANT: uncomment this if you are using jade to write your html files
// 'copy:app_html_to_root',
// 'less:for_app'
]);
// Just split bootstrap into several css mini files
// For the ones who would work with bootstrap CSS customisation instead of LESS
grunt.registerTask('bs', function(target) {
if (target === 'less') {
// Compile LESS and the concatinate the CSS into bootstrap.css
return grunt.task.run(['less']);
} else {
// With no argument just run join all bootstrap individual files into one
// Copies to "app/css/" folder for later concatination with user's css
// -- wraps all bootstrap folders into one
return grunt.task.run(['concat:bootstrap']);
}
});
// shortcut to bootstrap compiling task
grunt.registerTask('bootstrap', ['bs'])
grunt.registerTask('test', [
'clean:test',
// 'css_selectors',
'csslint:strict',
// 'concat:bootstrap', // -- wrap all bootstrap folders into one, being done above instead
'dom_munger:test', //get all required files to concat, cssmin and uglify
// 'concat:app_css',
// 'autoprefixer',
// 'cssmin',
'jshint:app_scripts',
// 'concat:app_scripts',
// 'uglify',
'copy:css_to_test',
'copy:scripts_to_test',
'copy:app_assets_to_test', // NEW PROCESS -- get all files from TEST
'copy:test_bower_asset',
// 'distribution', // To also package up the application for distribution
// 'htmlmin', // Testing does not require minification,
'copy:html_to_test' // to copy the html in the case that it fails
]);
// Tasks for the app distrubution packaging
grunt.registerTask('distribution', [
'clean:dist',
// 'css_selectors',
'csslint:strict',
// Process for the END DISTRUBUTED product
'dom_munger:dist',
'concat:app_css',
'autoprefixer',
'cssmin',
'jshint:app_scripts',
'concat:app_scripts',
'uglify',
'copy:components_to_dist',
'copy:app_assets_to_dist',
'copy:dist_bower_asset',
'copy:others',
// 'htmlmin'
]);
// Same as rolling out a production version
grunt.registerTask('production', ['distribution']);
grunt.registerTask('dist', ['production']);
// Serve the app and view changes live
grunt.registerTask('serve', function(target) {
if (target === 'dist') {
// return grunt.task.run(['default', 'connect:dist:keepalive']);
return grunt.task.run(['start', 'distribution', 'connect:dist:keepalive']);
}
grunt.task.run(['start', 'test', 'connect:livereload', 'watch']);
});
// Triggred when watch task is prompted, this is for extra info that you may need
grunt.event.on('watch', function(action, filepath, target) {
// On saving, get the file saved and check if it's HTML/JADE
// This enables GruntJS to keep track of the variable of the file is being editted at the moment
if (filepath.indexOf('html') > -1 || filepath.indexOf('jade') > -1) {
var file_exploded = filepath.split('/');
grunt.config.set('app.related_html_file', file_exploded.length > 1 ? file_exploded[file_exploded.length - 1] : file_exploded);
grunt.log.ok('--------- HTML file being editted has been updated: ' + grunt.config.get('app.related_html_file') + ' ------------');
}
grunt.log.ok("--------- <(0_0)> info: " + filepath + " has " + action + " || TASK: " + target + " ----------");
});
// Tell Grunt what to do when we type "grunt" into the terminal
// Could start watching for changes and running related tasks as per changes
// Run "$ grunt watch" after the default tasks or "$ grunt server" to view your changes live
// grunt.registerTask('default', ['start']); // default
grunt.registerTask('default', ['start', 'distribution', 'test']);
};