-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Add support for resolving assets (determined by `resolver.isAssetFile`) from Package Exports. - As with source files, assets in `"exports"` do not automatically append `sourceExts` or platform-specific exts. **However**, we allow expansion of *source resolutions* (i.e. `2x.png` etc) from an `"exports"` target. This is Metro/React Native-specific functionality which otherwise has no equivalent in the Package Exports spec. - Falls back to legacy file resolution logic when the asset is missing or `unstable_enablePackageExports` is `false`. Changelog: **[Experimental]** Add asset handling for package exports via `context.resolveAsset` Reviewed By: motiz88 Differential Revision: D43193253 fbshipit-source-id: 41ced8d6a91fc2d32051348635852c5df1f5d213
- Loading branch information
1 parent
aa20356
commit 6e6f36f
Showing
5 changed files
with
126 additions
and
34 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
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,42 @@ | ||
/** | ||
* 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. | ||
* | ||
* @flow strict | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import type {AssetResolution, ResolutionContext} from './types'; | ||
|
||
import path from 'path'; | ||
|
||
/** | ||
* Resolve a file path as an asset. Returns the set of files found after | ||
* expanding asset resolutions (e.g. `icon@2x.png`). Users may override this | ||
* behaviour via `context.resolveAsset`. | ||
*/ | ||
export default function resolveAsset( | ||
context: ResolutionContext, | ||
filePath: string, | ||
): AssetResolution | null { | ||
const dirPath = path.dirname(filePath); | ||
const extension = path.extname(filePath); | ||
const basename = path.basename(filePath, extension); | ||
|
||
try { | ||
if (!/@\d+(?:\.\d+)?x$/.test(basename)) { | ||
const assets = context.resolveAsset(dirPath, basename, extension); | ||
if (assets != null) { | ||
return { | ||
type: 'assetFiles', | ||
filePaths: assets, | ||
}; | ||
} | ||
} | ||
} catch (e) {} | ||
|
||
return null; | ||
} |
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