Simplify AssetModule logic

Reviewed By: jeanlauliac

Differential Revision: D6438714

fbshipit-source-id: ec1cfe0d439b1dcb07a0861872df5318f468396a
This commit is contained in:
Rafael Oleza 2017-12-04 16:36:45 -08:00 committed by Facebook Github Bot
parent fbdbd7ae2c
commit f3ea76a0b4
4 changed files with 50 additions and 264 deletions

View File

@ -12,29 +12,22 @@
'use strict';
const AssetPaths = require('./lib/AssetPaths');
const Module = require('./Module');
import type {CachedReadResult, ConstructorArgs, ReadResult} from './Module';
class AssetModule extends Module {
resolution: mixed;
_name: string;
_type: string;
_dependencies: Array<string>;
constructor(
args: ConstructorArgs & {dependencies: Array<string>},
platforms: Set<string>,
) {
constructor(args: ConstructorArgs & {dependencies: Array<string>}) {
super(args);
const {resolution, name, type} = AssetPaths.parse(this.path, platforms);
this.resolution = resolution;
this._name = name;
this._type = type;
this._dependencies = args.dependencies || [];
}
getPackage() {
return null;
}
isHaste() {
return false;
}
@ -53,10 +46,6 @@ class AssetModule extends Module {
return Promise.resolve({dependencies: this._dependencies});
}
getName() {
return super.getName().replace(/\/[^\/]+$/, `/${this._name}.${this._type}`);
}
hash() {
return `AssetModule : ${this.path}`;
}

View File

@ -110,22 +110,18 @@ class ModuleCache {
* this is an incorrect OOP design in the first place: AssetModule, being
* simpler than a normal Module, should not inherit the Module class.
*/
this._moduleCache[filePath] = new AssetModule(
{
dependencies: this._assetDependencies,
this._moduleCache[filePath] = new AssetModule({
depGraphHelpers: this._depGraphHelpers,
dependencies: this._assetDependencies,
file: filePath,
getTransformCacheKey: this._getTransformCacheKey,
globalTransformCache: null,
globalTransformCache: this._globalTransformCache,
localPath: toLocalPath(this._roots, filePath),
moduleCache: this,
reporter: this._reporter,
resetCache: this._opts.resetCache,
transformCode: this._transformCode,
options: this._getModuleOptions(),
},
this._platforms,
);
reporter: this._reporter,
transformCode: this._transformCode,
});
}
return this._moduleCache[filePath];
}

View File

@ -17,16 +17,7 @@ const AssetModule = require('../AssetModule');
describe('AssetModule:', () => {
const defaults = {file: '/arbitrary.png'};
it('has no dependencies by default', () => {
return new AssetModule(defaults)
.getDependencies()
.then(deps => expect(deps).toEqual([]));
});
it('can be parametrized with dependencies', () => {
const dependencies = ['arbitrary', 'dependencies'];
return new AssetModule({...defaults, dependencies})
.getDependencies()
.then(deps => expect(deps).toEqual(dependencies));
it('is an asset', () => {
expect(new AssetModule(defaults).isAsset()).toBe(true);
});
});