Add getCacheKey() to the open source transformer

Reviewed By: davidaurelio

Differential Revision: D4965737

fbshipit-source-id: a343d0cd2e8832567e4e2eed1e2ac0b1657d91a0
This commit is contained in:
Karol Kuczmarski 2017-04-28 16:44:59 -07:00 committed by Facebook Github Bot
parent fddb3b0737
commit 8fe24da6c2
4 changed files with 23 additions and 10 deletions

View File

@ -193,8 +193,8 @@ class Bundler {
/* $FlowFixMe: in practice it's always here. */ /* $FlowFixMe: in practice it's always here. */
this._transformer = new Transformer(opts.transformModulePath, maxWorkerCount); this._transformer = new Transformer(opts.transformModulePath, maxWorkerCount);
const getTransformCacheKey = (src, filename, options) => { const getTransformCacheKey = (options) => {
return transformCacheKey + getCacheKey(src, filename, options); return transformCacheKey + getCacheKey(options);
}; };
this._resolverPromise = Resolver.load({ this._resolverPromise = Resolver.load({

View File

@ -229,7 +229,7 @@ class URIBasedGlobalTransformCache {
const hash = crypto.createHash('sha1'); const hash = crypto.createHash('sha1');
const {sourceCode, filePath, transformOptions} = props; const {sourceCode, filePath, transformOptions} = props;
hash.update(this._optionsHasher.getTransformWorkerOptionsDigest(transformOptions)); hash.update(this._optionsHasher.getTransformWorkerOptionsDigest(transformOptions));
const cacheKey = props.getTransformCacheKey(sourceCode, filePath, transformOptions); const cacheKey = props.getTransformCacheKey(transformOptions);
hash.update(JSON.stringify(cacheKey)); hash.update(JSON.stringify(cacheKey));
hash.update(crypto.createHash('sha1').update(sourceCode).digest('hex')); hash.update(crypto.createHash('sha1').update(sourceCode).digest('hex'));
const digest = hash.digest('hex'); const digest = hash.digest('hex');

View File

@ -25,7 +25,7 @@ import type {MappingsMap} from './SourceMap';
import type {Reporter} from './reporting'; import type {Reporter} from './reporting';
type CacheFilePaths = {transformedCode: string, metadata: string}; type CacheFilePaths = {transformedCode: string, metadata: string};
export type GetTransformCacheKey = (sourceCode: string, filename: string, options: {}) => string; export type GetTransformCacheKey = (options: {}) => string;
const CACHE_NAME = 'react-native-packager-cache'; const CACHE_NAME = 'react-native-packager-cache';
const CACHE_SUB_DIR = 'cache'; const CACHE_SUB_DIR = 'cache';
@ -62,11 +62,7 @@ function hashSourceCode(props: {
transformOptionsKey: string, transformOptionsKey: string,
}): string { }): string {
return crypto.createHash('sha1') return crypto.createHash('sha1')
.update(props.getTransformCacheKey( .update(props.getTransformCacheKey(props.transformOptions))
props.sourceCode,
props.filePath,
props.transformOptions,
))
.update(props.sourceCode) .update(props.sourceCode)
.digest('hex'); .digest('hex');
} }

View File

@ -11,6 +11,7 @@
'use strict'; 'use strict';
const babel = require('babel-core'); const babel = require('babel-core');
const crypto = require('crypto');
const externalHelpersPlugin = require('babel-plugin-external-helpers'); const externalHelpersPlugin = require('babel-plugin-external-helpers');
const fs = require('fs'); const fs = require('fs');
const generate = require('babel-generator').default; const generate = require('babel-generator').default;
@ -22,6 +23,13 @@ const resolvePlugins = require('babel-preset-react-native/lib/resolvePlugins');
const {compactMapping} = require('./src/Bundler/source-map'); const {compactMapping} = require('./src/Bundler/source-map');
const cacheKeyParts = [
fs.readFileSync(__filename),
require('babel-plugin-external-helpers/package.json').version,
require('babel-preset-fbjs/package.json').version,
require('babel-preset-react-native/package.json').version,
];
/** /**
* Return a memoized function that checks for the existence of a * Return a memoized function that checks for the existence of a
* project level .babelrc file, and if it doesn't exist, reads the * project level .babelrc file, and if it doesn't exist, reads the
@ -126,4 +134,13 @@ function transform(src, filename, options) {
} }
} }
module.exports.transform = transform; function getCacheKey(options) {
var key = crypto.createHash('md5');
cacheKeyParts.forEach(part => key.update(part));
return key.digest('hex');
}
module.exports = {
transform,
getCacheKey,
};