This bug relates to https://github.com/webpack/webpack/blob/master/lib/javascript/JavascriptModulesPlugin.js#L904-L948
What is the current behavior?
__webpack_require__()
returns {}
on the second call to load a missing module.
If the current behavior is a bug, please provide the steps to reproduce.
I'm not sure how to exercise this code without a large project, but I can invest time into doing so if the current behavior is agreed to be incorrect. The current function renders to something like this:
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
This function is trivial, and it's easy to see that if modules[moduleId].call(...)
throws an exception, you'll end up with an invalid entry in your installedModules cache, which will get returned the very next time the function is called.
What is the expected behavior?
__webpack_require__()
should throw an error every time a request is made for a missing module.
Other relevant information: webpack version: master @ b6f6f5df495a4095a8c5b4ec1e1b18695b7975e9 Node.js version: 10.16.3 Operating System: Ubuntu 18.04
diff --git a/lib/javascript/JavascriptModulesPlugin.js b/lib/javascript/JavascriptModulesPlugin.js
index 4fb289e6d..bc1a0bbc4 100644
--- a/lib/javascript/JavascriptModulesPlugin.js
+++ b/lib/javascript/JavascriptModulesPlugin.js
@@ -907,7 +907,7 @@ class JavascriptModulesPlugin {
Template.indent("return __webpack_module_cache__[moduleId].exports;"),
"}",
"// Create a new module (and put it into the cache)",
- "var module = __webpack_module_cache__[moduleId] = {",
+ "var module = {",
Template.indent([
needModuleId ? "id: moduleId," : "// no module.id needed",
needModuleLoaded ? "loaded: false," : "// no module.loaded needed",
@@ -939,6 +939,9 @@ class JavascriptModulesPlugin {
""
])
: "",
+ "// Cache the module if it loaded successfully",
+ "__webpack_module_cache__[moduleId] = module;",
+ "",
"// Return the exports of the module",
"return module.exports;"
]);