Skip to content

Commit 168c113

Browse files
committed
细节调整、chapter7 新增内容
1 parent 0509872 commit 168c113

39 files changed

+450
-267
lines changed

README.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,33 @@ gulp 入门指南
33

44
gulp 是基于 node 实现 Web 前端自动化开发的工具,利用它能够极大的提高开发效率。
55

6-
Web前端开发工作中有很多“无脑工作”,比如压缩CSS/JS文件。而这些工作都是有规律的。找到这些规律,并编写 gulp 配置代码。
6+
Web 前端开发工作中有很多“重复工作”,比如压缩CSS/JS文件。而这些工作都是有规律的。找到这些规律,并编写 gulp 配置代码,让 gulp 自动执行这些“重复工作”
77

8-
[本书所有示例代码](demo/)
8+
## 目录
99

10-
**目录:**
10+
- [安装 Node 和 gulp](chapter1.md)
11+
- [使用 gulp 压缩 JS](chapter2.md)
12+
- [使用 gulp 压缩 CSS](chapter3.md)
13+
- [使用 gulp 压缩图片](chapter4.md)
14+
- [使用 gulp 编译 LESS](chapter5.md)
15+
- [使用 gulp 编译 Sass](chapter6.md)
16+
- [使用 gulp 构建一个项目](chapter7.md)
1117

12-
1. [安装 Node 和 gulp](chapter1.md)
13-
2. [使用 gulp 压缩 JS](chapter2.md)
14-
3. [使用 gulp 压缩 CSS](chapter3.md)
15-
4. [使用 gulp 压缩图片](chapter4.md)
16-
5. [使用 gulp 编译 LESS](chapter5.md)
17-
6. [使用 gulp 编译 Sass](chapter6.md)
18-
7. [使用 gulp 开发一个项目](chapter7.md)
1918

