Skip to content

Commit 1abb5ed

Browse files
dinoboffphated
authored andcommitted
Docs: Outline using named functions and when to use gulp.task
1 parent d942cf5 commit 1abb5ed

File tree

3 files changed

+60
-49
lines changed

3 files changed

+60
-49
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- replaced 3.x task system (orchestrator) with new task system (bach)
88
- removed gulp.reset
99
- removed 3 argument syntax for `gulp.task`
10-
- using strings when registering with `gulp.task` should only be done when you will call the task with the CLI
10+
- `gulp.task` should only be used when you will call the task with the CLI
1111
- added `gulp.series` and `gulp.parallel` methods for composing tasks. Everything must use these now.
1212
- added single argument syntax for `gulp.task` which allows a named function to be used as the name of the task and task function.
1313
- added `gulp.tree` method for retrieving the task tree. Pass `{ deep: true }` for an `archy` compatible node list.

README.md

+41-31
Original file line numberDiff line numberDiff line change
@@ -40,58 +40,68 @@ var paths = {
4040
images: 'client/img/**/*'
4141
};
4242

43+
/* Register some tasks to expose to the cli */
44+
gulp.task('build', gulp.series(
45+
clean,
46+
gulp.parallel(scripts, images)
47+
));
48+
gulp.task(clean);
49+
gulp.task(watch);
50+
51+
// The default task (called when you run `gulp` from cli)
52+
gulp.task('default', gulp.series('build'));
53+
54+
55+
/* Define our tasks using plain functions */
56+
4357
// Not all tasks need to use streams
4458
// A gulpfile is just another node program and you can use all packages available on npm
45-
gulp.task('clean', function() {
59+
function clean() {
4660
// You can use multiple globbing patterns as you would with `gulp.src`
4761
return del(['build']);
48-
});
62+
}
4963

50-
gulp.task('scripts', function() {
51-
// Minify and copy all JavaScript (except vendor scripts)
52-
// with sourcemaps all the way down
64+
// Copy all static images
65+
function images() {
66+
return gulp.src(paths.images)
67+
// Pass in options to the task
68+
.pipe(imagemin({optimizationLevel: 5}))
69+
.pipe(gulp.dest('build/img'));
70+
}
71+
72+
// Minify and copy all JavaScript (except vendor scripts)
73+
// with sourcemaps all the way down
74+
function scripts() {
5375
return gulp.src(paths.scripts)
5476
.pipe(sourcemaps.init())
5577
.pipe(coffee())
5678
.pipe(uglify())
5779
.pipe(concat('all.min.js'))
5880
.pipe(sourcemaps.write())
5981
.pipe(gulp.dest('build/js'));
60-
});
61-
62-
// Copy all static images
63-
gulp.task('images', function() {
64-
return gulp.src(paths.images)
65-
// Pass in options to the task
66-
.pipe(imagemin({optimizationLevel: 5}))
67-
.pipe(gulp.dest('build/img'));
68-
});
82+
}
6983

7084
// Rerun the task when a file changes
71-
gulp.task('watch', function() {
72-
gulp.watch(paths.scripts, 'scripts');
73-
gulp.watch(paths.images, 'images');
74-
});
75-
76-
gulp.task('all', gulp.parallel('watch', 'scripts', 'images'));
77-
// The default task (called when you run `gulp` from cli)
78-
gulp.task('default', gulp.series('clean', 'all'));
85+
function watch() {
86+
gulp.watch(paths.scripts, scripts);
87+
gulp.watch(paths.images, images);
88+
}
7989
```
8090

8191
## Incremental Builds
8292

8393
You can filter out unchanged files between runs of a task using
8494
the `gulp.src` function's `since` option and `gulp.lastRun`:
8595
```js
86-
gulp.task('images', function() {
96+
function images() {
8797
return gulp.src(paths.images, {since: gulp.lastRun('images')})
8898
.pipe(imagemin({optimizationLevel: 5}))
8999
.pipe(gulp.dest('build/img'));
90-
});
100+
}
91101

