From 1054f6a9ab27ecb25e9eb7909795ff13a24a3bab Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 20 May 2015 09:33:13 -0700 Subject: [PATCH] build(gulp): watcher should not trigger more than once during init Closes #2037 --- tools/build/watch.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/build/watch.js b/tools/build/watch.js index 31d536f626..1ec2516d93 100644 --- a/tools/build/watch.js +++ b/tools/build/watch.js @@ -12,8 +12,7 @@ function watch(globs, opts, tasks) { var useRunSequence = typeof tasks !== 'function'; var runTasks; - function noop() {} - var log = typeof opts.log === 'function' ? opts.log : noop; + var log = typeof opts.log === 'function' ? opts.log : function noop() {}; if (useRunSequence) { if (!Array.isArray(tasks)) tasks = [tasks]; @@ -35,7 +34,13 @@ function watch(globs, opts, tasks) { } var events = opts.events = opts.events || ['add', 'change', 'unlink']; - if (opts.ignoreInitial === undefined) opts.ignoreInitial = true; + + // Don't let chokidar call us while initializing because on large trees it might take too long for + // all the add events to be emitted which causes the initial callback to be triggered more than + // once. Instead, we call handleEvent directly. + var ignoreInitial = opts.ignoreInitial; + opts.ignoreInitial = true; + var delay = opts.delay; if (delay === undefined) delay = 100; @@ -54,6 +59,10 @@ function watch(globs, opts, tasks) { var eventsRecorded = 0; // Number of events recorded var timeoutId = null; // If non-null, event capture window is open + if (!ignoreInitial) { + handleEvent('change', 'madeupFilePath'); // synthetic event to kick off the first task run + } + return watcher; function handleEvent(event, filepath) {