[react-packager] Support @nx resolution postfix for assets

This commit is contained in:
Amjad Masad 2015-04-14 10:46:41 -07:00
parent 241e263e5e
commit 4a3e69ec74
5 changed files with 119 additions and 11 deletions

View File

@ -37,6 +37,12 @@ function ModuleDescriptor(fields) {
throw new Error('Cannot be an asset and a deprecated asset'); throw new Error('Cannot be an asset and a deprecated asset');
} }
this.resolution = fields.resolution;
if (this.isAsset && isNaN(this.resolution)) {
throw new Error('Expected resolution to be a number for asset modules');
}
this.altId = fields.altId; this.altId = fields.altId;
this._fields = fields; this._fields = fields;

View File

@ -169,7 +169,74 @@ describe('DependencyGraph', function() {
{ id: 'rootPackage/imgs/a.png', { id: 'rootPackage/imgs/a.png',
path: '/root/imgs/a.png', path: '/root/imgs/a.png',
dependencies: [], dependencies: [],
isAsset: true isAsset: true,
resolution: 1,
},
]);
});
});
pit('should get dependencies with assets and resolution', function() {
var root = '/root';
fs.__setMockFilesystem({
'root': {
'index.js': [
'/**',
' * @providesModule index',
' */',
'require("./imgs/a.png");',
'require("./imgs/b.png");',
'require("./imgs/c.png");',
].join('\n'),
'imgs': {
'a@1.5x.png': '',
'b@.7x.png': '',
'c.png': '',
'c@2x.png': '',
},
'package.json': JSON.stringify({
name: 'rootPackage'
}),
}
});
var dgraph = new DependencyGraph({
roots: [root],
fileWatcher: fileWatcher,
});
return dgraph.load().then(function() {
expect(dgraph.getOrderedDependencies('/root/index.js'))
.toEqual([
{
id: 'index',
altId: 'rootPackage/index',
path: '/root/index.js',
dependencies: [
'./imgs/a.png',
'./imgs/b.png',
'./imgs/c.png',
]
},
{
id: 'rootPackage/imgs/a.png',
path: '/root/imgs/a@1.5x.png',
resolution: 1.5,
dependencies: [],
isAsset: true,
},
{
id: 'rootPackage/imgs/b.png',
path: '/root/imgs/b@.7x.png',
resolution: 0.7,
dependencies: [],
isAsset: true
},
{
id: 'rootPackage/imgs/c.png',
path: '/root/imgs/c.png',
resolution: 1,
dependencies: [],
isAsset: true
}, },
]); ]);
}); });
@ -213,7 +280,8 @@ describe('DependencyGraph', function() {
id: 'rootPackage/imgs/a.png', id: 'rootPackage/imgs/a.png',
path: '/root/imgs/a.png', path: '/root/imgs/a.png',
dependencies: [], dependencies: [],
isAsset: true isAsset: true,
resolution: 1,
}, },
{ {
id: 'image!a', id: 'image!a',
@ -1332,6 +1400,7 @@ describe('DependencyGraph', function() {
path: '/root/foo.png', path: '/root/foo.png',
dependencies: [], dependencies: [],
isAsset: true, isAsset: true,
resolution: 1,
}, },
]); ]);
}); });

View File

@ -258,7 +258,7 @@ DependecyGraph.prototype.resolveDependency = function(
} }
// JS modules can be required without extensios. // JS modules can be required without extensios.
if (this._assetExts.indexOf(extname(modulePath)) === -1) { if (!this._isFileAsset(modulePath)) {
modulePath = withExtJs(modulePath); modulePath = withExtJs(modulePath);
} }
@ -266,10 +266,14 @@ DependecyGraph.prototype.resolveDependency = function(
// Maybe the dependency is a directory and there is an index.js inside it. // Maybe the dependency is a directory and there is an index.js inside it.
if (dep == null) { if (dep == null) {
modulePath = path.join(dir, depModuleId, 'index.js'); dep = this._graph[path.join(dir, depModuleId, 'index.js')];
} }
dep = this._graph[modulePath]; // Maybe it's an asset with @n.nx resolution and the path doesn't map
// to the id
if (dep == null && this._isFileAsset(modulePath)) {
dep = this._moduleById[this._lookupName(modulePath)];
}
if (dep == null) { if (dep == null) {
debug( debug(
@ -417,11 +421,14 @@ DependecyGraph.prototype._processModule = function(modulePath) {
var module; var module;
if (this._assetExts.indexOf(extname(modulePath)) > -1) { if (this._assetExts.indexOf(extname(modulePath)) > -1) {
moduleData.id = this._lookupName(modulePath); var assetData = extractResolutionPostfix(this._lookupName(modulePath));
moduleData.id = assetData.assetName;
moduleData.resolution = assetData.resolution;
moduleData.isAsset = true; moduleData.isAsset = true;
moduleData.dependencies = []; moduleData.dependencies = [];
module = Promise.resolve(new ModuleDescriptor(moduleData)); module = new ModuleDescriptor(moduleData);
this._updateGraphWithModule(module); this._updateGraphWithModule(module);
return Promise.resolve(module);
} }
var self = this; var self = this;
@ -652,6 +659,10 @@ DependecyGraph.prototype._processAssetChange_DEPRECATED = function(eventType, fi
} }
}; };
DependecyGraph.prototype._isFileAsset = function(file) {
return this._assetExts.indexOf(extname(file)) !== -1;
};
/** /**
* Extract all required modules from a `code` string. * Extract all required modules from a `code` string.
*/ */
@ -761,6 +772,27 @@ function extname(name) {
return path.extname(name).replace(/^\./, ''); return path.extname(name).replace(/^\./, '');
} }
function extractResolutionPostfix(filename) {
var ext = extname(filename);
var re = new RegExp('@([\\d\\.]+)x\\.' + ext + '$');
var match = filename.match(re);
var resolution;
if (!(match && match[1])) {
resolution = 1;
} else {
resolution = parseFloat(match[1], 10);
if (isNaN(resolution)) {
resolution = 1;
}
}
return {
resolution: resolution,
assetName: match ? filename.replace(re, '.' + ext) : filename,
};
}
function NotFoundError() { function NotFoundError() {
Error.call(this); Error.call(this);

View File

@ -57,6 +57,7 @@ describe('Packager', function() {
id: 'new_image.png', id: 'new_image.png',
path: '/root/img/new_image.png', path: '/root/img/new_image.png',
isAsset: true, isAsset: true,
resolution: 2,
dependencies: [] dependencies: []
} }
]; ];
@ -111,8 +112,8 @@ describe('Packager', function() {
isStatic: true, isStatic: true,
path: '/root/img/new_image.png', path: '/root/img/new_image.png',
uri: 'img/new_image.png', uri: 'img/new_image.png',
width: 50, width: 25,
height: 100, height: 50,
}; };
expect(p.addModule.mock.calls[3]).toEqual([ expect(p.addModule.mock.calls[3]).toEqual([

View File

@ -195,8 +195,8 @@ function generateAssetModule(module, relPath) {
isStatic: true, isStatic: true,
path: module.path, //TODO(amasad): this should be path inside tar file. path: module.path, //TODO(amasad): this should be path inside tar file.
uri: relPath, uri: relPath,
width: dimensions.width, width: dimensions.width / module.resolution,
height: dimensions.height, height: dimensions.height / module.resolution,
}; };
var code = 'module.exports = ' + JSON.stringify(img) + ';'; var code = 'module.exports = ' + JSON.stringify(img) + ';';