-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGruntfile.js
343 lines (317 loc) · 12.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
module.exports = function (grunt) {
/**
* Load required Grunt tasks. These are installed based on the versions listed
* in `package.json` when you do `npm install` in this directory.
*/
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-ngmin');
grunt.loadNpmTasks('grunt-recess');
/**
* This is the configuration object Grunt uses to give each plugin its
* instructions.
*/
grunt.initConfig({
/**
* The directory to which we throw our compiled project files.
*/
build_dir:'build',
/**
* We read in our `package.json` file so we can access the package name and
* version. It's already there, so we don't repeat ourselves here.
*/
pkg:grunt.file.readJSON("package.json"),
/**
* The banner is the comment that is placed at the top of our compiled
* source files. It is first processed as a Grunt template, where the `<%=`
* pairs are evaluated based on this very configuration object.
*/
meta:{
banner:'/**\n' +
' * <%= pkg.title || pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n' +
' * <%= pkg.homepage %>\n' +
' *\n' +
' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>\n' +
' * Licensed <%= pkg.licenses.type %> <<%= pkg.licenses.url %>>\n' +
' */\n'
},
/**
* This is a collection of file definitions we use in the configuration of
* build tasks. `js` is all project javascript, less tests. `atpl` contains
* our reusable components' template HTML files, while `ctpl` contains the
* same, but for our app's code. `html` is just our main HTML file and
* `less` is our main stylesheet.
*/
app_files:{
js:[ 'src/*.js', '!src/*.spec.js' ],
html:[ 'src/*.html' ],
less:'src/main.less',
unit:[ 'src/*.spec.js' ]
},
/**
* This is also a collection of file definitions we use in the
* configuration of build tasks, but it differs from the `src` property in
* that these values are entirely user-defined. While the `src` property
* ensures all standardized files are collected for compilation, it is the
* user's job to ensure non-standardized (i.e. vendor-related) files are
* handled appropriately.
*
* The `vendor.js` property holds files to be automatically concatenated
* and minified with our project source files.
*/
vendor_files:{
js:[
'vendor/jquery/jquery.min.js',
'vendor/angular-unstable/angular.min.js',
'vendor/hamsterjs/hamster.js',
'vendor/angular-mousewheel/mousewheel.js',
'vendor/google-code-prettify/src/prettify.js'
]
},
/**
* The directory to delete when `grunt clean` is executed.
*/
clean:[ '<%= build_dir %>' ],
/**
* Copies our static assets.
*/
copy:{
fonts:{
files:[
{
expand:true,
src:[ 'vendor/font-awesome/build/assets/font-awesome/font/*' ],
dest:'<%= build_dir %>/font',
flatten:true
}
]
}
},
/**
* `grunt concat` concatenates multiple source files into a single file.
*/
concat:{
/**
* The `dist` target is the concatenation of our application source code
* into a single file. All files matching what's in the `src.js`
* configuration property above will be included in the final build.
*
* In addition, the source is surrounded in the blocks specified in the
* `module.prefix` and `module.suffix` files, which are just run blocks
* to ensure nothing pollutes the global scope.
*
* The `options` array allows us to specify some customization for this
* operation. In this case, we are adding a banner to the top of the file,
* based on the above definition of `meta.banner`. This is simply a
* comment with copyright informaiton.
*/
dist:{
options:{
banner:'<%= meta.banner %>'
},
src:[ 'module.prefix', '<%= app_files.js %>', 'module.suffix' ],
dest:'<%= build_dir %>/<%= pkg.name %>.js'
},
/**
* The `libs` target is for all third-party libraries we need to include
* in the final distribution. They will be concatenated into a single
* `libs.js` file. One could combine this with the above for a single
* payload, but then concatenation order will obviously be important to
* get right.
*/
libs:{
src:[ '<%= vendor_files.js %>' ],
dest:'<%= build_dir %>/libs.js'
}
},
/**
* Use ng-min to annotate the sources before minifying
*/
ngmin:{
dist:{
src:[ '<%= build_dir %>/<%= pkg.name %>.js' ],
dest:'<%= build_dir %>/<%= pkg.name %>.annotated.js'
}
},
/**
* Minify the sources!
*/
uglify:{
options:{
banner:'<%= meta.banner %>'
},
dist:{
files:{
'<%= build_dir %>/<%= pkg.name %>.min.js':[ '<%= build_dir %>/<%= pkg.name %>.annotated.js' ]
}
}
},
/**
* recess handles our LESS compilation and uglification automatically. Only
* our `main.less` file is included in compilation; all other files must be
* imported from this file.
*/
recess:{
options:{
compile:true,
compress:false,
noUnderscores:false,
noIDs:false,
zeroUnits:false
},
build:{
src:[ '<%= app_files.less %>' ],
dest:'<%= build_dir %>/<%= pkg.name %>.css'
},
demo:{
src:[ '<%= app_files.less %>', 'src/demo.less' ],
dest:'<%= build_dir %>/<%= pkg.name %>-demo.css'
}
},
/**
* `jshint` defines the rules of our linter as well as which files we should
* check. This file, all java script sources, and all our unit tests are
* linted based on the policies listed in `options`. But we can allow
* specify exclusionary patterns for external components by prefixing them
* with an exclamation point (!).
*/
jshint:{
src:[
'Gruntfile.js',
'<%= app_files.js %>',
'<%= app_files.unit %>'
],
test:[
'<%= app_files.unit %>'
],
gruntfile:[
'Gruntfile.js'
],
options:{
curly:true,
immed:true,
newcap:true,
noarg:true,
sub:true,
boss:true,
eqnull:true
},
globals:{}
},
/**
* The Karma configurations.
*/
karma:{
options:{
configFile:'karma/karma-unit.js'
},
unit:{
background:true
},
continuous:{
singleRun:true
}
},
/**
* And for rapid development, we have a watch set up that checks to see if
* any of the files listed below change, and then to execute the listed
* tasks when they do. This just saves us from having to type "grunt" into
* the command-line every time we want to see what we're working on; we can
* instead just leave "grunt watch" running in a background terminal. Set it
* and forget it, as Ron Popeil used to tell us.
*
* But we don't need the same thing to happen for all the files.
*/
delta:{
/**
* By default, we want the Live Reload to work for all tasks; this is
* overridden in some tasks (like this file) where browser resources are
* unaffected. It runs by default on port 35729.
*/
options:{
livereload:true
},
/**
* When the Gruntfile changes, we just want to lint it. That said, the
* watch will have to be restarted if it should take advantage of any of
* the changes.
*/
gruntfile:{
files:'Gruntfile.js',
tasks:[ 'jshint:gruntfile' ],
options:{
livereload:false
}
},
/**
* When our source files change, we want to run most of our build tasks
* (excepting uglification).
*/
src:{
files:[
'<%= app_files.js %>'
],
tasks:[ 'jshint:src', /*'karma:unit:run',*/ 'concat:dist', 'ngmin:dist', 'uglify:dist' ]
},
/**
* When index.html changes, we need to compile just it.
*/
html:{
files:[ '<%= app_files.html %>' ],
tasks:[ 'index' ]
},
/**
* When the CSS files change, we need to compile and minify just them.
*/
less:{
files:[ 'src/**/*.less' ],
tasks:[ 'recess' ]
},
/**
* When a unit test file changes, we only want to linit it and run the
* unit tests.
*/
unittest:{
files:[
'<%= app_files.unit %>'
],
tasks:[ 'jshint:test', 'karma:unit:run' ],
options:{
livereload:false
}
}
}
});
/**
* In order to make it safe to just compile or copy *only* what was changed,
* we need to ensure we are starting from a clean, fresh build. So we rename
* the `watch` task to `delta` (that's why the configuration var above is
* `delta`) and then add a new task called `watch` that does a clean build
* before watching for changes.
*/
grunt.renameTask('watch', 'delta');
grunt.registerTask('watch', [ 'default', /*'karma:unit',*/ 'delta' ]);
/**
* The default task is to build.
*/
grunt.registerTask('default', [ 'build' ]);
grunt.registerTask('build', ['clean', 'jshint', /*'karma:continuous',*/ 'copy', 'concat', 'ngmin:dist', 'uglify', 'recess', 'index']);
/**
* A task to build the project, without some of the slower processes. This is
* used during development and testing and is part of the `watch`.
*/
grunt.registerTask('quick-build', ['clean', 'jshint', 'test', 'concat', 'recess', 'index']);
/**
* The index.html template includes the stylesheet and javascript sources
* based on dynamic names calculated in this Gruntfile. This task compiles it.
*/
grunt.registerTask('index', 'Process index.html template', function () {
grunt.file.copy('src/index.html', grunt.config.get('build_dir') + '/index.html', { process:grunt.template.process });
grunt.file.copy('src/sample1.html', grunt.config.get('build_dir') + '/sample1.html', { process:grunt.template.process });
});
};