diff --git a/lib/global-eval.js b/lib/global-eval.js index 6069f9d9..afa641ba 100644 --- a/lib/global-eval.js +++ b/lib/global-eval.js @@ -49,19 +49,15 @@ var __exec; '\n//# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(load.metadata.sourceMap))) || '') } - if (typeof require != 'undefined' && typeof process != 'undefined' && process.versions && process.versions.node) { - // global scoped eval for node - var vmModule = 'vm'; - try { - var vm = require(vmModule); - } - catch(e) {} + // Web Worker and Chrome Extensions use original ESML eval + // this may lead to some global module execution differences (eg var not defining onto global) + if (isWorker || isBrowser && window.chrome && window.chrome.extension) { __exec = function(load) { if (load.metadata.integrity) - throw new TypeError('Subresource integrity checking is unavailable in Node.'); + throw new TypeError('Subresource integrity checking is not supported in Web Workers or Chrome Extensions.'); try { preExec(this, load); - vm.runInThisContext(getSource(load)); + new Function(getSource(load)).call(__global); postExec(); } catch(e) { @@ -72,7 +68,7 @@ var __exec; } // use script injection eval to get identical global script behaviour - if (!__exec && typeof document != 'undefined' && !isWorker && !(isBrowser && window.chrome && window.chrome.extension)) { + else if (typeof document != 'undefined') { var head; var scripts = document.getElementsByTagName('script'); @@ -104,21 +100,21 @@ var __exec; throw e; } } - - // Web Worker and Chrome Extensions use original ESML eval - // this may lead to some global module execution differences (eg var not defining onto global) - else if (!__exec) { + else { + // global scoped eval for node + var vmModule = 'vm'; + var vm = require(vmModule); __exec = function(load) { if (load.metadata.integrity) - throw new TypeError('Subresource integrity checking is not supported in Web Workers or Chrome Extensions.'); + throw new TypeError('Subresource integrity checking is unavailable in Node.'); try { preExec(this, load); - new Function(getSource(load)).call(__global); + vm.runInThisContext(getSource(load)); postExec(); } catch(e) { postExec(); - throw addToError(e, 'Evaluating ' + load.address); + throw addToError(e.toString(), 'Evaluating ' + load.address); } }; }