Skip to content

Commit

Permalink
Ensure Ember 3.27+ can determine global for template compilation.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rwjblue committed Feb 26, 2021
1 parent 20184be commit 957dbc6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 957dbc6

Please sign in to comment.