This repo: https://github.com/patricksevat/circ-deps showcases a couple different ways how circular dependencies are being handled by Webpack
The main thing you need to know about Webpack in the context of circular dependencies is how it resolves module dependencies.
Given these files (use tabs to switch files):
dist/main.js, click on the tabs to go through it:__webpack_modules__ variableThis variable is an array or object which contains the source code for a particular file, but transpiled in a couple ways:
require(), import and exportmoduleA.js before and after transpilation__webpack_require__ and __webpack_module_cache__ variablesAs you can see in the example above,
import c from './moduleC''
is transformed to
__webpack_require__("./src/regular/c.js").
So what is __webpack_require__()?
Whenever __webpack_require__ is called:
moduleId ("./src/regular/c.js")moduleId in __webpack_module_cache__ and return a cache hit or resolve the module, save it to cache and return the result__webpack_module_cache__ cache hitIn case there is an entry in __webpack_module_cache__ for the requested moduleId Webpack will return the cached result:
__webpack_module_cache__ cache missIn case there is no entry in __webpack_module_cache__ for the requested moduleId Webpack will:
__webpack_modules__Now that you understand the basics of module resolution in Webpack, it's time to move to a circular dependency. click here