Skip to content

metro-plugin-cyclic-dependencies-detector does not work with expo-router #3868

@trcoffman

Description

@trcoffman

What happened?

I'm trying to get metro-plugin-cyclic-dependencies-detector to work. I have multiple import cycles in my app and this plugin is not detecting them and blowing up the way it should.

First off: the docs are wrong about how to set it up with expo. If you do it as suggested in the docs it causes a stack overflow due to unbounded recursion.

This is what I came up with to prevent the infinite recursion.

// Learn more https://docs.expo.io/guides/customizing-metro
const { CyclicDependencies } = require('@rnx-kit/metro-plugin-cyclic-dependencies-detector');
const { getDefaultConfig } = require('expo/metro-config');

/** @type {import('expo/metro-config').MetroConfig} */
const config = getDefaultConfig(__dirname);

const expoSerializer = config.serializer.customSerializer;

config.serializer.customSerializer = async (entryPoint, preModules, graph, options) => {
  CyclicDependencies({
    throwOnError: true,
  })(entryPoint, preModules, graph, options);
  return await expoSerializer(entryPoint, preModules, graph, options);
};

module.exports = config;

Affected Package

@rnx-kit/metro-plugin-cyclic-dependencies-detector

Version

2.0.0

Which platforms are you seeing this issue on?

  • Android
  • iOS
  • macOS
  • Windows

System Information

❯ npx react-native info                

⚠️ react-native depends on @react-native-community/cli for cli commands. To fix update your package.json to include:


  "devDependencies": {
    "@react-native-community/cli": "latest",
  }

Steps to Reproduce

  1. Use my metro config above in an expo project
  2. Introduce a cyclic dependency between 2 modules.
  3. Run the bundler and see that it doesn't fail.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions