Skip to content

Commit

Permalink
Don't rely on Node.js global object
Browse files Browse the repository at this point in the history
If `global` is used and handlebars is compiled for browser
usage without a Node.js `global` polyfill, handlebars
fails with a `global is undefined` error.

Fixes #1593
  • Loading branch information
jaylinski committed Dec 3, 2021
1 parent 8eefee5 commit 2954e7e
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 163 deletions.
6 changes: 6 additions & 0 deletions lib/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
env: {
// Handlebars should run natively in the browser
node: false
}
};
4 changes: 2 additions & 2 deletions lib/handlebars/compiler/code-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ let SourceNode;
try {
/* istanbul ignore next */
if (typeof define !== 'function' || !define.amd) {
// We don't support this in AMD environments. For these environments, we asusme that
// We don't support this in AMD environments. For these environments, we assume that
// they are running on the browser and thus have no need for the source-map library.
let SourceMap = require('source-map');
let SourceMap = require('source-map'); // eslint-disable-line no-undef
SourceNode = SourceMap.SourceNode;
}
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/helpers/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export default function(instance) {
execIteration(i, i, i === context.length - 1);
}
}
} else if (global.Symbol && context[global.Symbol.iterator]) {
} else if (typeof Symbol === 'function' && context[Symbol.iterator]) {
const newContext = [];
const iterator = context[global.Symbol.iterator]();
const iterator = context[Symbol.iterator]();
for (let it = iterator.next(); !it.done; it = iterator.next()) {
newContext.push(it.value);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/no-conflict.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function(Handlebars) {
/* istanbul ignore next */
let root = typeof global !== 'undefined' ? global : window,
let root = typeof global !== 'undefined' ? global : window, // eslint-disable-line no-undef
$Handlebars = root.Handlebars;
/* istanbul ignore next */
Handlebars.noConflict = function() {
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// USAGE:
// var handlebars = require('handlebars');
/* eslint-env node */
/* eslint-disable no-var */

// var local = handlebars.create();
Expand Down
1 change: 1 addition & 0 deletions lib/precompiler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env node */
/* eslint-disable no-console */
import Async from 'neo-async';
import fs from 'fs';
Expand Down
Loading

0 comments on commit 2954e7e

Please sign in to comment.