說起來慚愧,一直用公司內部的工具,沒有用這些紅得發紫的東西。今天東抄西拼終於搞出第一個gulp應用。gulp是做什么的,好處在哪兒我不廢話了。直入主題吧。
先在D盤下建立一個xxxx目錄,然后打開控制台,直接將npm install gulp。 里面多出一個node_modules目錄安裝成功。
然后xxxx目錄下面建一個src目錄,里面建一個index.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> <pre class="hljs-dark"><code class="hljs javascript"><span class="hljs-keyword">var gulp = <span class="hljs-built_in">require(<span class="hljs-string">'gulp'), preprocess = <span class="hljs-built_in">require(<span class="hljs-string">'gulp-preprocess'); gulp.task(<span class="hljs-string">'default', <span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">() { gulp.src(<span class="hljs-string">'src/index.html') .pipe(preprocess({USERNAME:<span class="hljs-string">'程序猿小卡'})) .pipe(gulp.dest(<span class="hljs-string">'dest/')); }); </span></span></span></span></span></span></span></span></span></span></span></span></code></pre> <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>vinyl-fs/lib/src/index.js</p> <pre class="hljs-dark"><code class="hljs js"><span class="hljs-keyword">var defaults = <span class="hljs-built_in">require(<span class="hljs-string">'lodash.defaults'); <span class="hljs-keyword">var through = <span class="hljs-built_in">require(<span class="hljs-string">'through2'); <span class="hljs-keyword">var gs = <span class="hljs-built_in">require(<span class="hljs-string">'glob-stream'); <span class="hljs-keyword">var File = <span class="hljs-built_in">require(<span class="hljs-string">'vinyl'); <span class="hljs-comment">// 省略非重要代碼若干行 <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">src<span class="hljs-params">(glob, opt) { <span class="hljs-comment">// 繼續省略代碼 <span class="hljs-keyword">var globStream = gs.create(glob, options); <span class="hljs-comment">// when people write to use just pass it through <span class="hljs-keyword">var outputStream = globStream .pipe(through.obj(createFile)) .pipe(getStats(options)); <span class="hljs-keyword">if (options.read !== <span class="hljs-literal">false) { outputStream = outputStream .pipe(getContents(options)); } <span class="hljs-comment">// 就是這里了 <span class="hljs-keyword">return outputStream .pipe(through.obj()); } </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre> <p>我們再看看<code>glob-stream/index.js</code>里的<code>create</code>方法,最后的<code>return aggregate.pipe(uniqueStream);</code>。好的,下一步就是真相了,我們去<code>ordered-read-streams</code>這個項目一探究竟。<br />參考:<a href="https://github.com/wearefractal/glob-stream/blob/master/index.js#L89" target="_blank">https://github.com/wearefractal/glob-stream/blob/master/index.js#L89</a></p> <p>glob-stream/index.js</p> <pre class="hljs-dark"><code class="hljs js"><span class="hljs-keyword">var through2 = <span class="hljs-built_in">require(<span class="hljs-string">'through2'); <span class="hljs-keyword">var Combine = <span class="hljs-built_in">require(<span class="hljs-string">'ordered-read-streams'); <span class="hljs-keyword">var unique = <span class="hljs-built_in">require(<span class="hljs-string">'unique-stream'); <span class="hljs-keyword">var glob = <span class="hljs-built_in">require(<span class="hljs-string">'glob'); <span class="hljs-keyword">var minimatch = <span class="hljs-built_in">require(<span class="hljs-string">'minimatch'); <span class="hljs-keyword">var glob2base = <span class="hljs-built_in">require(<span class="hljs-string">'glob2base'); <span class="hljs-keyword">var path = <span class="hljs-built_in">require(<span class="hljs-string">'path'); <span class="hljs-comment">// 必須省略很多代碼 <span class="hljs-comment">// create 方法 create: <span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">(globs, opt) { <span class="hljs-comment">// 繼續省略代碼 <span class="hljs-comment">// create all individual streams <span class="hljs-keyword">var streams = positives.map(<span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">(glob){ <span class="hljs-keyword">return gs.createStream(glob, negatives, opt); }); <span class="hljs-comment">// then just pipe them to a single unique stream and return it <span class="hljs-keyword">var aggregate = <span class="hljs-keyword">new Combine(streams); <span class="hljs-keyword">var uniqueStream = unique(<span class="hljs-string">'path'); <span class="hljs-comment">// TODO: set up streaming queue so items come in order <span class="hljs-keyword">return aggregate.pipe(uniqueStream); </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre> <p>真相來了,我們看下<code>ordered-read-streams</code>的代碼,可能剛開始看不是很懂,沒關系,知道它實現了自己的<code>Stream</code>就可以了(nodejs是有暴露相應的API讓開發者對Stream進行定制的),具體可參考:<a href="http://www.nodejs.org/api/stream.html#stream_api_for_stream_implementors" target="_blank">http://www.nodejs.org/api/stream.html#stream_api_for_stream_implementors</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> <p>ordered-read-streams/index.js</p> <pre class="hljs-dark"><code class="hljs js"><span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">OrderedStreams<span class="hljs-params">(streams, options) { <span class="hljs-keyword">if (!(<span class="hljs-keyword">this <span class="hljs-keyword">instanceof(OrderedStreams))) { <span class="hljs-keyword">return <span class="hljs-keyword">new OrderedStreams(streams, options); } streams = streams || []; options = options || {}; <span class="hljs-keyword">if (!<span class="hljs-built_in">Array.isArray(streams)) { streams = [streams]; } options.objectMode = <span class="hljs-literal">true; Readable.call(<span class="hljs-keyword">this, options); <span class="hljs-comment">// stream data buffer <span class="hljs-keyword">this._buffs = []; <span class="hljs-keyword">if (streams.length === <span class="hljs-number">0) { <span class="hljs-keyword">this.push(<span class="hljs-literal">null); <span class="hljs-comment">// no streams, close <span class="hljs-keyword">return; } streams.forEach(<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-params">(s, i) { <span class="hljs-keyword">if (!s.readable) { <span class="hljs-keyword">throw <span class="hljs-keyword">new <span class="hljs-built_in">Error(<span class="hljs-string">'All input streams must be readable'); } s.on(<span class="hljs-string">'error', <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-params">(e) { <span class="hljs-keyword">this.emit(<span class="hljs-string">'error', e); }.bind(<span class="hljs-keyword">this)); <span class="hljs-keyword">var buff = []; <span class="hljs-keyword">this._buffs.push(buff); s.on(<span class="hljs-string">'data', buff.unshift.bind(buff)); s.on(<span class="hljs-string">'end', flushStreamAtIndex.bind(<span class="hljs-keyword">this, i)); }, <span class="hljs-keyword">this); } </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre> <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>
好了,繼續安裝另一個插件gulp-htmlmin。照着readme安裝就是,不過一下安裝這么多依賴,黑壓壓一坨,着實嚇人!
然后在xxxx目錄下,建立一個gulpfile.js文件,內容直接抄gulp-htmlmin的readme:
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 'default' is not in your gulpfile”。只好求助谷歌,發現這個東西
var gulp = require('gulp'); var coffee = require('gulp-coffee'); gulp.task('scripts', function () { gulp.src('src/*.coffee') .pipe(coffee()) .pipe(gulp.dest('./')); }); gulp.task('watch', function () { gulp.watch('src/*.coffee', ['scripts']); }); gulp.task('default', ['scripts', 'watch']);
於是將原來的代碼改裝一下:
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']);
運行gulp命令,生成dest目錄,里面的index.html已經成功被壓縮。並且有了watch任務,以后我們每次修改html,都會同步到dest中去。
估計default任務應該是類似C語言的main方法那樣的東西,沒有它是無法帶動其他任務的。
接着我們好好學一下其基礎吧。
gulp有5個基本方法:src、dest、task、run、watch
gulp.src()
gulp模塊的src方法,用於產生數據流。它的參數表示所要處理的文件,一般有以下幾種形式:
js/app.js
:指定確切的文件名js/*.js
:某個目錄所有后綴名為js的文件js/**/*.js
:某個目錄及其所有子目錄中的所有后綴名為js的文件!js/app.js
:除了js/app.js以外的所有文件*.+(js|css)
:匹配項目根目錄下,所有后綴名為js或css的文件
src方法的參數還可以是一個數組,用來指定多個成員:
gulp.src(['js/**/*.js', '!js/**/*.min.js']);
gulp.dest()
gulp模塊的dest方法,可以用來傳送文件,同時寫入文件到指定目錄。可以重復的發送傳遞給它的數據,因此可以將文件傳送到多個目錄中。簡單的例子:
gulp.src('./client/templates/*.jade') .pipe(jade()) .pipe(gulp.dest('./build/templates')) .pipe(minify()) .pipe(gulp.dest('./build/minified_templates'));
gulp.task()
gulp模塊的task方法,用於定義具體的任務。它的第一個參數是任務名,第二個參數是任務函數。下面是一個非常簡單的任務函數:
gulp.task('greet', function () { console.log('Hello world!'); });
task方法還可以指定按順序運行的一組任務:
gulp.task('build', ['css', 'js', 'imgs']);
上面代碼先指定build任務,它按次序由css、js、imgs三個任務所組成。注意:由於每個任務都是異步調用,所以沒有辦法保證js任務的開始運行的時間,正好是css任務運行結束時間。
如果希望各個任務嚴格按次序運行,可以把前一個任務寫成后一個任務的依賴模塊:
gulp.task('css', ['greet'], function () { // Deal with CSS here });
上面代碼表明,css任務依賴greet任務,所以css一定會在greet運行完成后再運行。
如果一個任務的名字為default,就表明它是“默認任務”,在命令行直接輸入gulp命令,就會運行該任務:
gulp.task('default', function () { // Your default task });
gulp.run()
gulp模塊的run方法,表示要執行的任務。可能會使用單個參數的形式傳遞多個任務。注意:任務是盡可能多的並行執行的,並且可能不會按照指定的順序運行:
gulp.run('scripts','copyfiles','builddocs'); gulp.run('scripts','copyfiles','builddocs', function(err) { // 所有任務完成,或者觸發錯誤而終止 });
可以使用gulp.run
在其他任務中運行任務。也可以在默認任務中使用gulp.run
組織多個更小的任務為一個大任務。
gulp.watch()
gulp模塊的watch方法,用於指定需要監視的文件。一旦這些文件發生變動,就運行指定任務:
gulp.task('watch', function () { gulp.watch('templates/*.tmpl.html', ['build']); });
上面代碼指定,一旦templates目錄中的模板文件發生變化,就運行build任務。
watch方法也可以用回調函數,代替指定的任務:
gulp.watch('templates/*.tmpl.html', function (event) { console.log('Event type: ' + event.type); console.log('Event path: ' + event.path); });
另一種寫法是watch方法所監控的文件發生變化時(修改、增加、刪除文件),會觸發change事件,可以對change事件指定回調函數:
var watcher = gulp.watch('templates/*.tmpl.html', ['build']); watcher.on('change', function (event) { console.log('Event type: ' + event.type); console.log('Event path: ' + event.path); });
除了change事件,watch方法還可能觸發以下事件:
- end:回調函數運行完畢時觸發。
- error:發生錯誤時觸發。
- ready:當開始監聽文件時觸發。
- nomatch:沒有匹配的監聽文件時觸發。
watcher對象還包含其他一些方法:
watcher.end()
:停止watcher對象,不會再調用任務或回調函數。watcher.files()
:返回watcher對象監視的文件。watcher.add(glob)
:增加所要監視的文件,它還可以附件第二個參數,表示回調函數。watcher.remove(filepath)
:從watcher對象中移走一個監視的文件。
學完這些就可以到其官網上找插件了,畢竟插件才是王道。