Skip to content

Commit 33ced77

Browse files
grunt
1 parent 1be42e4 commit 33ced77

File tree

10 files changed

+475
-0
lines changed

10 files changed

+475
-0
lines changed
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
# Grunt快速入门教程
2+
3+
* Grunt介绍
4+
5+
* 中文主页 : http://www.gruntjs.net/
6+
* 是一套前端**自动化构建**工具,一个基于nodeJs的命令行工具
7+
* 它是一个**任务运行器**, 配合其丰富强大的**插件**
8+
* 常用功能:
9+
* **合并文件**(js/css)
10+
* **压缩文件**(js/css)
11+
* **语法检查**(js)
12+
* **less/sass预编译处理**
13+
* 其它...
14+
15+
* 安装nodejs, 查看版本
16+
```
17+
node -v
18+
```
19+
20+
* 创建一个简单的应用grunt_test
21+
```
22+
|- build----------构建生成的文件所在的文件夹
23+
|- src------------源码文件夹
24+
|- js---------------js源文件夹
25+
|- css--------------css源文件夹
26+
|- index.html-----页面文件
27+
|- Gruntfile.js---grunt配置文件(注意首字母大写)
28+
|- package.json---项目包配置文件
29+
{
30+
"name": "grunt_test",
31+
"version": "1.0.0"
32+
}
33+
```
34+
35+
* 全局安装 grunt-cli
36+
```
37+
npm install -g grunt-cli
38+
```
39+
40+
* 安装grunt
41+
```
42+
npm install grunt --save-dev
43+
```
44+
45+
* 运行构建项目命令
46+
```
47+
grunt //提示 Warning: Task "default" not found
48+
```
49+
50+
* 配置文件: Gruntfile.js
51+
* 此配置文件本质就是一个node函数类型模块
52+
* 配置编码包含3步:
53+
1. 初始化插件配置
54+
2. 加载插件任务
55+
3. 注册构建任务
56+
* 基本编码:
57+
```
58+
module.exports = function(grunt){
59+
// 1. 初始化插件配置
60+
grunt.initConfig({
61+
//主要编码处
62+
});
63+
// 2. 加载插件任务
64+
// grunt.loadNpmTasks('grunt-contrib-concat');
65+
// 3. 注册构建任务
66+
grunt.registerTask('default', []);
67+
};
68+
```
69+
* 命令: grunt //提示成功, 但没有任何效果(还没有使用插件定义任务)
70+
71+
* Grunt插件介绍
72+
* grunt官网的插件列表页面 http://www.gruntjs.net/plugins
73+
* 插件分类:
74+
* grunt团队贡献的插件 : 插件名大都以contrib-开头
75+
* 第三方提供的插件 : 大都不以contrib-开头
76+
* 常用的插件:
77+
* grunt-contrib-clean——清除文件(打包处理生成的)
78+
* grunt-contrib-concat——合并多个文件的代码到一个文件中
79+
* grunt-contrib-uglify——压缩js文件
80+
* grunt-contrib-jshint——javascript语法错误检查;
81+
* grunt-contrib-cssmin——压缩/合并css文件
82+
* grunt-contrib-htmlmin——压缩html文件
83+
* grunt-contrib-imagemin——压缩图片文件(无损)
84+
* grunt-contrib-copy——复制文件、文件夹
85+
* grunt-contrib-watch——实时监控文件变化、调用相应的任务重新执行
86+
87+
* 合并js: 使用concat插件
88+
* 命令:
89+
```
90+
npm install grunt-contrib-concat --save-dev
91+
```
92+
* 编码:
93+
* src/js/test1.js
94+
```
95+
(function () {
96+
function add(num1, num2) {
97+
return num1 + num2;
98+
}
99+
console.log(add(10, 20));
100+
})();
101+
```
102+
* src/js/test2.js
103+
```
104+
(function () {
105+
var arr = [2,3,4].map(function (item, index) {
106+
return item+1;
107+
});
108+
console.log(arr);
109+
})();
110+
```
111+
* 配置: Gruntfile.js
112+
* 配置任务:
113+
```
114+
concat: {
115+
options: { //可选项配置
116+
separator: ';' //使用;连接合并
117+
},
118+
build: { //此名称任意
119+
src: ["src/js/*.js"], //合并哪些js文件
120+
dest: "build/js/built.js" //输出的js文件
121+
}
122+
}
123+
```
124+
* 加载插件:
125+
```
126+
grunt.loadNpmTasks('grunt-contrib-concat');
127+
```
128+
* 注册任务:
129+
```
130+
grunt.registerTask('default', ['concat']);
131+
```
132+
* 命令:
133+
```
134+
grunt //会在build下生成一个built.js
135+
```
136+
137+
* 压缩js: 使用uglify插件
138+
* 下载
139+
```
140+
npm install grunt-contrib-uglify --save-dev
141+
```
142+
* 配置: Gruntfile.js
143+
* 配置任务:
144+
```
145+
pkg : grunt.file.readJSON('package.json'),
146+
uglify : {
147+
options: { //不是必须的
148+
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
149+
'<%= grunt.template.today("yyyy-mm-dd") %> */'
150+
},
151+
build: {
152+
files: {
153+
'build/js/built-<%=pkg.name%>-<%=pkg.version%>.min.js': ['build/js/built.js']
154+
}
155+
}
156+
}
157+
```
158+
* 加载任务:
159+
```
160+
grunt.loadNpmTasks('grunt-contrib-uglify');
161+
```
162+
* 注册任务:
163+
```
164+
grunt.registerTask('default', ['concat', 'uglify']);
165+
```
166+
* 命令:
167+
```
168+
grunt //会在build下生成一个压缩的js文件
169+
```
170+
171+
* js语法检查: 使用jshint插件
172+
* 命令:
173+
```
174+
npm install grunt-contrib-jshint --save-dev
175+
```
176+
* 编码: .jshintrc
177+
```
178+
{
179+
"curly": true,
180+
"eqeqeq": true,
181+
"eqnull": true,
182+
"expr" : true,
183+
"immed": true,
184+
"newcap": true,
185+
"noempty": true,
186+
"noarg": true,
187+
"regexp": true,
188+
"browser": true,
189+
"devel": true,
190+
"node": true,
191+
"boss": false,
192+
193+
//不能使用未定义的变量
194+
"undef": true,
195+
//语句后面必须有分号
196+
"asi": false,
197+
//预定义不检查的全局变量
198+
"predef": [ "define", "BMap", "angular", "BMAP_STATUS_SUCCESS"]
199+
}
200+
```
201+
* 修改src/js/test1.js
202+
```
203+
(function () {
204+
function add(num1, num2) {
205+
num1 = num1 + num3
206+
return num1 + num2;
207+
}
208+
console.log(add(10, 20));
209+
})();
210+
```
211+
* 配置 : Gruntfile.js
212+
* 配置任务:
213+
```
214+
jshint : {
215+
options: {
216+
jshintrc : '.jshintrc' //指定配置文件
217+
},
218+
build : ['Gruntfile.js', 'src/js/*.js'] //指定检查的文件
219+
}
220+
```
221+
* 加载任务:
222+
```
223+
grunt.loadNpmTasks('grunt-contrib-jshint');
224+
```
225+
* 注册任务:
226+
```
227+
grunt.registerTask('default', ['concat', 'uglify', 'jshint']);
228+
```
229+
* 命令:
230+
```
231+
grunt //提示变量未定义和语句后未加分号 -->修改后重新编译
232+
```
233+
234+
* 使用cssmin插件
235+
* 安装:
236+
```
237+
npm install grunt-contrib-cssmin --save-dev
238+
```
239+
* 编码:
240+
* test1.css
241+
```
242+
#box1 {
243+
width: 100px;
244+
height: 100px;
245+
background: red;
246+
}
247+
```
248+
* test2.css
249+
```
250+
#box2 {
251+
width: 200px;
252+
height: 200px;
253+
background: blue;
254+
}
255+
```
256+
* index.html
257+
```
258+
<link rel="stylesheet" href="build/css/output.min.css">
259+
<div id="box1"></div>
260+
<div id="box2"></div>
261+
```
262+
263+
* 配置 : Gruntfile.js
264+
* 配置任务:
265+
```
266+
cssmin:{
267+
options: {
268+
shorthandCompacting: false,
269+
roundingPrecision: -1
270+
},
271+
build: {
272+
files: {
273+
'build/css/output.min.css': ['src/css/*.css']
274+
}
275+
}
276+
}
277+
```
278+
* 加载任务:
279+
```
280+
grunt.loadNpmTasks('grunt-contrib-cssmin');
281+
```
282+
* 注册任务:
283+
```
284+
grunt.registerTask('default', ['concat', 'uglify', 'jshint', 'cssmin']);
285+
```
286+
* 命令:
287+
```
288+
grunt //在build/css/下生成output.min.css
289+
```
290+
291+
* 使用watch插件(真正实现自动化)
292+
* 命令: npm install grunt-contrib-watch --save-dev
293+
* 配置 : Gruntfile.js
294+
295+
* 配置任务:
296+
```
297+
watch : {
298+
scripts : {
299+
files : ['src/js/*.js', 'src/css/*.css'],
300+
tasks : ['concat', 'jshint', 'uglify', 'cssmin'],
301+
options : {spawn : false}
302+
}
303+
}
304+
```
305+
* 加载任务:
306+
```
307+
grunt.loadNpmTasks('grunt-contrib-watch');
308+
```
309+
* 注册任务:
310+
```
311+
grunt.registerTask('default', ['concat', 'uglify', 'jshint', 'watch']);
312+
改进:grunt.registerTask('myWatch', ['default','watch']);
313+
```
314+
* 命令:
315+
```
316+
grunt //控制台提示watch已经开始监听, 修改保存后自动编译处理
317+
// 优化后 grunt myWatch 用于开发,grunt 用于打包
318+
```

11-构建工具/02-grunt/README.md

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"curly": true,
3+
"eqeqeq": true,
4+
"eqnull": true,
5+
"expr" : true,
6+
"immed": true,
7+
"newcap": true,
8+
"noempty": true,
9+
"noarg": true,
10+
"regexp": true,
11+
"browser": true,
12+
"devel": true,
13+
"node": true,
14+
"boss": false,
15+
"undef": true,
16+
"asi": false,
17+
"predef": [ "define", "BMap", "angular", "BMAP_STATUS_SUCCESS"]
18+
}

0 commit comments

Comments
 (0)