-
Notifications
You must be signed in to change notification settings - Fork 7
/
Gruntfile.js
561 lines (475 loc) · 18.7 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
/*
* Gruntfile for the Grunt Front-End Workflow/Boilerplate
* (previously called Backbone/RequireJS multipage boilerplate).
*
* DEV URL: http://localhost:9001/
*
* @author Aki Karkkainen
* @url https://github.com/akikoo/grunt-frontend-workflow
* Twitter: http://twitter.com/akikoo
*
*/
// Needed for `grunt-express`.
var path = require('path');
// Grunt configuration wrapper function.
module.exports = function (grunt) {
'use strict';
// Configurable paths and other variables.
var config = {
webroot: 'www',
dist: 'www-built',
testroot: 'test',
tstamp: '<%= grunt.template.today("ddmmyyyyhhMMss") %>'
};
// Initialize our configuration object.
grunt.initConfig({
/*
* Get configuration options.
*/
config: config,
/*
* Get the project metadata.
*/
pkg: grunt.file.readJSON('package.json'),
/*
* Create a dynamic build header.
*/
banner: '/*! <%= pkg.name %> v<%= pkg.version %> | ' +
'<%= grunt.template.today("dd-mm-yyyy-hh:MM:ss") %>\n' +
' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %> |' +
' Licensed <%= pkg.license %>\n */\n',
/**
* Start (and supervise) an Express.js web server.
* DEV URL http://localhost:9001/.
* To view the local site on another device on the same LAN, use your
* master machine's IP address instead, for example http://10.0.0.32:9001/.
*/
express: {
server: {
options: {
// The port on which the webserver will respond.
port: 9001,
// Default 'localhost'. Setting this to '*' will make the server
// accessible from anywhere. Useful for cross-device testing.
hostname: '*',
// The base (or root) directory from which files will be served.
// Defaults to the project Gruntfile's directory.
bases: ['<%= config.webroot %>'],
// Start an express server using your own express application script.
// Server.js contains a custom RESTful API.
server: path.resolve('./server.js'),
// if you just specify `true`, default port `35729` will be used.
livereload: 1337
}
}
},
/*
* Keep multiple browsers & devices in sync when developing.
* For options, see http://www.browsersync.io/docs/options/
*/
browserSync: {
dev: {
bsFiles: {
src : [
'<%= config.webroot %>/css/*.css'
]
},
options: {
watchTask: true,
proxy: 'http://localhost:9001'
}
}
},
/*
* Run predefined tasks whenever watched file patterns are added, changed or deleted.
*/
watch: {
options: {
// Reload assets live in the browser.
// Default livereload listening port is 35729.
livereload: 1337
},
html: {
files: ['<%= config.webroot %>/html/*.html'],
tasks: [
'includereplace'
]
},
css: {
files: ['<%= config.webroot %>/scss/**/*.scss'],
tasks: [
'compass',
'csslint',
'autoprefixer'
]
},
js: {
files: ['<%= config.webroot %>/js/app/**/*.js'],
tasks: [
'jshint'
]
},
// Run unit tests with karma (server needs to be already running).
karma: {
files: ['<%= config.testroot %>/spec/*Spec.js'],
tasks: ['karma:unit:run'] // NOTE the :run flag
}
},
/*
* Compile Sass to CSS using Compass.
*/
compass: {
dist: {
options: {
httpPath: '/',
cssDir: '<%= config.webroot %>/css',
sassDir: '<%= config.webroot %>/scss',
imagesDir: '<%= config.webroot %>/img',
javascriptsDir: '<%= config.webroot %>/js',
outputStyle: 'expanded',
relativeAssets: true,
noLineComments: false,
force: true,
// Use `raw` since it's not directly available.
raw: 'Sass::Script::Number.precision = 15\n'
}
}
},
/*
* Parse CSS and add vendor-prefixed CSS properties using the Can I Use database.
* Based on Autoprefixer.
*/
autoprefixer: {
options: {
// Task-specific options go here.
browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1'] // Default.
},
// Target-specific file lists and/or options go here.
// if you have specified only the `src` param, the destination will
// be set automatically, so source files will be overwritten.
dist: {
// Process the precompiled styles.
src: '<%= config.webroot %>/css/*.css'
}
},
/*
* Lint CSS files.
*/
csslint: {
options: {
// Get CSSLint options from external file.
csslintrc: '.csslintrc'
},
strict: {
options: {},
src: ['<%= config.webroot %>/css/*.css']
},
lax: {
options: {}
// src: ['www/css/common/*.css']
}
},
/*
* Validate files with JSHint.
*/
jshint: {
// Configure JSHint (documented at http://www.jshint.com/docs/).
options: {
// Get JSHint options from external file.
jshintrc: '.jshintrc'
},
// Define the files to lint.
files: [
'Gruntfile.js',
// Only process custom scripts, excluding libraries.
'<%= config.webroot %>/js/app/**/*.js'
]
},
/*
* Compile YUIDoc Documentation.
*/
yuidoc: {
compile: {
options: {
paths: ['<%= config.webroot %>/js/app/'],
outdir: '<%= config.dist %>/docs/'
},
name: '<%= pkg.name %>',
description: '<%= pkg.description %>',
version: '<%= pkg.version %>'
}
},
/*
* Grunt task to include files and replace variables. Allows for parameterised includes.
*/
includereplace: {
dist: {
options: {
// Global variables available in all files.
globals: {
tstamp: '<%= config.tstamp %>'
},
// Optional variable prefix & suffix.
prefix: '<!-- @',
suffix: ' -->'
},
// Source-destination file mappings, using Files Array format.
files: [
{
// Enable dynamic expansion.
expand: true,
// Src matches are relative to (but don't include) this path.
cwd: '<%= config.webroot %>/html/',
// Actual files to match, relative to the cwd.
src: [
'index.html',
'styleguide.html'
],
// Destination path prefix.
dest: '<%= config.webroot %>/'
}
]
}
},
/*
* A mystical CSS icon solution.
* See http://filamentgroup.com/lab/grunticon/.
*/
grunticon: {
makeicons: {
options: {
// Required config.
src: '<%= config.webroot %>/img/icons',
dest: '<%= config.webroot %>/css/components/modules/icons',
// Optional grunticon config properties:
// CSS filenames.
datasvgcss: 'icons.data.svg.css',
datapngcss: 'icons.data.png.css',
urlpngcss: 'icons.fallback.css',
// Preview HTML filename.
previewhtml: 'preview.html',
// Grunticon loader code snippet filename.
loadersnippet: 'grunticon.loader.txt',
// Folder name (within dest) for png output.
pngfolder: 'png/',
// Prefix for CSS classnames.
cssprefix: 'icon-',
// CSS file path prefix - this defaults to "/" and will be
// placed before the "dest" path when stylesheets are loaded.
// This allows root-relative referencing of the CSS. If you
// don't want a prefix path, set to to "".
cssbasepath: '/'
}
}
},
/*
* Optimize RequireJS projects using r.js.
*/
requirejs: {
compile: {
options: {
// The JS source dir, relative to the 'appDir' if set below.
// No forward slash here!
baseUrl: 'js',
// The top level assets directory, relative to this file.
// All the files from this directory will be copied to 'dir'.
appDir: '<%= config.webroot %>',
// The CSS and JS output dir, relative to this file.
dir: '<%= config.dist %>',
// Include the main configuration file (paths, shim).
// Relative to this file.
mainConfigFile: '<%= config.webroot %>/js/config.js',
// (default) uses UglifyJS to minify the code.
optimize: 'uglify',
// Set to true, to skip optimizing other non-build layer JS
// files (speeds up builds).
skipDirOptimize: true,
// @import inlining, comment removal and line returns.
optimizeCss: 'standard',
// If the regexp matches, it means the file/directory will
// be excluded.
fileExclusionRegExp: /^\.|\.((json))|scss$/,
// List of modules that will be optimized. All their immediate
// and deep dependencies will be included.
modules: [
// First set up the common build layer. Module names are
// relative to 'baseUrl'.
{
name: 'config',
// List common dependencies here. Only need to list
// top level dependencies, "include" will find nested
// dependencies.
include: [
'jquery',
'backbone',
'underscore',
'handlebars',
'text'
]
},
// NOTE: If you're building a Single Page Application,
// you can combine the shim config with your page logic,
// resulting in only one http request (plus requirejs itself),
// like so:
/*
{
name: 'config',
// List common dependencies here. Only need to list
// top level dependencies, "include" will find nested dependencies.
include: [
'jquery',
'backbone',
'underscore',
'handlebars',
'text',
'app/mainpage'
]
},
*/
// Otherwise if you have multiple pages, do the following:
// Now set up a build layer for each main layer, but exclude the common one.
// "exclude" will exclude the nested, built dependencies from "config".
// Any "exclude" that includes built modules should be listed before the
// build layer that wants to exclude it. The "mainpage" and "subpage" modules
// are **not** the targets of the optimization, because shim config is
// in play, and shimmed dependencies need to maintain their load order.
// In this example, config.js will hold jquery, so other scripts need to be
// delayed from loading until config.js finishes. That loading sequence
// is controlled in mainpage.js.
{
name: 'app/mainpage',
exclude: ['config']
},
{
name: 'app/subpage',
exclude: ['config']
}
]
}
}
},
/*
* Grunt plugin for karma test runner.
*/
karma: {
// This is used in `default` (DEV) task.
unit: {
configFile: 'karma.conf.js',
// Don't block subsequent grunt tasks.
background: true
},
// Continuous integration mode: run tests once in PhantomJS browser.
// Run this with `grunt karma:continuous`. This is used in `dist` (BUILD) task.
continuous: {
configFile: 'karma.conf.js',
singleRun: true,
browsers: ['PhantomJS']
}
},
/*
* Minify PNG and JPEG images using OptiPNG and jpegtran.
*/
imagemin: {
dist: {
options: {
// PNG only.
optimizationLevel: 3
},
files: [{
// Enable dynamic expansion.
expand: true,
// Src matches are relative to (but don't include) this path.
cwd: '<%= config.webroot %>/img/',
// Actual pattern(s) to match, relative to the cwd.
src: [
'**/*.{png,jpg,jpeg}'
],
// Destination path prefix.
dest: '<%= config.dist %>/img/'
}]
}
},
/*
* Concatenate dynamic banner information to built CSS and JS files.
*/
concat: {
options: {
// Strip any existing JavaScript banner comments from source files.
stripBanners: true,
// Get dynamic build header.
banner: '<%= banner %>'
},
dist: {
files: [
{
// Enable dynamic expansion.
expand: true,
// Src matches are relative to (but don't include) this path.
cwd: '<%= config.dist %>/',
// Actual pattern(s) to match, relative to the cwd.
src: [
// Process only main css files in CSS root.
'css/*.css',
// Process only main js files in JS app root.
'js/app/*.js',
// Process also the common layer.
'js/config.js'
],
// Destination path prefix.
dest: '<%= config.dist %>/',
// Set nonull to true if you want the concat task to warn
// if a given file is missing or invalid.
nonull: false
}
]
}
}
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-express');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-contrib-csslint');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-yuidoc');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-include-replace');
grunt.loadNpmTasks('grunt-grunticon');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-browser-sync');
// The default (DEV) task can be run just by typing "grunt" on the command line.
grunt.registerTask('default', [
'includereplace',
'compass',
'csslint',
'autoprefixer',
'jshint',
'express',
// On change, run the tests specified in the unit target using the already running karma server.
'karma:unit',
'browserSync',
'watch'
]);
// The optimized (DIST) production build would be run by typing "grunt dist" on the command line.
grunt.registerTask('dist', [
'includereplace',
'compass',
'csslint',
'autoprefixer',
'jshint',
'express',
// Run the tests specified in the continuous target using the already running karma server.
'karma:continuous',
'requirejs',
'yuidoc',
'imagemin',
'concat'
]);
// The icons generator (ICONS) would be run by typing "grunt icons" on the command line.
grunt.registerTask('icons', [
'grunticon'
]);
};