diff --git a/Libraries/Image/AssetRegistry.js b/Libraries/Image/AssetRegistry.js index 088187ff3e70ab..436195f76f77a6 100644 --- a/Libraries/Image/AssetRegistry.js +++ b/Libraries/Image/AssetRegistry.js @@ -10,29 +10,4 @@ 'use strict'; -export type PackagerAsset = { - +__packager_asset: boolean, - +fileSystemLocation: string, - +httpServerLocation: string, - +width: ?number, - +height: ?number, - +scales: Array, - +hash: string, - +name: string, - +type: string, - ... -}; - -const assets: Array = []; - -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'); diff --git a/Libraries/Image/AssetSourceResolver.js b/Libraries/Image/AssetSourceResolver.js index bc671090b25b5f..5ebd97a3d0298f 100644 --- a/Libraries/Image/AssetSourceResolver.js +++ b/Libraries/Image/AssetSourceResolver.js @@ -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; } @@ -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; } @@ -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)); } /** diff --git a/Libraries/Image/RelativeImageStub.js b/Libraries/Image/RelativeImageStub.js index 040392a75e2e07..3303143d933780 100644 --- a/Libraries/Image/RelativeImageStub.js +++ b/Libraries/Image/RelativeImageStub.js @@ -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, diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index ee752bd7322c4a..c9fa18374bbf56 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -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; diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 2b97b8d6b59367..71df2005163baa 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -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'; diff --git a/package.json b/package.json index 8b4a62a4e6b4dc..6b1db5d97562e5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/assets/.npmignore b/packages/assets/.npmignore new file mode 100644 index 00000000000000..9b166b095d3fae --- /dev/null +++ b/packages/assets/.npmignore @@ -0,0 +1,3 @@ +**/__mocks__/** +**/__tests__/** +BUCK diff --git a/packages/assets/BUCK b/packages/assets/BUCK new file mode 100644 index 00000000000000..fa6f04f440ac24 --- /dev/null +++ b/packages/assets/BUCK @@ -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"], +) diff --git a/packages/assets/package.json b/packages/assets/package.json new file mode 100644 index 00000000000000..13064909c99bb9 --- /dev/null +++ b/packages/assets/package.json @@ -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" +} diff --git a/Libraries/Image/assetPathUtils.js b/packages/assets/path-support.js similarity index 96% rename from Libraries/Image/assetPathUtils.js rename to packages/assets/path-support.js index a393b7ca0bd936..d91943dbf1d92e 100644 --- a/Libraries/Image/assetPathUtils.js +++ b/packages/assets/path-support.js @@ -10,7 +10,7 @@ 'use strict'; -import type {PackagerAsset} from './AssetRegistry'; +import type {PackagerAsset} from './registry.js'; const androidScaleSuffix = { '0.75': 'ldpi', @@ -79,7 +79,6 @@ function getBasePath(asset: PackagerAsset): string { } module.exports = { - getAndroidAssetSuffix, getAndroidResourceFolderName, getAndroidResourceIdentifier, getBasePath, diff --git a/packages/assets/registry.js b/packages/assets/registry.js new file mode 100644 index 00000000000000..088187ff3e70ab --- /dev/null +++ b/packages/assets/registry.js @@ -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, + +hash: string, + +name: string, + +type: string, + ... +}; + +const assets: Array = []; + +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}; diff --git a/yarn.lock b/yarn.lock index 0a849b2118cf4a..4dca9a0e7b253b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"