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 {
|
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,13 +234,7 @@ 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.
|
|
||||||
if (cache) {
|
|
||||||
const {
|
const {
|
||||||
assetDataPlugins,
|
assetDataPlugins,
|
||||||
customTransformOptions,
|
customTransformOptions,
|
||||||
|
@ -263,11 +256,11 @@ class Bundler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
partialKey = stableHash([
|
const partialKey = stableHash([
|
||||||
// This is the hash related to the global Bundler config.
|
// This is the hash related to the global Bundler config.
|
||||||
this._baseHash,
|
this._baseHash,
|
||||||
|
|
||||||
// Path and code hash.
|
// Path.
|
||||||
module.localPath,
|
module.localPath,
|
||||||
|
|
||||||
// We cannot include "transformCodeOptions" because of "projectRoot".
|
// We cannot include "transformCodeOptions" because of "projectRoot".
|
||||||
|
@ -281,19 +274,15 @@ class Bundler {
|
||||||
platform,
|
platform,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
sha1 = (await this.getDependencyGraph()).getSha1(module.path);
|
const sha1 = (await this.getDependencyGraph()).getSha1(module.path);
|
||||||
fullKey = Buffer.concat([partialKey, Buffer.from(sha1, 'hex')]);
|
let fullKey = Buffer.concat([partialKey, Buffer.from(sha1, 'hex')]);
|
||||||
|
|
||||||
const result = await cache.get(fullKey);
|
const result = await cache.get(fullKey);
|
||||||
|
|
||||||
if (result) {
|
// A valid result from the cache is used directly; otherwise we call into
|
||||||
data = {result, sha1};
|
// the transformer to computed the corresponding result.
|
||||||
}
|
const data = result
|
||||||
}
|
? {result, sha1}
|
||||||
|
: await this._transformer.transform(
|
||||||
// Second, if there was no result, compute it ourselves.
|
|
||||||
if (!data) {
|
|
||||||
data = await this._transformer.transform(
|
|
||||||
module.path,
|
module.path,
|
||||||
module.localPath,
|
module.localPath,
|
||||||
code,
|
code,
|
||||||
|
@ -303,18 +292,15 @@ class Bundler {
|
||||||
this._opts.assetRegistryPath,
|
this._opts.assetRegistryPath,
|
||||||
this._opts.minifierPath,
|
this._opts.minifierPath,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
// Third, propagate the result to all cache layers.
|
// Only re-compute the full key if the SHA-1 changed. This is because
|
||||||
if (fullKey && partialKey && sha1 && cache) {
|
// references are used by the cache implementation in a weak map to keep
|
||||||
// It could be that the SHA-1 we had back when we sent to the transformer
|
// track of the cache that returned the result.
|
||||||
// was outdated; if so, recompute it.
|
|
||||||
if (sha1 !== data.sha1) {
|
if (sha1 !== data.sha1) {
|
||||||
fullKey = Buffer.concat([partialKey, Buffer.from(data.sha1, 'hex')]);
|
fullKey = Buffer.concat([partialKey, Buffer.from(data.sha1, 'hex')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.set(fullKey, data.result);
|
cache.set(fullKey, data.result);
|
||||||
}
|
|
||||||
|
|
||||||
return data.result;
|
return data.result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue