Skip to content

Instantly share code, notes, and snippets.

@FlorianRappl
Created February 24, 2019 19:57
Show Gist options
  • Save FlorianRappl/a64f70e23b55bd035c8a614234e3732c to your computer and use it in GitHub Desktop.
Save FlorianRappl/a64f70e23b55bd035c8a614234e3732c to your computer and use it in GitHub Desktop.
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