[react-packager] Use transformer name in cache name

Summary:
@public
Shouldn't confuse the cache from files transformed by different transformers. This takes into account the transformer in the cache hash name.

Test Plan:
* start server with --babel
* generate bundle
* start server with --jstransform
* generate bundle
* compare them and they're different
This commit is contained in:
Amjad Masad 2015-05-13 14:44:16 -07:00
parent ac49f8ca99
commit e5b939d623
3 changed files with 38 additions and 6 deletions

View File

@ -31,6 +31,10 @@ var validateOpts = declareOpts({
type: 'array', type: 'array',
required: true, required: true,
}, },
transformModulePath: {
type:'string',
required: true,
},
}); });
module.exports = Cache; module.exports = Cache;
@ -162,6 +166,8 @@ function cacheFilePath(options) {
var cacheVersion = options.cacheVersion || '0'; var cacheVersion = options.cacheVersion || '0';
hash.update(cacheVersion); hash.update(cacheVersion);
hash.update(options.transformModulePath);
var name = 'react-packager-cache-' + hash.digest('hex'); var name = 'react-packager-cache-' + hash.digest('hex');
return path.join(tmpdir, name); return path.join(tmpdir, name);
} }

View File

@ -32,10 +32,14 @@ describe('JSTransformer Cache', function() {
describe('getting/setting', function() { describe('getting/setting', function() {
it('calls loader callback for uncached file', function() { it('calls loader callback for uncached file', function() {
var cache = new Cache({projectRoots: ['/rootDir']}); var cache = new Cache({
projectRoots: ['/rootDir'],
transformModulePath: 'x.js',
});
var loaderCb = jest.genMockFn().mockImpl(function() { var loaderCb = jest.genMockFn().mockImpl(function() {
return Promise.resolve(); return Promise.resolve();
}); });
cache.get('/rootDir/someFile', loaderCb); cache.get('/rootDir/someFile', loaderCb);
expect(loaderCb).toBeCalledWith('/rootDir/someFile'); expect(loaderCb).toBeCalledWith('/rootDir/someFile');
}); });
@ -48,10 +52,15 @@ describe('JSTransformer Cache', function() {
} }
}); });
}); });
var cache = new Cache({projectRoots: ['/rootDir']});
var cache = new Cache({
projectRoots: ['/rootDir'],
transformModulePath: 'x.js',
});
var loaderCb = jest.genMockFn().mockImpl(function() { var loaderCb = jest.genMockFn().mockImpl(function() {
return Promise.resolve('lol'); return Promise.resolve('lol');
}); });
return cache.get('/rootDir/someFile', loaderCb).then(function(value) { return cache.get('/rootDir/someFile', loaderCb).then(function(value) {
expect(value).toBe('lol'); expect(value).toBe('lol');
}); });
@ -65,10 +74,15 @@ describe('JSTransformer Cache', function() {
} }
}); });
}); });
var cache = new Cache({projectRoots: ['/rootDir']});
var cache = new Cache({
projectRoots: ['/rootDir'],
transformModulePath: 'x.js',
});
var loaderCb = jest.genMockFn().mockImpl(function() { var loaderCb = jest.genMockFn().mockImpl(function() {
return Promise.resolve('lol'); return Promise.resolve('lol');
}); });
return cache.get('/rootDir/someFile', loaderCb).then(function() { return cache.get('/rootDir/someFile', loaderCb).then(function() {
var shouldNotBeCalled = jest.genMockFn(); var shouldNotBeCalled = jest.genMockFn();
return cache.get('/rootDir/someFile', shouldNotBeCalled) return cache.get('/rootDir/someFile', shouldNotBeCalled)
@ -126,8 +140,12 @@ describe('JSTransformer Cache', function() {
}); });
pit('should load cache from disk', function() { pit('should load cache from disk', function() {
var cache = new Cache({projectRoots: ['/rootDir']}); var cache = new Cache({
projectRoots: ['/rootDir'],
transformModulePath: 'x.js',
});
var loaderCb = jest.genMockFn(); var loaderCb = jest.genMockFn();
return cache.get('/rootDir/someFile', loaderCb).then(function(value) { return cache.get('/rootDir/someFile', loaderCb).then(function(value) {
expect(loaderCb).not.toBeCalled(); expect(loaderCb).not.toBeCalled();
expect(value).toBe('oh hai'); expect(value).toBe('oh hai');
@ -152,7 +170,10 @@ describe('JSTransformer Cache', function() {
return 123; return 123;
}; };
var cache = new Cache({projectRoots: ['/rootDir']}); var cache = new Cache({
projectRoots: ['/rootDir'],
transformModulePath: 'x.js',
});
var loaderCb = jest.genMockFn().mockImpl(function() { var loaderCb = jest.genMockFn().mockImpl(function() {
return Promise.resolve('new value'); return Promise.resolve('new value');
}); });
@ -193,7 +214,11 @@ describe('JSTransformer Cache', function() {
}); });
}); });
var cache = new Cache({projectRoots: ['/rootDir']}); var cache = new Cache({
projectRoots: ['/rootDir'],
transformModulePath: 'x.js',
});
cache.get('/rootDir/bar', function() { cache.get('/rootDir/bar', function() {
return Promise.resolve('bar value'); return Promise.resolve('bar value');
}); });

View File

@ -60,6 +60,7 @@ function Transformer(options) {
resetCache: options.resetCache, resetCache: options.resetCache,
cacheVersion: options.cacheVersion, cacheVersion: options.cacheVersion,
projectRoots: options.projectRoots, projectRoots: options.projectRoots,
transformModulePath: options.transformModulePath,
}); });
if (options.transformModulePath != null) { if (options.transformModulePath != null) {