Skip to content

Commit 23cec1e

Browse files
committed
build(gulp): use chokidar alone instead of gulp-watch
Closes angular#1759
1 parent c36ea02 commit 23cec1e

2 files changed

Lines changed: 110 additions & 43 deletions

File tree

gulpfile.js

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var merge = require('merge');
1616
var merge2 = require('merge2');
1717
var path = require('path');
1818
var semver = require('semver');
19-
var watch = require('gulp-watch');
19+
var watch = require('./tools/build/watch');
2020

2121
var clean = require('./tools/build/clean');
2222
var transpile = require('./tools/build/transpile');
@@ -222,9 +222,7 @@ gulp.task('build/checkCircularDependencies', function (done) {
222222
// ------------------
223223
// web servers
224224
gulp.task('serve.js.dev', ['build.js.dev'], function(neverDone) {
225-
watch('modules/**', function() {
226-
gulp.start('!broccoli.js.dev');
227-
});
225+
watch('modules/**', '!broccoli.js.dev');
228226

229227
jsserve(gulp, gulpPlugins, {
230228
path: CONFIG.dest.js.dev.es5,
@@ -304,7 +302,7 @@ function createDocsTasks(publicBuild) {
304302

305303
gulp.task(taskPrefix, [taskPrefix + '/assets', taskPrefix + '/app', taskPrefix + '/dgeni']);
306304
gulp.task(taskPrefix + '/watch', function() {
307-
return watch('docs/app/**/*', [taskPrefix + '/app']);
305+
return watch('docs/app/**/*', { ignoreInitial: false }, [taskPrefix + '/app']);
308306
});
309307

310308
gulp.task(taskPrefix + '/test', function (done) {
@@ -372,20 +370,17 @@ gulp.task('test.unit.js', ['build.js.dev'], function (neverDone) {
372370
'check-format'
373371
);
374372

375-
watch('modules/**', function() {
376-
runSequence(
377-
'!broccoli.js.dev',
378-
'!test.unit.js/karma-run'
379-
);
380-
});
373+
watch('modules/**', [
374+
'!broccoli.js.dev',
375+
'!test.unit.js/karma-run'
376+
]);
381377
});
382378

383379
gulp.task('watch.js.dev', ['build.js.dev'], function (neverDone) {
384-
watch('modules/**', function() {
385-
runSequence(
386-
'!broccoli.js.dev'
387-
);
388-
});
380+
watch('modules/**', [
381+
'!broccoli.js.dev',
382+
'!test.unit.js/karma-run',
383+
]);
389384
});
390385

391386

@@ -404,19 +399,16 @@ gulp.task('!test.unit.js/karma-run', function(done) {
404399

405400

406401
gulp.task('test.unit.dart', ['build/tree.dart'], function (done) {
407-
408402
runSequence(
409403
'!test.unit.dart/karma-server',
410404
'!test.unit.dart/karma-run'
411405
);
412406

413-
watch('modules/angular2/**', function() {
414-
runSequence(
415-
'!build/tree.dart',
416-
'build/format.dart',
417-
'!test.unit.dart/karma-run'
418-
);
419-
});
407+
watch('modules/angular2/**', [
408+
'!build/tree.dart',
409+
'build/format.dart',
410+
'!test.unit.dart/karma-run'
411+
]);
420412
});
421413

422414
gulp.task('!test.unit.dart/karma-run', function (done) {
@@ -454,37 +446,29 @@ gulp.task('test.unit.cjs/ci', function(done) {
454446

455447

456448
gulp.task('test.unit.cjs', ['build/clean.js', 'build.tools'], function (done) {
457-
function buildAndTest() {
458-
runSequence(
459-
'!build.js.cjs',
460-
'test.unit.cjs/ci'
461-
);
462-
}
449+
var buildAndTest = [
450+
'!build.js.cjs',
451+
'test.unit.cjs/ci'
452+
];
463453

464-
buildAndTest();
465-
466-
watch('modules/**', buildAndTest);
454+
watch('modules/**', { ignoreInitial: false }, buildAndTest);
467455
});
468456

469457

470458
gulp.task('test.unit.tools/ci', function(done) {
471459
fork('./tools/traceur-jasmine', ['dist/tools/**/*.spec.js'], {
472460
stdio: 'inherit'
473-
}).on('close', done);
461+
}).on('close', done.bind(null, null));
474462
});
475463

476464

477465
gulp.task('test.unit.tools', ['build/clean.tools'], function(done) {
478-
function buildAndTest() {
479-
runSequence(
480-
'!build.tools',
481-
'test.unit.tools/ci'
482-
);
483-
}
484-
485-
buildAndTest();
466+
var buildAndTest = [
467+
'!build.tools',
468+
'test.unit.tools/ci'
469+
];
486470

487-
watch('tools/**', buildAndTest);
471+
watch(['tools/**', '!tools/**/test-fixtures/**'], {ignoreInitial: false}, buildAndTest);
488472
});
489473

490474

tools/build/watch.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
var chokidar = require('chokidar');
2+
var runSequence = require('run-sequence');
3+
var path = require('path');
4+
5+
function watch(globs, opts, tasks) {
6+
if (typeof opts !== 'object' || Array.isArray(opts)) {
7+
tasks = opts;
8+
opts = {};
9+
}
10+
11+
var useRunSequence = typeof tasks !== 'function';
12+
var runTasks;
13+
14+
if (useRunSequence) {
15+
if (!Array.isArray(tasks)) tasks = [tasks];
16+
tasks = tasks.slice();
17+
tasks.push(tasksDone);
18+
runTasks = function runTaskSequence() {
19+
runSequence.apply(null, tasks);
20+
}
21+
} else {
22+
var sync = tasks.length === 0;
23+
runTasks = function runCallback() {
24+
try {
25+
tasks(tasksDone);
26+
if (sync) tasksDone();
27+
} catch (e) {
28+
return tasksDone(e);
29+
}
30+
}
31+
}
32+
33+
var events = opts.events = opts.events || ['add', 'change', 'unlink'];
34+
if (opts.ignoreInitial === undefined) opts.ignoreInitial = true;
35+
var delay = opts.delay;
36+
if (delay === undefined) delay = 100;
37+
38+
var watcher = chokidar.watch(globs, opts).
39+
on('all', handleEvent).
40+
on('error', function(err) {
41+
throw err;
42+
});
43+
44+
var close = watcher.close.bind(watcher);
45+
watcher.close = function() {
46+
if (timeoutId !== null) clearTimeout(timeoutId);
47+
close();
48+
}
49+
50+
var eventsRecorded = 0; // Number of events recorded
51+
var timeoutId = null; // If non-null, event capture window is open
52+
53+
return watcher;
54+
55+
function handleEvent(event, filepath) {
56+
// Ignore unwatched events
57+
if (events.indexOf(event) < 0) return;
58+
59+
// Increment number of events captured in this window
60+
++eventsRecorded;
61+
62+
if (timeoutId === null) {
63+
timeoutId = setTimeout(invokeCallback, delay);
64+
}
65+
}
66+
67+
function invokeCallback() {
68+
eventsRecorded = 0;
69+
runTasks();
70+
}
71+
72+
function tasksDone(err) {
73+
if (err) throw err;
74+
if (eventsRecorded) {
75+
// eventsRecorded has increased during the run, run again on the next turn
76+
timeoutId = setTimeout(invokeCallback, 0);
77+
} else {
78+
timeoutId = null;
79+
}
80+
}
81+
}
82+
83+
module.exports = watch;

0 commit comments

Comments
 (0)