Skip to content

Instantly share code, notes, and snippets.

@Belrestro
Created April 29, 2024 20:54
Show Gist options
  • Save Belrestro/3623e7cc038c105fe7a19b0abd386b8e to your computer and use it in GitHub Desktop.
Save Belrestro/3623e7cc038c105fe7a19b0abd386b8e to your computer and use it in GitHub Desktop.
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