From 0e583a981ff65e0b7ad2e1b226a812e9dd8ba094 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Tue, 5 Dec 2017 10:38:05 -0800 Subject: [PATCH] Get rid of outdated_dependencies Reviewed By: jeanlauliac Differential Revision: D6485979 fbshipit-source-id: 8cf21a8c531d96df93f66e1cc465fe7ae044ef8e --- packages/metro/src/lib/TransformCaching.js | 29 +++++---------- .../src/lib/__mocks__/TransformCaching.js | 5 +-- .../lib/__tests__/TransformCaching-test.js | 7 ++-- packages/metro/src/node-haste/Module.js | 35 ++++++------------- 4 files changed, 24 insertions(+), 52 deletions(-) diff --git a/packages/metro/src/lib/TransformCaching.js b/packages/metro/src/lib/TransformCaching.js index 9edac8f7..c42f8fd3 100644 --- a/packages/metro/src/lib/TransformCaching.js +++ b/packages/metro/src/lib/TransformCaching.js @@ -37,10 +37,7 @@ export type CachedResult = { map: CompactRawMappings, }; -export type TransformCacheResult = {| - +result: ?CachedResult, - +outdatedDependencies: $ReadOnlyArray, -|}; +export type TransformCacheResult = ?CachedResult; export type CacheOptions = { reporter: Reporter, @@ -74,8 +71,6 @@ export type TransformCache = { readSync(props: ReadTransformProps): TransformCacheResult, }; -const EMPTY_ARRAY = []; - /* 1 day */ const GARBAGE_COLLECTION_PERIOD = 24 * 60 * 60 * 1000; /* 4 days */ @@ -181,7 +176,7 @@ class FileBasedCache { return this._readFilesSync(props); } catch (error) { if (error.code === 'ENOENT') { - return {result: null, outdatedDependencies: EMPTY_ARRAY}; + return null; } throw error; } @@ -191,11 +186,11 @@ class FileBasedCache { const cacheFilePaths = this._getCacheFilePaths(props); const metadata = readMetadataFileSync(cacheFilePaths.metadata); if (metadata == null) { - return {result: null, outdatedDependencies: EMPTY_ARRAY}; + return null; } const sourceHash = hashSourceCode(props); if (sourceHash !== metadata.cachedSourceHash) { - return {result: null, outdatedDependencies: metadata.dependencies}; + return null; } const transformedCode = fs.readFileSync( cacheFilePaths.transformedCode, @@ -206,15 +201,12 @@ class FileBasedCache { .update(transformedCode) .digest('hex'); if (metadata.cachedResultHash !== codeHash) { - return {result: null, outdatedDependencies: metadata.dependencies}; + return null; } return { - result: { - code: transformedCode, - dependencies: metadata.dependencies, - map: metadata.sourceMap, - }, - outdatedDependencies: EMPTY_ARRAY, + code: transformedCode, + dependencies: metadata.dependencies, + map: metadata.sourceMap, }; } @@ -411,10 +403,7 @@ function unlinkIfExistsSync(filePath: string) { function none(): TransformCache { return { writeSync: () => {}, - readSync: () => ({ - result: null, - outdatedDependencies: [], - }), + readSync: () => null, }; } diff --git a/packages/metro/src/lib/__mocks__/TransformCaching.js b/packages/metro/src/lib/__mocks__/TransformCaching.js index 2de9a117..d6038f03 100644 --- a/packages/metro/src/lib/__mocks__/TransformCaching.js +++ b/packages/metro/src/lib/__mocks__/TransformCaching.js @@ -49,10 +49,7 @@ class TransformCacheMock { } readSync(props) { - return { - result: transformCache.get(transformCacheKeyOf(props)), - outdatedDependencies: [], - }; + return transformCache.get(transformCacheKeyOf(props)); } } diff --git a/packages/metro/src/lib/__tests__/TransformCaching-test.js b/packages/metro/src/lib/__tests__/TransformCaching-test.js index 27857540..01c94bf6 100644 --- a/packages/metro/src/lib/__tests__/TransformCaching-test.js +++ b/packages/metro/src/lib/__tests__/TransformCaching-test.js @@ -87,7 +87,7 @@ describe('TransformCaching.FileBasedCache', () => { ...args, cacheOptions: {reporter: {}, resetCache: false}, }); - expect(cachedResult.result).toEqual(result); + expect(cachedResult).toEqual(result); }); }); @@ -119,7 +119,7 @@ describe('TransformCaching.FileBasedCache', () => { ...args, cacheOptions: {reporter: {}, resetCache: false}, }); - expect(cachedResult.result).toEqual(result); + expect(cachedResult).toEqual(result); }); allCases.pop(); allCases.forEach(entry => { @@ -127,8 +127,7 @@ describe('TransformCaching.FileBasedCache', () => { ...argsFor(entry), cacheOptions: {reporter: {}, resetCache: false}, }); - expect(cachedResult.result).toBeNull(); - expect(cachedResult.outdatedDependencies).toEqual(['foo', 'bar']); + expect(cachedResult).toBeNull(); }); }); }); diff --git a/packages/metro/src/node-haste/Module.js b/packages/metro/src/node-haste/Module.js index 5a3df290..81321b79 100644 --- a/packages/metro/src/node-haste/Module.js +++ b/packages/metro/src/node-haste/Module.js @@ -19,7 +19,7 @@ const invariant = require('fbjs/lib/invariant'); const isAbsolutePath = require('absolute-path'); const jsonStableStringify = require('json-stable-stringify'); -const {join: joinPath, relative: relativePath, extname} = require('path'); +const {join: joinPath, relative: relativePath} = require('path'); import type { TransformedCode, @@ -44,10 +44,7 @@ export type ReadResult = { +source: string, }; -export type CachedReadResult = {| - +result: ?ReadResult, - +outdatedDependencies: $ReadOnlyArray, -|}; +export type CachedReadResult = ?ReadResult; export type TransformCode = ( module: Module, @@ -248,7 +245,7 @@ class Module { * To what we read from the cache or worker, we need to add id and source. */ _finalizeReadResult(source: string, result: TransformedCode): ReadResult { - return {...result, id: this._getHasteName(), source}; + return {...result, source}; } async _transformCodeFor( @@ -298,8 +295,9 @@ class Module { read(transformOptions: WorkerOptions): Promise { return Promise.resolve().then(() => { const cached = this.readCached(transformOptions); - if (cached.result != null) { - return cached.result; + + if (cached != null) { + return cached; } return this.readFresh(transformOptions); }); @@ -334,19 +332,11 @@ class Module { transformOptionsKey, ); const cachedResult = this._options.transformCache.readSync(cacheProps); - if (cachedResult.result == null) { - return { - result: null, - outdatedDependencies: cachedResult.outdatedDependencies, - }; + + if (cachedResult == null) { + return null; } - return { - result: this._finalizeReadResult( - cacheProps.sourceCode, - cachedResult.result, - ), - outdatedDependencies: [], - }; + return this._finalizeReadResult(cacheProps.sourceCode, cachedResult); } /** @@ -368,10 +358,7 @@ class Module { cacheProps.sourceCode, freshResult, ); - this._readResultsByOptionsKey.set(key, { - result: finalResult, - outdatedDependencies: [], - }); + this._readResultsByOptionsKey.set(key, finalResult); return finalResult; })(); this._readPromises.set(key, freshPromise);