Skip to content

Commit 320f674

Browse files
authored
chore(cli-repl): add env var for snapshot webpack debugging (#2120)
1 parent f4448b0 commit 320f674

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/cli-repl/webpack.config.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,25 @@ function makeLazyForwardModule(pkg) {
115115
: `require(${S(require.resolve(pkg))})`;
116116

117117
const moduleContents = require(pkg);
118-
let source = `'use strict';\nlet _cache;\n`;
119-
source += `function orig() {\n_cache = ${realRequire}; orig = () => _cache; return _cache;\n}\n`;
118+
let source = `'use strict';\nlet _cache, orig;\n`;
119+
source += `function _realModule() {\n_cache = ${realRequire}; orig = () => _cache; return _cache;\n}\n`;
120+
121+
if (process.env.MONGOSH_DEBUG_WEBPACK_FORWARDING_MODULES) {
122+
source += `const _startupSnapshot = __non_webpack_require__("v8").startupSnapshot;
123+
function _throwModule() {\nthrow new Error('cannot load package "' + ${S(
124+
pkg
125+
)} + '" while building snapshot');\n}
126+
if (_startupSnapshot.isBuildingSnapshot()) {
127+
orig = _throwModule;
128+
_startupSnapshot.addDeserializeCallback(() => orig = _realModule);
129+
} else {
130+
orig = _realModule;
131+
}
132+
`;
133+
} else {
134+
source += `orig = _realModule;\n`;
135+
}
136+
120137
if (typeof moduleContents === 'function') {
121138
source += `module.exports = function(...args) { return orig().apply(this, args); };\n`;
122139
} else {

0 commit comments

Comments
 (0)