Skip to content

Commit 2954e7e

Browse files
committed
Don't rely on Node.js global object
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
1 parent 8eefee5 commit 2954e7e

File tree

8 files changed

+317
-163
lines changed

8 files changed

+317
-163
lines changed

lib/.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
env: {
3+
// Handlebars should run natively in the browser
4+
node: false
5+
}
6+
};

lib/handlebars/compiler/code-gen.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ let SourceNode;
66
try {
77
/* istanbul ignore next */
88
if (typeof define !== 'function' || !define.amd) {
9-
// We don't support this in AMD environments. For these environments, we asusme that
9+
// We don't support this in AMD environments. For these environments, we assume that
1010
// they are running on the browser and thus have no need for the source-map library.
11-
let SourceMap = require('source-map');
11+
let SourceMap = require('source-map'); // eslint-disable-line no-undef
1212
SourceNode = SourceMap.SourceNode;
1313
}
1414
} catch (err) {

lib/handlebars/helpers/each.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ export default function(instance) {
6363
execIteration(i, i, i === context.length - 1);
6464
}
6565
}
66-
} else if (global.Symbol && context[global.Symbol.iterator]) {
66+
} else if (typeof Symbol === 'function' && context[Symbol.iterator]) {
6767
const newContext = [];
68-
const iterator = context[global.Symbol.iterator]();
68+
const iterator = context[Symbol.iterator]();
6969
for (let it = iterator.next(); !it.done; it = iterator.next()) {
7070
newContext.push(it.value);
7171
}

lib/handlebars/no-conflict.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function(Handlebars) {
22
/* istanbul ignore next */
3-
let root = typeof global !== 'undefined' ? global : window,
3+
let root = typeof global !== 'undefined' ? global : window, // eslint-disable-line no-undef
44
$Handlebars = root.Handlebars;
55
/* istanbul ignore next */
66
Handlebars.noConflict = function() {

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// USAGE:
22
// var handlebars = require('handlebars');
3+
/* eslint-env node */
34
/* eslint-disable no-var */
45

56
// var local = handlebars.create();

lib/precompiler.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-env node */
12
/* eslint-disable no-console */
23
import Async from 'neo-async';
34
import fs from 'fs';

0 commit comments

Comments
 (0)