This example assumes you have followed along with the basics, the default export (es5) example and the named exports example.
Note: This example is only valid for target 'es6'. We already covered the default export (es5) example.
We use the exact same source code as in the default export (es5) example.
However we changed the target in the webpack config:
We have an import chain where moduleA.js eventually depends on itself:
_10entry -> moduleA -> moduleC -> moduleB -> moduleA
And moduleA.js define a default export
Follow on for a detailed explanation, but in short:
module.exports.defaultcaught ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initializationmoduleA.js the first time (1)Similar to the previous examples, Webpack first looks in the cache and does not find a cache entry.
Next, the preliminary exports are set: module.exports = {}
And then the module function is executed.
What is new is that within the module function, there is a bit of code that defines module.exports.default as a getter
moduleA.js the first time (2)Similar to the named exports example, this getter is set directly on the module cache.
If we were to set a breakpoint before line 10, the module cache would be:
_10var __webpack_module_cache__ = {_10 "./src/regular named export/moduleA.js": {_10 exports: {_10 default: function () { return __WEBPACK_DEFAULT_EXPORT__; },_10 }_10 }_10}
And we also see that __WEBPACK_DEFAULT_EXPORT__ will not be initialized till line 12;
Let's fast forward a bit >>
moduleA.js the second time (1)We have recursively called __webpack_require__ a couple of times:
__webpack_require__('./src/regular es6/moduleA.js')__webpack_require__('./src/regular es6/moduleC.js')__webpack_require__('./src/regular es6/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 getter.
However when we try to access the getter on line 21, we get an error:
Uncaught ReferenceError: Cannot access 'WEBPACK_DEFAULT_EXPORT' before initializationOops! None of the calls to __webpack_require__() finish, and the default export circular dependency might have just broken your app!
'es6' will set a getter for default exports'es6', when the module which is depending on itself uses a default export, it will throw an errorCan we resolve the getter issue (Cannot access '{{name}}' before initialization) by using functions.
Click here to go to the next page.