From d12d0b4c5347c2ba27787b19d3fdf8811ea57ebf Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Thu, 12 Jan 2017 10:30:14 -0800 Subject: [PATCH] packager: do not try to fetch uncached files Reviewed By: davidaurelio Differential Revision: D4403623 fbshipit-source-id: 8319e6bac365fe5fcebefd4f338faf145f662ecb --- .../src/JSTransformer/worker/worker.js | 2 ++ .../src/lib/GlobalTransformCache.js | 36 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/packager/react-packager/src/JSTransformer/worker/worker.js b/packager/react-packager/src/JSTransformer/worker/worker.js index 9b443b845..f4aa2ff32 100644 --- a/packager/react-packager/src/JSTransformer/worker/worker.js +++ b/packager/react-packager/src/JSTransformer/worker/worker.js @@ -56,6 +56,8 @@ export type TransformOptions = { export type Options = { transform: TransformOptions, platform: string, + +dev: boolean, + +minify: boolean, }; export type Data = { diff --git a/packager/react-packager/src/lib/GlobalTransformCache.js b/packager/react-packager/src/lib/GlobalTransformCache.js index 77565905c..b327dbfcc 100644 --- a/packager/react-packager/src/lib/GlobalTransformCache.js +++ b/packager/react-packager/src/lib/GlobalTransformCache.js @@ -234,6 +234,29 @@ function globalizeTransformOptions( }; } +export type TransformProfile = {+dev: boolean, +minify: boolean, +platform: string}; + +function profileKey({dev, minify, platform}: TransformProfile): string { + return jsonStableStringify({dev, minify, platform}); +} + +/** + * We avoid doing any request to the server if we know the server is not + * going to have any key at all for a particular set of transform options. + */ +class TransformProfileSet { + _profileKeys: Set; + constructor(profiles: Iterable) { + this._profileKeys = new Set(); + for (const profile of profiles) { + this._profileKeys.add(profileKey(profile)); + } + } + has(profile: TransformProfile): boolean { + return this._profileKeys.has(profileKey(profile)); + } +} + /** * One can enable the global cache by calling configure() from a custom CLI * script. Eventually we may make it more flexible. @@ -242,12 +265,15 @@ class GlobalTransformCache { _fetcher: KeyURIFetcher; _store: ?KeyResultStore; + _profileSet: TransformProfileSet; static _global: ?GlobalTransformCache; constructor( fetchResultURIs: FetchResultURIs, - storeResults?: StoreResults, + storeResults: ?StoreResults, + profiles: Iterable, ) { + this._profileSet = new TransformProfileSet(profiles); this._fetcher = new KeyURIFetcher(fetchResultURIs); if (storeResults != null) { this._store = new KeyResultStore(storeResults); @@ -294,6 +320,10 @@ class GlobalTransformCache { } fetch(props: FetchProps, callback: FetchCallback) { + if (!this._profileSet.has(props.transformOptions)) { + process.nextTick(callback); + return; + } this._fetcher.fetch(GlobalTransformCache.keyOf(props), (error, uri) => { if (error != null) { callback(error); @@ -323,11 +353,13 @@ class GlobalTransformCache { */ static configure( fetchResultURIs: FetchResultURIs, - storeResults?: StoreResults, + storeResults: ?StoreResults, + profiles: Iterable, ) { GlobalTransformCache._global = new GlobalTransformCache( fetchResultURIs, storeResults, + profiles, ); }