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(),
|
isAsset: dep.isAsset(),
|
||||||
isPolyfill: dep.isPolyfill(),
|
isPolyfill: dep.isPolyfill(),
|
||||||
resolution: dep.resolution,
|
resolution: dep.resolution,
|
||||||
id: dep.getName(),
|
|
||||||
dependencies: moduleDependencies,
|
dependencies: moduleDependencies,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
@ -3648,13 +3647,13 @@ describe('traverseDependencies', function() {
|
||||||
|
|
||||||
it('returns correctly a JS module', async () => {
|
it('returns correctly a JS module', async () => {
|
||||||
const module = dependencyGraph.getModuleForPath('/root/index.js');
|
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);
|
expect(module.isAsset()).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns correctly an asset module', async () => {
|
it('returns correctly an asset module', async () => {
|
||||||
const module = dependencyGraph.getModuleForPath('/root/imgs/a.png');
|
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);
|
expect(module.isAsset()).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import type {CachedReadResult, ReadResult} from '../../node-haste/Module';
|
|
||||||
import type {TransformedCodeFile} from '../types.flow';
|
import type {TransformedCodeFile} from '../types.flow';
|
||||||
import type ModuleCache from './ModuleCache';
|
import type ModuleCache from './ModuleCache';
|
||||||
|
|
||||||
|
@ -19,7 +18,6 @@ module.exports = class Module {
|
||||||
moduleCache: ModuleCache;
|
moduleCache: ModuleCache;
|
||||||
name: string;
|
name: string;
|
||||||
path: string;
|
path: string;
|
||||||
type: 'Module';
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
path: string,
|
path: string,
|
||||||
|
@ -30,19 +28,6 @@ module.exports = class Module {
|
||||||
this.moduleCache = moduleCache;
|
this.moduleCache = moduleCache;
|
||||||
this.name = this.hasteID || getName(path);
|
this.name = this.hasteID || getName(path);
|
||||||
this.path = 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() {
|
getPackage() {
|
||||||
|
@ -52,10 +37,6 @@ module.exports = class Module {
|
||||||
isHaste() {
|
isHaste() {
|
||||||
return Boolean(this.hasteID);
|
return Boolean(this.hasteID);
|
||||||
}
|
}
|
||||||
|
|
||||||
hash() {
|
|
||||||
throw new Error('not implemented');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function getName(path) {
|
function getName(path) {
|
||||||
|
|
|
@ -46,15 +46,6 @@ const PACKAGE_JSON = path.sep + 'package.json';
|
||||||
const NULL_MODULE: Moduleish = {
|
const NULL_MODULE: Moduleish = {
|
||||||
path: '/',
|
path: '/',
|
||||||
getPackage() {},
|
getPackage() {},
|
||||||
hash() {
|
|
||||||
throw new Error('not implemented');
|
|
||||||
},
|
|
||||||
readCached() {
|
|
||||||
throw new Error('not implemented');
|
|
||||||
},
|
|
||||||
readFresh() {
|
|
||||||
return Promise.reject(new Error('not implemented'));
|
|
||||||
},
|
|
||||||
isHaste() {
|
isHaste() {
|
||||||
throw new Error('not implemented');
|
throw new Error('not implemented');
|
||||||
},
|
},
|
||||||
|
|
|
@ -17,10 +17,6 @@ class AssetModule extends Module {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
isHaste() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
isAsset() {
|
isAsset() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ const invariant = require('fbjs/lib/invariant');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
|
|
||||||
|
import type {Moduleish, Packageish} from './ResolutionRequest';
|
||||||
import type {
|
import type {
|
||||||
CustomResolver,
|
CustomResolver,
|
||||||
DoesFileExist,
|
DoesFileExist,
|
||||||
|
@ -42,16 +43,6 @@ export type ModuleMap = {
|
||||||
): ?string,
|
): ?string,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Packageish = {
|
|
||||||
redirectRequire(toModuleName: string): string | false,
|
|
||||||
getMain(): string,
|
|
||||||
};
|
|
||||||
|
|
||||||
export type Moduleish = {
|
|
||||||
+path: string,
|
|
||||||
getPackage(): ?Packageish,
|
|
||||||
};
|
|
||||||
|
|
||||||
export type ModuleishCache<TModule, TPackage> = {
|
export type ModuleishCache<TModule, TPackage> = {
|
||||||
getPackage(
|
getPackage(
|
||||||
name: string,
|
name: string,
|
||||||
|
|
|
@ -18,27 +18,17 @@ const {InvalidPackageError} = require('metro-resolver');
|
||||||
const {PackageResolutionError} = require('metro-core');
|
const {PackageResolutionError} = require('metro-core');
|
||||||
|
|
||||||
import type DependencyGraphHelpers from './DependencyGraphHelpers';
|
import type DependencyGraphHelpers from './DependencyGraphHelpers';
|
||||||
import type {Options as TransformWorkerOptions} from '../../JSTransformer/worker';
|
|
||||||
import type {ReadResult, CachedReadResult} from '../Module';
|
|
||||||
import type {ModuleResolver} from './ModuleResolution';
|
import type {ModuleResolver} from './ModuleResolution';
|
||||||
|
|
||||||
export type Packageish = {
|
export type Packageish = {
|
||||||
isHaste(): boolean,
|
|
||||||
getName(): string,
|
|
||||||
path: string,
|
path: string,
|
||||||
redirectRequire(toModuleName: string): string | false,
|
redirectRequire(toModuleName: string): string | false,
|
||||||
getMain(): string,
|
getMain(): string,
|
||||||
+root: string,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Moduleish = {
|
export type Moduleish = {
|
||||||
+path: string,
|
+path: string,
|
||||||
isHaste(): boolean,
|
|
||||||
getName(): string,
|
|
||||||
getPackage(): ?Packageish,
|
getPackage(): ?Packageish,
|
||||||
hash(): string,
|
|
||||||
readCached(transformOptions: TransformWorkerOptions): CachedReadResult,
|
|
||||||
readFresh(transformOptions: TransformWorkerOptions): Promise<ReadResult>,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ModuleishCache<TModule, TPackage> = {
|
export type ModuleishCache<TModule, TPackage> = {
|
||||||
|
|
|
@ -22,15 +22,13 @@ import type ModuleCache from './ModuleCache';
|
||||||
import type {LocalPath} from './lib/toLocalPath';
|
import type {LocalPath} from './lib/toLocalPath';
|
||||||
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
|
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';
|
||||||
|
|
||||||
export type ReadResult = {
|
type ReadResult = {
|
||||||
+code: string,
|
+code: string,
|
||||||
+dependencies: $ReadOnlyArray<TransformResultDependency>,
|
+dependencies: $ReadOnlyArray<TransformResultDependency>,
|
||||||
+map: Array<MetroSourceMapSegmentTuple>,
|
+map: Array<MetroSourceMapSegmentTuple>,
|
||||||
+source: string,
|
+source: string,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CachedReadResult = ?ReadResult;
|
|
||||||
|
|
||||||
export type TransformCode = (
|
export type TransformCode = (
|
||||||
module: Module,
|
module: Module,
|
||||||
sourceCode: ?string,
|
sourceCode: ?string,
|
||||||
|
@ -47,7 +45,6 @@ export type ConstructorArgs = {
|
||||||
class Module {
|
class Module {
|
||||||
localPath: LocalPath;
|
localPath: LocalPath;
|
||||||
path: string;
|
path: string;
|
||||||
type: string;
|
|
||||||
|
|
||||||
_moduleCache: ModuleCache;
|
_moduleCache: ModuleCache;
|
||||||
_transformCode: TransformCode;
|
_transformCode: TransformCode;
|
||||||
|
@ -60,20 +57,11 @@ class Module {
|
||||||
|
|
||||||
this.localPath = localPath;
|
this.localPath = localPath;
|
||||||
this.path = file;
|
this.path = file;
|
||||||
this.type = 'Module';
|
|
||||||
|
|
||||||
this._moduleCache = moduleCache;
|
this._moduleCache = moduleCache;
|
||||||
this._transformCode = transformCode;
|
this._transformCode = transformCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
isHaste(): boolean {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
getName(): string {
|
|
||||||
return this.localPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
getPackage() {
|
getPackage() {
|
||||||
return this._moduleCache.getPackageForModule(this);
|
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() {
|
isAsset() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,15 +23,13 @@ type PackageContent = {
|
||||||
|
|
||||||
class Package {
|
class Package {
|
||||||
path: string;
|
path: string;
|
||||||
root: string;
|
|
||||||
type: string;
|
|
||||||
|
|
||||||
|
_root: string;
|
||||||
_content: ?PackageContent;
|
_content: ?PackageContent;
|
||||||
|
|
||||||
constructor({file}: {file: string}) {
|
constructor({file}: {file: string}) {
|
||||||
this.path = path.resolve(file);
|
this.path = path.resolve(file);
|
||||||
this.root = path.dirname(this.path);
|
this._root = path.dirname(this.path);
|
||||||
this.type = 'Package';
|
|
||||||
this._content = null;
|
this._content = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,15 +73,7 @@ class Package {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
|
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
|
||||||
return path.join(this.root, main);
|
return path.join(this._root, main);
|
||||||
}
|
|
||||||
|
|
||||||
isHaste(): boolean {
|
|
||||||
return !!this.read().name;
|
|
||||||
}
|
|
||||||
|
|
||||||
getName(): string {
|
|
||||||
return this.read().name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
invalidate() {
|
invalidate() {
|
||||||
|
@ -107,7 +97,7 @@ class Package {
|
||||||
replacement || name;
|
replacement || name;
|
||||||
}
|
}
|
||||||
|
|
||||||
let relPath = './' + path.relative(this.root, name);
|
let relPath = './' + path.relative(this._root, name);
|
||||||
if (path.sep !== '/') {
|
if (path.sep !== '/') {
|
||||||
relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/');
|
relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/');
|
||||||
}
|
}
|
||||||
|
@ -129,7 +119,7 @@ class Package {
|
||||||
|
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
return path.join(
|
return path.join(
|
||||||
this.root,
|
this._root,
|
||||||
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
|
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
|
||||||
redirect,
|
redirect,
|
||||||
);
|
);
|
||||||
|
|
|
@ -45,28 +45,11 @@ describe('Module', () => {
|
||||||
it('Returns the correct values for many properties and methods', () => {
|
it('Returns the correct values for many properties and methods', () => {
|
||||||
expect(module.localPath).toBe('file.js');
|
expect(module.localPath).toBe('file.js');
|
||||||
expect(module.path).toBe('/root/to/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.isAsset()).toBe(false);
|
||||||
expect(module.isPolyfill()).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 () => {
|
it('returns the result from the transform code straight away', async () => {
|
||||||
fs.readFileSync.mockReturnValue('original code');
|
fs.readFileSync.mockReturnValue('original code');
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue