-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gulp:插件编写入门 #17
Labels
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
之前挖了个坑,准备写篇gulp插件编写入门的科普文,之后迟迟没有动笔,因为不知道该肿么讲清楚Stream这货,毕竟,gulp插件的实现不像grunt插件的实现那么直观。
好吧,于是决定单刀直入了。文中插件示例可在这里找到:https://github.com/chyingp/gulp-preprocess
写在前面
我们来看看下面的
gruntfile
,里面用到了笔者刚写的一个gulp插件gulp-preprocess
。好吧,npm publish的时候才发现几个月前就被抢注了。为什么星期天晚上在 http://npmjs.org/package/ 上没有搜到 TAT这个插件基于preprocess这个插件,插件使用方法请自行脑补。本文就讲解下如何实现 gulp-preprocess 这个插件
进入实战
关键代码
我们来看下最关键的几行代码。可以看到,上文的
preprocess()
的作用就是返回一个定制的 Object Stream ,这是实现gulp的流式操作必需的,其他gulp插件也大同小异。gulp-preprocess/index.js
接着,看下具体实现。实际上代码很短
引入依赖
首先,引入插件的依赖项。其中:
核心逻辑
其次,定义
gulp-preprocess
的主体代码。没错,就是下面这么短的代码。代码结构也比较清晰,下面还是简单做下分解介绍。核心代码分解
还是直接上代码,在关键位置加上注释。对 through2 不熟悉的童鞋可以参考这里
写在后面
要把gulp插件内部实现的原理讲透不是件容易的事情,因为实现还是比较复杂的,首先需要对Buffer、Stream 有一定的了解,包括如何通过Node暴露的API对Stream进行定制化。可以参考笔者的另一篇随笔《gulp.src()内部实现探究》,虽然也只是讲了很小的一部分。
The text was updated successfully, but these errors were encountered: