-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGruntfile.js
297 lines (270 loc) · 10.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
module.exports = function(grunt) {
require('time-grunt')(grunt); //Grunt处理任务进度条提示
grunt.initConfig({
//默认文件目录在这里
paths: {
assets: './assets', //输出的最终文件assets里面
scss: './css/sass', //推荐使用Sass
css: './css', //若简单项目,可直接使用原生CSS,同样可以grunt watch:base进行监控
js: './assets/js', //js文件相关目录
img: './assets/img' //图片相关
},
buildType: 'Build',
pkg: grunt.file.readJSON('package.json'),
archive_name: grunt.option('name') || 'StaticPage项目名称', //此处可根据自己的需求修改
//清理掉开发时才需要的文件
clean: {
pre: ['dist/', 'build/'], //删除掉先前的开发文件
post: ['<%= archive_name %>*.zip'] //先删除先前生成的压缩包
},
uglify: {
options: {
compress: {
drop_console: true
},
banner: '/** \n' +
'* Project : <%= pkg.name %> \n' +
'* Author : <%= pkg.author %> \n' +
'* Updated : <%= grunt.template.today() %> \n' +
'*/ \n'
},
dist: {
files: {
//'<%= paths.assets %>/js/min.v.js': '<%= paths.js %>/app.js'
//'projects/alimama/assets/js/app.min.js': 'projects/alimama/assets/js/app.js'
}
}
},
//调用谷歌高级压缩
'closure-compiler': {
base: {
closurePath: '/usr/local/Cellar/closure-compiler/20140407/libexec', //在这里指定谷歌高级压缩路径
js: [
'<%= paths.assets %>/js/v.js',
],
jsOutputFile: '<%= paths.assets %>/js/min.main.js', //输出的js为min.main.js
noreport: true,
maxBuffer: 500,
options: {
compilation_level: 'ADVANCED_OPTIMIZATIONS',
warning_level: "DEFAULT"
// language_in: 'ECMASCRIPT5_STRICT'
}
}
},
//压缩最终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'
},
expand: true,
cwd: 'build/',
src: ['**/*'],
dest: ''
}
},
copy: {
main: {
files: [{
expand: true,
src: ['assets/css/**'],
dest: 'build/'
}, {
expand: true,
src: ['assets/images/**'],
dest: 'build/'
}, {
expand: true,
src: ['assets/js/**'],
dest: 'build/'
}, {
expand: true,
src: ['*', '!.gitignore', '!.DS_Store', '!Gruntfile.js', '!package.json', '!resource/**','!h5/**','!projects/**','!node_modules/**', '!go.sh', '!.ftppass', '!<%= archive_name %>*.zip'],
dest: 'build/'
}, ]
},
xx: {
files: [{
expand: true,
src: ['assets/**'],
dest: 'build/'
},
{
expand: true,
src: ['admin/**'],
dest: 'build/'
},
{
expand: true,
src: ['demos/**'],
dest: 'build/'
},
]
},
images: {
expand: true,
cwd: 'img/',
src: ['**', '!github.png'],
dest: 'assets/images/',
flatten: true,
filter: 'isFile',
},
archive: {
files: [{
expand: true,
src: ['<%= archive_name %>.zip'],
dest: 'dist/'
}]
}
},
//Sass 预处理
sass: {
admin: {
options: {
sourcemap: true,
style: 'nested',
banner: '/** \n' +
'* Project : <%= pkg.name %> \n' +
'* Author : <%= pkg.author %> \n' +
'* Updated : <%= grunt.template.today() %> \n' +
'*/ \n'
},
files: {
'<%= paths.css %>/style.css': '<%= paths.scss %>/style.scss',
}
}
},
//压缩 css
cssmin: {
options: {
keepSpecialComments: 0,
banner: '/** \n' +
'* Project : <%= pkg.name %> \n' +
'* Author : <%= pkg.author %> \n' +
'* Updated : <%= grunt.template.today() %> \n' +
'*/ \n'
},
compress: {
files: {
'<%= paths.assets %>/css/min.style.css': [
'<%= paths.css %>/style.css'
]
}
}
},
// 格式化和清理html文件
htmlmin: {
dist: {
options: {
removeComments: true,
//collapseWhitespace: true //压缩html:根据情况开启与否
},
files: {
'build/index.html': 'build/index.html', //清除html中的注释
}
}
},
//优化图片 请参考 https://github.com/JamieMason/ImageOptim-CLI
imageoptim: {
myTask: {
options: {
jpegMini: false,
imageAlpha: true,
quitAfter: true
},
src: ['<%= paths.assets %>/images', '<%= paths.assets %>/images']
}
},
//监听变化 默认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
sass: {
files: '<%= paths.scss %>/**/*.scss',
tasks: ['sass: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', 'img/**'],
tasks: ['cssmin', 'uglify', 'copy:images']
}
},
//发布到FTP服务器 : 请注意密码安全,ftp的帐号密码保存在主目录 .ftppass 文件
'ftp-deploy': {
build: {
auth: {
host: '10.100.60.131',
port: 21,
authKey: 'key1'
},
src: 'build',
dest: '/newmedia/majinhui/demo1',
exclusions: ['path/to/source/folder/**/.DS_Store', 'path/to/source/folder/**/Thumbs.db', 'path/to/dist/tmp']
}
},
'sftp-deploy': {
build: {
auth: {
host: '121.199.36.124',
port: 22,
authKey: 'key2'
},
cache: 'sftpCache.json',
src: 'build',
dest: '/var/www/html/public',
exclusions: ['path/to/source/folder/**/.DS_Store', 'path/to/source/folder/**/Thumbs.db', 'path/to/dist/tmp'],
serverSep: '/',
concurrency: 4,
progress: true
}
}
});
//输出进度日志
grunt.event.on('watch', function(action, filepath, target) {
grunt.log.writeln(target + ': ' + '文件: ' + filepath + ' 变动状态: ' + action);
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-htmlmin');
grunt.loadNpmTasks('grunt-ftp-deploy');
grunt.loadNpmTasks('grunt-sftp-deploy');
grunt.loadNpmTasks('grunt-closure-compiler'); //增加谷歌高级压缩
grunt.loadNpmTasks('grunt-imageoptim');
/*下方为配置的常用 grunt 命令*/
grunt.registerTask('default', ['cssmin', 'uglify', 'htmlmin', 'copy:images']);
grunt.registerTask('styles', ['sass:admin', 'cssmin']);
//执行 grunt bundle --最终输出的文件 < name-生成日期.zip > 文件
grunt.registerTask('bundle', ['clean:pre', 'copy:images', 'copy:main', 'cssmin', 'copy:archive', 'clean:post', 'htmlmin', 'compress', ]);
//执行 grunt publish 可以直接上传项目文件到指定服务器FTP目录
grunt.registerTask('publish', ['ftp-deploy']);
//执行 grunt ssh 可以利用 ssh 上传到服务器
grunt.registerTask('ssh', ['sftp-deploy']);
grunt.registerTask('app', [ 'uglify']);
//执行 grunt gcc 可进行谷歌压缩
grunt.registerTask('gcc', ['closure-compiler']);
//
grunt.registerTask('copyxx', ['clean:pre','copy:xx']);
grunt.registerTask('publishxx', ['clean:pre','copy:xx','sftp-deploy']);
};