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
"use strict";
var LIVERELOAD_PORT, lrSnippet, mountFolder;
/*解决跨域问题*/
var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
LIVERELOAD_PORT = 35728;
/*页面自动刷新,即代码有修改页面就自动刷新*/
lrSnippet = require("connect-livereload")({
port: LIVERELOAD_PORT
});
mountFolder = function(connect, dir) {
return connect["static"](require("path").resolve(dir));
};
module.exports = function(grunt) {
var yeomanConfig;
/*自动读取并加载项目 packge.json 文件中 devDependencies 配置下以 grunt-* 开头的依赖库*/
require("load-grunt-tasks")(grunt);
/*构建完成后,会给出每个任务的耗时及占总耗时的百分比*/
require("time-grunt")(grunt);
yeomanConfig = {
app: "src/main/websrc",
dist: "dist",
docs: "documentation"
};
grunt.initConfig({
yeoman: yeomanConfig,
watch: {
compass: {
files: ["<%= yeoman.app %>/styles/**/*.{scss,sass}"],
tasks: ["compass:server"]
},
livereload: {
options: {
livereload: LIVERELOAD_PORT //或者true
},
files: [
"<%= yeoman.app %>/index.html",
"<%= yeoman.app %>/template/**/*.html",
"<%= yeoman.app %>/js/**/*.js",
"<%= yeoman.app %>/styles/**/*.scss",
".tmp/styles/**/*.css",
"<%= yeoman.app %>/images/**/*.{png,jpg,jpeg,gif,webp,svg}"
]
}
},
connect: {
proxies: [{
context: ['/api'],
host: 'localhost',
// host: '192.168.2.37',
port: 8091,
https: false,
xforward: true,
hideHeaders: ['x-removed-header']
}],
options: {
port: 9008,
hostname: "*"
},
livereload: {
options: {
middleware: function (connect) {
return [lrSnippet, mountFolder(connect, ".tmp"), mountFolder(connect, yeomanConfig.app), proxySnippet];
}
}
},
test: {
options: {
middleware: function (connect) {
return [mountFolder(connect, ".tmp"), mountFolder(connect, "test")];
}
}
},
dist: {
options: {
middleware: function (connect) {
//return [mountFolder(connect, yeomanConfig.dist)];
return [lrSnippet, mountFolder(connect, yeomanConfig.dist), proxySnippet];
}
}
}
},
open: {
server: {
url: "http://localhost:<%= connect.options.port %>"
}
},
clean: {
dist: {
files: [
{
dot: true,
src: [".tmp", "<%= yeoman.dist %>/*", "!<%= yeoman.dist %>/.git*"]
}
]
},
all: [".tmp", ".sass-cache", "readme.md", "node_modules", ".git"],
server: ".tmp"
},
jshint: { //代码质量检查
options: {
jshintrc: ".jshintrc"
},
all: ["Gruntfile.js", "<%= yeoman.app %>/app/**/*.js"]
},
injector: { // JS/CSS 自动注入工具, 可以自动将 JS/CSS 的引用注入到 HTML 文件中
options: {
relative: true
},
local_dependencies: {
files: {
"<%= yeoman.app %>/index.html": [
"<%= yeoman.app %>/app/**/*.module.js",
"<%= yeoman.app %>/app/**/*.js",
"!" + "<%= yeoman.app %>/**/*.spec.js",
"<%= yeoman.app %>/**/*.css"
]
}
}
},
compass: {
options: {
sassDir: "<%= yeoman.app %>/styles",
cssDir: ".tmp/styles",
imagesDir: "<%= yeoman.app %>/images/",
javascriptsDir: "<%= yeoman.app %>/js",
httpFontsPath: "fonts",
relativeAssets: true
},
dist: {
options: {
outputStyle: 'compressed',
debugInfo: false,
noLineComments: true
}
},
server: {
options: {
debugInfo: true
}
},
forvalidation: {
options: {
debugInfo: false,
noLineComments: false
}
}
},
useminPrepare: {
html: ["<%= yeoman.app %>/index.html","<%= yeoman.app %>/login.html","<%= yeoman.app %>/activation.html","<%= yeoman.app %>/failure.html","<%= yeoman.app %>/forget.html","<%= yeoman.app %>/register.html","<%= yeoman.app %>/success.html","<%= yeoman.app %>/resetpwd.html","<%= yeoman.app %>/olduser.html","<%= yeoman.app %>/demo.html","<%= yeoman.app %>/home.html","<%= yeoman.app %>/main.html"],
options: {
dest: "<%= yeoman.dist %>",
flow: {
steps: {
js: ["concat"],
css: ["concat"] //concat,cssmin
},
post: []
}
}
},
usemin: {
html: ["<%= yeoman.dist %>/**/*.html"],
options: {
dirs: ["<%= yeoman.dist %>"],
blockReplacements: {
css:function (block) {
var final_name = block.dest;
final_name += "?r="+Math.random();
var media = block.media ? ' media="' + block.media + '"' : '';
return '<link rel="stylesheet" href="' + final_name + '"' + media + '>';//此处为css标签的定制
},
js:function (block) {
var final_name = block.dest;
final_name += "?r="+Math.random();
var defer = block.defer ? 'defer ' : '';
var async = block.async ? 'async ' : '';
return '<script ' + defer + async + 'src="' + final_name + '"><\/script>';//次处为js标签的定制
}
}
}
},
htmlmin: {
dist: {
options: {},
files: [
{
expand: true,
cwd: "<%= yeoman.app %>",
src: ["*.html", "template/**/*.html"],
dest: "<%= yeoman.dist %>"
}
]
}
},
copy: {
dist: {
files: [
{
expand: true,
dot: true,
cwd: "<%= yeoman.app %>",
dest: "<%= yeoman.dist %>",
src: [
"data/**/*",
"font/**/*",
"images/**/*",
"**/*.html",
"template/**/*.png",
"template/**/*.jpg",
"*.ico"
]
},
{
expand: true,
cwd: ".tmp/images",
dest: "<%= yeoman.dist %>/images",
src: ["generated/*"]
},
{
expand: true,
cwd: "",
dest: "<%=yeoman.dist%>",
src: ["Gruntfile.js",'test/**/*']
}
]
},
styles: {
// expand: true,
// cwd: "<%= yeoman.app %>/styles",
// dest: ".tmp/styles/",
// src: "**/*.css"
}
},
concurrent: {
server: ["compass:server", "copy:styles"],
dist: ["compass:dist", "copy:styles", "htmlmin"]
},
cssmin: {
options: {
keepSpecialComments: '0'
},
build: {
files:{
"<%= yeoman.dist %>/styles/index.css":[".tmp/styles/*.css","<%= yeoman.app %>/styles/*.css","!.tmp/styles/login.css","!.tmp/styles/home.css"],
"<%= yeoman.dist %>/styles/login.css":[".tmp/styles/login.css"],
"<%= yeoman.dist %>/styles/home.css":[".tmp/styles/home.css"]
}
}
},
concat: {
options: {
separator: grunt.util.linefeed + ';' + grunt.util.linefeed
},
// cssbuild: {
// src: ".tmp/styles/*.css",
// dest: "<%= yeoman.dist %>/styles/app.css"
// },
dist: {}
},
uglify: {
options: {
mangle: true, //混淆变量名
compress: {
drop_console: true
}
},
dist: { // if do this uglify for app, the app can not start up
files: {
"<%= yeoman.dist %>/scripts/app.js": ["<%= yeoman.dist %>/scripts/app.js"],
"<%= yeoman.dist %>/scripts/ui.js": ["<%= yeoman.dist %>/scripts/ui.js"],
"<%= yeoman.dist %>/scripts/lgui.js": ["<%= yeoman.dist %>/scripts/lgui.js"],
"<%= yeoman.dist %>/scripts/regui.js": ["<%= yeoman.dist %>/scripts/regui.js"],
"<%= yeoman.dist %>/scripts/change.js": ["<%= yeoman.dist %>/scripts/change.js"],
"<%= yeoman.dist %>/scripts/failure.js": ["<%= yeoman.dist %>/scripts/failure.js"],
"<%= yeoman.dist %>/scripts/forget.js": ["<%= yeoman.dist %>/scripts/forget.js"],
"<%= yeoman.dist %>/scripts/register.js": ["<%= yeoman.dist %>/scripts/register.js"],
"<%= yeoman.dist %>/scripts/success.js": ["<%= yeoman.dist %>/scripts/success.js"],
"<%= yeoman.dist %>/scripts/resetpwd.js": ["<%= yeoman.dist %>/scripts/resetpwd.js"],
"<%= yeoman.dist %>/scripts/olduser.js": ["<%= yeoman.dist %>/scripts/olduser.js"]
}
}
},
mock: { //currently just list all mock api in this files, we need to separate them based the module.
your_target: {
options: {
port: 9001,
delay: 200,
cookie: {},
placeholders: {
getNumArr: function (len) { //返回len个随机整数
var arr = [];
for(var i=0;i<len;i++){
arr.push(this.d1000());
}
return arr;
},
getFloatArr: function (len) { //返回len个随机浮点数
var arr = [];
for(var i=0;i<len;i++){
arr.push(this.float(0,100,0,2));
}
return arr;
},
getLastDates: function (len,hasToday) { //返回最近len天数据,hasToday为true从今天开始往前推
var arr = [];
var dd = new Date(),d = dd.getDate();
var m = d,start = 1,end = len;
if(hasToday && hasToday==true){
start = 0;
end -= 1;
}
for(var i=start;i<=end;i++){
var day = m - i;
if(day == 0){
dd.setDate(m-i);
m += dd.getDate();
}
else{
dd.setDate(m-i);
}
arr.unshift(this.date(dd,'YYYY-MM-DD'));
}
return arr;
},
getLastDay: function (len) { //返回最近第len天,0是当天
var dd = new Date(),d = dd.getDate();
dd.setDate(d-len);
return this.date(dd,'YYYY-MM-DD')
},
getLastMonth: function (len) { //返回最近第len月的第一天,0是当月,eg:2017-03-01
var dd = new Date(),m = dd.getMonth();
dd.setMonth(m-len);
dd.setDate(1);
return this.date(dd,'YYYY-MM-DD')
},
getLastWeek: function (len) { //返回最近第几周的周一,0是当周的周一
var dd = new Date(),d = dd.getDate(),w = dd.getDay();
w = w == 0 ? 7 : w;
dd.setDate(d-w+1-len*7);
return this.date(dd,'YYYY-MM-DD');
},
getHourData: function (num,hour) { //根据小时对比返回当前应显示数
var nh = new Date().getHours();
if(nh>=hour){
return num;
}
else{
return 0;
}
}
},
route: {
}
},
cwd: './test/mocks',
src: ['*.json']
}
}
});
// grunt.registerTask("docs", function() {
// return grunt.task.run(["jade:docs", "connect:docs", "open", "watch"]);
// });
grunt.registerTask("server", function(target) {
if (target === "dist") {
return grunt.task.run(["serve:dist"]);
}
return grunt.task.run(["serve"]);
});
grunt.registerTask("serve", function(target) {
if (target === "dist") {
//return grunt.task.run(["build", "open", "connect:dist:keepalive"]);
return grunt.task.run(["build", "configureProxies", "connect:dist:keepalive" , "open"]);
}
return grunt.task.run(["clean:server", "concurrent:server", "configureProxies", "connect:livereload", "open", "watch"]);
});
grunt.registerTask("build", ["clean:dist", "useminPrepare", "concurrent:dist", "copy:dist", "concat", "cssmin", "uglify", "usemin"]);
return grunt.registerTask("default", ["build"]);
};