mirror of https://github.com/status-im/metro.git
More clean-up to Module.js
Reviewed By: jeanlauliac Differential Revision: D7758143 fbshipit-source-id: 11f4de9099e5677fdf2d62bd2911570ed962c793
This commit is contained in:
parent
1578d2f8cd
commit
a29d303273
File diff suppressed because it is too large
Load Diff
|
@ -104,7 +104,6 @@ describe('traverseDependencies', function() {
|
|||
isAsset: dep.isAsset(),
|
||||
isPolyfill: dep.isPolyfill(),
|
||||
resolution: dep.resolution,
|
||||
id: dep.getName(),
|
||||
dependencies: moduleDependencies,
|
||||
};
|
||||
}),
|
||||
|
@ -3648,13 +3647,13 @@ describe('traverseDependencies', function() {
|
|||
|
||||
it('returns correctly a JS module', async () => {
|
||||
const module = dependencyGraph.getModuleForPath('/root/index.js');
|
||||
expect(module.getName()).toBe('index.js');
|
||||
expect(module.path).toBe('/root/index.js');
|
||||
expect(module.isAsset()).toBe(false);
|
||||
});
|
||||
|
||||
it('returns correctly an asset module', async () => {
|
||||
const module = dependencyGraph.getModuleForPath('/root/imgs/a.png');
|
||||
expect(module.getName()).toBe('imgs/a.png');
|
||||
expect(module.path).toBe('/root/imgs/a.png');
|
||||
expect(module.isAsset()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
import type {CachedReadResult, ReadResult} from '../../node-haste/Module';
|
||||
import type {TransformedCodeFile} from '../types.flow';
|
||||
import type ModuleCache from './ModuleCache';
|
||||
|
||||
|
@ -19,7 +18,6 @@ module.exports = class Module {
|
|||
moduleCache: ModuleCache;
|
||||
name: string;
|
||||
path: string;
|
||||
type: 'Module';
|
||||
|
||||
constructor(
|
||||
path: string,
|
||||
|
@ -30,19 +28,6 @@ module.exports = class Module {
|
|||
this.moduleCache = moduleCache;
|
||||
this.name = this.hasteID || getName(path);
|
||||
this.path = path;
|
||||
this.type = 'Module';
|
||||
}
|
||||
|
||||
readCached(): CachedReadResult {
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
|
||||
readFresh(): Promise<ReadResult> {
|
||||
return Promise.reject(new Error('not implemented'));
|
||||
}
|
||||
|
||||
getName(): string {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
getPackage() {
|
||||
|
@ -52,10 +37,6 @@ module.exports = class Module {
|
|||
isHaste() {
|
||||
return Boolean(this.hasteID);
|
||||
}
|
||||
|
||||
hash() {
|
||||
throw new Error('not implemented');
|
||||
}
|
||||
};
|
||||
|
||||
function getName(path) {
|
||||
|
|
|
@ -46,15 +46,6 @@ const PACKAGE_JSON = path.sep + 'package.json';
|
|||
const NULL_MODULE: Moduleish = {
|
||||
path: '/',
|
||||
getPackage() {},
|
||||
hash() {
|
||||
throw new Error('not implemented');
|
||||
},
|
||||
readCached() {
|
||||
throw new Error('not implemented');
|
||||
},
|
||||
readFresh() {
|
||||
return Promise.reject(new Error('not implemented'));
|
||||
},
|
||||
isHaste() {
|
||||
throw new Error('not implemented');
|
||||
},
|
||||
|
|
|
@ -17,10 +17,6 @@ class AssetModule extends Module {
|
|||
return null;
|
||||
}
|
||||
|
||||
isHaste() {
|
||||
return false;
|
||||
}
|
||||
|
||||
isAsset() {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ const invariant = require('fbjs/lib/invariant');
|
|||
const path = require('path');
|
||||
const util = require('util');
|
||||
|
||||
import type {Moduleish, Packageish} from './ResolutionRequest';
|
||||
import type {
|
||||
CustomResolver,
|
||||
DoesFileExist,
|
||||
|
@ -42,16 +43,6 @@ export type ModuleMap = {
|
|||
): ?string,
|
||||
};
|
||||
|
||||
export type Packageish = {
|
||||
redirectRequire(toModuleName: string): string | false,
|
||||
getMain(): string,
|
||||
};
|
||||
|
||||
export type Moduleish = {
|
||||
+path: string,
|
||||
getPackage(): ?Packageish,
|
||||
};
|
||||
|
||||
export type ModuleishCache<TModule, TPackage> = {
|
||||
getPackage(
|
||||
name: string,
|
||||
|
|
|
@ -18,27 +18,17 @@ const {InvalidPackageError} = require('metro-resolver');
|
|||
const {PackageResolutionError} = require('metro-core');
|
||||
|
||||
import type DependencyGraphHelpers from './DependencyGraphHelpers';
|
||||
import type {Options as TransformWorkerOptions} from '../../JSTransformer/worker';
|
||||
import type {ReadResult, CachedReadResult} from '../Module';
|
||||
import type {ModuleResolver} from './ModuleResolution';
|
||||
|
||||
export type Packageish = {
|
||||
isHaste(): boolean,
|
||||
getName(): string,
|
||||
path: string,
|
||||
redirectRequire(toModuleName: string): string | false,
|
||||
getMain(): string,
|
||||
+root: string,
|
||||
};
|
||||
|
||||
export type Moduleish = {
|
||||
+path: string,
|
||||
isHaste(): boolean,
|
||||
getName(): string,
|
||||
getPackage(): ?Packageish,
|
||||
hash(): string,
|
||||
readCached(transformOptions: TransformWorkerOptions): CachedReadResult,
|
||||
readFresh(transformOptions: TransformWorkerOptions): Promise<ReadResult>,
|
||||
};
|
||||
|
||||
export type ModuleishCache<TModule, TPackage> = {
|
||||
|
|
|
@ -22,15 +22,13 @@ import type ModuleCache from './ModuleCache';
|
|||
import type {LocalPath} from './lib/toLocalPath';
|
||||
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
|
||||
|
||||
export type ReadResult = {
|
||||
type ReadResult = {
|
||||
+code: string,
|
||||
+dependencies: $ReadOnlyArray<TransformResultDependency>,
|
||||
+map: Array<MetroSourceMapSegmentTuple>,
|
||||
+source: string,
|
||||
};
|
||||
|
||||
export type CachedReadResult = ?ReadResult;
|
||||
|
||||
export type TransformCode = (
|
||||
module: Module,
|
||||
sourceCode: ?string,
|
||||
|
@ -47,7 +45,6 @@ export type ConstructorArgs = {
|
|||
class Module {
|
||||
localPath: LocalPath;
|
||||
path: string;
|
||||
type: string;
|
||||
|
||||
_moduleCache: ModuleCache;
|
||||
_transformCode: TransformCode;
|
||||
|
@ -60,20 +57,11 @@ class Module {
|
|||
|
||||
this.localPath = localPath;
|
||||
this.path = file;
|
||||
this.type = 'Module';
|
||||
|
||||
this._moduleCache = moduleCache;
|
||||
this._transformCode = transformCode;
|
||||
}
|
||||
|
||||
isHaste(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
getName(): string {
|
||||
return this.localPath;
|
||||
}
|
||||
|
||||
getPackage() {
|
||||
return this._moduleCache.getPackageForModule(this);
|
||||
}
|
||||
|
@ -109,18 +97,6 @@ class Module {
|
|||
};
|
||||
}
|
||||
|
||||
readCached(transformOptions: WorkerOptions): null {
|
||||
return null;
|
||||
}
|
||||
|
||||
readFresh(transformOptions: WorkerOptions): Promise<ReadResult> {
|
||||
return this.read(transformOptions);
|
||||
}
|
||||
|
||||
hash() {
|
||||
return `Module : ${this.path}`;
|
||||
}
|
||||
|
||||
isAsset() {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -23,15 +23,13 @@ type PackageContent = {
|
|||
|
||||
class Package {
|
||||
path: string;
|
||||
root: string;
|
||||
type: string;
|
||||
|
||||
_root: string;
|
||||
_content: ?PackageContent;
|
||||
|
||||
constructor({file}: {file: string}) {
|
||||
this.path = path.resolve(file);
|
||||
this.root = path.dirname(this.path);
|
||||
this.type = 'Package';
|
||||
this._root = path.dirname(this.path);
|
||||
this._content = null;
|
||||
}
|
||||
|
||||
|
@ -75,15 +73,7 @@ class Package {
|
|||
}
|
||||
|
||||
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
|
||||
return path.join(this.root, main);
|
||||
}
|
||||
|
||||
isHaste(): boolean {
|
||||
return !!this.read().name;
|
||||
}
|
||||
|
||||
getName(): string {
|
||||
return this.read().name;
|
||||
return path.join(this._root, main);
|
||||
}
|
||||
|
||||
invalidate() {
|
||||
|
@ -107,7 +97,7 @@ class Package {
|
|||
replacement || name;
|
||||
}
|
||||
|
||||
let relPath = './' + path.relative(this.root, name);
|
||||
let relPath = './' + path.relative(this._root, name);
|
||||
if (path.sep !== '/') {
|
||||
relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/');
|
||||
}
|
||||
|
@ -129,7 +119,7 @@ class Package {
|
|||
|
||||
if (redirect) {
|
||||
return path.join(
|
||||
this.root,
|
||||
this._root,
|
||||
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
|
||||
redirect,
|
||||
);
|
||||
|
|
|
@ -45,28 +45,11 @@ describe('Module', () => {
|
|||
it('Returns the correct values for many properties and methods', () => {
|
||||
expect(module.localPath).toBe('file.js');
|
||||
expect(module.path).toBe('/root/to/file.js');
|
||||
expect(module.type).toBe('Module');
|
||||
|
||||
expect(module.hash()).toBeDefined();
|
||||
expect(module.getName()).toBe('file.js');
|
||||
expect(module.isHaste()).toBe(false);
|
||||
expect(module.isAsset()).toBe(false);
|
||||
expect(module.isPolyfill()).toBe(false);
|
||||
});
|
||||
|
||||
it('reads the modules correctly', () => {
|
||||
const opts = {};
|
||||
|
||||
// Caches are not in Module.js anymore.
|
||||
expect(module.readCached()).toBe(null);
|
||||
|
||||
// When reading fresh, we call directly into read.
|
||||
module.readFresh(opts);
|
||||
expect(transformCode.mock.calls[0][0]).toBe(module);
|
||||
expect(transformCode.mock.calls[0][1]).toBe(null);
|
||||
expect(transformCode.mock.calls[0][2]).toBe(opts);
|
||||
});
|
||||
|
||||
it('returns the result from the transform code straight away', async () => {
|
||||
fs.readFileSync.mockReturnValue('original code');
|
||||
|
||||
|
|
Loading…
Reference in New Issue