20-
[点击此处](https://github.com/nimojs/gulp-book/issues)发帖告诉我,你还需要哪些新章节。([https://github.com/nimojs/gulp-book/issues](https://github.com/nimojs/gulp-book/issues))
21-
压缩JS文件的规律和 gulp 代码
22-
------------------------
19+
## 资源
20+
- [订阅本书](https://github.com/nimojs/gulp-book/issues/7)
21+
- [所有示例代码](https://github.com/nimojs/gulp-book/tree/master/demo)
22+
- [论坛](https://github.com/nimojs/gulp-book/issues)
23+
24+
25+
将规律转换为 gulp 代码
26+
-------------------
2327

2428
现有目录结构如下:
2529

2630
```
2731
└── js/
28-
├── index.js
29-
└── page-a.js
32+
└── a.js
3033
```
3134

3235
### 规律
@@ -38,7 +41,7 @@ gulp 是基于 node 实现 Web 前端自动化开发的工具,利用它能够
3841
### 编写 gulp 代码
3942

4043
```js
41-
// 压缩文件
44+
// 压缩 JavaScript 文件
4245
gulp.task('script', function() {
4346
// 1. 找到
4447
gulp.src('js/*.js')
@@ -55,16 +58,29 @@ gulp.task('script', function() {
5558

5659
```
5760
└── js/
58-
│ ├── index.js
59-
│ └── page-a.js
61+
│ └── a.js
6062
└── dist/
6163
└── js/
62-
├── index.js
63-
└── page-a.js
64+
└── a.js
65+
```
66+
67+
a.js 压缩前
68+
```
69+
function demo (msg) {
70+
alert('--------\r\n' + msg + '\r\n--------')
71+
}
72+
73+
demo('Hi')
74+
```
75+
a.js 压缩后
76+
```
77+
function demo(n){alert("--------\r\n"+n+"\r\n--------")}demo("Hi");
6478
```
6579

6680
此时 `dist/js` 目录下的 `.js` 文件都是压缩后的版本。
6781

82+
你还可以监控 `js/` 目录下的 js 文件,当某个文件被修改时,自动压缩修改文件。启动 gulp 后就可以他自动构建 Web 项目。
83+
6884
----------------
6985

7086
gulp 还可以做很多事,例如:

SUMMARY.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# 目录
2-
3-
- [安装 Node 和 gulp](chapter1.md)
4-
- [使用 gulp 压缩 JS](chapter2.md)
5-
- [使用 gulp 压缩 CSS](chapter3.md)
6-
- [使用 gulp 压缩图片](chapter4.md)
7-
- [使用 gulp 编译 LESS](chapter5.md)
8-
- [使用 gulp 编译 Sass](chapter6.md)
9-
- [使用 gulp 开发一个项目](chapter7.md)
1+
# 目录
2+
3+
- [安装 Node 和 gulp](chapter1.md)
4+
- [使用 gulp 压缩 JS](chapter2.md)
5+
- [使用 gulp 压缩 CSS](chapter3.md)
6+
- [使用 gulp 压缩图片](chapter4.md)
7+
- [使用 gulp 编译 LESS](chapter5.md)
8+
- [使用 gulp 编译 Sass](chapter6.md)
9+
- [使用 gulp 构建一个项目](chapter7.md)

chapter1.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
gulp 是基于 node 实现的,那么我们就需要先安装 node。
55

6-
Node 是一个基于Chrome JavaScript V8引擎建立的一个平台,可以利用它实现 Web服务,做类似PHP的事。
6+
> Node 是一个基于Chrome JavaScript V8引擎建立的一个平台,可以利用它实现 Web服务,做类似PHP的事。
77
88
打开 https://nodejs.org/ 点击绿色的 **INSTALL** 按钮下载安装 node。
99

10+
<a href="#hash_cli" name="hash_cli"></a>
11+
1012
使用终端/命令行
1113
-------------
1214

@@ -66,7 +68,7 @@ npm 模块管理器
6668
安装 gulp
6769
----
6870

69-
npm 基于终端/命令行的 node 包管理工具,可以利用它安装 gulp 所需的包。(在安装 node 时已经自动安装了 npm)
71+
npm node 的包管理工具,可以利用它安装 gulp 所需的包。(在安装 node 时已经自动安装了 npm)
7072

7173
在命令行输入
7274

chapter2.md

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,28 @@
1919
gulp 代码
2020
----
2121

22-
你可以 [下载所有示例代码](https://github.com/nimojs/gulp-book/archive/master.zip) - [或在线查看代码](https://github.com/nimojs/gulp-book/tree/master/demo/chapter2)
22+
你可以 [下载所有示例代码](https://github.com/nimojs/gulp-book/archive/master.zip) [在线查看代码](https://github.com/nimojs/gulp-book/tree/master/demo/chapter2)
2323

2424
**建议**:你可以只阅读下面的代码与注释或同时阅读代码解释
2525

2626
gulp 的所有配置代码都写在 `gulpfile.js` 文件。
2727

2828
**一、新建一个 `gulpfile.js` 文件**
29+
```
30+
chapter2
31+
└── gulpfile.js
32+
```
2933

3034
---------
3135

3236
**二、在 `gulpfile.js` 中编写代码**
3337

3438
```js
3539
// 获取 gulp
36-
var gulp = require('gulp');
40+
var gulp = require('gulp')
3741
```
3842

39-
> `require()` 是 node (CommonJS)中获取模块的语法,此处不做过多解释
43+
> `require()` 是 node (CommonJS)中获取模块的语法。
4044
>
4145
> 在 gulp 中你只需要理解 `require()` 可以获取模块。
4246
@@ -46,23 +50,24 @@ var gulp = require('gulp');
4650

4751
```js
4852
// 获取 uglify 模块(用于压缩 JS)
49-
var uglify = require('gulp-uglify');
53+
var uglify = require('gulp-uglify')
5054
```
5155

5256
---------
5357

5458
**四、创建压缩任务**
5559

5660
```js
57-
// 压缩文件
61+
// 压缩 js 文件
62+
// 在命令行使用 gulp script 启动此任务
5863
gulp.task('script', function() {
5964
// 1. 找到文件
6065
gulp.src('js/*.js')
6166
// 2. 压缩文件
6267
.pipe(uglify())
6368
// 3. 另存压缩后的文件
64-
.pipe(gulp.dest('dist/js'));
65-
});
69+
.pipe(gulp.dest('dist/js'))
70+
})
6671
```
6772

6873
- `gulp.task(name, fn)` - 定义任务,第一个参数是任务名,第二个参数是任务内容。
@@ -126,7 +131,14 @@ gulp-uglify@1.1.0 node_modules/gulp-uglify
126131
chapter2 $
127132
```
128133

129-
在你的文件夹中会 新增一个 `node_modules` 文件夹,这里面存放着 npm 安装的模块。
134+
在你的文件夹中会新增一个 `node_modules` 文件夹,这里面存放着 npm 安装的模块。
135+
136+
目录结构:
137+
```
138+
├── gulpfile.js
139+
└── node_modules
140+
└── gulp-uglify
141+
```
130142

131143
接着输入 `gulp script` 执行任务
132144

@@ -141,23 +153,49 @@ gulp script
141153

142154
**八、编写 js 文件**
143155

144-
我们发现 gulp 没有进行任何压缩操作。因为没有js目录,自然也没有 js 目录下的 `.js` 后缀文件。
156+
我们发现 gulp 没有进行任何压缩操作。因为没有js这个目录,也没有 js 目录下的 `.js` 后缀文件。
145157

146158
创建 `a.js` 文件,并编写如下内容
147159

148160
```
149161
// a.js
150162
function demo (msg) {
151-
alert('--------\r\n' + msg + '\r\n--------');
163+
alert('--------\r\n' + msg + '\r\n--------')
152164
}
153165
154-
demo('Hi');
166+
demo('Hi')
167+
```
168+
169+
目录结构:
170+
```
171+
├── gulpfile.js
172+
├── js
173+
│ └── a.js
174+
└── node_modules
175+
└── gulp-uglify
155176
```
156177

157178
接着在命令行输入 `gulp script` 执行任务
158179

159180
gulp 会在命令行当前目录下创建 `dist/js/` 文件夹,并创建压缩后的 `a.js` 文件。
160181

182+
目录结构:
183+
```
184+
├── gulpfile.js
185+
├── js
186+
│ └── a.js
187+
├── dist
188+
│ └── js
189+
│ └── a.js
190+
└── node_modules
191+
└── gulp-uglify
192+
```
193+
194+
[dist/js/a.js](https://github.com/nimojs/gulp-book/blob/master/demo/chapter2/dist/js/a.js)
195+
```js
196+
function demo(n){alert("--------\r\n"+n+"\r\n--------")}demo("Hi");
197+
```
198+
161199
---------
162200

163201
**九、检测代码修改自动执行任务**
@@ -174,16 +212,29 @@ gulp.watch('js/*.js', ['script']);
174212

175213
但是没有命令可以运行 `gulp.watch()`,需要将 `gulp.watch()` 包含在一个任务中。
176214

177-
修改代码如下:
178215
```
216+
// 在命令行使用 gulp auto 启动此任务
179217
gulp.task('auto', function () {
180218
// 监听文件修改,当文件被修改则执行 script 任务
181-
gulp.watch('js/*.js', ['script']);
219+
gulp.watch('js/*.js', ['script'])
182220
})
183221
```
184222

185223
接在在命令行输入 `gulp auto`,自动监听 `js/*.js` 文件的修改后压缩js。
186224

225+
```
226+
$gulp auto
227+
[21:09:45] Using gulpfile ~/Documents/code/gulp-book/demo/chapter2/gulpfile.js
228+
[21:09:45] Starting 'auto'...
229+
[21:09:45] Finished 'auto' after 9.19 ms
230+
```
231+
232+
此时修改 `js/a.js` 中的代码并保存。命令行将会出现提示,表示检测到文件修改并压缩文件。
233+
234+
```
235+
[21:11:01] Starting 'script'...
236+
[21:11:01] Finished 'script' after 2.85 ms
237+
```
187238
至此,我们完成了 gulp 压缩 js 文件的自动化代码编写。
188239

189240
**注意:**使用 `gulp.watch` 后你的命令行会进入“运行”状态,此时你不可以在命令行进行其他操作。可通过 `Ctrl + C` 停止 gulp。(Mac 中使用 `control + C`)
@@ -204,10 +255,10 @@ gulp.task('default', ['script', 'auto']);
204255

205256
```js
206257
// 获取 gulp
207-
var gulp = require('gulp');
258+
var gulp = require('gulp')
208259

209260
// 获取 uglify 模块(用于压缩 JS)
210-
var uglify = require('gulp-uglify');
261+
var uglify = require('gulp-uglify')
211262

212263
// 压缩 js 文件
213264
// 在命令行使用 gulp script 启动此任务
@@ -217,24 +268,24 @@ gulp.task('script', function() {
217268
// 2. 压缩文件
218269
.pipe(uglify())
219270
// 3. 另存压缩后的文件
220-
.pipe(gulp.dest('dist/js'));
221-
});
271+
.pipe(gulp.dest('dist/js'))
272+
})
222273

223274
// 在命令行使用 gulp auto 启动此任务
224275
gulp.task('auto', function () {
225276
// 监听文件修改,当文件被修改则执行 script 任务
226-
gulp.watch('js/*.js', ['script']);
227-
});
277+
gulp.watch('js/*.js', ['script'])
278+
})
228279

229280

230281
// 使用 gulp.task('default') 定义默认任务
231282
// 在命令行使用 gulp 启动 script 任务和 auto 任务
232-
gulp.task('default', ['script', 'auto']);
283+
gulp.task('default', ['script', 'auto'])
233284
```
234285

235-
去除注释后,你会发现只需要 11 行代码就可以让 gulp 自动监听 js 文件的修改后压缩代码。
286+
去除注释后,你会发现只需要 11 行代码就可以让 gulp 自动监听 js 文件的修改后压缩代码。但是还有还有一些性能问题和缺少容错性,将在后面的章节详细说明。
236287

237288

238289
你可以访问 [gulp-uglify](https://github.com/terinjokes/gulp-uglify) 以查看更多用法。
239290

240-
[阅读下一章节:使用 gulp 压缩 CSS](chapter3.md)
291+
[接着阅读:使用 gulp 压缩 CSS](chapter3.md)

0 commit comments

Comments
 (0)