Skip to content

Commit

Permalink
debug-require: Throw errors for unknown and unloaded modules.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored and timabbott committed Jul 29, 2020
1 parent 11b0303 commit d5d6e43
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion tools/debug-require-webpack-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export default class DebugRequirePlugin {
source,
`${requireFn}(${JSON.stringify(
debugRequireId,
)}).initialize(${JSON.stringify(Object.fromEntries(ids), null, "\t")});`,
)}).initialize(${JSON.stringify(
Object.fromEntries(ids),
null,
"\t",
)}, modules);`,
]);
},
);
Expand Down
14 changes: 12 additions & 2 deletions tools/debug-require.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
/* global __webpack_require__ */

let webpackModules;

function debugRequire(request) {
return __webpack_require__(debugRequire.ids[request]);
if (!Object.prototype.hasOwnProperty.call(debugRequire.ids, request)) {
throw new Error("Cannot find module '" + request + "'");
}
const moduleId = debugRequire.ids[request];
if (!Object.prototype.hasOwnProperty.call(webpackModules, moduleId)) {
throw new Error("Module '" + request + "' has not been loaded yet");
}
return __webpack_require__(moduleId);
}

debugRequire.initialize = function (ids) {
debugRequire.initialize = function (ids, modules) {
debugRequire.ids = ids;
webpackModules = modules;
};

module.exports = debugRequire;

0 comments on commit d5d6e43

Please sign in to comment.