microsoft / react-native-macos

A framework for building native macOS apps with React.
https://microsoft.github.io/react-native-windows/
MIT License
3.35k stars 128 forks source link

React Native Macos overrides `resolveRequest` property of `resolver` in Metro config #2123

Open tjzel opened 1 month ago

tjzel commented 1 month ago

Environment

react-native -v: 0.73.4
npm ls react-native-macos: 0.73.26
node -v: 20.11.0
npm -v: 10.2.3
yarn --version: 4.1.1
xcodebuild -version: Xcode 15.4 Build version 15F31d

Steps to reproduce the bug

When dealing with more complicated project configurations, namely monorepos, Metro has to be configured to prevent all sort of issues, like duplicated modules, correct resolving etc.

You can see an example of such setup in react-native-screens repo.

React Native Reanimated tries to actively support macOS. However, we are currently in the process of a migration to a monorepo setup and I noticed that resolveRequest function gets overridden by react-native-macos. This might cause the Metro bundler to fail or to duplicate some modules.

As a simple reproduction, you can create an App using react-native-macos and paste this into your metro.config.js:

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {
  resolver: {
    resolveRequest: () => {
      console.log('resolveRequest');
    },
  },
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

You will notice that nothing ever gets logged and module resolution doesn't fail, although it should.

Expected Behavior

User provided requestResolve function is acknowledged and used.

Actual Behavior

User provided requestResolve is not used.

Reproducible Demo

No response

Additional context

I understand that you might have the need of having your own custom resolveRequest function. However, this should not completely override the user's function.

For now we can solve the issue in our repository by patch-package. I'd be grateful if you could point me to the place in your code where you override the requestResolve function so we can properly embed react-native-macos in our new monorepo setup.

Saadnajmi commented 1 month ago

@tido64 / @acoates-ms any opinions here?

tido64 commented 1 month ago

Sorry, I'm a bit confused. Where does this override happen in react-native-macos? I've tried looking at the code but the only place I found that even sets resolver is in @react-native/community-cli-plugin. Furthermore, I'm pretty sure we use resolveRequest ourselves internally to support symlinks and other scenarios.