以前都是单枪匹马的干,从没用过模块化的打包工具,加入新的团队后,模块化开发学到不少,尤其是工具的使用。团队目前较多的使用gulp,也是最流行的一款前端打包工具。最近Team开始尝试用gulp,我也只是使用者,还没详细的看其实现原理等,属于小白。

今天就来把玩把玩建立自己的第一个gulp程序。

gulp和grunt一样是基于node,使用它们的前提是已经安装了node.js,具体安装过程可以百度一下,下面介绍我的第一个gulp程序创建过程:

1、在电脑某个盘中新建一个文件夹,比如gulp_test;

2、打开该文件夹,ctrl+shift打开命令窗口,输入:npm install gulp,马上可以看到gulp的下载。

3、继续安装插件gulp-htmlmin,从名字也可以看出是用来压缩hml的。

4、在gulp_test(你新建的文件夹名)中创建一个新的文件下比如src,用来放置你的资源文件。

5、在src中新建一个html,html内容随意。比如:

<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<h2>写在前面</h2>
<p>本来是想写个如何编写gulp插件的科普文的,突然探究欲又发作了,于是就有了这篇东西。。。翻了下源码看了下<code>gulp.src()</code>的实现,不禁由衷感慨:肿么这么复杂。。。</p>
<h2>进入正题</h2>
<p>首先我们看下<code>gulpfile</code>里面的内容是长什么样子的,很有express中间件的味道是不是~<br />我们知道<code>.pipe()</code>是典型的流式操作的API。很自然的,我们会想到<code>gulp.src()</code>这个API返回的应该是个Stream对象(也许经过层层封装)。本着一探究竟的目的,花了点时间把gulp的源码大致扫了下,终于找到了答案。</p>
<p>gulpfile.js</p>
<h2>提前剧透</h2>
<p>此处有内容剧透,如有对剧透不适者,请自行跳过本段落。。。</p>
<blockquote>
<p>gulp.src() 的确返回了定制化的Stream对象。可以在github上搜索<code>ordered-read-streams</code>这个项目。</p>
<p>大致关系是:<br />ordered-read-streams --> glob-stream --> vinyl-fs --> gulp.src()</p> </blockquote>
<h2>探究之路</h2>
<p>首先,我们看下<code>require('gulp')</code>返回了什么。从gulp的源码来看,返回了<code>Gulp</code>对象,该对象上有<code>src</code>、<code>pipe</code>、<code>dest</code>等方法。很好,找到了我们想要的<code>src</code>方法。接着往下看<br />参考:<a href="https://github.com/gulpjs/gulp/blob/master/index.js#L62" target="_blank">https://github.com/gulpjs/gulp/blob/master/index.js#L62</a></p>
<p>gulp/index.js</p>
<pre class="hljs-dark"><code class="hljs js"><span class="hljs-keyword">var inst = <span class="hljs-keyword">new Gulp();
<span class="hljs-built_in">module.exports = inst;
</span></span></span></code></pre>
<p>从下面的代码可以看到,<code>gulp.src</code>方法,实际上是<code>vfs.src</code>。继续<br />参考:<a href="https://github.com/gulpjs/gulp/blob/master/index.js#L25" target="_blank">https://github.com/gulpjs/gulp/blob/master/index.js#L25</a></p>
<p>gulp/index.js</p>
<pre class="hljs-dark"><code class="hljs js"><span class="hljs-keyword">var vfs = <span class="hljs-built_in">require(<span class="hljs-string">'vinyl-fs');
<span class="hljs-comment">// 省略很多行代码
Gulp.prototype.src = vfs.src;
</span></span></span></span></code></pre>
<p>接下来我们看下<code>vfs.src</code>这个方法。从<code>vinyl-fs/index.js</code>可以看到,<code>vfs.src</code>实际是<code>vinyl-fs/lib/src/index.js</code>。<br />参考:<a href="https://github.com/wearefractal/vinyl-fs/blob/master/index.js" target="_blank">https://github.com/wearefractal/vinyl-fs/blob/master/index.js</a></p>
<p>vinyl-fs/index.js</p>
<pre class="hljs-dark"><code class="hljs js"><span class="hljs-pi">'use strict'; <span class="hljs-built_in">module.exports = {
src: <span class="hljs-built_in">require(<span class="hljs-string">'./lib/src'),
dest: <span class="hljs-built_in">require(<span class="hljs-string">'./lib/dest'),
watch: <span class="hljs-built_in">require(<span class="hljs-string">'glob-watcher')
};
</span></span></span></span></span></span></span></span></code></pre>
<p>那么,我们看下<code>vinyl-fs/lib/src/index.js</code>。可以看到,<code>gulp.src()</code>返回的,实际是<code>outputStream</code>这货,而<code>outputStream</code>是<code>gs.create(glob, options).pipe()</code>获得的,差不多接近真相了,还有几步而已。<br />参考:<a href="https://github.com/wearefractal/vinyl-fs/blob/master/lib/src/index.js#L37" target="_blank">https://github.com/wearefractal/vinyl-fs/blob/master/lib/src/index.js#L37</a></p> <p>参考:<a href="https://github.com/armed/ordered-read-streams/blob/master/index.js" target="_blank">https://github.com/armed/ordered-read-streams/blob/master/index.js</a></p>
<h2>写在后面</h2>
<p>兜兜转转一大圈,终于找到了<code>gulp.src()</code>的源头,大致流程如下,算是蛮深的层级。代码细节神马的,有兴趣的同学可以深究一下。</p>
<blockquote>
<p>ordered-read-streams --> glob-stream --> vinyl-fs --> gulp.src()</p>
</blockquote>
</body>
</html>

