-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgruntfile.js
92 lines (92 loc) · 2.55 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
// 执行压缩、合并代码、优化图片 操作
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
//清除目录
clean: {
all: ['dest/*'],
unImg: ["dest/*", "!dest/images"]
},
// 复制文件
copy: {
main: {
files: [
{expand: true, src: ['index.html', 'favicon.ico'], dest: 'dest'}
]
}
},
//压缩JS
uglify: {
generated: {
files: [
{dest: 'dest/datas/datas.js', src: ['datas/*.js']},
{dest: 'dest/js/main.js', src: ['js/util.js', 'js/main.js']}
]
}
},
//压缩CSS
cssmin: {
generated: {
files: {
'dest/css/main.css': ['css/main.css']
}
}
},
//压缩图片
imagemin: {
dynamic: {
options: {
optimizationLevel: 7,
pngquant: true
},
files: [{expand: true,
cwd: 'images/',
src: ['**/*.{png,jpg,jpeg,gif,webp,svg}'],
dest: 'dest/images'
}]
}
},
// 处理html中css、js 引入合并问题
useminPrepare:{
html: 'index.html',
options: {
root: 'threeKingdoms_xq',
dest: 'dest'
}
},
usemin: {
html: 'dest/index.html'
},
//压缩HTML
htmlmin: {
options: {
removeComments: true,
collapseWhitespace: true
},
html: {
files: {
'dest/index.html': 'dest/index.html'
}
}
}
});
// 加载任务的插件
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-htmlmin');
grunt.loadNpmTasks('grunt-usemin');
// 定义任务指令
grunt.registerTask('g-copy', ['copy']);
grunt.registerTask('g-clean', ['clean']);
grunt.registerTask('g-uglify', ['uglify:generated']);
grunt.registerTask('g-cssmin', ['cssmin']);
grunt.registerTask('g-imagemin', ['imagemin']);
grunt.registerTask('g-htmlmin', ['usemin', 'htmlmin']);
grunt.registerTask('default', ['clean', 'copy', 'uglify:generated', 'cssmin', 'imagemin', 'usemin', 'htmlmin']);
// 不包括图片处理
// grunt.registerTask('default', ['clean:unImg', 'copy', 'uglify:generated', 'cssmin', 'usemin', 'htmlmin']);
};