Extract the logic to calculate the transform key to a separate module

Reviewed By: jeanlauliac

Differential Revision: D6674422

fbshipit-source-id: 1a439e2afe041084edd27637fb9b177980e667a1
This commit is contained in:
Rafael Oleza 2018-01-09 17:40:46 -08:00 committed by Facebook Github Bot
parent 00ce127113
commit fbe441febd
4 changed files with 147 additions and 61 deletions

View File

@ -12,27 +12,21 @@
'use strict';
const assert = require('assert');
const crypto = require('crypto');
const debug = require('debug')('Metro:Bundler');
const fs = require('fs');
const Transformer = require('../JSTransformer');
const Resolver = require('../Resolver');
const path = require('path');
const Transformer = require('../JSTransformer');
const assert = require('assert');
const defaults = require('../defaults');
const createModuleIdFactory = require('../lib/createModuleIdFactory');
const fs = require('fs');
const getTransformCacheKeyFn = require('../lib/getTransformCacheKeyFn');
const {sep: pathSeparator} = require('path');
const VERSION = require('../../package.json').version;
import type {HasteImpl} from '../node-haste/Module';
import type {MappingsMap, SourceMap} from '../lib/SourceMap';
import type {Options as JSTransformerOptions} from '../JSTransformer/worker';
import type {Reporter} from '../lib/reporting';
import type {TransformCache} from '../lib/TransformCaching';
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {PostProcessModules} from '../DeltaBundler';
import type {Options as JSTransformerOptions} from '../JSTransformer/worker';
import type {GlobalTransformCache} from '../lib/GlobalTransformCache';
import type {MappingsMap, SourceMap} from '../lib/SourceMap';
import type {TransformCache} from '../lib/TransformCaching';
import type {Reporter} from '../lib/reporting';
import type {HasteImpl} from '../node-haste/Module';
export type BundlingOptions = {|
+preloadedModules: ?{[string]: true} | false,
@ -103,7 +97,6 @@ export type Options = {|
class Bundler {
_opts: Options;
_getModuleId: (path: string) => number;
_transformer: Transformer;
_resolverPromise: Promise<Resolver>;
_projectRoots: $ReadOnlyArray<string>;
@ -114,44 +107,6 @@ class Bundler {
opts.projectRoots.forEach(verifyRootExists);
const transformModuleStr = fs.readFileSync(opts.transformModulePath);
const transformModuleHash = crypto
.createHash('sha1')
.update(transformModuleStr)
.digest('hex');
const stableProjectRoots = opts.projectRoots.map(p => {
return path.relative(path.join(__dirname, '../../../..'), p);
});
const cacheKeyParts = [
'metro-cache',
VERSION,
opts.cacheVersion,
stableProjectRoots
.join(',')
.split(pathSeparator)
.join('-'),
transformModuleHash,
];
this._getModuleId = createModuleIdFactory();
let getCacheKey = (options: mixed) => '';
if (opts.transformModulePath) {
/* $FlowFixMe: dynamic requires prevent static typing :'( */
const transformer = require(opts.transformModulePath);
if (typeof transformer.getCacheKey !== 'undefined') {
getCacheKey = transformer.getCacheKey;
}
}
const transformCacheKey = crypto
.createHash('sha1')
.update(cacheKeyParts.join('$'))
.digest('hex');
debug(`Using transform cache key "${transformCacheKey}"`);
this._transformer = new Transformer(
opts.transformModulePath,
opts.maxWorkers,
@ -164,17 +119,17 @@ class Bundler {
opts.workerPath || undefined,
);
const getTransformCacheKey = options => {
return transformCacheKey + getCacheKey(options);
};
this._resolverPromise = Resolver.load({
assetExts: opts.assetExts,
assetRegistryPath: opts.assetRegistryPath,
blacklistRE: opts.blacklistRE,
extraNodeModules: opts.extraNodeModules,
getPolyfills: opts.getPolyfills,
getTransformCacheKey,
getTransformCacheKey: getTransformCacheKeyFn({
cacheVersion: opts.cacheVersion,
projectRoots: opts.projectRoots,
transformModulePath: opts.transformModulePath,
}),
globalTransformCache: opts.globalTransformCache,
hasteImpl: opts.hasteImpl,
maxWorkers: opts.maxWorkers,

View File

@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`getTransformCacheKeyFn Should return always the same key for the same params 1`] = `"4f055d9baf74ca63c449a03d12d3ea5e06dce486012af2c6d0afa156e0a3350ab9187479"`;

View File

@ -0,0 +1,60 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+javascript_foundation
* @format
*/
'use strict';
const getTransformCacheKeyFn = require('../getTransformCacheKeyFn');
const path = require('path');
describe('getTransformCacheKeyFn', () => {
it('Should return always the same key for the same params', async () => {
expect(
getTransformCacheKeyFn({
cacheVersion: '1.0',
projectRoots: [__dirname],
transformModulePath: path.resolve(__dirname, '../../transformer.js'),
})(),
).toMatchSnapshot();
});
it('Should return a different key when the params change', async () => {
const baseParams = {
cacheVersion: '1.0',
projectRoots: [__dirname],
transformModulePath: path.resolve(__dirname, '../../transformer.js'),
};
const changedParams = [
{
...baseParams,
cacheVersion: '1.1',
},
{
...baseParams,
projectRoots: ['/foo'],
},
{
...baseParams,
transformModulePath: path.resolve(
__dirname,
'../../../src/defaultTransform.js',
),
},
];
const baseCacheKey = getTransformCacheKeyFn(baseParams)();
changedParams.forEach(params => {
expect(getTransformCacheKeyFn(params)()).not.toEqual(baseCacheKey);
});
});
});

View File

@ -0,0 +1,68 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @format
*/
'use strict';
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const VERSION = require('../../package.json').version;
/**
* Returns a function that will return the transform cache key based on some
* passed transform options.
*/
function getTransformCacheKeyFn(opts: {|
+cacheVersion: string,
+projectRoots: $ReadOnlyArray<string>,
+transformModulePath: string,
|}): (options: mixed) => string {
const transformModuleHash = crypto
.createHash('sha1')
.update(fs.readFileSync(opts.transformModulePath))
.digest('hex');
const stableProjectRoots = opts.projectRoots.map(p => {
return path.relative(path.join(__dirname, '../../../..'), p);
});
const cacheKeyParts = [
'metro-cache',
VERSION,
opts.cacheVersion,
stableProjectRoots
.join(',')
.split(path.sep)
.join('-'),
transformModuleHash,
];
const transformCacheKey = crypto
.createHash('sha1')
.update(cacheKeyParts.join('$'))
.digest('hex');
/* $FlowFixMe: dynamic requires prevent static typing :'( */
const transformer = require(opts.transformModulePath);
const getCacheKey =
typeof transformer.getCacheKey !== 'undefined'
? transformer.getCacheKey
: (options: mixed) => '';
return function(options: mixed): string {
return transformCacheKey + getCacheKey(options);
};
}
module.exports = getTransformCacheKeyFn;