Clean Bundler.js

Reviewed By: rafeca

Differential Revision: D7628724

fbshipit-source-id: 75256551bfcaf3729882b76eaade2979d8a85c64
This commit is contained in:
Miguel Jimenez Esun 2018-04-19 18:28:28 -07:00 committed by Facebook Github Bot
parent 01827a0fab
commit fecc8d3508
1 changed files with 63 additions and 77 deletions

View File

@ -103,7 +103,7 @@ const {hasOwnProperty} = Object.prototype;
class Bundler { class Bundler {
_opts: Options; _opts: Options;
_cache: ?Cache<TransformedCode>; _cache: Cache<TransformedCode>;
_baseHash: string; _baseHash: string;
_transformer: Transformer; _transformer: Transformer;
_depGraphPromise: Promise<DependencyGraph>; _depGraphPromise: Promise<DependencyGraph>;
@ -122,7 +122,7 @@ class Bundler {
}); });
this._opts = opts; this._opts = opts;
this._cache = opts.cacheStores.length ? new Cache(opts.cacheStores) : null; this._cache = new Cache(opts.cacheStores);
this._transformer = new Transformer({ this._transformer = new Transformer({
asyncRequireModulePath: opts.asyncRequireModulePath, asyncRequireModulePath: opts.asyncRequireModulePath,
@ -142,8 +142,7 @@ class Bundler {
assetExts: opts.assetExts, assetExts: opts.assetExts,
assetRegistryPath: opts.assetRegistryPath, assetRegistryPath: opts.assetRegistryPath,
blacklistRE: opts.blacklistRE, blacklistRE: opts.blacklistRE,
// TODO: T26134860 Only use experimental caches if stores are provided. experimentalCaches: true,
experimentalCaches: !!opts.cacheStores.length,
extraNodeModules: opts.extraNodeModules, extraNodeModules: opts.extraNodeModules,
getPolyfills: opts.getPolyfills, getPolyfills: opts.getPolyfills,
getTransformCacheKey, getTransformCacheKey,
@ -235,86 +234,73 @@ class Bundler {
transformCodeOptions: WorkerOptions, transformCodeOptions: WorkerOptions,
): Promise<TransformedCode> { ): Promise<TransformedCode> {
const cache = this._cache; const cache = this._cache;
let data = null;
let partialKey;
let fullKey;
let sha1;
// First, try getting the result from the cache if enabled. const {
if (cache) { assetDataPlugins,
const { customTransformOptions,
assetDataPlugins, enableBabelRCLookup,
customTransformOptions, dev,
enableBabelRCLookup, hot,
dev, inlineRequires,
hot, minify,
inlineRequires, platform,
minify, projectRoot: _projectRoot, // Blacklisted property.
platform, ...extra
projectRoot: _projectRoot, // Blacklisted property. } = transformCodeOptions;
...extra
} = transformCodeOptions;
for (const key in extra) { for (const key in extra) {
if (hasOwnProperty.call(extra, key)) { if (hasOwnProperty.call(extra, key)) {
throw new Error( throw new Error(
'Extra keys detected: ' + Object.keys(extra).join(', '), 'Extra keys detected: ' + Object.keys(extra).join(', '),
); );
}
}
partialKey = stableHash([
// This is the hash related to the global Bundler config.
this._baseHash,
// Path and code hash.
module.localPath,
// We cannot include "transformCodeOptions" because of "projectRoot".
assetDataPlugins,
customTransformOptions,
enableBabelRCLookup,
dev,
hot,
inlineRequires,
minify,
platform,
]);
sha1 = (await this.getDependencyGraph()).getSha1(module.path);
fullKey = Buffer.concat([partialKey, Buffer.from(sha1, 'hex')]);
const result = await cache.get(fullKey);
if (result) {
data = {result, sha1};
} }
} }
// Second, if there was no result, compute it ourselves. const partialKey = stableHash([
if (!data) { // This is the hash related to the global Bundler config.
data = await this._transformer.transform( this._baseHash,
module.path,
module.localPath, // Path.
code, module.localPath,
module.isPolyfill(),
transformCodeOptions, // We cannot include "transformCodeOptions" because of "projectRoot".
this._opts.assetExts, assetDataPlugins,
this._opts.assetRegistryPath, customTransformOptions,
this._opts.minifierPath, enableBabelRCLookup,
); dev,
hot,
inlineRequires,
minify,
platform,
]);
const sha1 = (await this.getDependencyGraph()).getSha1(module.path);
let fullKey = Buffer.concat([partialKey, Buffer.from(sha1, 'hex')]);
const result = await cache.get(fullKey);
// A valid result from the cache is used directly; otherwise we call into
// the transformer to computed the corresponding result.
const data = result
? {result, sha1}
: await this._transformer.transform(
module.path,
module.localPath,
code,
module.isPolyfill(),
transformCodeOptions,
this._opts.assetExts,
this._opts.assetRegistryPath,
this._opts.minifierPath,
);
// Only re-compute the full key if the SHA-1 changed. This is because
// references are used by the cache implementation in a weak map to keep
// track of the cache that returned the result.
if (sha1 !== data.sha1) {
fullKey = Buffer.concat([partialKey, Buffer.from(data.sha1, 'hex')]);
} }
// Third, propagate the result to all cache layers. cache.set(fullKey, data.result);
if (fullKey && partialKey && sha1 && cache) {
// It could be that the SHA-1 we had back when we sent to the transformer
// was outdated; if so, recompute it.
if (sha1 !== data.sha1) {
fullKey = Buffer.concat([partialKey, Buffer.from(data.sha1, 'hex')]);
}
cache.set(fullKey, data.result);
}
return data.result; return data.result;
} }