From 957dbc67ea8d1681389441d3441c229555d2f70b Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Fri, 26 Feb 2021 09:02:56 -0500 Subject: [PATCH] Ensure Ember 3.27+ can determine global for template compilation. Node 12+ has access to `globalThis` (including within a VM context), but older versions do not. Due to the detection done in https://git.io/Jtb7s, when we can't find `globalThis` (and don't define `global` global) evaluating `ember-template-compiler.js` throws an error "unable to locate global object". This ensures that either `globalThis` or `global` are defined. --- lib/utils.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 3194d7d4..fbc87268 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -143,11 +143,21 @@ function getTemplateCompiler(templateCompilerPath, EmberENV = {}) { // the shared global config let clonedEmberENV = JSON.parse(JSON.stringify(EmberENV)); - let context = vm.createContext({ + let sandbox = { EmberENV: clonedEmberENV, module: { require, exports: {} }, require, - }); + }; + + // if we are running on a Node version _without_ a globalThis + // we must provide a `global` + // + // this is due to https://git.io/Jtb7s (Ember 3.27+) + if (typeof globalThis === 'undefined') { + sandbox.global = sandbox; + } + + let context = vm.createContext(sandbox); script.runInContext(context);