Skip to content

Commit

Permalink
[react-packager] Add more information to deprecated asset requires
Browse files Browse the repository at this point in the history
  • Loading branch information
Amjad Masad committed Apr 17, 2015
1 parent dbe8e31 commit f117483
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ describe('DependencyGraph', function() {
{ id: 'image!a',
path: '/root/imgs/a.png',
dependencies: [],
isAsset_DEPRECATED: true
isAsset_DEPRECATED: true,
resolution: 1,
},
]);
});
Expand Down Expand Up @@ -288,7 +289,8 @@ describe('DependencyGraph', function() {
id: 'image!a',
path: '/root/imgs/a.png',
dependencies: [],
isAsset_DEPRECATED: true
isAsset_DEPRECATED: true,
resolution: 1,
},
]);
});
Expand Down Expand Up @@ -1350,6 +1352,7 @@ describe('DependencyGraph', function() {
path: '/root/foo.png',
dependencies: [],
isAsset_DEPRECATED: true,
resolution: 1,
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ DependecyGraph.prototype._processAsset_DEPRECATED = function(file) {
path: path.resolve(file),
isAsset_DEPRECATED: true,
dependencies: [],
resolution: extractAssetResolution(file).resolution,
});
}
};
Expand Down
15 changes: 13 additions & 2 deletions packager/react-packager/src/Packager/__tests__/Packager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('Packager', function() {
path: '/root/img/img.png',
isAsset_DEPRECATED: true,
dependencies: [],
resolution: 2,
},
{
id: 'new_image.png',
Expand Down Expand Up @@ -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'
]);
Expand Down
35 changes: 22 additions & 13 deletions packager/react-packager/src/Packager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -175,19 +177,26 @@ Packager.prototype.getGraphDebugInfo = function() {
};

function generateAssetModule_DEPRECATED(module) {
var code = 'module.exports = ' + JSON.stringify({
uri: module.id.replace(/^[^!]+!/, ''),
isStatic: true,
}) + ';';

return {
code: code,
sourceCode: code,
sourcePath: module.path,
};
}
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,
};

var sizeOf = Promise.promisify(imageSize);

var code = 'module.exports = ' + JSON.stringify(img) + ';';

return {
code: code,
sourceCode: code,
sourcePath: module.path,
};
});
}

function generateAssetModule(module, relPath) {
return sizeOf(module.path).then(function(dimensions) {
Expand Down

0 comments on commit f117483

Please sign in to comment.