[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 a68cc06f0b
commit ed3aaadc39
4 changed files with 40 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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