mirror of https://github.com/status-im/metro.git
Clean Bundler.js
Reviewed By: rafeca Differential Revision: D7628724 fbshipit-source-id: 75256551bfcaf3729882b76eaade2979d8a85c64
This commit is contained in:
parent
01827a0fab
commit
fecc8d3508
|
@ -103,7 +103,7 @@ const {hasOwnProperty} = Object.prototype;
|
|||
|
||||
class Bundler {
|
||||
_opts: Options;
|
||||
_cache: ?Cache<TransformedCode>;
|
||||
_cache: Cache<TransformedCode>;
|
||||
_baseHash: string;
|
||||
_transformer: Transformer;
|
||||
_depGraphPromise: Promise<DependencyGraph>;
|
||||
|
@ -122,7 +122,7 @@ class Bundler {
|
|||
});
|
||||
|
||||
this._opts = opts;
|
||||
this._cache = opts.cacheStores.length ? new Cache(opts.cacheStores) : null;
|
||||
this._cache = new Cache(opts.cacheStores);
|
||||
|
||||
this._transformer = new Transformer({
|
||||
asyncRequireModulePath: opts.asyncRequireModulePath,
|
||||
|
@ -142,8 +142,7 @@ class Bundler {
|
|||
assetExts: opts.assetExts,
|
||||
assetRegistryPath: opts.assetRegistryPath,
|
||||
blacklistRE: opts.blacklistRE,
|
||||
// TODO: T26134860 Only use experimental caches if stores are provided.
|
||||
experimentalCaches: !!opts.cacheStores.length,
|
||||
experimentalCaches: true,
|
||||
extraNodeModules: opts.extraNodeModules,
|
||||
getPolyfills: opts.getPolyfills,
|
||||
getTransformCacheKey,
|
||||
|
@ -235,13 +234,7 @@ class Bundler {
|
|||
transformCodeOptions: WorkerOptions,
|
||||
): Promise<TransformedCode> {
|
||||
const cache = this._cache;
|
||||
let data = null;
|
||||
let partialKey;
|
||||
let fullKey;
|
||||
let sha1;
|
||||
|
||||
// First, try getting the result from the cache if enabled.
|
||||
if (cache) {
|
||||
const {
|
||||
assetDataPlugins,
|
||||
customTransformOptions,
|
||||
|
@ -263,11 +256,11 @@ class Bundler {
|
|||
}
|
||||
}
|
||||
|
||||
partialKey = stableHash([
|
||||
const partialKey = stableHash([
|
||||
// This is the hash related to the global Bundler config.
|
||||
this._baseHash,
|
||||
|
||||
// Path and code hash.
|
||||
// Path.
|
||||
module.localPath,
|
||||
|
||||
// We cannot include "transformCodeOptions" because of "projectRoot".
|
||||
|
@ -281,19 +274,15 @@ class Bundler {
|
|||
platform,
|
||||
]);
|
||||
|
||||
sha1 = (await this.getDependencyGraph()).getSha1(module.path);
|
||||
fullKey = Buffer.concat([partialKey, Buffer.from(sha1, 'hex')]);
|
||||
|
||||
const sha1 = (await this.getDependencyGraph()).getSha1(module.path);
|
||||
let 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.
|
||||
if (!data) {
|
||||
data = await this._transformer.transform(
|
||||
// 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,
|
||||
|
@ -303,18 +292,15 @@ class Bundler {
|
|||
this._opts.assetRegistryPath,
|
||||
this._opts.minifierPath,
|
||||
);
|
||||
}
|
||||
|
||||
// Third, propagate the result to all cache layers.
|
||||
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.
|
||||
// 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')]);
|
||||
}
|
||||
|
||||
cache.set(fullKey, data.result);
|
||||
}
|
||||
|
||||
return data.result;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue