From ed3aaadc39bcb5b1981d0e379bb32b7f7e0eb960 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Fri, 17 Apr 2015 15:20:52 -0700 Subject: [PATCH] [react-packager] Add more information to deprecated asset requires --- .../__tests__/DependencyGraph-test.js | 7 ++-- .../haste/DependencyGraph/index.js | 1 + .../src/Packager/__tests__/Packager-test.js | 15 +++++++-- react-packager/src/Packager/index.js | 33 ++++++++++++------- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js b/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js index 98ae7eb7..d3f6ce28 100644 --- a/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js +++ b/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js @@ -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, }, ]); }); diff --git a/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js b/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js index dba6265a..66534597 100644 --- a/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js +++ b/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js @@ -642,6 +642,7 @@ DependecyGraph.prototype._processAsset_DEPRECATED = function(file) { path: path.resolve(file), isAsset_DEPRECATED: true, dependencies: [], + resolution: extractAssetResolution(file).resolution, }); } }; diff --git a/react-packager/src/Packager/__tests__/Packager-test.js b/react-packager/src/Packager/__tests__/Packager-test.js index 0c9d4a84..333e0f56 100644 --- a/react-packager/src/Packager/__tests__/Packager-test.js +++ b/react-packager/src/Packager/__tests__/Packager-test.js @@ -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' ]); diff --git a/react-packager/src/Packager/index.js b/react-packager/src/Packager/index.js index 74e2ff4c..2b1eb6b1 100644 --- a/react-packager/src/Packager/index.js +++ b/react-packager/src/Packager/index.js @@ -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 = {