forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
af2af90
commit 002535d
Showing
4 changed files
with
50 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* 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 | ||
* @format | ||
*/ | ||
|
||
import type {CustomResolver} from 'metro-resolver'; | ||
|
||
type ResolverConfig = { | ||
platformNameMap: {[platform: string]: string}, | ||
}; | ||
|
||
/** | ||
* Creates a custom Metro resolver that maps platform extensions to package names. | ||
* To be used in app's `metro.config.js` as `resolver.resolveRequest`. | ||
*/ | ||
export const getPlatformResolver = (config: ResolverConfig): CustomResolver => { | ||
return (context, moduleName, platform) => { | ||
// `customResolverOptions` is populated through `?resolver.platformExtension` query params | ||
// in the jsBundleURLForBundleRoot method of the react-native/React/Base/RCTBundleURLProvider.mm | ||
const platformExtension = context.customResolverOptions?.platformExtension; | ||
let modifiedModuleName = moduleName; | ||
if ( | ||
typeof platformExtension === 'string' && | ||
config.platformNameMap?.[platformExtension] | ||
) { | ||
const packageName = config.platformNameMap[platformExtension]; | ||
if (moduleName === 'react-native') { | ||
modifiedModuleName = packageName; | ||
} else if (moduleName.startsWith('react-native/')) { | ||
modifiedModuleName = `${packageName}/${modifiedModuleName.slice( | ||
'react-native/'.length, | ||
)}`; | ||
} | ||
} | ||
|
||
return context.resolveRequest(context, modifiedModuleName, platform); | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,44 +1,5 @@ | ||
/** | ||
* 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 | ||
* @format | ||
*/ | ||
if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) { | ||
require('../../../scripts/build/babel-register').registerForMonorepo(); | ||
} | ||
|
||
import type {CustomResolver} from 'metro-resolver'; | ||
|
||
type ResolverConfig = { | ||
platformNameMap: {[platform: string]: string}, | ||
}; | ||
|
||
/** | ||
* Creates a custom Metro resolver that maps platform extensions to package names. | ||
* To be used in app's `metro.config.js` as `resolver.resolveRequest`. | ||
*/ | ||
export const getPlatformResolver = (config: ResolverConfig): CustomResolver => { | ||
return (context, moduleName, platform) => { | ||
// `customResolverOptions` is populated through `?resolver.platformExtension` query params | ||
// in the jsBundleURLForBundleRoot method of the react-native/React/Base/RCTBundleURLProvider.mm | ||
const platformExtension = context.customResolverOptions?.platformExtension; | ||
let modifiedModuleName = moduleName; | ||
|
||
if ( | ||
typeof platformExtension === 'string' && | ||
config.platformNameMap?.[platformExtension] | ||
) { | ||
const packageName = config.platformNameMap[platformExtension]; | ||
if (moduleName === 'react-native') { | ||
modifiedModuleName = packageName; | ||
} else if (moduleName.startsWith('react-native/')) { | ||
modifiedModuleName = `${packageName}/${modifiedModuleName.slice( | ||
'react-native/'.length, | ||
)}`; | ||
} | ||
} | ||
|
||
return context.resolveRequest(context, modifiedModuleName, platform); | ||
}; | ||
}; | ||
export * from './getPlatformResolver'; |
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