Skip to content

Commit

Permalink
packager: GlobalTransformCache: globalized keyOf
Browse files Browse the repository at this point in the history
Reviewed By: davidaurelio

Differential Revision: D4243863

fbshipit-source-id: 917a8a116baf67c838c062b7b713dd4660d9f673
  • Loading branch information
Jean Lauliac authored and Facebook Github Bot committed Nov 30, 2016
1 parent 9b9fd2f commit cb254d1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packager/react-packager/src/JSTransformer/worker/worker.js
Expand Up @@ -46,7 +46,9 @@ type Transform = (
) => mixed, ) => mixed,
) => void; ) => void;


export type Options = {transform?: {}}; export type Options = {
transform?: {projectRoots: Array<string>},
};


export type Data = { export type Data = {
result: TransformedCode, result: TransformedCode,
Expand Down
43 changes: 32 additions & 11 deletions packager/react-packager/src/lib/GlobalTransformCache.js
Expand Up @@ -11,12 +11,12 @@


'use strict'; 'use strict';


const crypto = require('crypto');
const imurmurhash = require('imurmurhash'); const imurmurhash = require('imurmurhash');
const invariant = require('invariant'); const invariant = require('invariant');
const jsonStableStringify = require('json-stable-stringify'); const jsonStableStringify = require('json-stable-stringify');
const path = require('path'); const path = require('path');
const request = require('request'); const request = require('request');
const toFixedHex = require('./toFixedHex');


import type {Options as TransformOptions} from '../JSTransformer/worker/worker'; import type {Options as TransformOptions} from '../JSTransformer/worker/worker';
import type {CachedResult} from './TransformCache'; import type {CachedResult} from './TransformCache';
Expand Down Expand Up @@ -173,6 +173,30 @@ function validateCachedResult(cachedResult: mixed): ?CachedResult {
return undefined; return undefined;
} }


/**
* The transform options contain absolute paths. This can contain, for
* example, the username if someone works their home directory (very likely).
* We need to get rid of this user-and-machine-dependent data for the global
* cache, otherwise nobody would share the same cache keys.
*/
function globalizeTransformOptions(
options: TransformOptions,
): TransformOptions {
const {transform} = options;
if (transform == null) {
return options;
}
return {
...options,
transform: {
...transform,
projectRoots: transform.projectRoots.map(p => {
return path.relative(path.join(__dirname, '../../../../..'), p);
}),
},
};
}

/** /**
* One can enable the global cache by calling configure() from a custom CLI * One can enable the global cache by calling configure() from a custom CLI
* script. Eventually we may make it more flexible. * script. Eventually we may make it more flexible.
Expand All @@ -190,16 +214,13 @@ class GlobalTransformCache {
* Return a key for identifying uniquely a source file. * Return a key for identifying uniquely a source file.
*/ */
static keyOf(props: FetchProps) { static keyOf(props: FetchProps) {
const sourceDigest = toFixedHex(8, imurmurhash(props.sourceCode).result()); const stableOptions = globalizeTransformOptions(props.transformOptions);
const optionsHash = imurmurhash() const digest = crypto.createHash('sha1').update([
.hash(jsonStableStringify(props.transformOptions) || '') jsonStableStringify(stableOptions),
.hash(props.transformCacheKey) props.transformCacheKey,
.result(); imurmurhash(props.sourceCode).result().toString(),
const optionsDigest = toFixedHex(8, optionsHash); ].join('$')).digest('hex');
return ( return `${digest}-${path.basename(props.filePath)}`;
`${optionsDigest}${sourceDigest}` +
`${path.basename(props.filePath)}`
);
} }


/** /**
Expand Down

0 comments on commit cb254d1

Please sign in to comment.