UNPKG

4.59 kBMarkdownView Raw
1# [gulp](https://github.com/gulpjs/gulp)-watch [![Build Status: Linux][travis-image]][travis-url] [![Build Status: Windows][appveyor-image]][appveyor-url] [![Dependency Status][depstat-image]][depstat-url]
2
3File watcher that uses super-fast [chokidar](https://github.com/paulmillr/chokidar) and emits vinyl objects.
4
5## Installation
6
7```
8npm install --save-dev gulp-watch
9```
10
11## Usage
12
13```js
14var gulp = require('gulp'),
15 watch = require('gulp-watch');
16
17gulp.task('stream', function () {
18 // Endless stream mode
19 return watch('css/**/*.css', { ignoreInitial: false })
20 .pipe(gulp.dest('build'));
21});
22
23gulp.task('callback', function () {
24 // Callback mode, useful if any plugin in the pipeline depends on the `end`/`flush` event
25 return watch('css/**/*.css', function () {
26 gulp.src('css/**/*.css')
27 .pipe(gulp.dest('build'));
28 });
29});
30```
31
32> __Protip:__ until gulpjs 4.0 is released, you can use [gulp-plumber](https://github.com/floatdrop/gulp-plumber) to prevent stops on errors.
33
34More examples can be found in [`docs/readme.md`](/docs/readme.md).
35
36## API
37
38### watch(glob, [options, callback])
39
40Creates a watcher that will spy on files that are matched by `glob` which can be a
41glob string or array of glob strings.
42
43Returns a pass through stream that will emit vinyl files
44(with additional `event` property) that corresponds to event on file-system.
45
46#### Callback `function(vinyl)`
47
48This function is called when events happen on the file-system.
49All incoming files that are piped in are grouped and passed to the `events` stream as is.
50
51 * `vinyl` — is [vinyl](https://github.com/wearefractal/vinyl) object that corresponds to the file that caused the event. Additional `event` field is added to determine what caused changes.
52
53Possible events:
54
55 * `add` - file was added to watch or created
56 * `change` - file was changed
57 * `unlink` - file was deleted
58
59#### Options
60
61This object is passed to the [`chokidar` options](https://github.com/paulmillr/chokidar#api) directly. Options for [`gulp.src`](https://github.com/gulpjs/gulp/blob/master/docs/API.md#options) are also available. If you do not want content from `watch`, then add `read: false` to the `options` object.
62
63#### options.[ignoreInitial](https://github.com/paulmillr/chokidar#path-filtering)
64Type: `Boolean`
65Default: `true`
66
67> Indicates whether chokidar should ignore the initial add events or not.
68
69#### options.events
70Type: `Array`
71Default: `['add', 'change', 'unlink']`
72
73List of events, that should be watched by gulp-watch. Contains [event names from chokidar](https://github.com/paulmillr/chokidar#events).
74
75#### options.base
76Type: `String`
77Default: `undefined`
78
79Use explicit base path for files from `glob`. Read more about `base` and `cwd` in [gulpjs docs](https://github.com/gulpjs/gulp/blob/master/docs/API.md#options).
80
81#### options.name
82Type: `String`
83Default: `undefined`
84
85Name of the watcher. If it is present in options, you will get more readable output.
86
87#### options.verbose
88Type: `Boolean`
89Default: `false`
90
91This option will enable verbose output.
92
93#### options.readDelay
94Type: `Number`
95Default: `10`
96
97Wait for `readDelay` milliseconds before reading the file.
98
99#### options.read
100Type: `Boolean`
101Default: `true`
102
103Setting this to `false` will return `file.contents` as null and not read the file at all. Most useful as an optimization if your plugins pipeline doesn't make use of the file contents (e.g. `gulp-clean`), or to avoid reading the file twice if you use `gulp.src()` inside the callback instead of the file object that is passed as argument.
104
105### Methods
106
107Returned `Stream` from constructor has some useful methods:
108
109 * `add(path / paths)`
110 * `unwatch(path / paths)`
111 * `close()`
112
113### Events
114
115All events from [chokidar](http://npmjs.com/chokidar):
116
117 * `add`, `change`, `unlink`, `addDir`, `unlinkDir`, `error`, `ready`, `raw`
118
119
120### [Changelog](https://github.com/floatdrop/gulp-watch/releases)
121
122## License
123
124MIT (c) 2014 Vsevolod Strukchinsky (floatdrop@gmail.com)
125
126[npm-url]: https://npmjs.org/package/gulp-watch
127[npm-image]: http://img.shields.io/npm/v/gulp-watch.svg?style=flat
128
129[travis-url]: https://travis-ci.org/floatdrop/gulp-watch
130[travis-image]: http://img.shields.io/travis/floatdrop/gulp-watch.svg?style=flat
131
132[appveyor-url]: https://ci.appveyor.com/project/floatdrop/gulp-watch/branch/master
133[appveyor-image]: https://ci.appveyor.com/api/projects/status/gmjwsqmxht1m131s/branch/master?svg=true
134
135[depstat-url]: https://david-dm.org/floatdrop/gulp-watch
136[depstat-image]: http://img.shields.io/david/floatdrop/gulp-watch.svg?style=flat