-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gruntfile.js
160 lines (138 loc) · 5.89 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
module.exports=function(grunt){
require('time-grunt')(grunt);//Grunt处理任务进度条提示
grunt.initConfig({
//默认文件目录在这里
paths:{
minZip:'./minZip',//输出的最终文件minZip里面
less:'./css/less',//推荐使用Less
css:'./css', //若简单项目,可直接使用原生CSS,同样可以grunt watch:base进行监控
js:'./js', //js文件相关目录
images:'./images' //图片相关
},
buildType:'Build',
pkg: grunt.file.readJSON('package.json'),
archive_name: grunt.option('name') || 'StaticTemplate项目名称',//此处可根据自己的需求修改
uglify:{
options:{
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n', //js文件打上时间戳
mangle: {
except: ['jQuery', 'bootstrap']
}
},
dist: {
files: {
'<%= paths.minZip %>/js/min.base.js': '<%= paths.js %>/*.js'
}
}
},
jshint: {
// define the files to lint
files: ['gruntfile.js', '<%= paths.js %>/*.js'],
// configure JSHint (documented at http://www.jshint.com/docs/)
options: {
// more options here if you want to override JSHint defaults
globals: {
jQuery: true,
console: true,
module: true
}
}
},
//压缩最终Build文件夹
compress:{
main:{
options:{
archive:'<%= archive_name %>-<%= grunt.template.today("yyyy") %>年<%= grunt.template.today("mm") %>月<%= grunt.template.today("dd") %>日<%= grunt.template.today("h") %>时<%= grunt.template.today("TT") %>.zip'
},
files: [
{expand: true, cwd: 'build/', src: ['**'], dest: 'public/'}, // makes all src relative to cwd,只在build下压缩
]
}
},
//压缩前把用到的文件CP到指定文件夹下
copy:{
main:{
files:[
{expand: true, src: ['minZip/css/**'], dest: 'build/css/', flatten: true, filter:'isFile'},// flatten: true, filter:'isFile'只对文件copy
{expand: true, src: ['images/**'], dest: 'build/'},
{expand: true, src: ['minZip/js/**'], dest: 'build/js/', flatten: true, filter:'isFile'},
{expand: true, src: ['*', '!minZip/**', '!build/**', '!.gitignore', '!.DS_Store','!Gruntfile.js','!package.json','!node_modules/**','!go.sh','!.ftppass','!<%= archive_name %>*.zip'], dest: 'build/'}, //!minZip/**,类似这种如果不加**,还会有一个空文件夹
]
}
},
//Less 预处理
less: {
admin: {
options: {
paths: ['<%= paths.less %>']
},
files: {
'<%= paths.css %>/reset.css':'<%= paths.less %>/reset.less',
'<%= paths.css %>/index.css':'<%= paths.less %>/index.less'
}
}
},
//压缩 css
cssmin:{
options:{
keepSpecialComments: 0
},
compress:{
files:{
'<%= paths.minZip %>/css/min.style.css': [
'<%= paths.css %>/style.css'
]
}
}
},
//监听变化 默认grunt watch 监测所有开发文件变化
watch:{
options:{
//开启 livereload
livereload:true,
//显示日志
dateFormate:function(time){
grunt.log.writeln('编译完成,用时'+time+'ms ' + (new Date()).toString());
grunt.log.writeln('Wating for more changes...');
}
},
//css
less:{
files:'<%= paths.less %>/*.less',
tasks:['less:admin','cssmin']
},
css:{
files:'<%= paths.css %>/*.css',
tasks:['cssmin']
},
js:{
files:'<%= paths.js %>/*.js',
tasks:['uglify']
},
//若不使用Sass,可通过grunt watch:base 只监测style.css和js文件
base:{
files:['<%= paths.css %>/*.css','<%= paths.js %>/*.js','images/**'],
tasks:['cssmin','uglify']
}
}
});
//输出进度日志
grunt.event.on('watch', function(action, filepath, target) {
grunt.log.writeln(target + ': ' + '文件: '+filepath + ' 变动状态: ' + action);
});
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['cssmin','uglify']);
grunt.registerTask('package', ['copy','compress']);
grunt.registerTask('style', ['less:admin','cssmin']);//不能用less,The trick was that Grunt doesn't seem to like the repetition in names. 详情看stackoverflow的解释
/*http://stackoverflow.com/questions/22285942/grunt-throw-recursive-process-nexttick-detected*/
//执行 grunt bundle --最终输出的文件 < name-生成日期.zip > 文件
//grunt.registerTask('bundle', ['clean:pre','copy:images', 'copy:main','cssmin','copy:archive', 'clean:post','htmlmin','compress',]);
//执行 grunt publish 可以直接上传项目文件到指定服务器FTP目录
};