JSPM

  • Created
  • Published
  • Downloads 113990
  • Score
    100M100P100Q162038F
  • License MIT

Watch that you expect from gulp.watch

Package Exports

  • gulp-watch

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (gulp-watch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

gulp-watch NPM version Build Status Dependency Status

Watch, that gulp deserves

One picture is worth a thousand words:

Awesome demonstration

Usage

Continious stream of events

This is usefull, when you want blazingly fast rebuilding per-file.

// npm i gulp gulp-watch gulp-sass

var gulp = require('gulp'),
    watch = require('gulp-watch'),
    plumber = require('gulp-plumber'),
    sass = require('gulp-sass');

gulp.task('default', function () {
    gulp.src('scss/**', { read: false })
        .pipe(watch())
        .pipe(plumber()) // I recommend to pipe gulp-plumber after watch, check it's repo for explanation
        .pipe(sass())
        .pipe(gulp.dest('./dist/'));
});

If you want to watch all directories, include those, which will be created after.

gulp.task('default', function () {
    watch('sass/**/*.scss')
        .pipe(plumber())
        .pipe(sass())
        .pipe(gulp.dest('./dist/'));
});

Trigger for mocha

Problem with gulp.watch is that will run your test suit on every changed file per once. To avoid this gulp-batch was written first, but after some time it became clear, that gulp.watch should be a plugin with event batching abilities.

gulp.task('watch', function() {
    gulp.src(['lib/**', 'test/**'], { read: false })
        .pipe(watch(function(events, cb) {
             gulp.src(['test/*.js'])
                .pipe(mocha({ reporter: 'spec' }))
                .on('error', cb.bind(null, null))
                .on('end', cb);
        });
});

gulp.task('default', function () {
    gulp.run('mocha');
    gulp.run('watch');
});

// run `gulp watch` or just `gulp` for watching and rerunning tests

API

watch([options, callback])

This function creates have two different modes, that are depends on have you provice callback function, or not. If you do - you get batched mode, if you not - you get stream.

Callback signature: function(events, [done])

  • events - is Array of incoming events.
  • done - is callback for your function signal to batch, that you are done. This allows to run your callback as soon as previous end.

Options:

This object passed to gaze options directly, so see documentation there. For batched mode we are using gulp-batch, so options from there are available. And of course options for gulp.src used too. If you do not want content from watch, then add read: false to options object.

options.passThrough

Type: Boolean
Default: true

This options will pass vinyl objects, that was piped into watch to next Stream in pipeline.

options.name

Type: String
Default: undefined

Name of the watcher. If it present in options, you will get more readable output:

Naming watchers

options.glob

Type: String
Default: undefined

Glob, that will be passed to gaze.

Notes:

  1. you cannot pipe to watcher, that got this option (writable stream will be closed).
  2. you will receive vinyl File object only on changes.

Methods

Returned Stream from constructor have some useful methods:

  • close() - calling gaze.close and emitting end, after gaze.close is done.

Events

  • end - all files are stop being watched.
  • ready - all files, that are passed from gulp.src, are now being watched.
  • error - when something happened inside callback, you will get notified.

Returns

  • Batched mode - wrapped callback, that will gather events and call callback.
  • Stream mode - stream, that handles gulp.src piping.

License

(MIT License)

Copyright (c) 2013 Vsevolod Strukchinsky (floatdrop@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.