mirror of https://github.com/status-im/metro.git
packager: TransformCache: do not rehash transformOptions every single time
Reviewed By: davidaurelio Differential Revision: D4809963 fbshipit-source-id: 9d3ac907e822596567ee7af4427bcd24f1bac73f
This commit is contained in:
parent
5ded44cdc1
commit
83e38fe2ca
|
@ -19,7 +19,6 @@ const fs = require('fs');
|
|||
* this particular use case.
|
||||
*/
|
||||
const imurmurhash = require('imurmurhash');
|
||||
const jsonStableStringify = require('json-stable-stringify');
|
||||
const mkdirp = require('mkdirp');
|
||||
const path = require('path');
|
||||
const rimraf = require('rimraf');
|
||||
|
@ -64,6 +63,7 @@ function hashSourceCode(props: {
|
|||
sourceCode: string,
|
||||
getTransformCacheKey: GetTransformCacheKey,
|
||||
transformOptions: TransformOptions,
|
||||
transformOptionsKey: string,
|
||||
}): string {
|
||||
return imurmurhash(props.getTransformCacheKey(
|
||||
props.sourceCode,
|
||||
|
@ -79,11 +79,11 @@ function hashSourceCode(props: {
|
|||
*/
|
||||
function getCacheFilePaths(props: {
|
||||
filePath: string,
|
||||
transformOptions: TransformOptions,
|
||||
transformOptionsKey: string,
|
||||
}): CacheFilePaths {
|
||||
const hasher = imurmurhash()
|
||||
.hash(props.filePath)
|
||||
.hash(jsonStableStringify(props.transformOptions) || '');
|
||||
.hash(props.transformOptionsKey);
|
||||
const hash = toFixedHex(8, hasher.result());
|
||||
const prefix = hash.substr(0, 2);
|
||||
const fileName = `${hash.substr(2)}${path.basename(props.filePath)}`;
|
||||
|
@ -135,6 +135,7 @@ function writeSync(props: {
|
|||
sourceCode: string,
|
||||
getTransformCacheKey: GetTransformCacheKey,
|
||||
transformOptions: TransformOptions,
|
||||
transformOptionsKey: string,
|
||||
result: CachedResult,
|
||||
}): void {
|
||||
const cacheFilePath = getCacheFilePaths(props);
|
||||
|
@ -296,6 +297,7 @@ export type ReadTransformProps = {
|
|||
filePath: string,
|
||||
sourceCode: string,
|
||||
transformOptions: TransformOptions,
|
||||
transformOptionsKey: string,
|
||||
getTransformCacheKey: GetTransformCacheKey,
|
||||
cacheOptions: CacheOptions,
|
||||
};
|
||||
|
|
|
@ -15,9 +15,12 @@ jest
|
|||
.dontMock('../TransformCache')
|
||||
.dontMock('../toFixedHex')
|
||||
.dontMock('left-pad')
|
||||
.dontMock('lodash/throttle');
|
||||
.dontMock('lodash/throttle')
|
||||
.dontMock('crypto');
|
||||
|
||||
const imurmurhash = require('imurmurhash');
|
||||
const crypto = require('crypto');
|
||||
const jsonStableStringify = require('json-stable-stringify');
|
||||
|
||||
const memoryFS = new Map();
|
||||
|
||||
|
@ -66,6 +69,8 @@ describe('TransformCache', () => {
|
|||
getTransformCacheKey: () => 'abcdef',
|
||||
filePath,
|
||||
transformOptions,
|
||||
transformOptionsKey: crypto.createHash('md5')
|
||||
.update(jsonStableStringify(transformOptions)).digest('hex'),
|
||||
result: {
|
||||
code: `/* result for ${key} */`,
|
||||
dependencies: ['foo', `dep of ${key}`],
|
||||
|
@ -100,6 +105,7 @@ describe('TransformCache', () => {
|
|||
getTransformCacheKey: () => transformCacheKey,
|
||||
filePath: 'test.js',
|
||||
transformOptions: {foo: 1},
|
||||
transformOptionsKey: 'boo!',
|
||||
result: {
|
||||
code: `/* result for ${key} */`,
|
||||
dependencies: ['foo', `dep of ${key}`],
|
||||
|
|
|
@ -360,7 +360,7 @@ class Module {
|
|||
if (this._readResultsByOptionsKey.has(key)) {
|
||||
return this._readResultsByOptionsKey.get(key);
|
||||
}
|
||||
const result = this._readFromTransformCache(transformOptions);
|
||||
const result = this._readFromTransformCache(transformOptions, key);
|
||||
this._readResultsByOptionsKey.set(key, result);
|
||||
return result;
|
||||
}
|
||||
|
@ -369,8 +369,11 @@ class Module {
|
|||
* Read again from the TransformCache, on disk. `readCached` should be favored
|
||||
* so it's faster in case the results are already in memory.
|
||||
*/
|
||||
_readFromTransformCache(transformOptions: TransformOptions): ?ReadResult {
|
||||
const cacheProps = this._getCacheProps(transformOptions);
|
||||
_readFromTransformCache(
|
||||
transformOptions: TransformOptions,
|
||||
transformOptionsKey: string,
|
||||
): ?ReadResult {
|
||||
const cacheProps = this._getCacheProps(transformOptions, transformOptionsKey);
|
||||
const cachedResult = TransformCache.readSync(cacheProps);
|
||||
if (cachedResult) {
|
||||
return this._finalizeReadResult(cacheProps.sourceCode, cachedResult);
|
||||
|
@ -391,7 +394,7 @@ class Module {
|
|||
return promise;
|
||||
}
|
||||
const freshPromise = Promise.resolve().then(() => {
|
||||
const cacheProps = this._getCacheProps(transformOptions);
|
||||
const cacheProps = this._getCacheProps(transformOptions, key);
|
||||
return new Promise((resolve, reject) => {
|
||||
this._getAndCacheTransformedCode(
|
||||
cacheProps,
|
||||
|
@ -413,7 +416,7 @@ class Module {
|
|||
return freshPromise;
|
||||
}
|
||||
|
||||
_getCacheProps(transformOptions: TransformOptions) {
|
||||
_getCacheProps(transformOptions: TransformOptions, transformOptionsKey: string) {
|
||||
const sourceCode = this._readSourceCode();
|
||||
const moduleDocBlock = this._readDocBlock();
|
||||
const getTransformCacheKey = this._getTransformCacheKey;
|
||||
|
@ -428,6 +431,7 @@ class Module {
|
|||
sourceCode,
|
||||
getTransformCacheKey,
|
||||
transformOptions,
|
||||
transformOptionsKey,
|
||||
cacheOptions: {
|
||||
resetCache: this._options.resetCache,
|
||||
reporter: this._reporter,
|
||||
|
|
Loading…
Reference in New Issue