-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch createRequire() and loadConfig() to handle builtin modules in t…
…he standalone CLI Fix code style
- Loading branch information
1 parent
317fba1
commit 32b4a67
Showing
3 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const Module = require('node:module') | ||
|
||
/** | ||
* @param {Record<string, any>} mods | ||
*/ | ||
module.exports.patchRequire = function patchRequire(mods, parentCache) { | ||
function wrapRequire(origRequire) { | ||
return Object.assign( | ||
function (id) { | ||
// Patch require(…) to return the cached module | ||
if (mods.hasOwnProperty(id)) { | ||
return mods[id] | ||
} | ||
|
||
return origRequire.apply(this, arguments) | ||
}, | ||
|
||
// Make sure we carry over other properties of the original require(…) | ||
origRequire, | ||
|
||
{ | ||
resolve(id) { | ||
// Defer to the "parent" require cache when resolving the module | ||
// This also requires that the module be provided as a "native module" to JITI | ||
|
||
// The path returned here is VERY important as it ensures that the `isNativeRe` in JITI | ||
// passes which is required for the module to be loaded via the native require(…) function | ||
// Thankfully, the regex just means that it needs to be in a node_modules folder which is true | ||
// even when bundled using Vercel's `pkg` | ||
if (parentCache.hasOwnProperty(id)) { | ||
return parentCache[id].filename | ||
} | ||
|
||
return origRequire.resolve.apply(this, arguments) | ||
}, | ||
} | ||
) | ||
} | ||
|
||
let origRequire = Module.prototype.require | ||
let origCreateRequire = Module.createRequire | ||
|
||
// We have to augment the default "require" in every module | ||
Module.prototype.require = wrapRequire(origRequire) | ||
|
||
// And any "require" created by the "createRequire" method | ||
Module.createRequire = function () { | ||
return wrapRequire(origCreateRequire.apply(this, arguments)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters