mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary:
This kills fastfs in favor of Jest's hasteFS. It gets rid of a ton of code, including the mocking code in ResolutionRequest which we don't need any more. Next step after this is to rewrite HasteMap, ModuleCache, Module/Package. We are getting closer to a nicer and faster world! :)
Here is what I did:
* Use Jest's HasteFS instead of fastfs. A fresh instance is received every time something changes on the FS.
* HasteFS is not shared with everything any more. Only one reference is kept in DependencyGraph and there are a few smaller functions that are passed around (getClosestPackage and dirExists). Note: `dirExists` now does fs access instead of an offline check. This sucks but stat calls aren't slow and aren't going to be a bottleneck in ResolutionRequest, I promise! When it is time to tackle a ResolutionRequest rewrite with jest-resolve, this will go away. "It gets worse before it gets better" :) The ModuleGraph equivalent does *not* do fs access and retains the previous way of doing things because we shouldn't do online fs access there.
* Add flow annotations to ResolutionRequest. This required a few tiny hacks for now because of ModuleGraph's duck typing. I'll get rid of this soon.
* Updated ModuleGraph to work with the new code, also created a mock HasteFS instance there.
* I fixed a few tiny mock issues for `fs` to make the tests work; I had to add one tiny little internal update to `dgraph._hasteFS._files` because the file watching in the tests isn't real. It is instrumented through some function calls, therefore the hasteFS instance doesn't get automatically updated. One way to solve this is to add `JestHasteMap.emit('change', …)` for testing but I didn't want to cut a Jest release just for that. #movefast
(Note: I will likely land this in 1.5 weeks from now after my vacation and I have yet to fully test all the product flows. Please give me feedback so I can make sure this is solid!)
Reviewed By: davidaurelio
Differential Revision: D4204082
fbshipit-source-id: d6dc9fcb77ac224df4554a59f0fce241c01b0512
158 lines
4.3 KiB
JavaScript
158 lines
4.3 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-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
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const AssetModule = require('./AssetModule');
|
|
const Module = require('./Module');
|
|
const Package = require('./Package');
|
|
const Polyfill = require('./Polyfill');
|
|
|
|
import type Cache from './Cache';
|
|
import type DependencyGraphHelpers from './DependencyGraph/DependencyGraphHelpers';
|
|
import type {
|
|
TransformCode,
|
|
Options as ModuleOptions,
|
|
} from './Module';
|
|
|
|
type GetClosestPackageFn = (filePath: string) => ?string;
|
|
|
|
class ModuleCache {
|
|
|
|
_assetDependencies: mixed;
|
|
_cache: Cache;
|
|
_depGraphHelpers: DependencyGraphHelpers;
|
|
_getClosestPackage: GetClosestPackageFn;
|
|
_moduleCache: {[filePath: string]: Module};
|
|
_moduleOptions: ModuleOptions;
|
|
_packageCache: {[filePath: string]: Package};
|
|
_packageModuleMap: WeakMap<Module, string>;
|
|
_platforms: mixed;
|
|
_transformCacheKey: string;
|
|
_transformCode: TransformCode;
|
|
|
|
constructor({
|
|
assetDependencies,
|
|
cache,
|
|
depGraphHelpers,
|
|
extractRequires,
|
|
getClosestPackage,
|
|
moduleOptions,
|
|
transformCacheKey,
|
|
transformCode,
|
|
}: {
|
|
assetDependencies: mixed,
|
|
cache: Cache,
|
|
depGraphHelpers: DependencyGraphHelpers,
|
|
getClosestPackage: GetClosestPackageFn,
|
|
moduleOptions: ModuleOptions,
|
|
transformCacheKey: string,
|
|
transformCode: TransformCode,
|
|
}, platforms: mixed) {
|
|
this._assetDependencies = assetDependencies;
|
|
this._getClosestPackage = getClosestPackage;
|
|
this._cache = cache;
|
|
this._depGraphHelpers = depGraphHelpers;
|
|
this._moduleCache = Object.create(null);
|
|
this._moduleOptions = moduleOptions;
|
|
this._packageCache = Object.create(null);
|
|
this._packageModuleMap = new WeakMap();
|
|
this._platforms = platforms;
|
|
this._transformCacheKey = transformCacheKey;
|
|
this._transformCode = transformCode;
|
|
}
|
|
|
|
getModule(filePath: string) {
|
|
if (!this._moduleCache[filePath]) {
|
|
this._moduleCache[filePath] = new Module({
|
|
file: filePath,
|
|
moduleCache: this,
|
|
cache: this._cache,
|
|
transformCode: this._transformCode,
|
|
transformCacheKey: this._transformCacheKey,
|
|
depGraphHelpers: this._depGraphHelpers,
|
|
options: this._moduleOptions,
|
|
});
|
|
}
|
|
return this._moduleCache[filePath];
|
|
}
|
|
|
|
getAllModules() {
|
|
return this._moduleCache;
|
|
}
|
|
|
|
getAssetModule(filePath: string) {
|
|
if (!this._moduleCache[filePath]) {
|
|
this._moduleCache[filePath] = new AssetModule({
|
|
file: filePath,
|
|
moduleCache: this,
|
|
cache: this._cache,
|
|
dependencies: this._assetDependencies,
|
|
}, this._platforms);
|
|
}
|
|
return this._moduleCache[filePath];
|
|
}
|
|
|
|
getPackage(filePath: string) {
|
|
if (!this._packageCache[filePath]) {
|
|
this._packageCache[filePath] = new Package({
|
|
file: filePath,
|
|
cache: this._cache,
|
|
});
|
|
}
|
|
return this._packageCache[filePath];
|
|
}
|
|
|
|
getPackageForModule(module: Module): ?Package {
|
|
if (this._packageModuleMap.has(module)) {
|
|
const packagePath = this._packageModuleMap.get(module);
|
|
if (this._packageCache[packagePath]) {
|
|
return this._packageCache[packagePath];
|
|
} else {
|
|
this._packageModuleMap.delete(module);
|
|
}
|
|
}
|
|
|
|
const packagePath = this._getClosestPackage(module.path);
|
|
if (!packagePath) {
|
|
return null;
|
|
}
|
|
|
|
this._packageModuleMap.set(module, packagePath);
|
|
return this.getPackage(packagePath);
|
|
}
|
|
|
|
createPolyfill({file}: {file: string}) {
|
|
/* $FlowFixMe: there are missing arguments. */
|
|
return new Polyfill({
|
|
file,
|
|
cache: this._cache,
|
|
depGraphHelpers: this._depGraphHelpers,
|
|
moduleCache: this,
|
|
transformCode: this._transformCode,
|
|
transformCacheKey: this._transformCacheKey,
|
|
});
|
|
}
|
|
|
|
processFileChange(type: string, filePath: string) {
|
|
if (this._moduleCache[filePath]) {
|
|
this._moduleCache[filePath].invalidate();
|
|
delete this._moduleCache[filePath];
|
|
}
|
|
if (this._packageCache[filePath]) {
|
|
this._packageCache[filePath].invalidate();
|
|
delete this._packageCache[filePath];
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = ModuleCache;
|