@@ -40,58 +40,68 @@ var paths = {
40
40
images: ' client/img/**/*'
41
41
};
42
42
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
+
43
57
// Not all tasks need to use streams
44
58
// A gulpfile is just another node program and you can use all packages available on npm
45
- gulp . task ( ' clean ' , function () {
59
+ function clean () {
46
60
// You can use multiple globbing patterns as you would with `gulp.src`
47
61
return del ([' build' ]);
48
- });
62
+ }
49
63
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 () {
53
75
return gulp .src (paths .scripts )
54
76
.pipe (sourcemaps .init ())
55
77
.pipe (coffee ())
56
78
.pipe (uglify ())
57
79
.pipe (concat (' all.min.js' ))
58
80
.pipe (sourcemaps .write ())
59
81
.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
+ }
69
83
70
84
// 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
+ }
79
89
```
80
90
81
91
## Incremental Builds
82
92
83
93
You can filter out unchanged files between runs of a task using
84
94
the ` gulp.src ` function's ` since ` option and ` gulp.lastRun ` :
85
95
``` js
86
- gulp . task ( ' images ' , function () {
96
+ function images () {
87
97
return gulp .src (paths .images , {since: gulp .lastRun (' images' )})
88
98
.pipe (imagemin ({optimizationLevel: 5 }))
89
99
.pipe (gulp .dest (' build/img' ));
90
- });
100
+ }
91
101
92
- gulp . task ( ' watch ' , function () {
93
- gulp .watch (paths .images , ' images' );
94
- });
102
+ function watch () {
103
+ gulp .watch (paths .images , images);
104
+ }
95
105
```
96
106
Task run times are saved in memory and are lost when gulp exits. It will only
97
107
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
103
113
104
114
[ gulp-newer] example:
105
115
``` js
106
- gulp . task ( ' images ' , function () {
116
+ function images () {
107
117
var dest = ' build/img' ;
108
118
return gulp .src (paths .images )
109
119
.pipe (newer (dest)) // pass through newer images only
110
120
.pipe (imagemin ({optimizationLevel: 5 }))
111
121
.pipe (gulp .dest (dest));
112
- });
122
+ }
113
123
```
114
124
115
125
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:
119
129
120
130
[ gulp-remember] example:
121
131
``` js
122
- gulp . task ( ' scripts ' , function () {
132
+ function scripts () {
123
133
return gulp .src (scriptsGlob)
124
134
.pipe (cache (' scripts' )) // only pass through changed files
125
135
.pipe (header (' (function () {' )) // do special things to the changed files...
@@ -128,7 +138,7 @@ gulp.task('scripts', function () {
128
138
.pipe (remember (' scripts' )) // add back all files to the stream
129
139
.pipe (concat (' app.js' )) // do things that require all files
130
140
.pipe (gulp .dest (' public/' ))
131
- });
141
+ }
132
142
```
133
143
134
144
## Want to test the latest and greatest?
0 commit comments