Skip to content

Commit cb254d1

Browse files
Jean LauliacFacebook Github Bot
Jean Lauliac
authored and
Facebook Github Bot
committed
packager: GlobalTransformCache: globalized keyOf
Reviewed By: davidaurelio Differential Revision: D4243863 fbshipit-source-id: 917a8a116baf67c838c062b7b713dd4660d9f673
1 parent 9b9fd2f commit cb254d1

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

packager/react-packager/src/JSTransformer/worker/worker.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ type Transform = (
4646
) => mixed,
4747
) => void;
4848

49-
export type Options = {transform?: {}};
49+
export type Options = {
50+
transform?: {projectRoots: Array<string>},
51+
};
5052

5153
export type Data = {
5254
result: TransformedCode,

packager/react-packager/src/lib/GlobalTransformCache.js

+32-11
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
'use strict';
1313

14+
const crypto = require('crypto');
1415
const imurmurhash = require('imurmurhash');
1516
const invariant = require('invariant');
1617
const jsonStableStringify = require('json-stable-stringify');
1718
const path = require('path');
1819
const request = require('request');
19-
const toFixedHex = require('./toFixedHex');
2020

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

176+
/**
177+
* The transform options contain absolute paths. This can contain, for
178+
* example, the username if someone works their home directory (very likely).
179+
* We need to get rid of this user-and-machine-dependent data for the global
180+
* cache, otherwise nobody would share the same cache keys.
181+
*/
182+
function globalizeTransformOptions(
183+
options: TransformOptions,
184+
): TransformOptions {
185+
const {transform} = options;
186+
if (transform == null) {
187+
return options;
188+
}
189+
return {
190+
...options,
191+
transform: {
192+
...transform,
193+
projectRoots: transform.projectRoots.map(p => {
194+
return path.relative(path.join(__dirname, '../../../../..'), p);
195+
}),
196+
},
197+
};
198+
}
199+
176200
/**
177201
* One can enable the global cache by calling configure() from a custom CLI
178202
* script. Eventually we may make it more flexible.
@@ -190,16 +214,13 @@ class GlobalTransformCache {
190214
* Return a key for identifying uniquely a source file.
191215
*/
192216
static keyOf(props: FetchProps) {
193-
const sourceDigest = toFixedHex(8, imurmurhash(props.sourceCode).result());
194-
const optionsHash = imurmurhash()
195-
.hash(jsonStableStringify(props.transformOptions) || '')
196-
.hash(props.transformCacheKey)
197-
.result();
198-
const optionsDigest = toFixedHex(8, optionsHash);
199-
return (
200-
`${optionsDigest}${sourceDigest}` +
201-
`${path.basename(props.filePath)}`
202-
);
217+
const stableOptions = globalizeTransformOptions(props.transformOptions);
218+
const digest = crypto.createHash('sha1').update([
219+
jsonStableStringify(stableOptions),
220+
props.transformCacheKey,
221+
imurmurhash(props.sourceCode).result().toString(),
222+
].join('$')).digest('hex');
223+
return `${digest}-${path.basename(props.filePath)}`;
203224
}
204225

205226
/**

0 commit comments

Comments
 (0)