You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
663 B
JavaScript
26 lines
663 B
JavaScript
"use strict";
|
|
|
|
/**
|
|
* `eval` the given source as a CommonJS module, using properties found in `context` as top-level variables.
|
|
*
|
|
* Based on `vm.runInContext` found in Node.js, this is a cross-env solution.
|
|
*/
|
|
function evalModule( source, context ) {
|
|
|
|
const argumentKeys = Object.keys( context );
|
|
const argumentValues = argumentKeys.map( argument => context[ argument ] );
|
|
|
|
const sandbox = { exports: {} };
|
|
argumentKeys.push( "module", "exports", source );
|
|
argumentValues.push( sandbox, sandbox.exports );
|
|
|
|
Function( ...argumentKeys )( ...argumentValues );
|
|
|
|
return sandbox.exports;
|
|
|
|
}
|
|
|
|
// Exports
|
|
|
|
module.exports = { evalModule };
|