Skip to content

Commit

Permalink
Add @react-native/assets package for Asset Registry related code
Browse files Browse the repository at this point in the history
Summary: Changelog: [Internal]

Reviewed By: GijsWeterings

Differential Revision: D22628865

fbshipit-source-id: 66ff311b2dab4e0c42f906d5a2642997b1570ae4
  • Loading branch information
cpojer authored and facebook-github-bot committed Jul 23, 2020
1 parent 91d16bb commit 63377fa
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 42 deletions.
27 changes: 1 addition & 26 deletions Libraries/Image/AssetRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,4 @@

'use strict';

export type PackagerAsset = {
+__packager_asset: boolean,
+fileSystemLocation: string,
+httpServerLocation: string,
+width: ?number,
+height: ?number,
+scales: Array<number>,
+hash: string,
+name: string,
+type: string,
...
};

const assets: Array<PackagerAsset> = [];

function registerAsset(asset: PackagerAsset): number {
// `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: number): PackagerAsset {
return assets[assetId - 1];
}

module.exports = {registerAsset, getAssetByID};
module.exports = require('@react-native/assets/registry');
22 changes: 11 additions & 11 deletions Libraries/Image/AssetSourceResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@ export type ResolvedAssetSource = {|
+scale: number,
|};

import type {PackagerAsset} from './AssetRegistry';
import type {PackagerAsset} from '@react-native/assets/registry';

const PixelRatio = require('../Utilities/PixelRatio');
const Platform = require('../Utilities/Platform');

const assetPathUtils = require('./assetPathUtils');
const invariant = require('invariant');

const {
getAndroidResourceFolderName,
getAndroidResourceIdentifier,
getBasePath,
} = require('@react-native/assets/path-support');

/**
* Returns a path like 'assets/AwesomeModule/icon@2x.png'
*/
function getScaledAssetPath(asset): string {
const scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get());
const scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
const assetDir = assetPathUtils.getBasePath(asset);
const assetDir = getBasePath(asset);
return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type;
}

Expand All @@ -41,11 +46,8 @@ function getScaledAssetPath(asset): string {
*/
function getAssetPathInDrawableFolder(asset): string {
const scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get());
const drawbleFolder = assetPathUtils.getAndroidResourceFolderName(
asset,
scale,
);
const fileName = assetPathUtils.getAndroidResourceIdentifier(asset);
const drawbleFolder = getAndroidResourceFolderName(asset, scale);
const fileName = getAndroidResourceIdentifier(asset);
return drawbleFolder + '/' + fileName + '.' + asset.type;
}

Expand Down Expand Up @@ -133,9 +135,7 @@ class AssetSourceResolver {
Platform.OS === 'android',
'resource identifiers work on Android',
);
return this.fromSource(
assetPathUtils.getAndroidResourceIdentifier(this.asset),
);
return this.fromSource(getAndroidResourceIdentifier(this.asset));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/RelativeImageStub.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// This is a stub for flow to make it understand require('./icon.png')
// See metro/src/Bundler/index.js

const AssetRegistry = require('./AssetRegistry');
const AssetRegistry = require('@react-native/assets/registry');

module.exports = (AssetRegistry.registerAsset({
__packager_asset: true,
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/__tests__/resolveAssetSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('resolveAssetSource', () => {
beforeEach(() => {
jest.resetModules();

AssetRegistry = require('../AssetRegistry');
AssetRegistry = require('@react-native/assets/registry');
resolveAssetSource = require('../resolveAssetSource');
NativeSourceCode = require('../../NativeModules/specs/NativeSourceCode')
.default;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/resolveAssetSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

'use strict';

const AssetRegistry = require('./AssetRegistry');
const AssetRegistry = require('@react-native/assets/registry');
const AssetSourceResolver = require('./AssetSourceResolver');

import type {ResolvedAssetSource} from './AssetSourceResolver';
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@react-native-community/cli-platform-android": "^4.10.0",
"@react-native-community/cli-platform-ios": "^4.10.0",
"@react-native-community/cli": "^4.10.0",
"@react-native/assets": "1.0.0",
"@react-native/polyfills": "1.0.0",
"abort-controller": "^3.0.0",
"anser": "^1.4.9",
Expand Down
3 changes: 3 additions & 0 deletions packages/assets/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
BUCK
34 changes: 34 additions & 0 deletions packages/assets/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load("@fbsource//tools/build_defs:js_glob.bzl", "js_glob")
load("@fbsource//tools/build_defs/third_party:yarn_defs.bzl", "yarn_workspace")
load("@fbsource//xplat/js:JS_DEFS.bzl", "rn_library")

rn_library(
name = "assets",
srcs = js_glob([
"**/*",
"package.json",
]),
labels = ["supermodule:xplat/default/public.react_native.core"],
skip_processors = True,
visibility = ["PUBLIC"],
worker = "//xplat/js:experimental-packager",
)

yarn_workspace(
name = "yarn-workspace",
srcs = glob(
[
"**/*.js",
"**/*.json",
],
exclude = [
"**/__fixtures__/**",
"**/__flowtests__/**",
"**/__mocks__/**",
"**/__tests__/**",
"**/node_modules/**",
"**/node_modules/.bin/**",
],
),
visibility = ["PUBLIC"],
)
10 changes: 10 additions & 0 deletions packages/assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@react-native/assets",
"version": "1.0.0",
"description": "Asset support code for React Native.",
"repository": {
"type": "git",
"url": "git@github.com:facebook/react-native.git"
},
"license": "MIT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

import type {PackagerAsset} from './AssetRegistry';
import type {PackagerAsset} from './registry.js';

const androidScaleSuffix = {
'0.75': 'ldpi',
Expand Down Expand Up @@ -79,7 +79,6 @@ function getBasePath(asset: PackagerAsset): string {
}

module.exports = {
getAndroidAssetSuffix,
getAndroidResourceFolderName,
getAndroidResourceIdentifier,
getBasePath,
Expand Down
38 changes: 38 additions & 0 deletions packages/assets/registry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

'use strict';

export type PackagerAsset = {
+__packager_asset: boolean,
+fileSystemLocation: string,
+httpServerLocation: string,
+width: ?number,
+height: ?number,
+scales: Array<number>,
+hash: string,
+name: string,
+type: string,
...
};

const assets: Array<PackagerAsset> = [];

function registerAsset(asset: PackagerAsset): number {
// `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: number): PackagerAsset {
return assets[assetId - 1];
}

module.exports = {registerAsset, getAssetByID};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,11 @@
"@react-native-community/eslint-plugin@file:packages/eslint-plugin-react-native-community":
version "1.1.0"

"@react-native/assets@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@react-native/assets/-/assets-1.0.0.tgz#c6f9bf63d274bafc8e970628de24986b30a55c8e"
integrity sha512-KrwSpS1tKI70wuKl68DwJZYEvXktDHdZMG0k2AXD/rJVSlB23/X2CB2cutVR0HwNMJIal9HOUOBB2rVfa6UGtQ==

"@react-native/polyfills@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@react-native/polyfills/-/polyfills-1.0.0.tgz#05bb0031533598f9458cf65a502b8df0eecae780"
Expand Down

0 comments on commit 63377fa

Please sign in to comment.