Skip to content

Commit c76fb40

Browse files
committed
[ReactNative] Register assets with AssetRegistry
1 parent 736d860 commit c76fb40

File tree

5 files changed

+66
-53
lines changed

5 files changed

+66
-53
lines changed

Libraries/Image/AssetRegistry.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2004-present Facebook. All Rights Reserved.
3+
*
4+
* @providesModule AssetRegistry
5+
*/
6+
'use strict';
7+
8+
var assets = [];
9+
10+
function registerAsset(asset) {
11+
// `push` returns new array length, so the first asset will
12+
// get id 1 (not 0) to make the value truthy
13+
return assets.push(asset);
14+
}
15+
16+
function getAssetByID(assetId) {
17+
return assets[assetId - 1];
18+
}
19+
20+
module.exports = { registerAsset, getAssetByID };

Libraries/Image/__tests__/resolveAssetSource-test.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@
88
*/
99
'use strict';
1010

11-
jest.dontMock('../resolveAssetSource');
11+
jest
12+
.dontMock('AssetRegistry')
13+
.dontMock('../resolveAssetSource');
1214

1315
var resolveAssetSource;
1416
var SourceCode;
17+
var AssetRegistry;
1518

1619
function expectResolvesAsset(input, expectedSource) {
17-
expect(resolveAssetSource(input)).toEqual(expectedSource);
20+
var assetId = AssetRegistry.registerAsset(input);
21+
expect(resolveAssetSource(assetId)).toEqual(expectedSource);
1822
}
1923

2024
describe('resolveAssetSource', () => {
2125
beforeEach(() => {
2226
jest.resetModuleRegistry();
2327
SourceCode = require('NativeModules').SourceCode;
28+
AssetRegistry = require('AssetRegistry');
2429
resolveAssetSource = require('../resolveAssetSource');
2530
});
2631

@@ -32,6 +37,22 @@ describe('resolveAssetSource', () => {
3237
expect(resolveAssetSource(source2)).toBe(source2);
3338
});
3439

40+
it('does not change deprecated assets', () => {
41+
expect(resolveAssetSource({
42+
isStatic: true,
43+
deprecated: true,
44+
width: 100,
45+
height: 200,
46+
uri: 'logo',
47+
})).toEqual({
48+
isStatic: true,
49+
deprecated: true,
50+
width: 100,
51+
height: 200,
52+
uri: 'logo',
53+
});
54+
});
55+
3556
it('ignores any weird data', () => {
3657
expect(resolveAssetSource(null)).toBe(null);
3758
expect(resolveAssetSource(42)).toBe(null);
@@ -81,25 +102,6 @@ describe('resolveAssetSource', () => {
81102
});
82103
});
83104

84-
it('does not change deprecated assets', () => {
85-
expectResolvesAsset({
86-
__packager_asset: true,
87-
deprecated: true,
88-
fileSystemLocation: '/root/app/module/a',
89-
httpServerLocation: '/assets/module/a',
90-
width: 100,
91-
height: 200,
92-
scales: [1],
93-
hash: '5b6f00f',
94-
name: 'logo',
95-
type: 'png',
96-
}, {
97-
isStatic: true,
98-
width: 100,
99-
height: 200,
100-
uri: 'logo',
101-
});
102-
});
103105
});
104106

105107
describe('bundle was loaded from file', () => {

Libraries/Image/resolveAssetSource.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
'use strict';
1212

13+
var AssetRegistry = require('AssetRegistry');
1314
var PixelRatio = require('PixelRatio');
1415
var SourceCode = require('NativeModules').SourceCode;
1516

@@ -44,58 +45,47 @@ function pickScale(scales, deviceScale) {
4445
}
4546

4647
function resolveAssetSource(source) {
47-
if (!source || typeof source !== 'object') {
48-
return null;
49-
}
50-
51-
if (!source.__packager_asset) {
48+
if (typeof source === 'object') {
5249
return source;
5350
}
5451

55-
// Deprecated assets are managed by Xcode for now,
56-
// just returning image name as `uri`
57-
// Examples:
58-
// require('image!deprecatd_logo_example')
59-
// require('./new-hotness-logo-example.png')
60-
if (source.deprecated) {
61-
return {
62-
width: source.width,
63-
height: source.height,
64-
isStatic: true,
65-
uri: source.name || source.uri, // TODO(frantic): remove uri
66-
};
52+
var asset = AssetRegistry.getAssetByID(source);
53+
if (asset) {
54+
return assetToImageSource(asset);
6755
}
6856

57+
return null;
58+
}
59+
60+
function assetToImageSource(asset) {
6961
// TODO(frantic): currently httpServerLocation is used both as
7062
// path in http URL and path within IPA. Should we have zipArchiveLocation?
71-
var path = source.httpServerLocation;
63+
var path = asset.httpServerLocation;
7264
if (path[0] === '/') {
7365
path = path.substr(1);
7466
}
7567

76-
var scale = pickScale(source.scales, PixelRatio.get());
68+
var scale = pickScale(asset.scales, PixelRatio.get());
7769
var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
7870

79-
var fileName = source.name + scaleSuffix + '.' + source.type;
71+
var fileName = asset.name + scaleSuffix + '.' + asset.type;
8072
var serverURL = getServerURL();
8173
if (serverURL) {
8274
return {
83-
width: source.width,
84-
height: source.height,
75+
width: asset.width,
76+
height: asset.height,
8577
uri: serverURL + path + '/' + fileName +
86-
'?hash=' + source.hash,
78+
'?hash=' + asset.hash,
8779
isStatic: false,
8880
};
8981
} else {
9082
return {
91-
width: source.width,
92-
height: source.height,
83+
width: asset.width,
84+
height: asset.height,
9385
uri: path + '/' + fileName,
9486
isStatic: true,
9587
};
9688
}
97-
98-
return source;
9989
}
10090

10191
module.exports = resolveAssetSource;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ describe('Packager', function() {
166166
};
167167

168168
expect(p.addModule.mock.calls[3][0]).toEqual({
169-
code: 'lol module.exports = ' +
169+
code: 'lol module.exports = require("AssetRegistry").registerAsset(' +
170170
JSON.stringify(imgModule) +
171-
'; lol',
172-
sourceCode: 'module.exports = ' +
171+
'); lol',
172+
sourceCode: 'module.exports = require("AssetRegistry").registerAsset(' +
173173
JSON.stringify(imgModule) +
174-
';',
174+
');',
175175
sourcePath: '/root/img/new_image.png'
176176
});
177177

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ Packager.prototype.generateAssetModule = function(ppackage, module) {
219219

220220
ppackage.addAsset(img);
221221

222-
var code = 'module.exports = ' + JSON.stringify(img) + ';';
222+
var ASSET_TEMPLATE = 'module.exports = require("AssetRegistry").registerAsset(%json);';
223+
var code = ASSET_TEMPLATE.replace('%json', JSON.stringify(img));
223224

224225
return new ModuleTransport({
225226
code: code,

0 commit comments

Comments
 (0)