forked from CaryLandholt/AngularFun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrunt.js
399 lines (366 loc) · 11 KB
/
grunt.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
// Build configurations.
module.exports = function (grunt) {
grunt.initConfig({
/*
Deletes dist and temp directories.
The temp directory is used during the build process.
The dist directory contains the artifacts of the build.
These directories should be deleted before subsequent builds.
*/
delete: {
dist: {
files: ['./dist/']
},
temp: {
files: ['./temp/']
}
},
// CoffeeScript linting rules.
coffeeLint: {
scripts: {
files: ['./src/scripts/**/*.coffee', './test/scripts/**/*.coffee'],
// Use one tab for indentation.
indentation: {
value: 1,
level: 'error'
},
// No maximum line length.
max_line_length: {
level: 'ignore'
},
// Using tabs should not result in an error.
no_tabs: {
level: 'ignore'
}
}
},
// Compile CoffeeScript (.coffee) files to JavaScript (.js).
coffee: {
scripts: {
files: {
'./temp/scripts/': './src/scripts/**/*.coffee',
'./test/scripts/': './test/scripts/**/*.coffee'
},
// Don't include a surrounding Immediately-Invoked Function Expression (IIFE) in the compiled output.
// For more information on IIFEs, please visit http://benalman.com/news/2010/11/immediately-invoked-function-expression/
bare: true
}
},
// Compile LESS (.less) files to CSS (.css).
less: {
styles: {
files: {
'./temp/styles/styles.css': './src/styles/styles.less'
}
}
},
/*
Compile template files (.template) to HTML (.html).
.template files are essentially html; however, you can take advantage of features provided by grunt such as underscore templating.
The example below demonstrates the use of the environment configuration setting.
In 'prod' the concatenated and minified scripts are used along with a QueryString parameter of the hash of the file contents to address browser caching.
In environments other than 'prod' the individual files are used and loaded with RequireJS.
<% if (config.environment === 'prod') { %>
<script src="/scripts/scripts.min.js?v=<%= config.hash('./temp/scripts/scripts.min.js') %>"></script>
<% } else { %>
<script data-main="/scripts/main.js" src="/scripts/libs/require.js"></script>
<% } %>
*/
template: {
views: {
files: {
'./temp/views/': './src/views/**/*.template'
}
},
dev: {
files: {
'./temp/index.html': './src/index.template'
},
environment: 'dev'
},
prod: {
files: '<config:template.dev.files>',
environment: 'prod'
}
},
/*
Creates a single file consisting of multiple views (html) files surrounded by script tags.
For example, take the following two files:
<!-- /temp/views/people.html (compiled from /src/views/people.template) -->
<ul ng-hide="!people.length">
<li class="row" ng-repeat="person in people | orderBy:'name'">
<a ng-href="#/people/{{person.id}}" ng-bind="person.name"></a>
</li>
</ul>
<!-- /temp/views/repos.html (compiled from /src/views/repos.html) -->
<ul ng-hide="!repos.length">
<li ng-repeat="repo in repos | orderBy:'pushed_at':true">
<a ng-href="{{repo.url}}" ng-bind="repo.name" target="_blank"></a>
<div ng-bind="repo.description"></div>
</li>
</ul>
AngularJS will interpret inlined scripts with type of "text/ng-template" in lieu of retrieving the view from the server.
The id of the script tag must match the path requested.
Since the path includes the temp directory, this must be trimmed.
The output of the configuration below is:
<!-- /temp/views/views.html -->
<script id="/views/people.html" type="text/ng-template">
<ul ng-hide="!people.length">
<li class="row" ng-repeat="person in people | orderBy:'name'">
<a ng-href="#/people/{{person.id}}" ng-bind="person.name"></a>
</li>
</ul>
</script>
<script id="/views/repos.html" type="text/ng-template">
<ul ng-hide="!repos.length">
<li ng-repeat="repo in repos | orderBy:'pushed_at':true">
<a ng-href="{{repo.url}}" ng-bind="repo.name" target="_blank"></a>
<div ng-bind="repo.description"></div>
</li>
</ul>
</script>
Now the views.html file can be included in the application and avoid making requests to the server for the views.
*/
inlineTemplate: {
views: {
files: {
'./temp/views/views.html': './temp/views/**/*.html'
},
type: 'text/ng-template',
trim: 'temp'
}
},
// Copies directories and files from one location to another.
copy: {
// Copies libs and img directories to temp.
temp: {
files: {
'./temp/scripts/libs/': './src/scripts/libs/',
'./temp/img/': './src/img/'
}
},
/*
Copies the contents of the temp directory to the dist directory.
In 'dev' individual files are used.
*/
dev: {
files: {
'./dist/': './temp/'
}
},
/*
Copies select files from the temp directory to the dist directory.
In 'prod' minified files are used along with img and libs.
The dist artifacts contain only the files necessary to run the application.
*/
prod: {
files: {
'./dist/img/': './temp/img/',
'./dist/scripts/': './temp/scripts/scripts.min.js',
'./dist/scripts/libs': ['./temp/scripts/libs/html5shiv-printshiv.js', './temp/scripts/libs/json2.js'],
'./dist/styles/': './temp/styles/styles.min.css',
'./dist/index.html': './temp/index.min.html'
}
},
// Task is run when a watched script is modified.
scripts: {
files: {
'./dist/scripts/': './temp/scripts/'
}
},
// Task is run when a watched style is modified.
styles: {
files: {
'./dist/styles/': './temp/styles/'
}
},
// Task is run when the watched index.template file is modified.
index: {
files: {
'./dist/': './temp/index.html'
}
},
// Task is run when a watched view is modified.
views: {
files: {
'./dist/views/': './temp/views/'
}
}
},
/*
RequireJS optimizer configuration for both scripts and styles.
This configuration is only used in the 'prod' build.
The optimizer will scan the main file, walk the dependency tree, and write the output in dependent sequence to a single file.
Since RequireJS is not being used outside of the main file or for dependency resolution (this is handled by AngularJS), RequireJS is not needed for final output and is excluded.
RequireJS is still used for the 'dev' build.
The main file is used only to establish the proper loading sequence.
*/
requirejs: {
scripts: {
baseUrl: './temp/scripts/',
findNestedDependencies: true,
logLevel: 0,
mainConfigFile: './temp/scripts/main.js',
name: 'main',
// Exclude main from the final output to avoid the dependency on RequireJS at runtime.
onBuildWrite: function (moduleName, path, contents) {
var modulesToExclude = ['main'],
shouldExcludeModule = modulesToExclude.indexOf(moduleName) >= 0;
if (shouldExcludeModule) {
return '';
}
return contents;
},
optimize: 'uglify',
out: './temp/scripts/scripts.min.js',
preserveLicenseComments: false,
skipModuleInsertion: true,
uglify: {
// Let uglifier replace variables to further reduce file size.
no_mangle: false
}
},
styles: {
baseUrl: './temp/styles/',
cssIn: './temp/styles/styles.css',
logLevel: 0,
optimizeCss: 'standard',
out: './temp/styles/styles.min.css'
}
},
/*
Minifiy index.html.
Extra white space and comments will be removed.
Content within <pre /> tags will be left unchanged.
IE conditional comments will be left unchanged.
As of this writing, the output is reduced by over 14%.
*/
minifyHtml: {
prod: {
files: {
'./temp/index.min.html': './temp/index.html'
}
}
},
// Sets up file watchers and runs tasks when watched files are changed.
watch: {
scripts: {
files: './src/scripts/**/*.coffee',
tasks: 'coffeeLint:scripts coffee:scripts copy:scripts reload'
},
styles: {
files: './src/styles/**/*.less',
tasks: 'less copy:styles reload'
},
index: {
files: './src/index.template',
tasks: 'template:dev copy:index reload'
},
views: {
files: './src/views/**/*.template',
tasks: 'template:views copy:views reload'
}
},
/*
Runs a web server at the specified port.
Can optionally watch for changes to the file referenced in the watch setting.
The web server will automatically restart once the changes have been saved.
*/
server: {
app: {
src: './server.coffee',
port: 3005,
watch: './routes.coffee'
}
},
/*
Leverages the LiveReload browser plugin to automatically reload the browser when watched files have changed.
As of this writing, Chrome, Firefox, and Safari are supported.
Get the plugin:
here http://help.livereload.com/kb/general-use/browser-extensions
*/
reload: {
liveReload: true,
port: 35729
},
hash: {
dist: {
files: ['./dist/scripts/**/*.js']
}
}
});
/*
Register grunt tasks supplied by grunt-hustler.
Referenced in package.json.
https://github.com/CaryLandholt/grunt-hustler
*/
grunt.loadNpmTasks('grunt-hustler');
/*
Register grunt tasks supplied by grunt-reload.
Referenced in package.json.
https://github.com/webxl/grunt-reload
*/
grunt.loadNpmTasks('grunt-reload');
// A task to run unit tests in testacular.
grunt.registerTask('unit-tests', 'run the testacular test driver on jasmine unit tests', function () {
var done = this.async();
require('child_process').exec('./node_modules/testacular/bin/testacular start ./testacular.conf.js --single-run', function (err, stdout) {
grunt.log.write(stdout);
done(err);
});
});
/*
Compiles the app with non-optimized build settings and places the build artifacts in the dist directory.
Enter the following command at the command line to execute this build task:
grunt
*/
grunt.registerTask('default', [
'delete',
'coffeeLint',
'coffee',
'less',
'template:views',
'copy:temp',
'template:dev',
'copy:dev',
'delete:temp'
]);
/*
Compiles the app with non-optimized build settings, places the build artifacts in the dist directory, and watches for file changes.
Enter the following command at the command line to execute this build task:
grunt dev
*/
grunt.registerTask('dev', [
'default',
'reload',
'watch'
]);
/*
Compiles the app with optimized build settings and places the build artifacts in the dist directory.
Enter the following command at the command line to execute this build task:
grunt prod
*/
grunt.registerTask('prod', [
'delete',
'coffeeLint',
'coffee',
'less',
'template:views',
'inlineTemplate',
'copy:temp',
'requirejs',
'template:prod',
'minifyHtml',
'copy:prod',
'delete:temp'
]);
/*
Compiles the app with non-optimized build settings, places the build artifacts in the dist directory, and runs unit tests.
Enter the following command at the command line to execute this build task:
grunt test
*/
grunt.registerTask('test', [
'default',
'unit-tests'
]);
};