mirror of https://github.com/status-im/metro.git
packager: HasteMap: @flow + fixes
Summary: Add Flow types, revealing a few problems, such as `isHaste` having the wrong return value in the "pseudo-mocks". But since the buck worker is in fact working, I guess these functions were never called... The point of typing this file is that I'm going to start aggressively pruning dead code in `node-haste` and hopefully, eventually, get rid of `Moduleish` and `Packageish`. Reviewed By: davidaurelio Differential Revision: D5052379 fbshipit-source-id: dab3f18f05fcf43fbbc48b589170b1cf367d6a48
This commit is contained in:
parent
6e5730601a
commit
a948fdb31b
|
@ -13,10 +13,10 @@
|
||||||
|
|
||||||
import type {CachedReadResult, ReadResult} from '../../node-haste/Module';
|
import type {CachedReadResult, ReadResult} from '../../node-haste/Module';
|
||||||
import type {TransformedCodeFile} from '../types.flow';
|
import type {TransformedCodeFile} from '../types.flow';
|
||||||
import type {ModuleCache} from './node-haste.flow';
|
import type ModuleCache from './ModuleCache';
|
||||||
|
|
||||||
module.exports = class Module {
|
module.exports = class Module {
|
||||||
hasteID: Promise<?string>;
|
hasteID: ?string;
|
||||||
moduleCache: ModuleCache;
|
moduleCache: ModuleCache;
|
||||||
name: Promise<string>;
|
name: Promise<string>;
|
||||||
path: string;
|
path: string;
|
||||||
|
@ -27,9 +27,9 @@ module.exports = class Module {
|
||||||
moduleCache: ModuleCache,
|
moduleCache: ModuleCache,
|
||||||
info: TransformedCodeFile,
|
info: TransformedCodeFile,
|
||||||
) {
|
) {
|
||||||
this.hasteID = Promise.resolve(info.hasteID);
|
this.hasteID = info.hasteID;
|
||||||
this.moduleCache = moduleCache;
|
this.moduleCache = moduleCache;
|
||||||
this.name = this.hasteID.then(name => name || getName(path));
|
this.name = Promise.resolve(this.hasteID || getName(path));
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.type = 'Module';
|
this.type = 'Module';
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ module.exports = class Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
isHaste() {
|
isHaste() {
|
||||||
return this.hasteID.then(Boolean);
|
return Boolean(this.hasteID);
|
||||||
}
|
}
|
||||||
|
|
||||||
hash() {
|
hash() {
|
||||||
|
|
|
@ -34,11 +34,11 @@ module.exports = class ModuleCache {
|
||||||
this.packages = new Map();
|
this.packages = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
getAssetModule(path: string) {
|
getAssetModule(path: string): Module {
|
||||||
return this.getModule(path);
|
return this.getModule(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
getModule(path: string) {
|
getModule(path: string): Module {
|
||||||
let m = this.modules.get(path);
|
let m = this.modules.get(path);
|
||||||
if (!m) {
|
if (!m) {
|
||||||
m = new Module(path, this, this.getTransformedFile(path));
|
m = new Module(path, this, this.getTransformedFile(path));
|
||||||
|
@ -47,7 +47,7 @@ module.exports = class ModuleCache {
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPackage(path: string) {
|
getPackage(path: string): Package {
|
||||||
let p = this.packages.get(path);
|
let p = this.packages.get(path);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
p = new Package(path, this.getPackageData(path));
|
p = new Package(path, this.getPackageData(path));
|
||||||
|
@ -64,7 +64,7 @@ module.exports = class ModuleCache {
|
||||||
return pkg;
|
return pkg;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPackageOf(filePath: string) {
|
getPackageOf(filePath: string): ?Package {
|
||||||
const candidate = this._getClosestPackage(filePath);
|
const candidate = this._getClosestPackage(filePath);
|
||||||
return candidate != null ? this.getPackage(candidate) : null;
|
return candidate != null ? this.getPackage(candidate) : null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,12 +49,12 @@ module.exports = class Package {
|
||||||
return path.join(this.root, main);
|
return path.join(this.root, main);
|
||||||
}
|
}
|
||||||
|
|
||||||
getName() {
|
getName(): Promise<string> {
|
||||||
return Promise.resolve(nullthrows(this.data.name));
|
return Promise.resolve(nullthrows(this.data.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
isHaste() {
|
isHaste(): boolean {
|
||||||
return Promise.resolve(!!this.data.name);
|
return !!this.data.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
redirectRequire(name: string) {
|
redirectRequire(name: string) {
|
||||||
|
|
|
@ -31,6 +31,8 @@ const ResolutionRequest = require('../../node-haste/DependencyGraph/ResolutionRe
|
||||||
|
|
||||||
const defaults = require('../../../defaults');
|
const defaults = require('../../../defaults');
|
||||||
|
|
||||||
|
import type {Moduleish, Packageish} from '../../node-haste/DependencyGraph/ResolutionRequest';
|
||||||
|
|
||||||
type ResolveOptions = {|
|
type ResolveOptions = {|
|
||||||
assetExts: Extensions,
|
assetExts: Extensions,
|
||||||
extraNodeModules: {[id: string]: string},
|
extraNodeModules: {[id: string]: string},
|
||||||
|
@ -45,7 +47,7 @@ const platforms = new Set(defaults.platforms);
|
||||||
* a jest-haste-map's ModuleMap instance. Eventually, though, we'll
|
* a jest-haste-map's ModuleMap instance. Eventually, though, we'll
|
||||||
* want to figure out how to reunify and get rid of `HasteMap`.
|
* want to figure out how to reunify and get rid of `HasteMap`.
|
||||||
*/
|
*/
|
||||||
function getFakeModuleMap(hasteMap: HasteMap) {
|
function getFakeModuleMap(hasteMap: HasteMap<Module, Packageish>) {
|
||||||
return {
|
return {
|
||||||
getModule(name: string, platform: ?string): ?string {
|
getModule(name: string, platform: ?string): ?string {
|
||||||
const module = hasteMap.getModule(name, platform);
|
const module = hasteMap.getModule(name, platform);
|
||||||
|
@ -58,7 +60,7 @@ function getFakeModuleMap(hasteMap: HasteMap) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const nullModule = {
|
const nullModule: Moduleish = {
|
||||||
path: '/',
|
path: '/',
|
||||||
getPackage() {},
|
getPackage() {},
|
||||||
hash() {
|
hash() {
|
||||||
|
@ -66,6 +68,8 @@ const nullModule = {
|
||||||
},
|
},
|
||||||
readCached() { throw new Error('not implemented'); },
|
readCached() { throw new Error('not implemented'); },
|
||||||
readFresh() { return Promise.reject(new Error('not implemented')); },
|
readFresh() { return Promise.reject(new Error('not implemented')); },
|
||||||
|
isHaste() { throw new Error('not implemented'); },
|
||||||
|
getName() { throw new Error('not implemented'); },
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
|
exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
|
||||||
|
|
|
@ -5,7 +5,11 @@
|
||||||
* This source code is licensed under the BSD-style license found in the
|
* 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
|
* 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.
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*
|
||||||
|
* @flow
|
||||||
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
@ -18,15 +22,37 @@ const GENERIC_PLATFORM = 'generic';
|
||||||
const NATIVE_PLATFORM = 'native';
|
const NATIVE_PLATFORM = 'native';
|
||||||
const PACKAGE_JSON = path.sep + 'package.json';
|
const PACKAGE_JSON = path.sep + 'package.json';
|
||||||
|
|
||||||
class HasteMap extends EventEmitter {
|
import type {Moduleish, Packageish, ModuleishCache} from './ResolutionRequest';
|
||||||
|
import type DependencyGraphHelpers from './DependencyGraphHelpers';
|
||||||
|
|
||||||
|
type Options<TModule, TPackage> = {|
|
||||||
|
extensions: Array<string>,
|
||||||
|
files: Array<string>,
|
||||||
|
helpers: DependencyGraphHelpers,
|
||||||
|
moduleCache: ModuleishCache<TModule, TPackage>,
|
||||||
|
platforms: Set<string>,
|
||||||
|
preferNativePlatform: boolean,
|
||||||
|
|};
|
||||||
|
|
||||||
|
class HasteMap<TModule: Moduleish, TPackage: Packageish> extends EventEmitter {
|
||||||
|
|
||||||
|
_extensions: Array<string>;
|
||||||
|
_files: Array<string>;
|
||||||
|
_helpers: DependencyGraphHelpers;
|
||||||
|
_map: {};
|
||||||
|
_moduleCache: ModuleishCache<TModule, TPackage>;
|
||||||
|
_packages: {};
|
||||||
|
_platforms: Set<string>;
|
||||||
|
_preferNativePlatform: boolean;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
extensions,
|
extensions,
|
||||||
files,
|
files,
|
||||||
moduleCache,
|
|
||||||
preferNativePlatform,
|
|
||||||
helpers,
|
helpers,
|
||||||
|
moduleCache,
|
||||||
platforms,
|
platforms,
|
||||||
}) {
|
preferNativePlatform,
|
||||||
|
}: Options<TModule, TPackage>) {
|
||||||
super();
|
super();
|
||||||
this._extensions = extensions;
|
this._extensions = extensions;
|
||||||
this._files = files;
|
this._files = files;
|
||||||
|
@ -35,8 +61,8 @@ class HasteMap extends EventEmitter {
|
||||||
this._platforms = platforms;
|
this._platforms = platforms;
|
||||||
this._preferNativePlatform = preferNativePlatform;
|
this._preferNativePlatform = preferNativePlatform;
|
||||||
|
|
||||||
this._processHastePackage = throat(1, this._processHastePackage.bind(this));
|
(this: any)._processHastePackage = throat(1, this._processHastePackage.bind(this));
|
||||||
this._processHasteModule = throat(1, this._processHasteModule.bind(this));
|
(this: any)._processHasteModule = throat(1, this._processHasteModule.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
|
@ -60,7 +86,7 @@ class HasteMap extends EventEmitter {
|
||||||
return this._files;
|
return this._files;
|
||||||
}
|
}
|
||||||
|
|
||||||
processFileChange(type, absPath) {
|
processFileChange(type: string, absPath: string) {
|
||||||
return Promise.resolve().then(() => {
|
return Promise.resolve().then(() => {
|
||||||
/*eslint no-labels: 0 */
|
/*eslint no-labels: 0 */
|
||||||
let invalidated;
|
let invalidated;
|
||||||
|
@ -96,7 +122,7 @@ class HasteMap extends EventEmitter {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getModule(name, platform = null): Module {
|
getModule(name: string, platform: ?string): ?TModule {
|
||||||
const modulesMap = this._map[name];
|
const modulesMap = this._map[name];
|
||||||
if (modulesMap == null) {
|
if (modulesMap == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -117,11 +143,11 @@ class HasteMap extends EventEmitter {
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPackage(name): Package {
|
getPackage(name: string): TPackage {
|
||||||
return this._packages[name];
|
return this._packages[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
_processHasteModule(file, previousName) {
|
_processHasteModule(file: string, previousName: ?string) {
|
||||||
const module = this._moduleCache.getModule(file);
|
const module = this._moduleCache.getModule(file);
|
||||||
return Promise.resolve().then(() => {
|
return Promise.resolve().then(() => {
|
||||||
const isHaste = module.isHaste();
|
const isHaste = module.isHaste();
|
||||||
|
@ -136,7 +162,7 @@ class HasteMap extends EventEmitter {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_processHastePackage(file, previousName) {
|
_processHastePackage(file: string, previousName: ?string) {
|
||||||
const p = this._moduleCache.getPackage(file);
|
const p = this._moduleCache.getPackage(file);
|
||||||
return Promise.resolve().then(() => {
|
return Promise.resolve().then(() => {
|
||||||
const isHaste = p.isHaste();
|
const isHaste = p.isHaste();
|
||||||
|
@ -157,7 +183,7 @@ class HasteMap extends EventEmitter {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateHasteMap(name, mod) {
|
_updateHasteMap(name: string, mod: TModule | TPackage) {
|
||||||
let existingModule;
|
let existingModule;
|
||||||
|
|
||||||
if (mod.type === 'Package') {
|
if (mod.type === 'Package') {
|
||||||
|
|
|
@ -49,21 +49,26 @@ export type ModuleMap = {
|
||||||
): ?string,
|
): ?string,
|
||||||
};
|
};
|
||||||
|
|
||||||
type Packageish = {
|
export type Packageish = {
|
||||||
|
isHaste(): boolean,
|
||||||
|
getName(): Promise<string>,
|
||||||
|
path: string,
|
||||||
redirectRequire(toModuleName: string): string | false,
|
redirectRequire(toModuleName: string): string | false,
|
||||||
getMain(): string,
|
getMain(): string,
|
||||||
+root: string,
|
+root: string,
|
||||||
};
|
};
|
||||||
|
|
||||||
type Moduleish = {
|
export type Moduleish = {
|
||||||
+path: string,
|
+path: string,
|
||||||
|
isHaste(): boolean,
|
||||||
|
getName(): Promise<string>,
|
||||||
getPackage(): ?Packageish,
|
getPackage(): ?Packageish,
|
||||||
hash(): string,
|
hash(): string,
|
||||||
readCached(transformOptions: TransformWorkerOptions): CachedReadResult,
|
readCached(transformOptions: TransformWorkerOptions): CachedReadResult,
|
||||||
readFresh(transformOptions: TransformWorkerOptions): Promise<ReadResult>,
|
readFresh(transformOptions: TransformWorkerOptions): Promise<ReadResult>,
|
||||||
};
|
};
|
||||||
|
|
||||||
type ModuleishCache<TModule, TPackage> = {
|
export type ModuleishCache<TModule, TPackage> = {
|
||||||
getPackage(
|
getPackage(
|
||||||
name: string,
|
name: string,
|
||||||
platform?: string,
|
platform?: string,
|
||||||
|
|
|
@ -59,8 +59,8 @@ class Package {
|
||||||
return path.join(this.root, main);
|
return path.join(this.root, main);
|
||||||
}
|
}
|
||||||
|
|
||||||
isHaste(): Promise<boolean> {
|
isHaste(): boolean {
|
||||||
return Promise.resolve().then(() => !!this.read().name);
|
return !!this.read().name;
|
||||||
}
|
}
|
||||||
|
|
||||||
getName(): Promise<string> {
|
getName(): Promise<string> {
|
||||||
|
|
Loading…
Reference in New Issue