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

fix: resolver.resolveRequest is always undefined #1982

Merged
merged 1 commit into from
Jun 22, 2023

Conversation

ammarahm-ed
Copy link
Contributor

@ammarahm-ed ammarahm-ed commented Jun 22, 2023

Summary:

I just upgraded to react-native 0.72.0 in our project and found out that resolver.resolveRequest from the project config is always getting overridden by the getOverrideConfig function. The PR helps fix the issue so that we are able to define our custom resolveRequest functions.

For example here's the metro.config.js file of our project where we are using resolveRequest. This was working in RN 0.71.11 but stopped working after upgrading to 0.72.0.

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
const config = {
  ...
};
const mergedConfig = mergeConfig(getDefaultConfig(__dirname), config);

mergedConfig.resolver = {
  ...
  resolveRequest: (context, moduleName, platform) => {
    if (moduleName ==='react') {
    console.log("LOADING REACT!");
      // Resolve react package from mobile app's node_modules folder always.
      return {
        filePath: path.resolve(path.join(__dirname, '../node_modules', "react","index.js")),
        type: 'sourceFile',
      };
    }
    return context.resolveRequest(context, moduleName, platform);
  }
};

module.exports = mergedConfig;

The problematic code is here:

resolveRequest:
outOfTreePlatforms.length === 0
? undefined
: reactNativePlatformResolver(
outOfTreePlatforms.reduce<{[platform: string]: string}>(

and then the undefined value replaces the project config value for resolveConfig here:

return mergeConfig(
await loadConfig({cwd: ctx.root, ...options}),
overrideConfig,
);

Test Plan:

To test the issue, create a new react-native 0.72.0 project & replace contents of metro.config.js with the following:

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mergedConfig = mergeConfig(getDefaultConfig(__dirname),{});

mergedConfig.resolver = {
  resolveRequest: (context, moduleName, platform) => {

    console.log("Resolve Request Received"):

    return context.resolveRequest(context, moduleName, platform);
  }
};

module.exports = mergedConfig;

Without the fix "Resolve Request Received" never get's logged since resolver.resolveRequest is always undefined in the final config used by metro bundler.

Copy link
Collaborator

@huntie huntie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks :)

@thymikee Can we cherry pick this to 11.x as well? We should distribute this fix to users in the next React Native minor release.

Copy link
Collaborator

@szymonrybczak szymonrybczak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks! Tests are failing, because yesterday was released 0.72 - in snapshots we still have 0.71, so no worries :)

@thymikee thymikee merged commit 29fc304 into react-native-community:main Jun 22, 2023
4 of 7 checks passed
@ammarahm-ed ammarahm-ed deleted the patch-2 branch June 23, 2023 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants