-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
329 lines (314 loc) · 12.8 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
/**
* Grunt tasks for handling assets
* Removes dependency on specific framework for asset pipeline
* INSTRUCTIONS:
* Specify folder locations in tasks/options/grunt-folders.json
* Keep JS and CSS files you work on in seperate folders from destination (e.g. public) folder.
* CAREFUL: JS and CSS destination folders get cleaned programatically.
* JS: Specify JS file groupings in asset folder as js.json. Use grunt globbing pattern to specify folders
* CSS: put main scss and print scss file in root (along with any other file that you want as a standalone css file) all other scss files in a subfolder (e.g. src)
* Run 'grunt' from the command line to get sass, linting and image handling occurring while you work
* Run 'grunt build' to run the full build task set
*/
module.exports = function(grunt) {
var folders = grunt.file.readJSON('grunt-folders.json');
var jsMap = grunt.file.readJSON(folders.js.src + '/jsmanifest.json');
require('time-grunt')(grunt);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
cssHashFormat: '.{{hash}}.min.css',
jsHashFormat: '.{{hash}}.min.js',
sass: {
options: {
banner: '/* THIS IS A COMPILED FILE. DO NOT EDIT DIRECTLY. USE THE SCSS FILES IN ' + folders.css.src.toUpperCase() + ' */'
},
dist: {
files: [{
expand: true,
cwd: folders.css.src,
src: ['*.scss'],
dest: folders.css.src,
ext: '.css'
}]
}
},
csslint: { //test css that has been produced by sass
options: {
csslintrc: '.csslintrc'
},
strict: {
options: {
import: 2
},
src: [folders.css.src + '/**/*.css']
},
lax: {
options: {
import: false
},
src: [folders.css.src + '/**/*.css']
}
},
cssmin: { //export minified css
minify: {
expand: true,
cwd: folders.css.src,
src: ['*.css', '!*.min.css'],
dest: folders.css.src,
ext: '.min.css'
}
},
// styleguide: { //undecided. lots of overhead, adding html fixtures in comments in css files
// options: {
// name: 'Generated styleguide'
// },
// dist: {
// files: {
// 'docs/css': folders.css.src + '/*.scss'
// }
// }
// },
//JS
jshint: {
files: [folders.js.src + '/**/*.js']
},
jasmine: {
testdev: {
src: folders.js.src + '/**/*.js',
options: {
'specs': folders.spec.src + '/*Spec.js',
'helpers': folders.spec.src + '/*Helper.js',
'ignoreEmpty': true
}
},
testprod: {
src: folders.js.dest + '/**/*.js',
options: {
'specs': folders.spec.src + '/*Spec.js',
'helpers': folders.spec.src + '/*Helper.js',
'ignoreEmpty': true
}
},
coverage: { //'istanbul' coverage, non-failing
src: folders.js.src + '/**/*.js',
options: {
'specs': folders.spec.src + '/*Spec.js',
'helpers': folders.spec.src + '/*Helper.js',
'template': require('grunt-template-jasmine-istanbul'),
'templateOptions': {
'coverage': folders.spec.coverage + '/coverage.json',
'report': [
{'type': 'html', options: {dir: folders.spec.coverage}},
{'type': 'cobertura', options: {dir: folders.spec.coverage + '/cobertura'}},
{'type': 'text-summary'}
],
'thresholds': { //dont' force fail on lack of coverage
'lines': 0,
'statements': 0,
'branches': 0,
'functions': 0
}
}
}
}
},
uglify: { //minify and concat js
target: {
files: (function () { //result in [public/folder/filename.min.js = {srcfiles/file.js, /srcfiles/file2.js}] etc
var obj = {};
var group;
var fileName;
for (group in jsMap) {
fileName = folders.js.dest + '/' + group + '.min.js';
obj[fileName] = jsMap[group];
}
return obj;
}())
}
},
//images
imagemin: {//NB: crunch gifs and jpgs only, pngs are just copied (in seperate task)
dynamic: {
files: [{
expand: true,
cwd: folders.img.src,
src: '**/*.{jpg,gif}',
dest: folders.img.dest,
}]
}
},
copy: {
pngs: { //for pngs
files: [
{
'expand': true,
'cwd': folders.img.src,
'src': '**/*.png',
'dest': folders.img.dest,
'filter': 'isFile'
}
]
},
js : { //for js in dev mode
files: [{
expand: true,
cwd: folders.js.src,
src: '**/*.js',
dest: folders.js.dest
}]
}
},
favicons: { //favico and all the mobile variants
options: {
html: folders.img.out + '/icons.html',
HTMLPrefix: folders.img.icons.dest.replace(folders.output_redirect_path[0] + '/', folders.output_redirect_path[1]) + '/'
},
icons: {
src: folders.img.icons.src,
dest: folders.img.icons.dest
}
},
//watch for file updates
watch: {
css: {
files: folders.css.src + '/**/*.scss',
tasks: ['clean:on_sass', 'sass', 'csslint', 'cssmin', 'hashify:css', 'assetincludes:css' ]
},
images: {
files: folders.img.src + '/**/*.*',
tasks: ['images'],//runs over entire folder :(
options: {
event: ['added', 'changed']
}
},
images_del: {
files: folders.img.src + '/**/*.*',
tasks: [],//on watch as below. maybe make own task?
options: {
event: ['deleted']
}
},
icons: {
files: folders.img.icons.src,
tasks: ['favicons']
},
js: {
files: [folders.js.src + '/**/*.js', folders.spec.src + '/**/*.js'],
tasks: ['jshint', 'jasmine:testdev', 'copy:js', 'jasminecoverage', 'assetincludes:js']
},
jsmanifest: {
files: [folders.js.src + '/jsmanifest.json'],
tasks: ['clean:on_manifest', 'assetincludes:js']
}
},
//versioning via hashes of md5 content
hashify: {
css: {
options: {
copy: false, // remove originals
hashmap: folders.css.out + '/hashmap.json'
},
files: [{
expand: true,
cwd: folders.css.src,
src: '**/*.min.css',
dest: folders.css.dest,
ext: '<%= cssHashFormat %>'
}]
},
js: {
options: {
copy: false, // remove originals
hashmap: folders.js.out + '/hashmap.json'
},
files: [{
expand: true,
cwd: folders.js.dest,
src: '**/*.min.js',
dest: folders.js.dest,
ext: '<%= jsHashFormat %>'
}]
}
},
assetincludes: { //create include files for js and css in different environments
css: {
options: {
hashifyMap: folders.css.out + '/hashmap.json',
dest: folders.css.out,
hashtemplate: folders.css.dest.replace(folders.output_redirect_path[0] + '/', folders.output_redirect_path[1]) + '/{{file}}<%= cssHashFormat %>'
}
},
js: {
options: {
hashifyMap: folders.js.out + '/hashmap.json',
manifest: jsMap,
dest: folders.js.out,
assetsrc: folders.js.src,
siteroot: folders.js.dest.replace(folders.output_redirect_path[0] + '/', folders.output_redirect_path[1]),
template: '<script src="{{file}}"></script>',
hashtemplate: folders.js.dest.replace(folders.output_redirect_path[0] + '/', folders.output_redirect_path[1]) + '/{{file}}<%= jsHashFormat %>'
}
}
},
clean: { //delete contents of given folders to work with a clean set
on_min: [folders.js.dest + '/**/*.js', folders.css.dest + '/**/*.css'],
on_img: [folders.img.dest],
on_manifest: [folders.js.out + '/*.{dev,prod}'],
on_sass: [folders.css.src + '/*.css', folders.css.out]
},
notify: { //throw alerts in addition to failures
dev: {
options: {
title: 'COMPLETE',
message: 'Ready to work'
}
},
build: {
options: {
title: 'BUILD COMPLETE',
message: 'End of build process'
}
}
}
});
//load plugins and custom tasks
require('load-grunt-tasks')(grunt, {pattern: ['grunt-*', '!grunt-template-*']});
//setup tasks
grunt.registerTask('default', ['setup', 'lint', 'cssmin', 'hashify:css', 'copy:js', 'assetincludes', 'watch', 'notify:dev']);//use this when developing to autoupdate css and images
grunt.registerTask('images', ['imagemin', 'copy:pngs']);//watched via dev task
//testing
//jasmine coverage fails catastrophically if there are no js files present, so check for files
grunt.registerTask('jasminecoverage', function () {
if (grunt.file.expand(folders.js.src + "/**/*.js").length > 0) {
grunt.task.run("jasmine:coverage")
}
});
grunt.registerTask('jasminetests', ['jasmine:testdev', 'jasmine:testprod'])
grunt.registerTask('lint', ['csslint', 'jshint']);//linting on assets on build and on demand
grunt.registerTask('tests', ['lint', 'jasminetests', 'jasminecoverage']);//unit tests
grunt.registerTask('minify', ['cssmin', 'uglify']);
//build
grunt.registerTask('setup', ['clean', 'sass', 'images', 'favicons']);
grunt.registerTask('fingerprint', ['hashify', 'assetincludes']);//make prod files and their references
grunt.registerTask('build', ['setup', 'minify', 'tests', 'fingerprint', 'notify:build']); //everything
//this function caters for the situation in which an image file is deleted.
//Instead of cleaning the whole folder, it deletes the specific file
//There is probably a better way to do this, perhaps with a template or a different task
grunt.event.on('watch', function(action, filepath, target) {
var file;
if (filepath.indexOf(folders.img.src) > -1 && action === 'deleted') {//specifically track image deletion
file = filepath.replace(folders.img.src, folders.img.dest);
if (grunt.file.exists(file)) {
grunt.file.delete(file);
grunt.log.write(file + ' deleted ');
}
}
});
};
//TODO: awaiting feedback on pull req for jasmine update
//
//todo: loadscreens, js docs?
//todo: better way of handling individual deletes on images
//todo: images are not versioned, could be nicer. hashify src in templates, sass - usemin? only update changed?
//TODO: nice-to-have warning file in any folder that gets CLEANED
//TODO: can we check for an install non-node dependencies?