Skip to content

Commit 8fae65e

Browse files
committed
Merge pull request gulpjs#48 from lazd/funcdocs
gulp API documentation enhancements
2 parents eeff27e + 9b737fa commit 8fae65e

File tree

1 file changed

+105
-20
lines changed

1 file changed

+105
-20
lines changed

README.md

Lines changed: 105 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ The gulp community is growing, with new plugins being added daily. See the [npm
8888

8989
### gulp.src(globs[, options])
9090

91-
Takes a glob and represents a file structure. Can be piped to plugins. You can specify a single glob or an array of globs (see docs). All options are passed directly through to [glob-stream]. See the [glob-stream documentation] for more information.
91+
Takes a glob and represents a file structure. Can be piped to plugins.
9292

9393
```javascript
9494
gulp.src('./client/templates/*.jade')
@@ -97,19 +97,32 @@ gulp.src('./client/templates/*.jade')
9797
.pipe(gulp.dest('./build/minified_templates'));
9898
```
9999

100+
#### globs
101+
Type: `String` or `Array`
102+
103+
Glob or globs to read.
104+
105+
#### options
106+
Type: `Object`
107+
108+
Options to pass to [node-glob] through [glob-stream].
109+
110+
gulp adds two additional options in addition to the [options supported by node-glob][node-glob documentation]:
111+
100112
#### options.buffer
101113
Type: `Boolean`
102114
Default: `true`
103115

104-
Setting this to `false` will return `file.contents` as a stream and not buffer files. This may not be supported by many plugins.
116+
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 may not implement support for streams.
105117

106118
#### options.read
107119
Type: `Boolean`
108120
Default: `true`
109121

110122
Setting this to `false` will return `file.contents` as null and not read the file at all.
111123

112-
### gulp.dest(path[, options])
124+
125+
### gulp.dest(path)
113126

114127
Can be piped to and it will write files. Re-emits all data passed to it so you can pipe to multiple folders.
115128

@@ -121,33 +134,48 @@ gulp.src('./client/templates/*.jade')
121134
.pipe(gulp.dest('./build/minified_templates'));
122135
```
123136

124-
### gulp.task(name[, deps], fn)
137+
#### path
138+
Type: `String`
139+
140+
The path (folder) to write files to.
125141

126-
Tasks that you want to run from the command line should not have spaces in them.
127142

128-
The task system is [Orchestrator] so check there for more detailed information.
143+
### gulp.task(name[, deps], fn)
144+
145+
Define a task using [Orchestrator].
129146

130147
```javascript
131148
gulp.task('somename', function() {
132149
// Do stuff
133150
});
134151
```
135152

136-
#### Task dependencies
153+
#### name
154+
155+
The name of the task. Tasks that you want to run from the command line should not have spaces in them.
137156

138-
This lets you specify tasks to be executed and completed before your task will run.
157+
#### deps
158+
Type: `Array`
159+
160+
An array of tasks to be executed and completed before your task will run.
139161

140162
```javascript
141-
gulp.task('somename', ['array', 'of', 'task', 'names'], function() {
163+
gulp.task('mytask', ['array', 'of', 'task', 'names'], function() {
142164
// Do stuff
143165
});
144166
```
145167

146-
If the dependencies are asynchronous it is not guaranteed that they will finish before `'somename'` is executed. To ensure they are completely finished, you need to make sure the dependency tasks have asynchronous support through one of the methods outlined below. The most simple method is to return the stream. By returning the stream, Orchestrator is able to listen for the end event and only run `'somename'` once each dependencies' stream end event has been emitted. You can also use callbacks or promises to do your own cool stuff.
168+
**Note:** If the dependencies are asynchronous it is not guaranteed that they will finish before `mytask` is executed. To ensure they are completely finished, you need to make sure the dependency tasks have asynchronous support through one of the methods outlined below.
169+
170+
#### fn
171+
172+
The function that performs the task's operations. Generally this takes the form of `gulp.src().pipe(someplugin())`.
147173

148-
#### Async tasks
174+
#### Async task support
149175

150-
With callbacks:
176+
Tasks can be made asynchronous if its `fn` does one of the following:
177+
178+
##### Accept a callback
151179

152180
```javascript
153181
gulp.task('somename', function(cb) {
@@ -156,7 +184,7 @@ gulp.task('somename', function(cb) {
156184
});
157185
```
158186

159-
Wait for stream to end:
187+
##### Return a stream
160188

161189
```javascript
162190
gulp.task('somename', function() {
@@ -167,7 +195,7 @@ gulp.task('somename', function() {
167195
});
168196
```
169197
170-
With promises:
198+
##### Return a promise
171199
172200
```javascript
173201
var Q = require('q');
@@ -186,7 +214,10 @@ gulp.task('somename', function() {
186214
187215
### gulp.run(tasks...[, cb])
188216
189-
Triggers tasks to be executed. *Does not run in order*.
217+
#### tasks
218+
Type: `String`
219+
220+
Tasks to be executed. You may pass any number of tasks as individual arguments. **Note:** Tasks are run concurrently and therefore do not run in order, see [Orchestrator] for more information.
190221
191222
```javascript
192223
gulp.run('scripts', 'copyfiles', 'builddocs');
@@ -198,21 +229,73 @@ gulp.run('scripts', 'copyfiles', 'builddocs', function(err) {
198229
});
199230
```
200231
201-
Use gulp.run to run tasks from other tasks. You will probably use this in your default task and to group small tasks into larger tasks.
232+
Use `gulp.run` to run tasks from other tasks. You will probably use this in your default task and to group small tasks into larger tasks.
202233
203234
### gulp.watch(glob, cb)
204235
205-
glob can be a standard glob or an array of globs. cb is called on each fs change with an object describing the change.
236+
#### glob
237+
Type: `String` or `Array`
238+
239+
A single glob or array of globs that indicate which files to watch for changes.
240+
241+
#### cb(event)
242+
Type: `Function`
243+
244+
Callback to be called on each change.
206245
207246
```javascript
208247
gulp.watch('js/**/*.js', function(event) {
248+
console.log('File '+event.path+' was '+event.type+', running tasks...');
209249
gulp.run('scripts', 'copyfiles');
210250
});
211251
```
212252
253+
The callback will be passed an object, `event`, that describes the change:
254+
255+
##### event.type
256+
Type: `String`
257+
258+
The type of change that occurred, either `added`, `changed` or `deleted`.
259+
260+
##### event.path
261+
Type: `String`
262+
263+
The path to the file that triggered the event.
264+
265+
213266
### gulp.env
214267
215-
gulp.env is an optimist arguments object. Running `gulp test dostuff --production` will yield `{_:["test","dostuff"],production:true}`. Plugins don't use this.
268+
`gulp.env` is a [node-optimist] arguments object. For instance, if you run:
269+
270+
```
271+
gulp test dostuff --production
272+
```
273+
274+
Which results in the following `gulp.env`:
275+
276+
```
277+
{
278+
_: ['test', 'dostuff'],
279+
production: true
280+
}
281+
```
282+
283+
You can use this to conditionally enable certain plugins:
284+
285+
```
286+
gulp.task('scripts', function() {
287+
var stream = gulp.src(['client/js/**/*.js', '!client/js/vendor/**']);
288+
289+
// Only uglify in production
290+
if (!gulp.env.production) {
291+
stream = stream.pipe(uglify());
292+
}
293+
294+
stream.pipe(gulp.dest('build/js'));
295+
});
296+
```
297+
298+
**Note:** Plugins should not perform differently based on `gulp.env`.
216299
217300
218301
## gulp CLI
@@ -223,7 +306,7 @@ Tasks can be executed by running `gulp <task> <othertask>`. Just running `gulp`
223306
224307
### Compilers
225308
226-
You can use any language you want for your gulpfile. You will have to specify the language module name so the CLI can load it (and its assosciated extensions) before attempting to find your gulpfile. Make sure you have this module installed accessible by the folder you are running the CLI in.
309+
You can use any language you want for your gulpfile. You will have to specify the language module name so the CLI can load it (and its associated extensions) before attempting to find your gulpfile. Make sure you have this module installed accessible by the folder you are running the CLI in.
227310
228311
Example:
229312
@@ -271,7 +354,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
271354
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/wearefractal/gulp/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
272355
273356
274-
[glob-stream documentation]: https://github.com/wearefractal/glob-stream
357+
[node-optimist]: https://github.com/substack/node-optimist
358+
[node-glob documentation]: https://github.com/isaacs/node-glob#options
359+
[node-glob]: https://github.com/isaacs/node-glob
275360
[glob-stream]: https://github.com/wearefractal/glob-stream
276361
[Orchestrator]: https://github.com/robrich/orchestrator
277362
[plugin search]: https://npmjs.org/browse/keyword/gulpplugin

0 commit comments

Comments
 (0)