-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
392 lines (323 loc) · 11.5 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
/*global module:false*/
module.exports = function(grunt) {
//package config
var pkg = grunt.file.readJSON('package.json');
var config = getConfig();
//publish version
var publishVersion = config.publishVersion;
//subject publish version
var subPublishVersion = config.subPublishVersion;
var pageList = getListConfig(),
//base path
basePath = "src/",
baseStylePath = basePath + "style/",
baseJsPath = basePath + "js/",
//dest path
destPath = 'dest/',
destToPath = destPath + publishVersion + '/',
//dev path
devPath = 'dev/',
devCssPath = devPath + "style/";
//less 编译数组列表
var lessCompile = {};
var cssCompile = {};
//js编译列表
var jsConcat = {};
var jsMinify = {};
//开发模式编译数组列表
var lessDevCompile = {};
for (var i in pageList){
//less 编译数组列表
if(i.indexOf(".css") > -1){
var arrCssItem = [];
var cssDestPath = destToPath + "css/" + i;
var cssDestMinPath = destToPath + "css/" + i.replace(".css", "-min.css");
for(var j = 0; j < pageList[i].length; j++){
//online模式数组
arrCssItem[j] = basePath + pageList[i][j];
//dev模式数组
lessDevCompile[devPath + pageList[i][j].replace(".less", ".css")] = basePath + pageList[i][j];
}
lessCompile[cssDestPath] = arrCssItem;
cssCompile[cssDestMinPath] = cssDestPath;
}
//js 编译数组列表
if(i.indexOf(".js") > -1){
var arrJsItem = [];
var jsDestPath = destToPath + "js/" + i;
var jsDestMinPath = destToPath + "js/" + i.replace(".js", "-min.js");
for(var j = 0; j < pageList[i].length; j++){
arrJsItem[j] = basePath + pageList[i][j];
}
jsConcat[jsDestPath] = arrJsItem;
jsMinify[jsDestMinPath] = jsDestPath;
}
}
//console.log(lessCompile);
//console.log(cssCompile);
//console.log(jsConcat);
//console.log(jsMinify);
//console.log(lessDevCompile);
var jsImportConcat = {};
var jsImportMinify = {};
jsImportConcat[destToPath + 'cross.js'] = ['cross.list.js', 'cross.config.js', 'cross.import.js'];
jsImportMinify[destToPath + 'cross-min.js'] = destToPath + 'cross.js';
//从cross.config.js获取配置信息
function getConfig(){
var configContent = grunt.file.read("cross.config.js");
eval(configContent);
return Config;
};
//从cross.list.js获取配置信息
function getListConfig(){
var listContent = grunt.file.read("cross.list.js");
eval(listContent);
return crossList;
};
// 项目配置
grunt.initConfig({
//清除dest目录所有文件
pkg: pkg,
clean: {
options:{
force: true
},
dest: {
src: [destPath]
},
dev: {
src: [devPath]
},
js: {
src: [destToPath + 'js']
},
css: {
src: [destToPath + 'css']
},
img: {
src: [destToPath + 'img']
}
},
//将css背景图片资源复制到dest中
copy: {
destImg: {
expand: true,
cwd: basePath + 'img',
src: '**',
dest: destToPath + 'img'
}
},
qunit: {
files: ['test/**/*.html']
},
//css检查
csslint: {
options: {
//csslintrc: 'style/.csslintrc'
"qualified-headings": true,
"unique-headings": false,
"known-properties": false
},
test:{
src: [ devCssPath + '**/*.css']
}
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
browser: true,
globals: {
jQuery: true
}
},
test: {
src: [ baseJsPath + "**/*.js", "!" + baseJsPath + "core/*"]
}
},
watch: {
options: {
//spawn 设置为false 编译速度更快
spawn: false,
},
//配置文件修改监听
configFiles: {
files: ["Gruntfile.js", "cross.config.js", "cross.list.js", "cross.import.js"],
options: {
reload: true
},
tasks: ['dest-import']
},
//less文件修改监听
less: {
files: [ baseStylePath + '**/*.less', baseStylePath + '**/*.css'],
tasks: ['less:dev']
},
//css语法检查
css: {
files: '<%= csslint.test.src %>',
tasks: [ "csslint" ]
},
//js语法检查
js: {
files: "<%= jshint.test.src %>",
tasks: [ "jshint"]
}
},
//合并js
concat:{
import: {
files: jsImportConcat
},
js: {
files: jsConcat
}
},
//压缩js
uglify: {
options:{
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd") %> */',
compress: {
//移除console
drop_console: true
}
},
import: {
files: jsImportMinify
},
//页面文件压缩
dest: {
files: jsMinify
}
},
//less
less:{
//开发模式
dev:{
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',
//压缩
compress: false,
//替换变量
modifyVars: {
imgPath: '"../../../src/img/"'
}
},
files: lessDevCompile
},
//线上发布
dest:{
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',
//压缩
compress: false,
//ie兼容
ieCompat: true,
//插件配置
plugins: [
//浏览器兼容性处理,css3浏览器修复,自动添加-webkit等前缀
new (require('less-plugin-autoprefix'))({
browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1', 'ie 8', 'ie 9']
})
],
//替换变量
modifyVars: {
imgPath: '"../img/"'
}
},
files: lessCompile
}
},
//梳理css
csscomb: {
dest: {
options: {
//config: 'style/.csscomb.json'
},
files: cssCompile
}
},
cssmin: {
dest: {
options: {
},
files: cssCompile
}
},
//image min
imagemin: {
dest: {
options: {
optimizationLevel: 3 //定义图片优化水平
},
files: [{
expand: true,
cwd: destToPath + 'img/',
src: ['**/*.{png,jpg,jpeg}'], // 优化 cwd路径目录下所有 png/jpg/jpeg 图片
dest: destToPath + 'img/'
}]
}
}
});
// 加载所有依赖插件
require('load-grunt-tasks')(grunt, { scope: 'dependencies'});
//替换html文件中的version版本号
grunt.registerTask('replace-html', 'HTML version code replace.', function(){
//HTML文件替换任务
grunt.log.writeln("Running 'replace HTML files' task");
var fs = require('fs');
//同步的方式读取file文件
var filenameArr = fs.readdirSync('../');
//静态html文件对象
var staticFilePath = {};
//create a list of filenameArr
for (var i = filenameArr.length - 1; i >= 0; i--) {
if (/(?=.+).html/.test(filenameArr[i])) {
staticFilePath['../' + filenameArr[i]] = '../' + filenameArr[i];
};
};
//替换js/css目录中的url版本号
for (var m in staticFilePath){
var pageContent = grunt.file.read(m).replace(/v[0-9]{10}/g, publishVersion).replace(/[0-9]+.[0-9]+(?="\>\<\/script\>)/, subPublishVersion);
grunt.file.write(staticFilePath[m], pageContent);
console.log("'" + m + "' replace successfully");
}
grunt.log.oklns("finish replace HTML.");
});
//dest import
grunt.registerTask('dest-import', ['concat:import', 'uglify:import']);
//注册开发模式任务
//生成css
grunt.registerTask('dev-css', ['dest-import', 'less:dev']);
//dev
grunt.registerTask('dev', ['clean:dev', 'dev-css']);
//注册线上模式任务
//清除dest目录
grunt.registerTask('dest-clean', ['clean:dest']);
//生成css
grunt.registerTask('dest-css', ['clean:css', 'dest-import', 'less:dest', 'csscomb', 'cssmin']);
//生成js
grunt.registerTask('dest-js', ['clean:js', 'concat', 'uglify']);
//压缩图片
grunt.registerTask('dest-img', ['clean:img', 'copy:destImg', 'imagemin']);
//build 任务配置
grunt.registerTask('build', ['less:dest', 'csscomb', 'cssmin', 'concat', 'uglify', 'copy:destImg', 'imagemin']);
//online
grunt.registerTask('dest', 'CROSS dest task', function(){
grunt.log.writeln('Processing dest concat task...');
//文件清理,执行build,清除缓存文件
grunt.task.run(['replace-html', 'dest-clean', 'build']);
});
//default
grunt.registerTask('default', function(){
grunt.task.run(['dest']);
});
};