-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
159 lines (154 loc) · 4.68 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
module.exports = function(grunt){
grunt.initConfig({
pkg:grunt.file.readJSON('package.json'),
//1、jshint——javascript语法错误检查
jshint:{
build: [ 'Gruntfile.js','<%=pkg.website%>/js-dev/*.js'],
options: {
jshintrc: '.jshintrc'
}
},
//2、concat——合并多个js、css文件的代码到一个文件中
concat: {
css:{
src: '<%=pkg.website%>/css-dev/*.css',
dest: '<%=pkg.website%>/css/<%=pkg.concatcss%>.css'
},
js:{
src: '<%=pkg.website%>/js-dev/*.js',
dest: '<%=pkg.website%>/js/<%=pkg.concatjs%>.js'
}
},
//3、uglify——压缩javascript代码
uglify: {
build: {
src: '<%=pkg.website%>/js/<%=pkg.concatjs%>.js',
dest: '<%=pkg.website%>/js/<%=pkg.jsmin%>.js'
}
},
//4、cssmin——压缩css代码
cssmin: {
build: {
src: '<%=pkg.website%>/css/<%=pkg.concatcss%>.css',
dest: '<%=pkg.website%>/css/<%=pkg.cssmin%>.css'
}
},
//5、imagemin——压缩图片工具
imagemin: {
/* 压缩图片大小 */
dist: {
options: {
optimizationLevel: 3 //定义 PNG 图片优化水平
},
files: [
{
expand: true,
cwd: '<%=pkg.website%>/images/',
src: ['**/*.{png,jpg,jpeg}'], // 优化 img 目录下所有 png/jpg/jpeg 图片
dest: '<%=pkg.website%>/images/' // 优化后的图片保存位置,覆盖旧图片,并且不作提示
}
]
}
},
//6、watch——实时监控文件变化、调用相应的任务重新执行
watch: {
js: {
files: [
'<%=pkg.website%>/js-dev/*.js'
],
tasks: [
'concat',
'jshint',
'uglify'
],
options: { spawn: false}
},
css:{
files: [
'<%=pkg.website%>/css-dev/*.css'
],
tasks: [
'concat',
'cssmin'
],
options: { spawn: false}
},
client: {
options: {
livereload: true
},
// '**' 表示包含所有的子目录
// '*' 表示包含所有的文件
files: ['<%=pkg.website%>/*.html', '<%=pkg.website%>/css-dev/*', '<%=pkg.website%>/js-dev/*', '<%=pkg.website%>/images/**/*']
}
},
//7、clean——清空文件、文件夹
clean: {
cleanoutput: {
files: [{
src: 'output/'
}]
}
},
//8、copy——复制文件、文件夹
copy: {
main: {
files:[
//js
{
expand: true,
flatten: true,
src: ['<%=pkg.website%>/js-dev/*.js'],
dest: '<%=pkg.website%>/dist/js/',
filter: 'isFile'
},
//less
{
expand: true,
flatten: true,
src: ['<%=pkg.website%>/css-dev/*.less'],
dest: '<%=pkg.website%>/dist/css/',
filter: 'isFile'
},
//css
{
expand: true,
flatten: true,
src: ['<%=pkg.website%>/css-dev/*.css'],
dest: '<%=pkg.website%>/dist/css/',
filter: 'isFile'
}
]
}
},
//9、karma——前端自动化测试工具
//10、connect——服务器
/* connect: {
options: {
port: 9000,
hostname: '*', //默认就是这个值,可配置为本机某个 IP,localhost 或域名
//livereload: 35729 //声明给 watch 监听的端口
},
server: {
options: {
open: true, //自动打开网页 http://
base: [
'./ng' //主目录
]
}
},
}*/
});
//引入插件
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
//grunt.loadNpmTasks('grunt-contrib-connect');
//注册插件
grunt.registerTask('default',['jshint','uglify','concat','cssmin','watch']);
};