gulp.src(globs[, options])
Emits files matching provided glob or an array of globs. Returns a stream of Vinyl files that can be piped to plugins.
gulp.src('client/templates/*.jade')
.pipe(jade())
.pipe(minify())
.pipe(gulp.dest('build/minified_templates'));
globs
Type: String or Array
要讀取的glob或glob數組。Globs使用node-glob語法,除此之外它還支持取反。以!開頭的glob從結果中排除匹配的文件。
options
Type: Object
通過glob-stream傳遞給 node-glob的選項。
gulp支持除ignore之外所有node-glob支持的選項 和 glob-stream支持的選項,并且還支持以下選項:
options.buffer
Type: Boolean
Default: true
Setting this to false will return file.contents as a stream and not buffer files. This is useful when working with large files.Note: Plugins might not implement support for streams.
options.read
Type: Boolean
Default: true
Setting this to false will return file.contents as null and not read the file at all.
options.base
Type: String
Default: everything before a glob starts (see glob2base)
E.g., 考慮client/js/somedir中的somefile.js:
gulp.src('client/js/**/*.js') // 匹配'client/js/somedir/somefile.js'并將`base`解析為`client/js/`
.pipe(minify())
.pipe(gulp.dest('build')); //寫入'build/somedir/somefile.js'
gulp.src('client/js/**/*.js', { base: 'client' })
.pipe(minify())
.pipe(gulp.dest('build')); // 寫入'build/js/somedir/somefile.js'
gulp.dest(path[, options])
Can be piped to and it will write files. Re-emits all data passed to it so you can pipe to multiple folders. Folders that don't exist will be created.
gulp.src('./client/templates/*.jade')
.pipe(jade())
.pipe(gulp.dest('./build/templates'))
.pipe(minify())
.pipe(gulp.dest('./build/minified_templates'));
寫入路徑是通過把文件相對路徑添加到給出的目的目錄后得出的。反過來說,相對路徑是相對于文件base的。詳細查看上面的gulp.src。
path
Type: String or Function
寫入文件的路徑(輸出文件夾)。或者是一個返回路徑的函數,函數必須提供一個vinyl File instance.
options
Type: Object
options.cwd
Type: String
Default: process.cwd()
cwd for the output folder, only has an effect if provided output folder is relative.
options.mode
Type: String
Default: 0777
指明任何需要創建的文件夾模式的八進制權限字符串。
gulp.task(name [, deps, fn])
使用Orchestrator定義一個任務。
gulp.task('somename', function() {
// Do stuff
});
name
Type: String
任務的名稱。在命令行中輸入的任務名稱之間要有空格。
deps
Type: Array
在你的任務執行前要執行并結束的一個任務組。
gulp.task('mytask', ['array', 'of', 'task', 'names'], function() {
// Do stuff
});
注意:你的任務是在依賴任務結束前運行的嗎?要保證你的依賴任務正確使用異步運行提示:接受一個回調函數或返回一個promise或事件流。
如果你僅想執行一系列任務,你可以忽略函數:
gulp.task('build', ['array', 'of', 'task', 'names']);
注意:這些任務會并行執行(同時),因此不要以為他們會依次開始或結束。
fn
Type: Function
這個函數執行任務的主要操作。一般來說會是以下形式:
gulp.task('buildStuff', function() {
// Do something that "builds stuff"
var stream = gulp.src(/*some source path*/)
.pipe(somePlugin())
.pipe(someOtherPlugin())
.pipe(gulp.dest(/*some destination*/));
return stream;
});
異步任務支持
如果該函數有以下特性,那么這個任務就是異步的。
接受一個回調函數
// run a command in a shell
var exec = require('child_process').exec;
gulp.task('jekyll', function(cb) {
// build Jekyll
exec('jekyll build', function(err) {
if (err) return cb(err); // return error
cb(); // finished task
});
});
返回一個流
gulp.task('somename', function() {
var stream = gulp.src('client/**/*.js')
.pipe(minify())
.pipe(gulp.dest('build'));
return stream;
});
返回一個promise
var Q = require('q');
gulp.task('somename', function() {
var deferred = Q.defer();
// do async stuff
setTimeout(function() {
deferred.resolve();
}, 1);
return deferred.promise;
});
注意: 默認情況下,任務以最大的并行性運行。——例如,它同時啟動所有任務,不做等待。如果你想創建一系列按需執行的任務,你需要做兩件事:
- 給一個提示表明任務結束
- 并且給出一個任務依賴另一個任務的提示
對下面的例子,我們假設你有兩個任務,你希望他們順序執行。
- 在任務one中你添加一個表明任務結束的提示。Either take in a callback and call it when you're done or return a promise or stream that the engine should wait to resolve or end respectively.
- 在任務two中你添加一個提示,說明它依賴任務one的完成。
因此,任務代碼如下:
var gulp = require('gulp');
//接受一個回調函數以讓引擎知道它何時結束
gulp.task('one', function(callback) {
// do stuff -- async or otherwise
callback(err); // if err is not null and not undefined, the run will stop, and note that it failed
});
// 指明在這個任務開始前依賴任務必須完成
gulp.task('two', ['one'], function() {
//任務one現在完成了
});
gulp.task('default', ['one', 'two']);
gulp.watch(glob [, opts], tasks) or gulp.watch(glob [, opts, cb])
監聽文件變化,并執行一些動作。This always returns an EventEmitter that emits change events.
gulp.watch(glob[, opts], tasks)
glob
Type: String or Array
用來指明哪些文件要監測的glob或者glob數組。
opts
Type: Object
傳遞給gaze的配置選項。
tasks
Type: Array
當文件改變時需要執行的任務的名稱。
var watcher = gulp.watch('js/**/*.js', ['uglify','reload']);
watcher.on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
gulp.watch(glob[, opts, callback])
glob
Type: String or Array
用來指明哪些文件要監測的glob或者glob數組。
opts
Type: Object
傳遞給gaze的配置選項。
callback(event)
Type: Function
每次發生改變都會調用的函數。
gulp.watch('js/**/*.js', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
回調函數被傳遞一個event對象,用來描述這次改變:
event.type
Type: String
發生的改變的類型:added、changed或deleted。
event.path
Type: String
觸發這個事件的文件的路徑。