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

View File

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

View File

@ -17,16 +17,7 @@ const AssetModule = require('../AssetModule');
describe('AssetModule:', () => { describe('AssetModule:', () => {
const defaults = {file: '/arbitrary.png'}; const defaults = {file: '/arbitrary.png'};
it('has no dependencies by default', () => { it('is an asset', () => {
return new AssetModule(defaults) expect(new AssetModule(defaults).isAsset()).toBe(true);
.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));
}); });
}); });