Skip to content

Commit 5e1b427

Browse files
author
Robert Jackson
authored
Merge pull request #685 from ember-cli/update-globalThis-check
Ensure global is present for Node 10 + globalThis polyfill
2 parents f941253 + d675f10 commit 5e1b427

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/utils.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ function buildOptions(projectConfig, templateCompilerPath, pluginInfo) {
118118
return htmlbarsOptions;
119119
}
120120

121+
const hasGlobalThis = (function () {
122+
try {
123+
let context = vm.createContext();
124+
125+
// we must create a sandboxed context to test if `globalThis` will be
126+
// present _within_ it because in some contexts a globalThis polyfill has
127+
// been evaluated. In that case globalThis would be available on the
128+
// current global context but **would not** be inherited to the global
129+
// contexts created by `vm.createContext`
130+
let type = vm.runInContext(`typeof globalThis`, context);
131+
132+
return type !== 'undefined';
133+
} catch (e) {
134+
return false;
135+
}
136+
})();
137+
121138
function getTemplateCompiler(templateCompilerPath, EmberENV = {}) {
122139
let templateCompilerFullPath = require.resolve(templateCompilerPath);
123140
let cacheData = TemplateCompilerCache.get(templateCompilerFullPath);
@@ -164,7 +181,7 @@ function getTemplateCompiler(templateCompilerPath, EmberENV = {}) {
164181
// we must provide a `global`
165182
//
166183
// this is due to https://git.io/Jtb7s (Ember 3.27+)
167-
if (typeof globalThis === 'undefined') {
184+
if (!hasGlobalThis) {
168185
sandbox.global = sandbox;
169186
}
170187

0 commit comments

Comments
 (0)