forked from Tencent/tmt-workflow
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGruntfile.js
354 lines (321 loc) · 12.3 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
/**
* f2e-workflow v1.6.9
* https://github.com/Mobile-Team/f2e-workflow
* @hzlzh <[email protected]>
*/
module.exports = function(grunt) {
// time-grunt 初始化
require('time-grunt')(grunt);
// Grunt 配置初始化
grunt.initConfig({
// 读取 package.json 依赖
pkg: grunt.file.readJSON('package.json'),
// Less 编译 CSS,若使用 Sass 请自行替换
less: {
// 编译
// less 分支 -> 开发向
dev: {
files: [{
expand: true, // 启用动态扩展
cwd: 'css/', // CSS 文件源的文件夹
src: ['*.less', '!import*.less'], // 匹配规则
dest: 'tmp/css/', //导出 CSS 和雪碧图的路径地址
ext: '.css' // 导出的 CSS 名
}],
options: {
yuicompress: false // 开启 YUI CSS 压缩 (http://yui.github.io/yuicompressor/)
}
},
// less 分支 -> 发布向
release: {
files: [{
expand: true, //启用动态扩展
cwd: 'css/', // CSS 文件源的文件夹
src: ['*.less', '!import*.less'], // 匹配规则
dest: 'tmp/css/', //导出 CSS 和雪碧图的路径地址
ext: '.css' // 导出的 CSS名
}],
options: {
yuicompress: true // 开启 YUI CSS 压缩 (http://yui.github.io/yuicompressor/)
}
}
},
// CSS 验证检查 (https://github.com/gruntjs/grunt-contrib-csslint)
csslint: {
options: {
formatters: [{
id: 'junit-xml',
dest: 'report/csslint_junit.xml'
}, {
id: 'csslint-xml',
dest: 'report/csslint.xml'
}]
},
strict: {
options: {
import: 2
},
src: ['publish/css/*.css']
},
lax: {
options: {
import: false
},
src: ['publish/css/*.css']
}
},
// CSS 压缩 (https://github.com/gruntjs/grunt-contrib-cssmin)
cssmin: {
min: {
files: [{
expand: true,
cwd: 'tmp/css',
src: ['*.sprite.css'],
dest: 'tmp/css',
ext: '.css'
}]
}
},
// 自动合并生成雪碧图
sprite: {
sprite: {
files: [{
expand: true, // 启用动态扩展
cwd: 'tmp/css', // CSS 文件源的文件夹
src: ['*.css'], // 匹配规则
dest: 'tmp/', // 导出 CSS 和雪碧图的路径地址
ext: '.sprite.css' // 导出的 CSS 名
}],
// options
options: {
// 选择图片处理引擎: auto, canvas, gm
'engine': 'gm',
// 设置雪碧图合并算法,如:二叉树算法(top-down, left-right, diagonal, alt-diagonal)
'algorithm': 'binary-tree',
// 默认给雪碧图追加时间戳,如:background-image:url(../sprite/[email protected]?20140304100328);
'imagestamp': true,
// 默认给样式文件追加时间戳,如:.TmTStamp{content:"20140304100328"}
'cssstamp': false,
// 每次编译生成新文件名,如:style-20140304102859.png
'newsprite': false
}
}
},
// 时间戳,去缓存
// https://www.npmjs.org/package/grunt-timestamp
timestamp: {
timestamp: {
files: [{
// Use dynamic extend name
expand: true,
// Open source dir
cwd: 'tmp/css',
// Match files
src: ['*.sprite.css'],
// Output files
dest: 'tmp/css',
// Set extend middle name
ext: '.timestamp'
}],
options: {
// Timestamp display text
'timestampName': 'Timetamp',
// Date format
'timestampFormat': 'yyyy/mm/dd HH:MM:ss',
// Add timestamp at the end of the files' content(.css/.js).
'timestampType': 'time',
// Timestamp type like time(2014/04/02 22:17:07) | md5/sha1/ha256/sha512).
'fileEndStamp': true,
// Add timestamp at images of CSS style.
'cssImgStamp': true,
// Rename file name with timestamp inside.
'fileNameStamp': true
}
}
},
// PNG 压缩 (更多配置说明:https://www.npmjs.org/package/grunt-pngmin)
pngmin: {
compile: {
options: {
ext: '.png', // 后缀名
force: true, // 生成优化后的图片覆盖原始图片
iebug: false // 为 IE6 优化图片,如需要可设置`true`
},
files: [{
src: ['release/sprite/*.png'],
dest: 'release/sprite/'
}, {
expand: true,
cwd: 'release/img',
src: ['**/*.png'],
dest: 'release/img',
ext: '.png'
}]
}
},
// 复制文件夹操作
copy: {
// 移动 slice/ 到 tmp/ 供下一步的 合并雪碧图 task 使用
slice: {
files: [{
expand: true,
cwd: 'slice/',
src: ['**'],
dest: 'tmp/slice/'
}, ]
},
// copy 分支 -> 开发向
dev: {
files: [{
expand: true,
cwd: 'tmp/css/',
src: ['**', '!*.timestamp.css', '!*.sprite.css', '!*.min.css'],
dest: 'publish/css/'
}, {
expand: true,
cwd: 'slice/',
src: ['**'],
dest: 'publish/slice/'
}, {
expand: true,
cwd: 'img/',
src: ['**'],
dest: 'publish/img/'
}, ]
},
// copy 分支 -> 发布向
release: {
files: [{
expand: true,
cwd: 'tmp/css/',
src: ['*.css', '!*.timestamp.css', '!*.sprite.css', '!*.min.css'],
dest: 'release/css/'
}, {
expand: true,
cwd: 'img/',
src: ['**'],
dest: 'release/img/'
}, {
expand: true,
cwd: 'tmp/sprite/',
src: ['**'],
dest: 'release/sprite/'
}]
},
// copy 分支 -> 调试向
debug: {
files: [{
expand: true,
cwd: 'tmp/css/',
src: ['*.css', '!*.timestamp.css', '!*.sprite.css', '!*.min.css'],
dest: 'publish/css/'
}, {
expand: true,
cwd: 'img/',
src: ['**'],
dest: 'publish/img/'
}, {
expand: true,
cwd: 'tmp/sprite/',
src: ['**'],
dest: 'publish/sprite/'
}]
}
},
// 检测 文件/代码 变动事件
watch: {
files: 'css/*.less',
tasks: ['less:dev', 'copy:dev', 'clean:dev']
},
// FTP 部署,上传 release/ 所有文件到预先设置的FTP
'ftp-deploy': {
push: {
auth: {
host: 'xxx.xxx.xxx.xxx',
port: 21000,
authKey: 'xxx'
},
src: 'release/',
dest: 'proj-<%= pkg.name %>/',
exclusions: ['**/.DS_Store', '**/Thumbs.db', 'tmp'] // 不上传文件类型
}
},
// 自动生成 @2x 图片对应的 @1x 图 (已存在图片不再生成,仅缺失图片触发此操作)
_2x2x: {
scale: {
imgsrcdir: "slice", // 源目录,此目录中的 @2x -> @1x
imgdesdir: "slice", // 目标目录
option: {
'overwrite': true // 是否覆盖原图
}
}
},
// 清理临时目录
clean: {
// clean 开发向
dev: ['tmp/', 'publish/sprite/', 'release/'],
// clean 发布向
release: ['tmp/', 'publish/', 'release/'],
// clean 调试向
debug: ['tmp/', 'publish/slice/']
},
// 文件夹打包压缩 Zip
compress: {
main: {
options: {
archive: 'proj-<%= pkg.name %>-' + 'release.zip' // 设置压缩包名称
},
files: [{
expand: true,
src: "**/*",
cwd: "release/"
} // 设置压缩范围为整个 `release/` 发布目录
]
}
}
});
// // 加载官方插件
// grunt.loadNpmTasks('grunt-contrib-less');
// grunt.loadNpmTasks('grunt-contrib-csslint');
// grunt.loadNpmTasks('grunt-contrib-cssmin');
// grunt.loadNpmTasks('grunt-contrib-copy');
// grunt.loadNpmTasks('grunt-contrib-clean');
// grunt.loadNpmTasks('grunt-contrib-watch');
// grunt.loadNpmTasks('grunt-contrib-compress');
//
// // 加载其他插件
// grunt.loadNpmTasks('grunt-sprite');
// grunt.loadNpmTasks('grunt-ftp-deploy');
// grunt.loadNpmTasks('grunt-pngmin');
// grunt.loadNpmTasks('grunt-timestamp');
// grunt.loadNpmTasks('grunt-2x2x');
require('jit-grunt')(grunt);
// 按需 load,文档:https://www.npmjs.org/package/jit-grunt
/* 任务注册开始 */
// == 默认工作流 ==
// 输出目录为:../publish/(css/ + img/ + slice/)
// 注:仅做编译操作 Less/Sass -> CSS,无其他操作
grunt.registerTask('default', ['less:dev', 'copy:dev', 'clean:dev', 'watch']);
// == 完整发布流 ==
// 输出目录为:../publish/(css/ + img/ + sprite/)
// 注:包括 Less/Sass 编译+压缩+雪碧图拼合+PNG压缩,仅执行1次流,不含(文件变动 watch)
grunt.registerTask('all', ['less:release', 'sprite-cssmin', 'timestamp', 'copy:release', 'pngmin']);
// == 调试工作流 ==
// 输出目录为:../publish/(css/ + img/ + sprite/)
// 注:同 `grunt all`,但不删除 tmp/ 目录,供调试查看使用,含(文件变动 watch)
grunt.registerTask('debug', ['clean:release', 'less:release', 'sprite-cssmin', 'copy:debug', 'watch']);
// == FTP 发布操作 ==
// 注:将 `grunt all` 生成结果使用 FTP 上传到服务端
grunt.registerTask('push', ['all', 'ftp-deploy:push', 'clean:release']);
// == ZIP 发布操作 ==
// 注:将 `grunt all` 生成结果使用 ZIP 生成包文件
grunt.registerTask('zip', ['all', 'compress', 'clean:release']);
// 定义别名 `grunt sprite-cssmin`
// 注:拷贝移动 slice -> 合并雪碧图 sprite -> CSS 压缩
grunt.registerTask('sprite-cssmin', ['copy:slice', 'sprite', 'cssmin']);
// for test build
grunt.registerTask('test', ['less:dev', 'copy:dev', 'clean:dev']);
// 定义别名 `grunt 2x2x`
// 注:@2x 图 生成 @1x 图
grunt.registerTask('2x2x', ['_2x2x']);
}