-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgruntfile.js
379 lines (379 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**************************
*@BY xiaoyin
***tpl默认目录结构
**************************/
/*global module:false*/
module.exports = function(grunt) {
'use strict';
require('time-grunt')(grunt);
var config = grunt.file.readJSON('config.json');
var date = new Date();
var time = {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate()
};
//time.day = 11;
if (time.day < 10) {
time.day = '0' + time.day;
}
var pathApp = config.isDev ? config.pathProject + '/' + time.year + "/" + time.month + time.day + "/" + config.pathApp : config.pathEdit;
//process.exit();
var fs = require("fs");
var path = require("path");
var is_dir;
function mkdirs(dirname) {
//console.log(dirname);
if (!fs.existsSync(dirname)) {
if (mkdirs(path.dirname(dirname))) {
fs.mkdirSync(dirname);
is_dir = false;
return true;
}
} else {
return true;
}
}
mkdirs(pathApp);
grunt.initConfig({
config: config,
pathApp: pathApp,
pathAppSrc: config.isDev ? pathApp + config.pathSrc : pathApp,
pathAppBuild: config.isDev ? pathApp + config.pathBuild : pathApp,
copy: {
pc: {
expand: true,
cwd: '<%=config.pathTplpc%>',
src: '**/* ',
dest: '<%=pathAppSrc%>',
},
m: {
expand: true,
cwd: '<%=config.pathTplm%>',
src: '**/*',
dest: '<%=pathAppSrc%>',
},
img: {
expand: true,
cwd: '<%=config.pathImg%>',
src: '**/*',
dest: '<%=pathAppSrc%>images/',
},
buildcss: {
expand: true,
cwd: '<%=pathAppSrc%>css/',
src: 'style.css',
dest: '<%=pathAppBuild%>css/',
},
buildimg: {
expand: true,
cwd: '<%=pathAppSrc%>images/',
src: '**/*',
dest: '<%=pathAppBuild%>images/',
},
buildjs: {
expand: true,
cwd: '<%=pathAppSrc%>js/',
src: ['**/*', '!/*.js'],
dest: '<%=pathAppBuild%>js/',
},
buildhtml: {
expand: true,
cwd: '<%=pathAppSrc%>',
src: '*.html',
dest: '<%=pathAppBuild%>',
},
lesscss: {
expand: true,
cwd: '<%=pathAppSrc%>/css/less/',
src: '*.css',
dest: '<%=pathAppSrc%>/css',
}
},
clean: {
css: {
expand: true,
cwd: '<%=pathAppSrc%>',
src: ['css/**/*.css', '!css/style.min.css'],
},
app: {
expand: true,
cwd: '<%=pathAppSrc%>',
src: '**/*',
},
},
connect: {
options: {
port: 9000,
hostname: '*', //默认就是这个值,可配置为本机某个 IP,localhost 或域名
livereload: 35888 //声明给 watch 监听的端口
},
server: {
options: {
open: true, //自动打开网页 http://
base: [
'<%=pathAppSrc%>' //主目录
]
}
}
},
csslint: {
strict: {
options: {
import: 2,
csslintrc: '.csslint'
},
src: ['<%=pathAppSrc%>css/**/*.css'],
},
},
autoprefixer: {
options: {
browers: ['aliases']
//browsers:['last 2 versions','chrome','ie 8', 'ie 9','moz'],
},
css: {
expand: true,
cwd: '<%=pathAppSrc%>',
src: 'css/**/*.css',
dest: '<%=pathAppSrc%>',
ext: '.css',
}
},
cssmin: {
/*压缩 CSS 文件为 .min.css */
options: {
keepSpecialComments: 1 /* 移除 CSS 文件中的所有注释 */
},
min: {
expand: true,
cwd: '<%=pathAppSrc%>',
src: 'css/**/*.css',
dest: '<%=pathAppSrc%>',
ext: '.css'
}
},
csscomb: {
files: {
expand: true,
cwd: '<%=pathAppSrc%>',
src: 'css/**/*.css',
dest: '<%=pathAppSrc%>',
ext: '.css'
}
},
jshint: {
/* 检查 js 语法 */
all: ['Gruntfile.js', '<%=pathAppSrc%>js/*.js']
},
imagemin: {
/* 压缩优化图片大小 */
dist: {
options: {
optimizationLevel: 7
},
files: [{
expand: true,
cwd: '<%=pathAppSrc%>images/',
src: ['**/*.{png,jpg,jpeg}'], // 优化 img 目录下所有 png/jpg/jpeg 图片
dest: '<%=pathAppBuild%>images/' // 优化后的图片保存位置,默认覆盖
}]
}
},
concat: {
/* 合并 CSS 文件 */
css: {
src: ['<%=pathAppSrc%>css/**/*.css', '!<%=pathAppSrc%>css/style.css'],
dest: '<%=pathAppSrc%>css/style.css',
},
js: {
src: ['<%=pathAppSrc%>js/*.js'],
/* 根据目录下文件情况配置 如果可以使用 require.js/LABjs 等配置更佳 */
dest: '<%=pathAppSrc%>js/concat.js'
}
},
less: {
files: {
expand: true,
cwd: '<%=pathAppSrc%>',
src: '**/*.less',
dest: '<%=pathAppSrc%>',
ext: '.css'
}
},
uglify: {
/* 最小化、混淆、合并 JavaScript 文件 */
target: {
files: {
'<%=pathAppSrc%>js/all.min.js': ['<%=pathAppSrc%>js/all.js']
}
},
minjs: { //最小化、混淆所有 js/ 目录下的 JavaScript 文件
files: [{
expand: true,
cwd: '<%=pathAppSrc%>js/',
src: ['*.js', '!**/*.min.js'],
dest: '<%=pathAppBuild%>js/',
ext: '.js'
}]
},
test: {
src: "C:/Users/Administrator/Desktop/test/js/posts.js",
dest: "C:/Users/Administrator/Desktop/test/js/posts.min.js"
}
},
//压缩HTML
htmlmin: {
options: {
removeComments: false,
removeCommentsFromCDATA: false,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: false,
removeRedundantAttributes: true,
useShortDoctype: false,
removeEmptyAttributes: true,
removeOptionalTags: false
},
html: {
files: [{
expand: true,
cwd: '<%=pathAppBuild%>',
src: ['*.html'],
dest: '<%=pathAppBuild%>'
}]
}
},
hashmap: {
options: {
output: '<%=pathAppBuild%>json/hashmap.json',
rename: '#{= dirname}/#{= hash}.#{= basename}#{= extname}',
//rename: '#{= dirname}/#{= basename}#{= extname}?v=#{= hash}',
keep: false,
hashlen: 8
},
all: {
cwd: '<%=pathAppBuild%>',
src: '**/*.{css,js,eot,svg,ttf,woff}',
dest: '<%=pathAppBuild%>'
}
},
htmlurlrev: {
options: {
assets: '<%= hashmap.options.output %>',
hashmap_rename: '<%= hashmap.options.rename %>'
},
files: {
src: ['<%=pathAppBuild%>*.html'],
},
},
shell: {
options: {
stderr: false,
},
subl: {
stdout: false,
stderr: false,
command: 'RunHiddenConsole D:/Program Files (x86)/Sublime Text 3/sublime_text.exe <%=pathAppSrc%>',
},
webs: {
command: 'RunHiddenConsole D:/Program Files (x86)/JetBrains/WebStorm 2017.1/bin/webstorm64.exe <%=pathAppSrc%>',
},
},
watch: {
/* 监控文件变化并执行相应任务 */
img: {
files: ['<%=pathAppSrc%>**/*.{png,jpg,jpeg}'],
options: {
event: ['changed', 'added'],
livereload: 35888
},
},
pathImg: {
files: ['<%=config.pathImg%>**/*.{png,jpg,jpeg,gif}'],
options: {
event: ['changed', 'added'],
},
tasks: ['newer:copy:img']
},
css: {
options: {
event: ['changed', 'added'],
livereload: 35888
},
files: ['<%=pathAppSrc%>/**/*.css'],
//tasks: ['newer:copy:lesscss']
},
less: {
options: {
event: ['changed', 'added'],
livereload: 35888
},
files: ['<%=pathAppSrc%>/**/*.less'],
tasks: ['newer:less', 'newer:autoprefixer', 'newer:csscomb', 'newer:cssmin', 'concat:css']
},
js: {
options: {
livereload: 35888
},
files: ['<%=pathAppSrc%>**/*.js'],
},
html: {
options: {
livereload: 35888
},
files: ['<%=pathAppSrc%>/**/*.html']
}
}
});
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-csslint');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-htmlmin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-csscomb');
grunt.loadNpmTasks('grunt-hashmap');
grunt.loadNpmTasks('grunt-htmlurlrev');
grunt.loadNpmTasks('grunt-newer');
grunt.loadNpmTasks('grunt-shell');
// define default tasker
if (config.isDev) {
grunt.log.writeln("This is develop model;");
if (config.isPc) {
console.log("This is Pc project;");
if (typeof is_dir != 'undefined') {
grunt.registerTask('default', ['copy:pc', 'copy:img', 'connect', 'shell:webs', 'watch']);
} else {
console.log('project already exist!');
grunt.registerTask('default', ['connect', 'shell:webs', 'watch']);
}
} else {
console.log("This is Mobile project;");
console.log(pathApp);
if (typeof is_dir != 'undefined') {
grunt.registerTask('default', ['copy:m', 'copy:img', 'connect','shell:webs', 'watch']);
} else {
console.log('project already exist!');
grunt.registerTask('default', ['connect', 'shell:webs', 'watch']);
}
}
} else {
grunt.log.writeln("This is Edit model;");
grunt.log.writeln(pathApp);
grunt.registerTask('default', ['connect','shell:webs','watch']);
}
grunt.registerTask('winBuild', ['copy:buildcss', 'copy:buildimg', 'copy:buildjs', 'uglify:minjs', 'copy:buildhtml', /*'hashmap','htmlurlrev',*/ 'htmlmin']);
grunt.registerTask('build', ['copy:buildcss', 'copy:buildimg', 'copy:buildjs', 'uglify:minjs', 'copy:buidhtml', 'hashmap', 'htmlurlrev', 'htmlmin']);
grunt.registerTask('css', ['concat:css', 'cssmin']);
grunt.registerTask('jsmin', ['uglify:test']);
grunt.registerTask('test', function() {
console.log(grunt.config.get('watch.less.files'));
});
};