UNPKG

7.27 kBMarkdownView Raw
1gulp-if ![status](https://secure.travis-ci.org/robrich/gulp-if.png?branch=master)
2=======
3
4A ternary gulp plugin: conditionally control the flow of vinyl objects.
5
6**Note**: Badly behaved plugins can often get worse when used with gulp-if. Typically the fix is not in gulp-if.
7
8**Note**: Works great with [lazypipe](https://github.com/OverZealous/lazypipe), see below
9
10## Usage
11
121: Conditionally filter content
13
14**Condition**
15
16![][condition]
17
18```javascript
19var gulpif = require('gulp-if');
20var uglify = require('gulp-uglify');
21
22var condition = true; // TODO: add business logic
23
24gulp.task('task', function() {
25 gulp.src('./src/*.js')
26 .pipe(gulpif(condition, uglify()))
27 .pipe(gulp.dest('./dist/'));
28});
29```
30Only uglify the content if the condition is true, but send all the files to the dist folder
31
32
332: Ternary filter
34
35**Ternary**
36
37![][ternary]
38
39```javascript
40var gulpif = require('gulp-if');
41var uglify = require('gulp-uglify');
42var beautify = require('gulp-beautify');
43
44var condition = function (file) {
45 // TODO: add business logic
46 return true;
47}
48
49gulp.task('task', function() {
50 gulp.src('./src/*.js')
51 .pipe(gulpif(condition, uglify(), beautify()))
52 .pipe(gulp.dest('./dist/'));
53});
54```
55
56If condition returns true, uglify else beautify, then send everything to the dist folder
57
58
593: Remove things from the stream
60
61**Remove from here on**
62
63![][exclude]
64
65```javascript
66var gulpIgnore = require('gulp-ignore');
67var uglify = require('gulp-uglify');
68var jshint = require('gulp-jshint');
69
70var condition = './gulpfile.js';
71
72gulp.task('task', function() {
73 gulp.src('./*.js')
74 .pipe(jshint())
75 .pipe(gulpIgnore.exclude(condition))
76 .pipe(uglify())
77 .pipe(gulp.dest('./dist/'));
78});
79```
80
81Run JSHint on everything, remove gulpfile from the stream, then uglify and write everything else.
82
83
844: Exclude things from the stream
85
86**Exclude things from entering the stream**
87
88![][glob]
89
90```javascript
91var uglify = require('gulp-uglify');
92
93gulp.task('task', function() {
94 gulp.src(['./*.js', '!./node_modules/**'])
95 .pipe(uglify())
96 .pipe(gulp.dest('./dist/'));
97});
98```
99
100Grab all JavaScript files that aren't in the node_modules folder, uglify them, and write them.
101This is fastest because nothing in node_modules ever leaves `gulp.src()`
102
103
104## works great with [lazypipe](https://github.com/OverZealous/lazypipe)
105
106Lazypipe creates a function that initializes the pipe chain on use. This allows you to create a chain of events inside the gulp-if condition. This scenario will run jshint analysis and reporter only if the linting flag is true.
107
108```js
109var gulpif = require('gulp-if');
110var jshint = require('gulp-jshint');
111var uglify = require('gulp-uglify');
112
113var linting = false;
114var compressing = false;
115
116var jshintChannel = lazypipe()
117 // adding a pipeline step
118 .pipe(jshint) // notice the stream function has not been called!
119 .pipe(jshint.reporter)
120 // adding a step with an argument
121 .pipe(jshint.reporter, 'fail');
122
123gulp.task('scripts', function () {
124 return gulp.src(paths.scripts.src)
125 .pipe(gulpif(linting, jshintChannel()))
126 .pipe(gulpif(compressing, uglify()))
127 .pipe(gulp.dest(paths.scripts.dest));
128});
129```
130[source](https://github.com/spenceralger/gulp-jshint/issues/38#issuecomment-40423932)
131
132## works great inside [lazypipe](https://github.com/OverZealous/lazypipe)
133
134Lazypipe assumes that all function parameters are static, gulp-if arguments need to be instantiated inside each lazypipe. This difference can be easily solved by passing a function on the lazypipe step
135
136```js
137var gulpif = require('gulp-if');
138var jshint = require('gulp-jshint');
139var uglify = require('gulp-uglify');
140
141var compressing = false;
142
143var jsChannel = lazypipe()
144 // adding a pipeline step
145 .pipe(jshint) // notice the stream function has not been called!
146 .pipe(jshint.reporter)
147 // adding a step with an argument
148 .pipe(jshint.reporter, 'fail')
149 // you can't say: .pipe(gulpif, compressing, uglify)
150 // because uglify needs to be instantiated separately in each lazypipe instance
151 // you can say this instead:
152 .pipe(function () {
153 return gulpif(compressing, uglify());
154 });
155 // why does this work? lazypipe calls the function, passing in the no arguments to it,
156 // it instantiates a new gulp-if pipe and returns it to lazypipe.
157
158gulp.task('scripts', function () {
159 return gulp.src(paths.scripts.src)
160 .pipe(jsChannel())
161 .pipe(gulp.dest(paths.scripts.dest));
162});
163```
164
165[source](https://github.com/robrich/gulp-if/issues/32)
166
167## gulp-if API
168
169### gulpif(condition, stream [, elseStream, [, minimatchOptions]])
170
171gulp-if will pipe data to `stream` whenever `condition` is truthy.
172
173If `condition` is falsey and `elseStream` is passed, data will pipe to `elseStream`
174
175After data is piped to `stream` or `elseStream` or neither, data is piped down-stream.
176
177#### Parameters
178
179##### condition
180
181Type: `boolean` or [`stat`](http://nodejs.org/api/fs.html#fs_class_fs_stats) object or `function` that takes in a vinyl file and returns a boolean or `RegularExpression` that works on the `file.path`
182
183The condition parameter is any of the conditions supported by [gulp-match](https://github.com/robrich/gulp-match). The `file.path` is passed into `gulp-match`.
184
185If a function is given, then the function is passed a vinyl `file`. The function should return a `boolean`.
186
187##### stream
188
189Stream for gulp-if to pipe data into when condition is truthy.
190
191##### elseStream
192
193Optional, Stream for gulp-if to pipe data into when condition is falsey.
194
195##### minimatchOptions
196
197Optional, if it's a glob condition, these options are passed to [https://github.com/isaacs/minimatch](minimatch).
198
199
200LICENSE
201-------
202
203(MIT License)
204
205Copyright (c) 2014 [Richardson & Sons, LLC](http://richardsonandsons.com/)
206
207Permission is hereby granted, free of charge, to any person obtaining
208a copy of this software and associated documentation files (the
209"Software"), to deal in the Software without restriction, including
210without limitation the rights to use, copy, modify, merge, publish,
211distribute, sublicense, and/or sell copies of the Software, and to
212permit persons to whom the Software is furnished to do so, subject to
213the following conditions:
214
215The above copyright notice and this permission notice shall be
216included in all copies or substantial portions of the Software.
217
218THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
219EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
220MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
221NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
222LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
223OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
224WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
225
226[condition]: https://rawgithub.com/robrich/gulp-if/master/img/condition.svg
227[ternary]: https://rawgithub.com/robrich/gulp-if/master/img/ternary.svg
228[exclude]: https://rawgithub.com/robrich/gulp-if/master/img/exclude.svg
229[glob]: https://rawgithub.com/robrich/gulp-if/master/img/glob.svg