[react-packager] Add more information to deprecated asset requires

This commit is contained in:
Amjad Masad 2015-04-17 15:20:52 -07:00
parent dbe8e31c20
commit f1174836d7
4 changed files with 40 additions and 16 deletions

View File

@ -129,7 +129,8 @@ describe('DependencyGraph', function() {
{ id: 'image!a',
path: '/root/imgs/a.png',
dependencies: [],
isAsset_DEPRECATED: true
isAsset_DEPRECATED: true,
resolution: 1,
},
]);
});
@ -288,7 +289,8 @@ describe('DependencyGraph', function() {
id: 'image!a',
path: '/root/imgs/a.png',
dependencies: [],
isAsset_DEPRECATED: true
isAsset_DEPRECATED: true,
resolution: 1,
},
]);
});
@ -1350,6 +1352,7 @@ describe('DependencyGraph', function() {
path: '/root/foo.png',
dependencies: [],
isAsset_DEPRECATED: true,
resolution: 1,
},
]);
});

View File

@ -642,6 +642,7 @@ DependecyGraph.prototype._processAsset_DEPRECATED = function(file) {
path: path.resolve(file),
isAsset_DEPRECATED: true,
dependencies: [],
resolution: extractAssetResolution(file).resolution,
});
}
};

View File

@ -52,6 +52,7 @@ describe('Packager', function() {
path: '/root/img/img.png',
isAsset_DEPRECATED: true,
dependencies: [],
resolution: 2,
},
{
id: 'new_image.png',
@ -98,12 +99,22 @@ describe('Packager', function() {
'source /root/bar.js',
'/root/bar.js'
]);
var imgModule_DEPRECATED = {
isStatic: true,
path: '/root/img/img.png',
uri: 'img',
width: 25,
height: 50,
deprecated: true,
};
expect(p.addModule.mock.calls[2]).toEqual([
'lol module.exports = ' +
JSON.stringify({ uri: 'img', isStatic: true}) +
JSON.stringify(imgModule_DEPRECATED) +
'; lol',
'module.exports = ' +
JSON.stringify({ uri: 'img', isStatic: true}) +
JSON.stringify(imgModule_DEPRECATED) +
';',
'/root/img/img.png'
]);

View File

@ -20,6 +20,8 @@ var Activity = require('../Activity');
var declareOpts = require('../lib/declareOpts');
var imageSize = require('image-size');
var sizeOf = Promise.promisify(imageSize);
var validateOpts = declareOpts({
projectRoots: {
type: 'array',
@ -142,7 +144,7 @@ Packager.prototype._transformModule = function(module) {
var transform;
if (module.isAsset_DEPRECATED) {
transform = Promise.resolve(generateAssetModule_DEPRECATED(module));
transform = generateAssetModule_DEPRECATED(module);
} else if (module.isAsset) {
transform = generateAssetModule(
module,
@ -175,20 +177,27 @@ Packager.prototype.getGraphDebugInfo = function() {
};
function generateAssetModule_DEPRECATED(module) {
var code = 'module.exports = ' + JSON.stringify({
uri: module.id.replace(/^[^!]+!/, ''),
isStatic: true,
}) + ';';
return sizeOf(module.path).then(function(dimensions) {
var img = {
isStatic: true,
path: module.path,
uri: module.id.replace(/^[^!]+!/, ''),
width: dimensions.width / module.resolution,
height: dimensions.height / module.resolution,
deprecated: true,
};
return {
code: code,
sourceCode: code,
sourcePath: module.path,
};
var code = 'module.exports = ' + JSON.stringify(img) + ';';
return {
code: code,
sourceCode: code,
sourcePath: module.path,
};
});
}
var sizeOf = Promise.promisify(imageSize);
function generateAssetModule(module, relPath) {
return sizeOf(module.path).then(function(dimensions) {
var img = {