fix(@embark/coderunner): use custom require function in vm context

Supply a custom require function to the vm context for `doEval` so that module
resolution succeeds for both DApp dependencies and Embark dependencies.
This commit is contained in:
Michael Bradley, Jr 2019-01-08 11:04:40 -06:00 committed by Iuri Matias
parent c6c6af01c9
commit 2dea50ab13
1 changed files with 12 additions and 2 deletions

View File

@ -6,11 +6,21 @@ const noop = function() {};
class RunCode { class RunCode {
constructor({logger}) { constructor({logger}) {
this.logger = logger; this.logger = logger;
const customRequire = (mod) => {
return require(customRequire.resolve(mod));
};
customRequire.resolve = (mod) => {
return require.resolve(
mod,
{paths: [fs.dappPath('node_modules'), fs.embarkPath('node_modules')]}
);
};
const newGlobal = Object.create(global); const newGlobal = Object.create(global);
newGlobal.fs = fs; newGlobal.fs = fs;
this.context = Object.assign({}, { this.context = Object.assign({}, {
global: newGlobal, console, exports, require, module, __filename, __dirname, process, global: newGlobal, console, exports, require: customRequire, module,
setTimeout, setInterval, clearTimeout, clearInterval __filename, __dirname, process, setTimeout, setInterval, clearTimeout,
clearInterval
}); });
} }