-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
297 lines (267 loc) · 7.86 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
'use strict';
module.exports = function (grunt) {
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Automatically load required Grunt tasks
require('jit-grunt')(grunt);
require('jit-grunt')(grunt, {
useminPrepare: 'grunt-usemin'
});
// Define the configuration for all the tasks
grunt.initConfig({
//从package.json中加载所有的grunt插件
pkg: grunt.file.readJSON('package.json'),
//jshint
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: {
src: [
'Gruntfile.js',
'src/js/{,*/}*.js', //这是一种很暴力的方式
'src/views/js/{,*/}*.js'
]
}
},
//clean
clean: {
build: {
src: ['dist/']
}
},
//useminPrepare
useminPrepare: {
html: [
'src/index.html',
'src/project-2048.html',
'src/project-mobile.html',
'src/project-webperf.html',
'src/views/pizza.html'
],
options: {
dest: 'dist'
}
},
//Concat
concat: {
options: {
separator: ';'
},
// dist configuration is provided by useminPrepare
dist: {}
},
//cssmin
// Minify , Compress the css
cssmin: {
dist: {},
views: {
files: {
'dist/css/style.min.css': ['src/css/style.css'],
'dist/views/css/pizza.min.css': ['src/views/css/style.css', 'src/views/css/bootstrap-grid.css']
}
}
// 'dist/css/style.min.css': ['src/css/style.css'],
// dist configuration is provided by useminPrepare
// common_target: {
// files: [{
// expand: true,
// cwd: 'src/css',
// src: ['*.css', '!*.min.css'],
// dest: 'dist/css',
// ext: '.min.css'
// }]
// },
// views: {
// files: {
// 'dist/views/css/pizza.min.css': ['src/views/css/style.css', 'src/views/css/bootstrap-grid.css']
// }
// }
},
//Uglify
// Minify , Compress the js
uglify: {
dist: {},
views: {
files: { //先提前生成js文件,供后面processhtml使用
'dist/views/js/main.min.js': ['src/views/js/main.js']
}
}
// dist configuration is provided by useminPrepare
// common_target: {
// files: [{
// expand: true,
// cwd: 'src/js',
// src: ['**/*.js', '!*.min.js'],
// dest: 'dist/js',
// ext: '.min.js'
// }]
// },
//This configuration will compress and mangle the input files using the default options.
// views: {
// files: {
// 'dist/views/js/pizza.min.js': ['src/views/js/main.js']
// }
// }
},
//copy
copy: {
dist: { //对于其他的资源文件,例如图片 进行COPY
cwd: 'src',
src: ['**', '!css/**/*.css', '!views/css/**/*.css', '!js/**/*.js', '!views/js/**/*.js'], //'!*.html',
dest: 'dist',
expand: true
}
},
//filerev
//Static file asset revisioning through content hashing
filerev: {
options: {
encoding: 'utf8',
algorithm: 'md5',
length: 20
},
release: {
// filerev:release hashes(md5) all assets (images, js and css )
// in dist directory
files: [{
src: [
'dist/css/*.css',
'dist/views/css/*.css',
'dist/js/*.js',
'dist/views/js/*.js'
]
}]
}
},
// Usemin
// Replaces all assets with their revved version in html and css files.
// options.assetDirs contains the directories for finding the assets
// according to their relative paths
usemin: {
html: ['dist/*.html', 'dist/views/*.html'],
css: ['dist/css/*.css', 'dist/views/css/*.css'],
JavaScript: ['dist/js/*.js', 'dist/views/js/*.js'],
options: {
assetsDirs: ['dist', 'dist/css', 'dist/js', 'dist/views', 'dist/views/css', 'dist/views/js']
}
},
//processhtml
processhtml: {
views: {
files: {
'dist/index.html': ['dist/index.html'],
'dist/project-2048.html': ['dist/project-2048.html'],
'dist/project-mobile.html': ['dist/project-mobile.html'],
'dist/project-webperf.html': ['dist/project-webperf.html'],
'dist/views/pizza.html': ['dist/views/pizza.html']
}
}
},
//htmlmin
htmlmin: { // Task
dist: { // Target
options: { // Target options
removeComments: true,
collapseWhitespace: true,
removeCommentsFromCDATA: true,
minifyJS: true,
minifyCSS: true
},
files: { // Dictionary of files
'dist/index.html': 'dist/index.html',
'dist/project-2048.html': 'dist/project-2048.html',
'dist/project-mobile.html': 'dist/project-mobile.html',
'dist/project-webperf.html': 'dist/project-webperf.html',
'dist/views/pizza.html': 'dist/views/pizza.html'
}
}
},
//connect for livereload Preview
connect: {
options: {
port: 9000,
// Change this to '0.0.0.0' to access the server from outside.
hostname: 'localhost',
livereload: 35729
},
dist: {
options: {
open: true,
base: {
path: 'dist',
options: {
index: 'index.html',
maxAge: 300000
}
}
}
}
},
//watch for livereload Preview
watch: {
copy: {
files: ['src/**', '!src/**/*.css', '!src/**/*.js'],
tasks: ['build']
},
scripts: {
files: ['src/js/*.js', 'src/views/js/*.js'],
tasks: ['build']
},
styles: {
files: ['src/css/*.css', 'src/views/css/*.css'],
tasks: ['build']
},
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'src/{,*/}*.html',
'.tmp/css/{,*/}*.css',
'src/img/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
]
}
},
pagespeed: {
options: {
nokey: true,
url: "https://developers.google.com"
},
prod: {
options: {
url: "https://developers.google.com/speed/docs/insights/v1/getting_started",
locale: "en_GB",
strategy: "desktop",
threshold: 80
}
},
paths: {
options: {
paths: ["/speed/docs/insights/v1/getting_started", "/speed/docs/about"],
locale: "en_GB",
strategy: "desktop",
threshold: 80
}
}
}
});
grunt.registerTask('build', [
'clean',
'jshint',
'useminPrepare',
'concat',
'cssmin',
'uglify',
'copy', //复制一些资源文件,图片
'usemin', //用加密后的文件名同步到*.html
'processhtml', //可以把js文件生成放入html中
// 'filerev', //给文件名加密
'htmlmin' //这样做是做好的办法,会是生成的html最小
]);
grunt.registerTask('serve',['build','connect:dist','watch']);
grunt.registerTask('pagespeed',['build', 'pagespeed']);
grunt.registerTask('default',['build']);
};