From 8b8f5bc40efc658c3dc8bc83de592293e245f37c Mon Sep 17 00:00:00 2001 From: Rafael de Oleza Date: Fri, 13 Jul 2018 14:28:42 +0100 Subject: [PATCH] Tweak jest-haste-map watchman initial query to make it faster (#6689) --- CHANGELOG.md | 4 ++++ .../src/crawlers/__tests__/watchman.test.js | 9 +++++++-- packages/jest-haste-map/src/crawlers/watchman.js | 16 ++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e147b7289c6..14c2f2a201b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - `[jest-cli]` Watch plugins now have access to a broader range of global configuration options in their `updateConfigAndRun` callbacks, so they can provide a wider set of extra features ([#6473](https://github.com/facebook/jest/pull/6473)) +## Fixes + +- `[jest-haste-map]` Optimize watchman crawler by using `glob` on initial query ([#6689](https://github.com/facebook/jest/pull/6689)) + ## 23.4.0 ### Features diff --git a/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js b/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js index 36c190cce31c..0db22b9abd21 100644 --- a/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js +++ b/packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js @@ -141,7 +141,12 @@ describe('watchman watch', () => { expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']); - expect(query[2].suffix).toEqual(['js', 'json']); + expect(query[2].glob).toEqual([ + 'fruits/**/*.js', + 'fruits/**/*.json', + 'vegetables/**/*.js', + 'vegetables/**/*.json', + ]); expect(data.clocks).toEqual({ [ROOT_MOCK]: 'c:fake-clock:1', @@ -412,7 +417,7 @@ describe('watchman watch', () => { expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']); - expect(query[2].suffix).toEqual(['js', 'json']); + expect(query[2].glob).toEqual(['**/*.js', '**/*.json']); expect(data.clocks).toEqual({ [ROOT_MOCK]: 'c:fake-clock:1', diff --git a/packages/jest-haste-map/src/crawlers/watchman.js b/packages/jest-haste-map/src/crawlers/watchman.js index 9da058486f78..9e6af68d9a71 100644 --- a/packages/jest-haste-map/src/crawlers/watchman.js +++ b/packages/jest-haste-map/src/crawlers/watchman.js @@ -93,18 +93,30 @@ module.exports = async function watchmanCrawl( Array.from(rootProjectDirMappings).map( async ([root, directoryFilters]) => { const expression = Array.from(defaultWatchExpression); + const glob = []; + if (directoryFilters.length > 0) { expression.push([ 'anyof', ...directoryFilters.map(dir => ['dirname', dir]), ]); + + for (const directory of directoryFilters) { + for (const extension of extensions) { + glob.push(`${directory}/**/*.${extension}`); + } + } + } else { + for (const extension of extensions) { + glob.push(`**/*.${extension}`); + } } const query = clocks[root] ? // Use the `since` generator if we have a clock available {expression, fields, since: clocks[root]} - : // Otherwise use the `suffix` generator - {expression, fields, suffix: extensions}; + : // Otherwise use the `glob` filter + {expression, fields, glob}; const response = await cmd('query', root, query);