Skip to content

Commit 5bbb351

Browse files
author
Amjad Masad
committed
[react-packager] Support @nx resolution postfix for assets
1 parent 62d7cd6 commit 5bbb351

5 files changed

Lines changed: 119 additions & 11 deletions

File tree

packager/react-packager/src/DependencyResolver/ModuleDescriptor.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ function ModuleDescriptor(fields) {
3737
throw new Error('Cannot be an asset and a deprecated asset');
3838
}
3939

40+
this.resolution = fields.resolution;
41+
42+
if (this.isAsset && isNaN(this.resolution)) {
43+
throw new Error('Expected resolution to be a number for asset modules');
44+
}
45+
4046
this.altId = fields.altId;
4147

4248
this._fields = fields;

packager/react-packager/src/DependencyResolver/haste/DependencyGraph/__tests__/DependencyGraph-test.js

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,74 @@ describe('DependencyGraph', function() {
169169
{ id: 'rootPackage/imgs/a.png',
170170
path: '/root/imgs/a.png',
171171
dependencies: [],
172-
isAsset: true
172+
isAsset: true,
173+
resolution: 1,
174+
},
175+
]);
176+
});
177+
});
178+
179+
pit('should get dependencies with assets and resolution', function() {
180+
var root = '/root';
181+
fs.__setMockFilesystem({
182+
'root': {
183+
'index.js': [
184+
'/**',
185+
' * @providesModule index',
186+
' */',
187+
'require("./imgs/a.png");',
188+
'require("./imgs/b.png");',
189+
'require("./imgs/c.png");',
190+
].join('\n'),
191+
'imgs': {
192+
'a@1.5x.png': '',
193+
'b@.7x.png': '',
194+
'c.png': '',
195+
'c@2x.png': '',
196+
},
197+
'package.json': JSON.stringify({
198+
name: 'rootPackage'
199+
}),
200+
}
201+
});
202+
203+
var dgraph = new DependencyGraph({
204+
roots: [root],
205+
fileWatcher: fileWatcher,
206+
});
207+
return dgraph.load().then(function() {
208+
expect(dgraph.getOrderedDependencies('/root/index.js'))
209+
.toEqual([
210+
{
211+
id: 'index',
212+
altId: 'rootPackage/index',
213+
path: '/root/index.js',
214+
dependencies: [
215+
'./imgs/a.png',
216+
'./imgs/b.png',
217+
'./imgs/c.png',
218+
]
219+
},
220+
{
221+
id: 'rootPackage/imgs/a.png',
222+
path: '/root/imgs/a@1.5x.png',
223+
resolution: 1.5,
224+
dependencies: [],
225+
isAsset: true,
226+
},
227+
{
228+
id: 'rootPackage/imgs/b.png',
229+
path: '/root/imgs/b@.7x.png',
230+
resolution: 0.7,
231+
dependencies: [],
232+
isAsset: true
233+
},
234+
{
235+
id: 'rootPackage/imgs/c.png',
236+
path: '/root/imgs/c.png',
237+
resolution: 1,
238+
dependencies: [],
239+
isAsset: true
173240
},
174241
]);
175242
});
@@ -213,7 +280,8 @@ describe('DependencyGraph', function() {
213280
id: 'rootPackage/imgs/a.png',
214281
path: '/root/imgs/a.png',
215282
dependencies: [],
216-
isAsset: true
283+
isAsset: true,
284+
resolution: 1,
217285
},
218286
{
219287
id: 'image!a',
@@ -1332,6 +1400,7 @@ describe('DependencyGraph', function() {
13321400
path: '/root/foo.png',
13331401
dependencies: [],
13341402
isAsset: true,
1403+
resolution: 1,
13351404
},
13361405
]);
13371406
});

packager/react-packager/src/DependencyResolver/haste/DependencyGraph/index.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,22 @@ DependecyGraph.prototype.resolveDependency = function(
258258
}
259259

260260
// JS modules can be required without extensios.
261-
if (this._assetExts.indexOf(extname(modulePath)) === -1) {
261+
if (!this._isFileAsset(modulePath)) {
262262
modulePath = withExtJs(modulePath);
263263
}
264264

265265
dep = this._graph[modulePath];
266266

267267
// Maybe the dependency is a directory and there is an index.js inside it.
268268
if (dep == null) {
269-
modulePath = path.join(dir, depModuleId, 'index.js');
269+
dep = this._graph[path.join(dir, depModuleId, 'index.js')];
270270
}
271271

272-
dep = this._graph[modulePath];
272+
// Maybe it's an asset with @n.nx resolution and the path doesn't map
273+
// to the id
274+
if (dep == null && this._isFileAsset(modulePath)) {
275+
dep = this._moduleById[this._lookupName(modulePath)];
276+
}
273277

274278
if (dep == null) {
275279
debug(
@@ -417,11 +421,14 @@ DependecyGraph.prototype._processModule = function(modulePath) {
417421
var module;
418422

419423
if (this._assetExts.indexOf(extname(modulePath)) > -1) {
420-
moduleData.id = this._lookupName(modulePath);
424+
var assetData = extractResolutionPostfix(this._lookupName(modulePath));
425+
moduleData.id = assetData.assetName;
426+
moduleData.resolution = assetData.resolution;
421427
moduleData.isAsset = true;
422428
moduleData.dependencies = [];
423-
module = Promise.resolve(new ModuleDescriptor(moduleData));
429+
module = new ModuleDescriptor(moduleData);
424430
this._updateGraphWithModule(module);
431+
return Promise.resolve(module);
425432
}
426433

427434
var self = this;
@@ -652,6 +659,10 @@ DependecyGraph.prototype._processAssetChange_DEPRECATED = function(eventType, fi
652659
}
653660
};
654661

662+
DependecyGraph.prototype._isFileAsset = function(file) {
663+
return this._assetExts.indexOf(extname(file)) !== -1;
664+
};
665+
655666
/**
656667
* Extract all required modules from a `code` string.
657668
*/
@@ -761,6 +772,27 @@ function extname(name) {
761772
return path.extname(name).replace(/^\./, '');
762773
}
763774

775+
function extractResolutionPostfix(filename) {
776+
var ext = extname(filename);
777+
var re = new RegExp('@([\\d\\.]+)x\\.' + ext + '$');
778+
779+
var match = filename.match(re);
780+
var resolution;
781+
782+
if (!(match && match[1])) {
783+
resolution = 1;
784+
} else {
785+
resolution = parseFloat(match[1], 10);
786+
if (isNaN(resolution)) {
787+
resolution = 1;
788+
}
789+
}
790+
791+
return {
792+
resolution: resolution,
793+
assetName: match ? filename.replace(re, '.' + ext) : filename,
794+
};
795+
}
764796

765797
function NotFoundError() {
766798
Error.call(this);

packager/react-packager/src/Packager/__tests__/Packager-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('Packager', function() {
5757
id: 'new_image.png',
5858
path: '/root/img/new_image.png',
5959
isAsset: true,
60+
resolution: 2,
6061
dependencies: []
6162
}
6263
];
@@ -111,8 +112,8 @@ describe('Packager', function() {
111112
isStatic: true,
112113
path: '/root/img/new_image.png',
113114
uri: 'img/new_image.png',
114-
width: 50,
115-
height: 100,
115+
width: 25,
116+
height: 50,
116117
};
117118

118119
expect(p.addModule.mock.calls[3]).toEqual([

packager/react-packager/src/Packager/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ function generateAssetModule(module, relPath) {
195195
isStatic: true,
196196
path: module.path, //TODO(amasad): this should be path inside tar file.
197197
uri: relPath,
198-
width: dimensions.width,
199-
height: dimensions.height,
198+
width: dimensions.width / module.resolution,
199+
height: dimensions.height / module.resolution,
200200
};
201201

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

0 commit comments

Comments
 (0)