6、在gulp_test目录下创建gulpfile.js文件,内容如下:

var gulp = require('gulp');
var htmlmin = require('gulp-htmlmin'); gulp.task('minify', function() {
gulp.src('src/*.html')
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('dist'))
});
gulp.task('watch', function () {
console.log('这是测试这是测试!')
gulp.watch('src/*.html', ['minify']);
});
gulp.task('default', ['minify', 'watch']);

7、在src目录下打开命令窗口,运行:gulp,你就能看到程序自动创建了dist文件,其中包含了被压缩后的html文件。

第一个gulp程序就这样搞定了,每次修改完代码后运行gulp就能自动压缩,很方便。

最新文章

  1. Elasticsearch【mappings】类型配置操作
  2. Highcharts使用简例 + 异步动态读取数据
  3. PL/SQL基础-异常处理
  4. Redis的WEB界面管理工具phpRedisAdmin
  5. 高仿bootstrap的layout效果(一)
  6. 提升web响应速度的思路
  7. pl/sql乱码
  8. android studio 加载第三方类库
  9. Error accessing PRODUCT_USER_PROFILE
  10. HTML常用标签和属性大全
  11. 【css面试题】三个DIV要求水平对齐,左右两个DIV宽度固定为100px,中间那个DIV充满剩余的宽度(至少2种方法)
  12. Hrbustoj 2266 Legendary Weights(辗转相除求最大公约数)
  13. 团队作业4——第一次项目冲刺(Alpha版本)6th day
  14. NOIP2014-10-30模拟赛
  15. Dynamics CRM 配置 OAuth 2.0
  16. Python线性表——单链表
  17. laravel——基础增删改查
  18. 获取resource下文件
  19. springboot 学习之路 1(简单入门)
  20. 在VS代码中使用版本控制

热门文章

  1. SecureCRT上传下载文件
  2. loj6570 毛毛虫计数(生成函数FFT)
  3. 9012年,我终于找到了Pypi稳定的源....
  4. 963 AlvinZH打怪刷经验(背包DP大作战R)
  5. vue-cli 3.5 解决 typescript cannot find file 问题。
  6. 基础篇:3.1)规范化:3d草绘
  7. mysql里面 limit的奇效
  8. border.css(解决移动端1px问题)
  9. saltstack源码详解一
  10. 转帖 利用伪元素和css3实现鼠标移入下划线向两边展开效果