Skip to content

Commit

Permalink
Merge pull request #685 from ember-cli/update-globalThis-check
Browse files Browse the repository at this point in the history
Ensure global is present for Node 10 + globalThis polyfill
  • Loading branch information
rwjblue authored Mar 18, 2021
2 parents f941253 + d675f10 commit 5e1b427
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ function buildOptions(projectConfig, templateCompilerPath, pluginInfo) {
return htmlbarsOptions;
}

const hasGlobalThis = (function () {
try {
let context = vm.createContext();

// we must create a sandboxed context to test if `globalThis` will be
// present _within_ it because in some contexts a globalThis polyfill has
// been evaluated. In that case globalThis would be available on the
// current global context but **would not** be inherited to the global
// contexts created by `vm.createContext`
let type = vm.runInContext(`typeof globalThis`, context);

return type !== 'undefined';
} catch (e) {
return false;
}
})();

function getTemplateCompiler(templateCompilerPath, EmberENV = {}) {
let templateCompilerFullPath = require.resolve(templateCompilerPath);
let cacheData = TemplateCompilerCache.get(templateCompilerFullPath);
Expand Down Expand Up @@ -164,7 +181,7 @@ function getTemplateCompiler(templateCompilerPath, EmberENV = {}) {
// we must provide a `global`
//
// this is due to https://git.io/Jtb7s (Ember 3.27+)
if (typeof globalThis === 'undefined') {
if (!hasGlobalThis) {
sandbox.global = sandbox;
}

Expand Down

0 comments on commit 5e1b427

Please sign in to comment.