-
-
Save FlorianRappl/a64f70e23b55bd035c8a614234e3732c to your computer and use it in GitHub Desktop.
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
function evalModule(name: string, content: string, dependencies: DependencyMap = {}) { | |
const mod = { | |
exports: {}, | |
}; | |
const require = (moduleName: string) => dependencies[moduleName] || | |
console.error(`Cannot find module "${moduleName}" (required by ${name})`, dependencies); | |
try { | |
const importer = new Function('module', 'exports', 'require', content); | |
importer(mod, mod.exports, require); | |
} catch (e) { | |
console.error(`Error while evaluating module "${name}".`, e); | |
} | |
return mod.exports; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment