-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
executable file
·361 lines (333 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
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
'use strict';
module.exports = function(grunt) {
// Load all grunt plugins that configure in `package.json`
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-notify');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-jsdoc');
grunt.loadNpmTasks('grunt-html2js');
/**
* Load the custom project configuration file
* Because each project is different, so user can define their project's information in this file
*/
var projectConfig = require('./project.config.js');
var commonConfig = {
/**
* We read our `package.json` file so we can access the package name and version.
* It's already there, we don't need to repeat here. This can reduce our maintain cause.
*/
pkg: grunt.file.readJSON('package.json'),
/**
* `meta` store all basic information that will be used in js, css, html... files
* For example, `banner` is the comment that is placed at the top of our compiled files.
*/
meta: {
banner: '/*! \n' + ' * <%= pkg.title || pkg.name %> - v<%= pkg.version %>\n' + ' * Build Time: <%= grunt.template.today("dddd, mmmm dS, yyyy, h:MM:ss TT") %>\n' + ' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>;\n' + ' */\n'
},
/**
* `clean`: Clean files and folders
* https://www.npmjs.org/package/grunt-contrib-clean
*/
clean: {
development: ['<%= project_config.build_dir %>'],
production : ['<%= project_config.production_dir %>']
},
/**
* `jshint` defines the rules of our linter as well as which files we
* should check. This file, all javascript sources, and all our unit tests
* are linted based on the policies listed in `options`. But we can also
* specify exclusionary patterns by prefixing them with an exclamation
* point (!); this is useful when code comes from a third party but is
* nonetheless inside `src/`.
*/
jshint: {
options: {
force : true,
reporter : 'checkstyle',
reporterOutput: '<%= project_config.log_dir %>jshint-reporter.xml'
},
app_js : ['<%= project_config.app_files.app_js.src %>']
},
/**
* `concat`: Concatenate files
* https://www.npmjs.org/package/grunt-contrib-concat
*/
concat: {
production : {
options: {
banner: '<%= meta.banner %>'
},
src : ['<%= project_config.app_files.app_js.src %>'],
dest : '<%= project_config.production_dir %>js/<%= pkg.name %>.js'
}
},
/**
* `grunt-html2js`: Compiles AngularJS templates to JavaScript
* 1. In `development` mode, we don't compile it
* 2. In `procduction` mode, we compile it, and put it to `partials/template.js`
*/
html2js: {
development : {
options: {
module: 'template',
base: 'src'
},
src : [],
dest : '<%= project_config.build_dir %>/js/<%= pkg.name %>.tpl.js'
},
production : {
options: {
module: 'template',
base: 'src'
},
src : ['<%= project_config.app_files.tpl.src %>'],
dest : '<%= project_config.production_dir %>/js/<%= pkg.name %>.tpl.js'
}
},
/**
*
*/
index: {
development: {
dir: '<%= project_config.build_dir %>',
cwd: '<%= project_config.build_dir%>',
src: [
'vendor/**/jquery.js',
'vendor/**/angular.js',
'vendor/**/*.css',
'assets/**/*.css',
'vendor/**/*.js',
'common/**/*.js',
'**/*.js',
'!vendor/**/*.min.*',
'!vendor/**/*.min.css'
]
},
production: {
dir: '<%= project_config.production_dir %>',
cwd: '<%= project_config.production_dir%>',
src: [
'vendor/**/jquery.js',
'vendor/**/angular.js',
'vendor/**/*.js',
'vendor/**/*.css',
'assets/**/*.css',
'js/**/*.tpl.js',
'js/**/*.js',
'!js/**/*.min.js',
'!vendor/**/*.min.js',
'!vendor/**/*.min.css'
]
}
},
/**
* `less`: Compile LESS files to CSS
* https://www.npmjs.org/package/grunt-contrib-less
*/
less: {
development: {
options: {
compress: false
},
files : [
{
'<%= project_config.app_files.less.dest %>': '<%= project_config.app_files.less.src %>'
}
]
},
production : {
options: {
compress: true
},
files : [
{
'<%= project_config.production_dir %>asset/css/<%= pkg.name %>.min.css': '<%= project_config.app_files.less.src %>'
}
]
}
},
/**
* `copy`: Copy files and folders
* https://npmjs.org/package/grunt-contrib-copy
*/
copy: {
development: {
files: [
{
expand: true,
cwd : '<%= project_config.source_dir %>',
src : ['*.html', '**/*', '<%= project_config.app_files.asset %>', '<%= project_config.vendor%>', '!**/*.less'],
dest : '<%= project_config.build_dir %>'
}
]
},
production : {
files: [
{
expand: true,
cwd : '<%= project_config.source_dir %>',
src : ['*.html', '<%= project_config.app_files.asset %>', '<%= project_config.vendor%>', '!**/*.less'],
dest : '<%= project_config.production_dir %>'
}
]
}
},
/**
* `uglify`: JavaScript parser, mangler/compressor and beautifier toolkit
* https://www.npmjs.org/package/uglify-js
*/
uglify: {
production : {
files : [
{
src : ['<%= project_config.production_dir %>js/<%= pkg.name %>.js'],
dest: '<%= project_config.production_dir %>js/<%= pkg.name %>.min.js'
},
{
src : ['<%= project_config.production_dir %>js/<%= pkg.name %>.tpl.js'],
dest: '<%= project_config.production_dir %>js/<%= pkg.name %>.tpl.min.js'
}
]
}
},
/**
* `connect`: Start a connect web server
* https://www.npmjs.org/package/grunt-contrib-connect
*/
connect: {
project: {
options: {
port : 9000,
hostname: '127.0.0.1',
base : '<%= project_config.build_dir %>',
open : true
}
},
api : {
options: {
port : 9001,
hostname: '127.0.0.1',
base : '<%= project_config.api_dir %>',
open : true
}
}
},
/**
* `notify`: Automatic Notifications when Grunt tasks fail
* https://github.com/dylang/grunt-notify
*/
notify: {
release: {
options: {
title : 'Release success',
message: 'Generate release build success. Please get it in "release" folder.'
}
},
build : {
options: {
title : 'Build success',
message: 'Build project successful. Please get it in "build" folder'
}
},
project: {
options: {
title : 'Start project Server',
message: 'Server is ready! You can go to http://localhost:9000'
}
},
api : {
options: {
title : 'Start API Document server',
message: 'Server is ready! You can go to http://localhost:9001'
}
}
},
/**
* `qunit`: Run QUnit unit tests in a headless PhantomJS instance.
* https://www.npmjs.org/package/grunt-contrib-qunit
*/
qunit: {
all: ['test/unit/qunit.html']
},
/**
* `jsdoc`: Integrates jsdoc3 generation into your Grunt build
* https://www.npmjs.org/package/grunt-jsdoc
*/
jsdoc: {
dist: {
src : ['<%= project_config.app_files.app_js.src%>'],
dest : '<%= project_config.api_dir %>',
options: {
verbose : true,
configure: 'jsdoc-template/jsdoc-jaguar/conf.json',
template : 'jsdoc-template/jsdoc-jaguar/',
private : false
}
}
},
/**
* `watch`: Run predefined tasks whenever watched file patterns are added, changed or deleted
* https://npmjs.org/package/grunt-contrib-watch
*/
watch: {
development: {
files: ['<%= project_config.source_dir %>**/*'],
tasks: ['build']
}
}
};
/**
* A utility function to get all app JavaScript sources.
*/
function filterForJS(files) {
return files.filter(function(file) {
return file.match(/\.js$/);
});
}
/**
* A utility function to get all app CSS sources.
*/
function filterForCSS(files) {
return files.filter(function(file) {
return file.match(/\.css$/);
});
}
/**
* The index.html template includes the stylesheet and javascript sources
* based on dynamic names calculated in this Gruntfile. This task assembles
* the list into variables for the template to use and then runs the
* compilation.
*/
grunt.registerMultiTask('index', 'Process index.html template', function() {
console.info(this.filesSrc);
// Replace source_dir, build_dir, production_dir to ''
// var dirRE = new RegExp('^(' + grunt.config('project_config.build_dir') + '|' + grunt.config('project_config.source_dir') + '|' + grunt.config('project_config.production_dir') + ')\/', 'g');
var jsFiles = filterForJS(this.filesSrc);
var cssFiles = filterForCSS(this.filesSrc);
grunt.file.copy(this.data.dir + 'index-original.html', this.data.dir + 'index.html', {
process: function(contents, path) {
return grunt.template.process(contents, {
data: {
scripts: jsFiles,
styles : cssFiles,
version: grunt.config('pkg.version')
}
});
}
});
});
// Project configuration.
grunt.initConfig(grunt.util._.extend(commonConfig, projectConfig));
grunt.registerTask('development', ['build', 'connect:project', 'notify:project', 'watch']);
grunt.registerTask('default', ['build']);
grunt.registerTask('build', ['clean:development', 'jshint', 'less:development', 'copy:development', 'index:development', 'notify:build']);
grunt.registerTask('release', ['clean:production', 'jshint', 'concat:production', 'html2js:production', 'less:production', 'copy:production', 'uglify:production', 'index:production', 'notify:release']);
grunt.registerTask('api-doc', ['jsdoc', 'notify:api']);
};