This example assumes you have followed along with the basics and the default export example.
The full source code is very similar to the previous example, but now we have:
entry.js and moduleB.jsmoduleA.jsNote: This example uses ES5, but the same is true for ES6
We have an import chain where moduleA.js eventually depends on itself:
_10entry -> moduleA -> moduleC -> moduleB -> moduleA
And moduleA.js defines named exports
Follow on for a detailed explanation, but in short:
Uncaught ReferenceError: Cannot access '{{namedExportName}}' before initializationmoduleA.js the first time (1)Similar to the previous example, Webpack first looks in the cache and does not find a cache entry.
Next, the preliminary exports are set: module.exports = {}
And the module function is executed.
What is new is that within the module function, is a bit of code which sets getters for the named exports on the module cache.
moduleA.js the first time (2)The first thing to note about these getters are that these are set directly on the module cache.
__webpack_exports__ is a reference to __webpack_module_cache__["./src/regular named export/moduleA.js"].exports
If we were to set a breakpoint before line 12, the module cache would be:
_10var __webpack_module_cache__ = {_10  "./src/regular named export/moduleA.js": {_10    exports: {_10      namedA: function () { return namedA; },_10      namedWithImportedValue: function () { return namedWithImportedValue; }_10    }_10  }_10}
moduleA.js the first time (3)The second important thing to note is that these getters return variables which will be instantiated later
Let's fast forward a bit and see why this matters >>
moduleA.js the second time (1)We have recursively called __webpack_require__ a couple of times:
__webpack_require__('./src/regular/moduleA.js')__webpack_require__('./src/regular/moduleC.js')__webpack_require__('./src/regular/moduleB.js')And we are now about to execute the moduleB.js code
moduleA.js the second time (2)There now is a cache hit when requiring moduleA. The cache returns the export object containing the getters.
However when we try to access the getter on line 21, we get an error:
main.js:9 Uncaught ReferenceError: Cannot access 'namedWithImportedValue' before initializationOops! None of the calls to __webpack_require__() finish, and the named circular dependency might have just broken your app!
Let's see how es6 behaves vs es5. Click here for the default exports (es6) example