[react-packager] Implement Packager::getAssets

This commit is contained in:
Amjad Masad 2015-04-20 15:54:24 -07:00
parent 10cd77b533
commit 201d65dead
4 changed files with 48 additions and 16 deletions

View File

@ -17,6 +17,7 @@ module.exports = Package;
function Package(sourceMapUrl) {
this._finalized = false;
this._modules = [];
this._assets = [];
this._sourceMapUrl = sourceMapUrl;
}
@ -36,6 +37,10 @@ Package.prototype.addModule = function(
});
};
Package.prototype.addAsset = function(asset) {
this._assets.push(asset);
};
Package.prototype.finalize = function(options) {
options = options || {};
if (options.runMainModule) {
@ -49,6 +54,8 @@ Package.prototype.finalize = function(options) {
Object.freeze(this._modules);
Object.seal(this._modules);
Object.freeze(this._assets);
Object.seal(this._assets);
this._finalized = true;
};
@ -146,6 +153,10 @@ Package.prototype.getSourceMap = function(options) {
return map;
};
Package.prototype.getAssets = function() {
return this._assets;
};
Package.prototype._getMappings = function() {
var modules = this._modules;

View File

@ -76,6 +76,18 @@ describe('Package', function() {
expect(s).toEqual(genSourceMap(p._modules));
});
});
describe('getAssets()', function() {
it('should save and return asset objects', function() {
var p = new Package('test_url');
var asset1 = {};
var asset2 = {};
p.addAsset(asset1);
p.addAsset(asset2);
p.finalize();
expect(p.getAssets()).toEqual([asset1, asset2]);
});
});
});
function genSourceMap(modules) {

View File

@ -140,6 +140,14 @@ describe('Packager', function() {
expect(p.finalize.mock.calls[0]).toEqual([
{runMainModule: true}
]);
expect(p.addAsset.mock.calls[0]).toEqual([
imgModule_DEPRECATED
]);
expect(p.addAsset.mock.calls[1]).toEqual([
imgModule
]);
});
});

View File

@ -100,9 +100,9 @@ Packager.prototype.kill = function() {
};
Packager.prototype.package = function(main, runModule, sourceMapUrl, isDev) {
var transformModule = this._transformModule.bind(this);
var ppackage = new Package(sourceMapUrl);
var transformModule = this._transformModule.bind(this, ppackage);
var findEventId = Activity.startEvent('find dependencies');
var transformEventId;
@ -140,16 +140,13 @@ Packager.prototype.getDependencies = function(main, isDev) {
return this._resolver.getDependencies(main, { dev: isDev });
};
Packager.prototype._transformModule = function(module) {
Packager.prototype._transformModule = function(ppackage, module) {
var transform;
if (module.isAsset_DEPRECATED) {
transform = generateAssetModule_DEPRECATED(module);
transform = this.generateAssetModule_DEPRECATED(ppackage, module);
} else if (module.isAsset) {
transform = generateAssetModule(
module,
getPathRelativeToRoot(this._projectRoots, module.path)
);
transform = this.generateAssetModule(ppackage, module);
} else {
transform = this._transformer.loadFileAndTransform(
path.resolve(module.path)
@ -166,17 +163,11 @@ Packager.prototype._transformModule = function(module) {
});
};
function verifyRootExists(root) {
// Verify that the root exists.
assert(fs.statSync(root).isDirectory(), 'Root has to be a valid directory');
}
Packager.prototype.getGraphDebugInfo = function() {
return this._resolver.getDebugInfo();
};
function generateAssetModule_DEPRECATED(module) {
Packager.prototype.generateAssetModule_DEPRECATED = function(ppackage, module) {
return sizeOf(module.path).then(function(dimensions) {
var img = {
isStatic: true,
@ -187,6 +178,7 @@ function generateAssetModule_DEPRECATED(module) {
deprecated: true,
};
ppackage.addAsset(img);
var code = 'module.exports = ' + JSON.stringify(img) + ';';
@ -196,9 +188,11 @@ function generateAssetModule_DEPRECATED(module) {
sourcePath: module.path,
};
});
}
};
Packager.prototype.generateAssetModule = function(ppackage, module) {
var relPath = getPathRelativeToRoot(this._projectRoots, module.path);
function generateAssetModule(module, relPath) {
return sizeOf(module.path).then(function(dimensions) {
var img = {
isStatic: true,
@ -208,6 +202,8 @@ function generateAssetModule(module, relPath) {
height: dimensions.height / module.resolution,
};
ppackage.addAsset(img);
var code = 'module.exports = ' + JSON.stringify(img) + ';';
return {
@ -231,4 +227,9 @@ function getPathRelativeToRoot(roots, absPath) {
);
}
function verifyRootExists(root) {
// Verify that the root exists.
assert(fs.statSync(root).isDirectory(), 'Root has to be a valid directory');
}
module.exports = Packager;