Set resolve modulesDirectories so module resolves default to Gatsby #299
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.
This was the previous behavior but
a387663
moved our resolve.modulesDirectories config to resolve.root which while
speeding up resolving (and bringing us in line with Webpack's suggested
setup https://webpack.github.io/docs/resolving.html) did break certain
important use cases.
I discovered this recently when my Gatsby site for React Headroom broke.
I rebuilt the react-headroom in Gatsby using 0.11.0. After making some
changes, I rebuilt it with 0.11.1. A user then pointed out that the site
was now broken
(KyleAMathews/react-headroom#82).
After investigating, the problem turned out that two copies of React
were being loaded. One from Gatsby and another from React Headroom. The
Gatsby site is in a subdirectory of the React Headroom and imported the
Headroom component from its src directory (i.e.
import Headroom from '../../src/index.js'
). This component in turn imported React.The default Webpack module resolution algorithm is similar to the
node.js resolving process and looks for the nearest node_modules
directory. This is generally what we want but causes trouble when both
Gatsby and another node_modules directory provide the same module.
So to fix this, this commit makes Webpack default to looking inside the
Gatsby site's node_modules directory and only thereafter looks in the
node_modules relative to the module being resolved.
So for the React Headroom component, React is now being correctly
resolved from the Gatsby site preventing the "two copies of React"
problem but react-addons-pure-render-mixin, a direct dependency of React
Headroom that's not included in Gatsby, is resolved to the Headroom
node_modules directory.