92-
gulp.task('watch', function() {
93-
gulp.watch(paths.images, 'images');
94-
});
102+
function watch() {
103+
gulp.watch(paths.images, images);
104+
}
95105
```
96106
Task run times are saved in memory and are lost when gulp exits. It will only
97107
save time during the `watch` task when running the `images` task
@@ -103,13 +113,13 @@ If you want to compare modification time between files instead, we recommend the
103113

104114
[gulp-newer] example:
105115
```js
106-
gulp.task('images', function() {
116+
function images() {
107117
var dest = 'build/img';
108118
return gulp.src(paths.images)
109119
.pipe(newer(dest)) // pass through newer images only
110120
.pipe(imagemin({optimizationLevel: 5}))
111121
.pipe(gulp.dest(dest));
112-
});
122+
}
113123
```
114124

115125
If you can't simply filter out unchanged files, but need them in a later phase
@@ -119,7 +129,7 @@ of the stream, we recommend these plugins:
119129

120130
[gulp-remember] example:
121131
```js
122-
gulp.task('scripts', function () {
132+
function scripts() {
123133
return gulp.src(scriptsGlob)
124134
.pipe(cache('scripts')) // only pass through changed files
125135
.pipe(header('(function () {')) // do special things to the changed files...
@@ -128,7 +138,7 @@ gulp.task('scripts', function () {
128138
.pipe(remember('scripts')) // add back all files to the stream
129139
.pipe(concat('app.js')) // do things that require all files
130140
.pipe(gulp.dest('public/'))
131-
});
141+
}
132142
```
133143

134144
## Want to test the latest and greatest?

docs/API.md

+18-17
Original file line numberDiff line numberDiff line change
@@ -207,24 +207,35 @@ Octal permission specifying the mode the directory should be created with: e.g.
207207

208208
### gulp.task([name,] fn)
209209

210-
Define a task using [Undertaker].
210+
Define a task exposed to gulp-cli, `gulp.series`, `gulp.parallel` and
211+
`gulp.lastRun`; inherited from [undertaker].
211212

212213
```js
213214
gulp.task('somename', function() {
214215
// Do stuff
215216
});
216217
```
217218

219+
Or get a task that has been registered.
220+
221+
```js
222+
// somenameTask will be the registered task function
223+
var somenameTask = gulp.task('somename');
224+
```
225+
218226
#### name
219227
Type: `String`
220228

221-
Optional, The name of the task. Tasks that you want to run from the command line
222-
should not have spaces in them.
223-
224229
If the name is not provided, the task will be named after the function
225-
`name` attribute, set on any named function.
230+
`name` or `displayName` property. The name argument is required if the
231+
`name` and `displayName` properties of `fn` are empty.
232+
233+
Since the task can be run from the command line, you should avoid using
234+
spaces in task names.
226235

227-
[Function.name] is not writable; it cannot be set or edited.
236+
**Note:** [Function.name] is not writable; it cannot be set or edited. If
237+
you need to assign a function name or use characters that aren't allowed
238+
in function names, use the `displayName` property.
228239
It will be empty for anonymous functions:
229240

230241
```js
@@ -238,16 +249,6 @@ bar.name = 'bar'
238249
bar.name === '' // true
239250
```
240251

241-
You should either provide the task name or avoid anonymous functions.
242-
243-
You can also omit the function if you only want to run a bundle of dependency tasks:
244-
245-
```js
246-
gulp.task('build', ['array', 'of', 'task', 'names']);
247-
```
248-
249-
**Note:** The tasks will run in parallel (all at once), so don't assume that the tasks will start/finish in order.
250-
251252
#### fn
252253
Type: `Function`
253254

@@ -511,7 +512,7 @@ The path to the file that triggered the event.
511512
[RxJS]: https://www.npmjs.com/package/rx
512513
[stream]: http://nodejs.org/api/stream.html
513514
[async-done]: https://www.npmjs.com/package/async-done
514-
[Undertaker]: https://github.com/phated/undertaker
515+
[undertaker]: https://github.com/gulpjs/undertaker
515516
[vinyl File instance]: https://github.com/gulpjs/vinyl
516517
[Vinyl files]: https://github.com/gulpjs/vinyl-fs
517518
[fs stats]: https://nodejs.org/api/fs.html#fs_class_fs_stats

0 commit comments

Comments
 (0)