-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(metro-service): consume
@react-native/assets-registry
- Loading branch information
Showing
14 changed files
with
256 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@rnx-kit/metro-service": patch | ||
--- | ||
|
||
Reuse code from `@react-native/assets-registry` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// @ts-check | ||
import flowRemoveTypes from "flow-remove-types"; | ||
import * as fs from "node:fs"; | ||
import { createRequire } from "node:module"; | ||
import * as path from "node:path"; | ||
|
||
function main() { | ||
const require = createRequire(import.meta.url); | ||
|
||
const flowOptions = { all: true, pretty: true }; | ||
/** @type {{ encoding: "utf-8" }} */ | ||
const fsOptions = { encoding: "utf-8" }; | ||
/** @type {fs.NoParamCallback} */ | ||
const rethrow = (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
}; | ||
|
||
const assetsRegistry = path.dirname( | ||
require.resolve("@react-native/assets-registry/package.json") | ||
); | ||
|
||
for (const filename of ["path-support.js", "registry.js"]) { | ||
const p = path.join(assetsRegistry, filename); | ||
fs.readFile(p, fsOptions, (err, source) => { | ||
rethrow(err); | ||
const t = [ | ||
`// THIS FILE WAS GENERATED BY '${path.basename(import.meta.url)}'`, | ||
"/* eslint-disable @typescript-eslint/ban-ts-comment */", | ||
"// @ts-nocheck", | ||
"// prettier-ignore", | ||
flowRemoveTypes(source, flowOptions).toString(), | ||
].join("\n"); | ||
fs.writeFile(path.join("src", "assets-registry", filename), t, rethrow); | ||
}); | ||
} | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { PackagerAsset } from "./registry"; | ||
export declare function getAndroidResourceFolderName( | ||
asset: PackagerAsset, | ||
scale: number | ||
): string | "raw"; | ||
export declare function getAndroidResourceIdentifier( | ||
asset: PackagerAsset | ||
): string; | ||
export declare function getBasePath(asset: PackagerAsset): string; |
95 changes: 95 additions & 0 deletions
95
packages/metro-service/src/assets-registry/path-support.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// THIS FILE WAS GENERATED BY 'copy-assets-registry.mjs' | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
// @ts-nocheck | ||
// prettier-ignore | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* strict | ||
*/ | ||
|
||
'use strict'; | ||
|
||
|
||
const androidScaleSuffix = { | ||
'0.75': 'ldpi', | ||
'1': 'mdpi', | ||
'1.5': 'hdpi', | ||
'2': 'xhdpi', | ||
'3': 'xxhdpi', | ||
'4': 'xxxhdpi', | ||
}; | ||
|
||
const ANDROID_BASE_DENSITY = 160; | ||
|
||
/** | ||
* FIXME: using number to represent discrete scale numbers is fragile in essence because of | ||
* floating point numbers imprecision. | ||
*/ | ||
function getAndroidAssetSuffix(scale) { | ||
if (scale.toString() in androidScaleSuffix) { | ||
return androidScaleSuffix[scale.toString()]; | ||
} | ||
// NOTE: Android Gradle Plugin does not fully support the nnndpi format. | ||
// See https://issuetracker.google.com/issues/72884435 | ||
if (Number.isFinite(scale) && scale > 0) { | ||
return Math.round(scale * ANDROID_BASE_DENSITY) + 'dpi'; | ||
} | ||
throw new Error('no such scale ' + scale.toString()); | ||
} | ||
|
||
// See https://developer.android.com/guide/topics/resources/drawable-resource.html | ||
const drawableFileTypes = new Set([ | ||
'gif', | ||
'jpeg', | ||
'jpg', | ||
'ktx', | ||
'png', | ||
'svg', | ||
'webp', | ||
'xml', | ||
]); | ||
|
||
function getAndroidResourceFolderName( | ||
asset, | ||
scale, | ||
) { | ||
if (!drawableFileTypes.has(asset.type)) { | ||
return 'raw'; | ||
} | ||
const suffix = getAndroidAssetSuffix(scale); | ||
if (!suffix) { | ||
throw new Error( | ||
"Don't know which android drawable suffix to use for scale: " + | ||
scale + | ||
'\nAsset: ' + | ||
JSON.stringify(asset, null, '\t') + | ||
'\nPossible scales are:' + | ||
JSON.stringify(androidScaleSuffix, null, '\t'), | ||
); | ||
} | ||
return 'drawable-' + suffix; | ||
} | ||
|
||
function getAndroidResourceIdentifier(asset) { | ||
return (getBasePath(asset) + '/' + asset.name) | ||
.toLowerCase() | ||
.replace(/\//g, '_') // Encode folder structure in file name | ||
.replace(/([^a-z0-9_])/g, '') // Remove illegal chars | ||
.replace(/^assets_/, ''); // Remove "assets_" prefix | ||
} | ||
|
||
function getBasePath(asset) { | ||
const basePath = asset.httpServerLocation; | ||
return basePath.startsWith('/') ? basePath.substr(1) : basePath; | ||
} | ||
|
||
module.exports = { | ||
getAndroidResourceFolderName, | ||
getAndroidResourceIdentifier, | ||
getBasePath, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export type PackagerAsset = { | ||
readonly __packager_asset: boolean; | ||
readonly fileSystemLocation: string; | ||
readonly httpServerLocation: string; | ||
readonly width?: null | undefined | number; | ||
readonly height?: null | undefined | number; | ||
readonly scales: number[]; | ||
readonly hash: string; | ||
readonly name: string; | ||
readonly type: string; | ||
}; | ||
export declare function registerAsset(asset: PackagerAsset): number; | ||
export declare function getAssetByID(assetId: number): PackagerAsset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// THIS FILE WAS GENERATED BY 'copy-assets-registry.mjs' | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
// @ts-nocheck | ||
// prettier-ignore | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* strict | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
|
||
const 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}; |
Oops, something went wrong.