[ReactNative] Register assets with AssetRegistry

This commit is contained in:
Alex Kotliarskyi 2015-05-07 17:30:41 -07:00
Родитель 736d860571
Коммит c76fb40ec4
5 изменённых файлов: 66 добавлений и 53 удалений

Просмотреть файл

@ -0,0 +1,20 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @providesModule AssetRegistry
*/
'use strict';
var assets = [];
function registerAsset(asset) {
// `push` returns new array length, so the first asset will
// get id 1 (not 0) to make the value truthy
return assets.push(asset);
}
function getAssetByID(assetId) {
return assets[assetId - 1];
}
module.exports = { registerAsset, getAssetByID };

Просмотреть файл

@ -8,19 +8,24 @@
*/ */
'use strict'; 'use strict';
jest.dontMock('../resolveAssetSource'); jest
.dontMock('AssetRegistry')
.dontMock('../resolveAssetSource');
var resolveAssetSource; var resolveAssetSource;
var SourceCode; var SourceCode;
var AssetRegistry;
function expectResolvesAsset(input, expectedSource) { function expectResolvesAsset(input, expectedSource) {
expect(resolveAssetSource(input)).toEqual(expectedSource); var assetId = AssetRegistry.registerAsset(input);
expect(resolveAssetSource(assetId)).toEqual(expectedSource);
} }
describe('resolveAssetSource', () => { describe('resolveAssetSource', () => {
beforeEach(() => { beforeEach(() => {
jest.resetModuleRegistry(); jest.resetModuleRegistry();
SourceCode = require('NativeModules').SourceCode; SourceCode = require('NativeModules').SourceCode;
AssetRegistry = require('AssetRegistry');
resolveAssetSource = require('../resolveAssetSource'); resolveAssetSource = require('../resolveAssetSource');
}); });
@ -32,6 +37,22 @@ describe('resolveAssetSource', () => {
expect(resolveAssetSource(source2)).toBe(source2); expect(resolveAssetSource(source2)).toBe(source2);
}); });
it('does not change deprecated assets', () => {
expect(resolveAssetSource({
isStatic: true,
deprecated: true,
width: 100,
height: 200,
uri: 'logo',
})).toEqual({
isStatic: true,
deprecated: true,
width: 100,
height: 200,
uri: 'logo',
});
});
it('ignores any weird data', () => { it('ignores any weird data', () => {
expect(resolveAssetSource(null)).toBe(null); expect(resolveAssetSource(null)).toBe(null);
expect(resolveAssetSource(42)).toBe(null); expect(resolveAssetSource(42)).toBe(null);
@ -81,25 +102,6 @@ describe('resolveAssetSource', () => {
}); });
}); });
it('does not change deprecated assets', () => {
expectResolvesAsset({
__packager_asset: true,
deprecated: true,
fileSystemLocation: '/root/app/module/a',
httpServerLocation: '/assets/module/a',
width: 100,
height: 200,
scales: [1],
hash: '5b6f00f',
name: 'logo',
type: 'png',
}, {
isStatic: true,
width: 100,
height: 200,
uri: 'logo',
});
});
}); });
describe('bundle was loaded from file', () => { describe('bundle was loaded from file', () => {

Просмотреть файл

@ -10,6 +10,7 @@
*/ */
'use strict'; 'use strict';
var AssetRegistry = require('AssetRegistry');
var PixelRatio = require('PixelRatio'); var PixelRatio = require('PixelRatio');
var SourceCode = require('NativeModules').SourceCode; var SourceCode = require('NativeModules').SourceCode;
@ -44,58 +45,47 @@ function pickScale(scales, deviceScale) {
} }
function resolveAssetSource(source) { function resolveAssetSource(source) {
if (!source || typeof source !== 'object') { if (typeof source === 'object') {
return null;
}
if (!source.__packager_asset) {
return source; return source;
} }
// Deprecated assets are managed by Xcode for now, var asset = AssetRegistry.getAssetByID(source);
// just returning image name as `uri` if (asset) {
// Examples: return assetToImageSource(asset);
// require('image!deprecatd_logo_example')
// require('./new-hotness-logo-example.png')
if (source.deprecated) {
return {
width: source.width,
height: source.height,
isStatic: true,
uri: source.name || source.uri, // TODO(frantic): remove uri
};
} }
return null;
}
function assetToImageSource(asset) {
// TODO(frantic): currently httpServerLocation is used both as // TODO(frantic): currently httpServerLocation is used both as
// path in http URL and path within IPA. Should we have zipArchiveLocation? // path in http URL and path within IPA. Should we have zipArchiveLocation?
var path = source.httpServerLocation; var path = asset.httpServerLocation;
if (path[0] === '/') { if (path[0] === '/') {
path = path.substr(1); path = path.substr(1);
} }
var scale = pickScale(source.scales, PixelRatio.get()); var scale = pickScale(asset.scales, PixelRatio.get());
var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
var fileName = source.name + scaleSuffix + '.' + source.type; var fileName = asset.name + scaleSuffix + '.' + asset.type;
var serverURL = getServerURL(); var serverURL = getServerURL();
if (serverURL) { if (serverURL) {
return { return {
width: source.width, width: asset.width,
height: source.height, height: asset.height,
uri: serverURL + path + '/' + fileName + uri: serverURL + path + '/' + fileName +
'?hash=' + source.hash, '?hash=' + asset.hash,
isStatic: false, isStatic: false,
}; };
} else { } else {
return { return {
width: source.width, width: asset.width,
height: source.height, height: asset.height,
uri: path + '/' + fileName, uri: path + '/' + fileName,
isStatic: true, isStatic: true,
}; };
} }
return source;
} }
module.exports = resolveAssetSource; module.exports = resolveAssetSource;

Просмотреть файл

@ -166,12 +166,12 @@ describe('Packager', function() {
}; };
expect(p.addModule.mock.calls[3][0]).toEqual({ expect(p.addModule.mock.calls[3][0]).toEqual({
code: 'lol module.exports = ' + code: 'lol module.exports = require("AssetRegistry").registerAsset(' +
JSON.stringify(imgModule) + JSON.stringify(imgModule) +
'; lol', '); lol',
sourceCode: 'module.exports = ' + sourceCode: 'module.exports = require("AssetRegistry").registerAsset(' +
JSON.stringify(imgModule) + JSON.stringify(imgModule) +
';', ');',
sourcePath: '/root/img/new_image.png' sourcePath: '/root/img/new_image.png'
}); });

Просмотреть файл

@ -219,7 +219,8 @@ Packager.prototype.generateAssetModule = function(ppackage, module) {
ppackage.addAsset(img); ppackage.addAsset(img);
var code = 'module.exports = ' + JSON.stringify(img) + ';'; var ASSET_TEMPLATE = 'module.exports = require("AssetRegistry").registerAsset(%json);';
var code = ASSET_TEMPLATE.replace('%json', JSON.stringify(img));
return new ModuleTransport({ return new ModuleTransport({
code: code, code: code,