-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
271 lines (243 loc) · 5.65 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
'use strict';
/**
* [使用此模块需要先加载grunt 加载方法可看官网
* http://www.gruntjs.net/getting-started
*
* 具体配置需根据个人项目自行配置
*
* 使用 grunt server 可以不用反复重启程序
* 使用 grunt install 打包应用
*
* ]
* @param {[type]} grunt [description]
* @return {[type]} [description]
*/
module.exports = function(grunt){
require('load-grunt-tasks')(grunt);
var port = require('./core/config').initBase.base.port;
//自定义json常量
var config = {
port:port, //grunt server启动端口
dist:'dist' //目标目录
};
grunt.initConfig({
config:config,
pkg: grunt.file.readJSON('package.json'),//引入package.json
//清除目录 grunt clean --force
clean:{
dist:['<%= config.dist %>/<%= pkg.name%>']
},
//复制不需要压缩的文件
copy:{
main:{
files:[
{
src:'core/config.js',
dest:'<%= config.dist %>/<%= pkg.name%>/core/config.js'
},
{ src:'core/restUrl.js',
dest:'<%= config.dist %>/<%= pkg.name%>/core/restUrl.js'
},
{
src:'package.json',
dest:'<%= config.dist %>/<%= pkg.name%>/package.json'
},
{
src:'Gruntfile.js',
dest:'<%= config.dist %>/<%= pkg.name%>/Gruntfile.js'
},
{
src:'start.sh',
dest:'<%= config.dist %>/<%= pkg.name%>/start.sh'
},
{
expand:true,
cwd:'node_modules',
src:'*/**',
dest:'<%= config.dist %>/<%= pkg.name%>/node_modules'
},
{
expand:true,
cwd:'public/layui',
src:'*/**',
dest:'<%= config.dist %>/<%= pkg.name%>/public/layui'
}
]
}
},
//压缩JS
uglify:{
options:{
banner:'/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
buildsome:{
options: {
report: 'min'//输出压缩率,可选的值有 false(不输出信息),gzip
},
files: [
{
'<%= config.dist %>/<%= pkg.name%>/core/initCore.js': ['core/initCore.js']
},
{
'<%= config.dist %>/<%= pkg.name%>/app.js': ['app.js']
},
{
'<%= config.dist %>/<%= pkg.name%>/bin/www': ['bin/www']
}
]
},
buildall:{
files: [
{
expand:true,
cwd:'core/util',//js目录下
src:'**/*.js',//所有js文件
dest: '<%= config.dist %>/<%= pkg.name%>/core/util'//输出到此目录下
},
{
expand:true,
cwd:'public/js',//js目录下
src:'**/*.js',//所有js文件
dest: '<%= config.dist %>/<%= pkg.name%>/public/js'//输出到此目录下
},
{
expand:true,
cwd:'routes',//js目录下
src:'**/*.js',//所有js文件
dest: '<%= config.dist %>/<%= pkg.name%>/routes'//输出到此目录下
}
]
}
},
//压缩图片
imagemin:{
dynamic:{
files:[
{
expand: true,
cwd: 'public/image',
src: ['**/*.{png,jpg,jpeg,gif,webp,svg}'],
dest: '<%= config.dist %>/<%= pkg.name%>/public/image'
}
]
}
},
//压缩CSS
cssmin:{
dist: {
options: {
report: 'gzip'
},
files: [
{
expand: true,
cwd: 'public/css',
src: ['**/*.css'],
dest: '<%= config.dist %>/<%= pkg.name%>/public/css'
}
]
}
},
//压缩HTML
htmlmin:{
options: {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeOptionalTags: true
},
html: {
files: [
{
expand: true,
cwd: 'views',
src: ['**/*.html'],
dest: '<%= config.dist %>/<%= pkg.name%>/views'
}
]
}
},
//shell 执行命令并启动
shell:{
options: {
stderr: false
},
stop: {
command: '<%=config.dist%>/<%= pkg.name%>/start.sh stop'
},
another: 'chmod 755 <%=config.dist%>/<%= pkg.name%>/start.sh && <%=config.dist%>/<%= pkg.name%>/sh/start.sh start' // shorthand
},
//grunt-concurrent
concurrent:{
tasks:['nodemon','watch'],
options:{
logConcurrentOutput:true
}
},
//后端node.js虚拟服务
nodemon:{
options:{},
dev: {
options: {
file: './bin/www',
args:[],
ignoredFiles:['node_modules/**'],
watcedFolders:['public','routes','views'],
debug:true,
delayTime:1,
env:{
PORT: '<%= config.port%>',
},
cwd:__dirname
}
}
},
//观测变化,自动重启 watch
watch:{
jade:{
files:['views/**'],
options: {
livereload: true //监听前面声明的端口
},
},
js:{
files:['routes/**','public/**'],
options: {
livereload: true //监听前面声明的端口
},
}
},
});
//加载 uglify
grunt.loadNpmTasks('grunt-contrib-uglify');
//图片压缩
grunt.loadNpmTasks('grunt-contrib-imagemin');
//压缩CSS
grunt.loadNpmTasks('grunt-contrib-cssmin');
//压缩HTML
grunt.loadNpmTasks('grunt-contrib-htmlmin');
//加载 copy
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-eslint');
// 注册默认任务. 压缩顺序
grunt.registerTask('install', ['copy','uglify','imagemin','cssmin','htmlmin']);
//shell
//grunt.loadNpmTasks('shelljs');
//启动 || 停止
grunt.registerTask('start', ['shell']);
grunt.registerTask('stop', ['shell:stop']);
//加载server
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-nodemon');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.option('force',true);
//后端node.js虚拟服务
grunt.registerTask('server', ['concurrent']);
//默认任务
grunt.registerTask('default', ['concurrent']);
};