Created
April 29, 2024 20:54
-
-
Save Belrestro/3623e7cc038c105fe7a19b0abd386b8e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const path = require('node:path'); | |
const fs = require('node:fs'); | |
const assert = require('node:assert'); | |
const requireImpl = (pth) => { | |
assert((typeof pth) === 'string'); | |
let moduleText; | |
const modulePath = path.join(process.cwd(), pth); | |
try { | |
moduleText = fs.readFileSync(modulePath); | |
} catch (err) { | |
throw new Error(`module not found ${modulePath}`) | |
} | |
const ctx = { | |
module: {}, | |
}; | |
const fn = new Function(`Object.assign(global, this); ${moduleText}`); | |
fn.bind(ctx)(); | |
return ctx.module.exports; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment