packager: do not try to fetch uncached files

Reviewed By: davidaurelio

Differential Revision: D4403623

fbshipit-source-id: 8319e6bac365fe5fcebefd4f338faf145f662ecb
This commit is contained in:
Jean Lauliac 2017-01-12 10:30:14 -08:00 committed by Facebook Github Bot
parent b9307f21eb
commit 48a7ede4d2
2 changed files with 36 additions and 2 deletions

View File

@ -56,6 +56,8 @@ export type TransformOptions = {
export type Options = { export type Options = {
transform: TransformOptions, transform: TransformOptions,
platform: string, platform: string,
+dev: boolean,
+minify: boolean,
}; };
export type Data = { export type Data = {

View File

@ -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<string>;
constructor(profiles: Iterable<TransformProfile>) {
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 * One can enable the global cache by calling configure() from a custom CLI
* script. Eventually we may make it more flexible. * script. Eventually we may make it more flexible.
@ -242,12 +265,15 @@ class GlobalTransformCache {
_fetcher: KeyURIFetcher; _fetcher: KeyURIFetcher;
_store: ?KeyResultStore; _store: ?KeyResultStore;
_profileSet: TransformProfileSet;
static _global: ?GlobalTransformCache; static _global: ?GlobalTransformCache;
constructor( constructor(
fetchResultURIs: FetchResultURIs, fetchResultURIs: FetchResultURIs,
storeResults?: StoreResults, storeResults: ?StoreResults,
profiles: Iterable<TransformProfile>,
) { ) {
this._profileSet = new TransformProfileSet(profiles);
this._fetcher = new KeyURIFetcher(fetchResultURIs); this._fetcher = new KeyURIFetcher(fetchResultURIs);
if (storeResults != null) { if (storeResults != null) {
this._store = new KeyResultStore(storeResults); this._store = new KeyResultStore(storeResults);
@ -294,6 +320,10 @@ class GlobalTransformCache {
} }
fetch(props: FetchProps, callback: FetchCallback) { fetch(props: FetchProps, callback: FetchCallback) {
if (!this._profileSet.has(props.transformOptions)) {
process.nextTick(callback);
return;
}
this._fetcher.fetch(GlobalTransformCache.keyOf(props), (error, uri) => { this._fetcher.fetch(GlobalTransformCache.keyOf(props), (error, uri) => {
if (error != null) { if (error != null) {
callback(error); callback(error);
@ -323,11 +353,13 @@ class GlobalTransformCache {
*/ */
static configure( static configure(
fetchResultURIs: FetchResultURIs, fetchResultURIs: FetchResultURIs,
storeResults?: StoreResults, storeResults: ?StoreResults,
profiles: Iterable<TransformProfile>,
) { ) {
GlobalTransformCache._global = new GlobalTransformCache( GlobalTransformCache._global = new GlobalTransformCache(
fetchResultURIs, fetchResultURIs,
storeResults, storeResults,
profiles,
); );
} }