diff --git a/packages/metro/src/lib/GlobalTransformCache.js b/packages/metro/src/lib/GlobalTransformCache.js index 294a146e..89835510 100644 --- a/packages/metro/src/lib/GlobalTransformCache.js +++ b/packages/metro/src/lib/GlobalTransformCache.js @@ -39,6 +39,8 @@ type FetchOptions = {agent?: ?HttpAgent | HttpsAgent}; * application's top-level `Server` class. */ export type GlobalTransformCache = { + keyOf(props: FetchProps): string, + /** * Synchronously determine if it is worth trying to fetch a result from the * cache. This can be used, for instance, to exclude sets of options we know @@ -50,7 +52,7 @@ export type GlobalTransformCache = { * Try to fetch a result. It doesn't actually need to fetch from a server, * the global cache could be instantiated locally for example. */ - fetch(props: FetchProps): Promise, + fetch(key: string): Promise, /** * Try to store a result. Callsites won't necessarily wait for the success or @@ -60,7 +62,7 @@ export type GlobalTransformCache = { * no-op in production, and only do the storage operation from a script * running on a Continuous Integration platform. */ - store(props: FetchProps, result: CachedResult): Promise, + store(key: string, result: CachedResult): Promise, }; type FetchResultURIs = (keys: Array) => Promise>; @@ -361,17 +363,17 @@ class URIBasedGlobalTransformCache { * This may return `null` if either the cache doesn't have a value for that * key yet, or an error happened, processed separately. */ - async fetch(props: FetchProps): Promise { - const uri = await this._fetcher.fetch(this.keyOf(props)); + async fetch(key: string): Promise { + const uri = await this._fetcher.fetch(key); if (uri == null) { return null; } return await this._fetchResultFromURI(uri); } - async store(props: FetchProps, result: CachedResult): Promise { + async store(key: string, result: CachedResult): Promise { if (this._store != null) { - await this._store.store(this.keyOf(props), result); + await this._store.store(key, result); } } } diff --git a/packages/metro/src/lib/__tests__/GlobalTransformCache-test.js b/packages/metro/src/lib/__tests__/GlobalTransformCache-test.js index d0900574..2f2faca5 100644 --- a/packages/metro/src/lib/__tests__/GlobalTransformCache-test.js +++ b/packages/metro/src/lib/__tests__/GlobalTransformCache-test.js @@ -46,18 +46,22 @@ describe('GlobalTransformCache', () => { const transformOptions = await getTransformOptions(); const result = await Promise.all([ - cache.fetch({ - localPath: 'some/where/foo.js', - sourceCode: '/* beep */', - getTransformCacheKey: () => 'abcd', - transformOptions, - }), - cache.fetch({ - localPath: 'some/where/else/bar.js', - sourceCode: '/* boop */', - getTransformCacheKey: () => 'abcd', - transformOptions, - }), + cache.fetch( + cache.keyOf({ + localPath: 'some/where/foo.js', + sourceCode: '/* beep */', + getTransformCacheKey: () => 'abcd', + transformOptions, + }), + ), + cache.fetch( + cache.keyOf({ + localPath: 'some/where/else/bar.js', + sourceCode: '/* boop */', + getTransformCacheKey: () => 'abcd', + transformOptions, + }), + ), ]); expect(result).toMatchSnapshot(); }); diff --git a/packages/metro/src/node-haste/Module.js b/packages/metro/src/node-haste/Module.js index e2672e3f..d9ee7606 100644 --- a/packages/metro/src/node-haste/Module.js +++ b/packages/metro/src/node-haste/Module.js @@ -263,7 +263,7 @@ class Module { globalCache: GlobalTransformCache, ): Promise { const result = await this._transformCodeFor(cacheProps); - globalCache.store(cacheProps, result); + globalCache.store(globalCache.keyOf(cacheProps), result); return result; } @@ -274,7 +274,9 @@ class Module { if (globalCache == null || !globalCache.shouldFetch(cacheProps)) { return await this._transformCodeFor(cacheProps); } - const globalCachedResult = await globalCache.fetch(cacheProps); + const globalCachedResult = await globalCache.fetch( + globalCache.keyOf(cacheProps), + ); if (globalCachedResult != null) { return globalCachedResult; }