Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure global is present for Node 10 + globalThis polyfill #685

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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