Skip to content

Commit

Permalink
Add getCacheKey() to the open source transformer
Browse files Browse the repository at this point in the history
Reviewed By: davidaurelio

Differential Revision: D4965737

fbshipit-source-id: a343d0cd2e8832567e4e2eed1e2ac0b1657d91a0
  • Loading branch information
Karol Kuczmarski authored and facebook-github-bot committed Apr 28, 2017
1 parent fddb3b0 commit 8fe24da
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packager/src/Bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ class Bundler {
/* $FlowFixMe: in practice it's always here. */
this._transformer = new Transformer(opts.transformModulePath, maxWorkerCount);

const getTransformCacheKey = (src, filename, options) => {
return transformCacheKey + getCacheKey(src, filename, options);
const getTransformCacheKey = (options) => {
return transformCacheKey + getCacheKey(options);
};

this._resolverPromise = Resolver.load({
Expand Down
2 changes: 1 addition & 1 deletion packager/src/lib/GlobalTransformCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class URIBasedGlobalTransformCache {
const hash = crypto.createHash('sha1');
const {sourceCode, filePath, transformOptions} = props;
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(crypto.createHash('sha1').update(sourceCode).digest('hex'));
const digest = hash.digest('hex');
Expand Down
8 changes: 2 additions & 6 deletions packager/src/lib/TransformCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {MappingsMap} from './SourceMap';
import type {Reporter} from './reporting';

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_SUB_DIR = 'cache';
Expand Down Expand Up @@ -62,11 +62,7 @@ function hashSourceCode(props: {
transformOptionsKey: string,
}): string {
return crypto.createHash('sha1')
.update(props.getTransformCacheKey(
props.sourceCode,
props.filePath,
props.transformOptions,
))
.update(props.getTransformCacheKey(props.transformOptions))
.update(props.sourceCode)
.digest('hex');
}
Expand Down
19 changes: 18 additions & 1 deletion packager/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

const babel = require('babel-core');
const crypto = require('crypto');
const externalHelpersPlugin = require('babel-plugin-external-helpers');
const fs = require('fs');
const generate = require('babel-generator').default;
Expand All @@ -22,6 +23,13 @@ const resolvePlugins = require('babel-preset-react-native/lib/resolvePlugins');

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
* project level .babelrc file, and if it doesn't exist, reads the
Expand Down Expand Up @@ -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,
};

0 comments on commit 8fe24da

Please sign in to comment.