Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow out of tree platforms to work without custom metro configs #1115

Merged
merged 6 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface PlatformConfig<
DependencyConfig,
DependencyParams
> {
npmPackageName?: string;
acoates-ms marked this conversation as resolved.
Show resolved Hide resolved
projectConfig: (
projectRoot: string,
projectParams: ProjectParams | void,
Expand Down
25 changes: 21 additions & 4 deletions packages/cli/src/tools/loadMetroConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path';
// @ts-ignore - no typed definition for the package
import {loadConfig} from 'metro-config';
import {Config} from '@react-native-community/cli-types';
import {reactNativePlatformResolver} from './metroPlatformResolver';

const INTERNAL_CALLSITES_REGEX = new RegExp(
[
Expand All @@ -21,6 +22,12 @@ const INTERNAL_CALLSITES_REGEX = new RegExp(

export interface MetroConfig {
resolver: {
resolveRequest?: (
context: any,
realModuleName: string,
platform: string,
moduleName: string,
) => any;
resolverMainFields: string[];
platforms: string[];
};
Expand Down Expand Up @@ -50,6 +57,19 @@ export interface MetroConfig {
export const getDefaultConfig = (ctx: Config): MetroConfig => {
return {
resolver: {
resolveRequest:
Object.keys(ctx.platforms).filter(
platform => ctx.platforms[platform].npmPackageName,
).length === 0
? undefined
: reactNativePlatformResolver(
Object.keys(ctx.platforms)
.filter(platform => ctx.platforms[platform].npmPackageName)
acoates-ms marked this conversation as resolved.
Show resolved Hide resolved
.reduce<{[platform: string]: string}>((result, platform) => {
result[platform] = ctx.platforms[platform].npmPackageName!;
return result;
}, {}),
),
resolverMainFields: ['react-native', 'browser', 'main'],
platforms: [...Object.keys(ctx.platforms), 'native'],
},
Expand Down Expand Up @@ -77,10 +97,7 @@ export const getDefaultConfig = (ctx: Config): MetroConfig => {
babelTransformerPath: require.resolve(
'metro-react-native-babel-transformer',
),
assetRegistryPath: path.join(
ctx.reactNativePath,
'Libraries/Image/AssetRegistry',
),
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
},
watchFolders: [],
};
Expand Down
49 changes: 49 additions & 0 deletions packages/cli/src/tools/metroPlatformResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* This is an implementation of a metro resolveRequest option which will remap react-native imports
* to different npm packages based on the platform requested. This allows a single metro instance/config
* to produce bundles for multiple out of tree platforms at a time.
*
* @param platformImplementations
* A map of platform to npm package that implements that platform
*
* Ex:
* {
* windows: 'react-native-windows'
* macos: 'react-native-macos'
* }
*/
// @ts-ignore - no typed definition for the package
import {resolve} from 'metro-resolver';
acoates-ms marked this conversation as resolved.
Show resolved Hide resolved

export function reactNativePlatformResolver(platformImplementations: {
[platform: string]: string;
}) {
return (
context: any,
_realModuleName: string,
platform: string,
moduleName: string,
) => {
let backupResolveRequest = context.resolveRequest;
delete context.resolveRequest;

try {
let modifiedModuleName = moduleName;
if (platformImplementations[platform]) {
if (moduleName === 'react-native') {
modifiedModuleName = platformImplementations[platform];
} else if (moduleName.startsWith('react-native/')) {
modifiedModuleName = `${
platformImplementations[platform]
}/${modifiedModuleName.slice('react-native/'.length)}`;
}
}
let result = resolve(context, modifiedModuleName, platform);
context.resolveRequest = backupResolveRequest;
return result;
} catch (e) {
context.resolveRequest = backupResolveRequest;
acoates-ms marked this conversation as resolved.
Show resolved Hide resolved
throw e;
}
};
}