reduce eagerness of RefreshUtils to reduce circular dependency errors #627
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In our project, we have a lot (around 400) of - generally unproblematic - circular dependencies:
Technically, this is a circular depenceny if a file somewhere were to import
secondHook
fromindex.js
, a lasomeComponent.js -> src/hooks/index.js -> src/hooks/secondHook.js -> src/hooks/index.js
.In practice, these kinds of re-exports are not a problem and webpack has always known to deal with that.
Now I added fast-refresh to replace our old
react-hot-loader
setup (which was running nicely) and it keeps chasing me from one circular dependency to the next - and most of them, like the above we really do not want to resolve. They are a conscious decision.I have skimmed the source of
react-refresh-webpack-plugin
a bit and from my reading, the problem comes from the fact thatreact-refresh-webpack-plugin
eagerly tries to register all components. Since we don't have components circularly importing from each other, that should be fine - but it also accesses the getter for everything that is not a component to check if it is a component.This PR attempts to fix that by looking at the casing of the exports first - and only handling uppercased exports as potential components that have to be checked by
isLikelyComponentType
. That leads to a lot less eager evaluation and fixes the circular dependencies in our project.I am not an expert in this and I might have missed some edge cases, but I would really like to hear what you think about it!