Skip to content

Conversation

@grabbou
Copy link
Member

@grabbou grabbou commented Dec 19, 2018

Metro depends on some private React Native files when starting the server. We are using "require.resolve", which does not work when we actually run CLI from within React Native master (there's no "react-native" package in "node_modules").

I wanted to avoid hacking around "require.resolve" and paths to support that particular use-case. Doing that might become brittle in the future when we add PnP support etc., where "node_modules" awareness might become a problem.

I consider this a temporary hack and hopefully we can do better in the future, e.g. by creating a special package, such as: "react-native-bundler-compat", so that other bundles, such as Webpack can also use these files.

To make this working in React Native, the following "metro.config.js" has to be created:

module.exports = {
  serializer: {
    getModulesRunBeforeMainModule: () => [
      require.resolve('./Libraries/Core/InitializeCore'),
    ],
    getPolyfills: require('./rn-get-polyfills'),
  },
  resolver: {
    hasteImplModulePath: require.resolve('./jest/hasteImpl'),
  },
  transformer: {
    assetRegistryPath: require.resolve(
      './Libraries/Image/AssetRegistry'
    ),
  },
};

to overwrite the default paths.

Also, fixes #68 as we don't overwrite config ourselves but pass it straight to Metro.

*
* See "react-native/metro.config.js"
*/
if (!config.resolver.hasteImplModulePath) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The idea is when React Native provides its "defaults" in "metro.config.js", this code will not execute and hence, not cause an error.

I was a bit confused coming up with a good documentation for that code (lines 135-145) - if anyone feels like this can be described in a better way, please comment.

Copy link
Member

Choose a reason for hiding this comment

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

Couldn't find the react-native/metro.config.js file you're talking about, can you link to it?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not there yet. I am assuming in this PR that it will be created afterwards. See the contents of the file I have included in the first comment for details.

@grabbou
Copy link
Member Author

grabbou commented Jan 14, 2019

Removed custom code to overwrite config with argv as it is already part of Metro: https://github.com/facebook/metro/blob/8ee697c49d2ed78e0e12ae3b6969b8e94b9630e4/packages/metro-config/src/loadConfig.js#L241

@grabbou
Copy link
Member Author

grabbou commented Jan 14, 2019

Applied some minor changes - the code is ready to go.

@grabbou grabbou merged commit 38502ce into master Jan 15, 2019
@Kudo
Copy link
Member

Kudo commented Jan 16, 2019

Thanks @grabbou, hope to release a new version of local-cli.
RN Android master branch code broke on metro upgrade breaking change for a while.

@thymikee thymikee deleted the feat/metro-2 branch February 16, 2019 15:12
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.

assetExts passed to react-native start as a command line argument is ignored

4 participants