Skip to content

Commit

Permalink
Replaced gulp.watch with latest chokidar which is more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparsz committed Jan 27, 2021
1 parent 7eec422 commit d93fcaa
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 59 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.3.0] - 2021-01-27
### Changed
- Replaced gulp.watch with latest chokidar which is more reliable

## [1.2.8] - 2020-11-12
### Changed
- Updated dependencies
Expand Down
42 changes: 42 additions & 0 deletions lib/gulp/task-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const gulp = require('gulp');
const chokidar = require('chokidar');
const debounce = require('just-debounce');
const asyncDone = require('async-done');


/**
* Gulp task watch function using latest chokidar
* with better OSX support and file watching functionality
* than outdated gulp.watch
*/
module.exports = function taskWatch (globs, callback) {
const parallelCallback = gulp.parallel(callback);
const debouncedCallback = debounce(onChange, 200);
let running = false;
let queued = false;

function onChange () {
if (running) {
queued = true;
} else {
running = true;
asyncDone(parallelCallback, runComplete);
}
}
function runComplete (err) {
running = false;

// If we have a run queued, start onChange again
if (queued) {
queued = false;
onChange();
}
}

return chokidar.watch(globs, {
ignoreInitial: true
})
.on('add', debouncedCallback)
.on('change', debouncedCallback)
.on('unlink', debouncedCallback);
};
235 changes: 184 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@videinfra/static-website-builder",
"version": "1.2.8",
"version": "1.3.0",
"description": "Customizable static site project builder",
"license": "MIT",
"engines": {
Expand Down Expand Up @@ -29,6 +29,7 @@
"babel-loader": "^8.2.1",
"browser-sync": "^2.26.13",
"chalk": "4.0.0",
"chokidar": "^3.5.1",
"cross-env": "^7.0.2",
"cssnano": "^4.1.10",
"del": "^5.1.0",
Expand Down
4 changes: 3 additions & 1 deletion plugins/example/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const getPaths = require('@videinfra/example-website-builder/lib/get-path');

const taskStart = require('@videinfra/example-website-builder/lib/gulp/task-start');
const taskEnd = require('@videinfra/example-website-builder/lib/gulp/task-end');
const taskWatch = require('../../lib/gulp/task-watch');


// Paths and files which gulp will watch and run on
Expand All @@ -32,14 +33,15 @@ function example () {
// Do something....

// Output into destination folder for 'example'
.pipe(taskBeforeDest())
.pipe(gulp.dest(getPaths.getDestPath('example')))

// End task, handles reloading on file change
.pipe(taskEnd());
}

function exampleWatch () {
return gulp.watch(getGlobPaths(), example);
return taskWatch(getGlobPaths(), example);
}


Expand Down

0 comments on commit d93fcaa

Please sign in to comment.