@@ -26,13 +26,7 @@ var INSTANCE = 0x2;
26
26
// IE8 has a broken defineProperty but no defineProperties so this saves a try/catch.
27
27
var PROPERTY_DESCRIPTOR_SUPPORT = ! ! ( Object . defineProperty && Object . defineProperties ) ;
28
28
29
- // The global context. Rhino uses a different "global" keyword so
30
- // do an extra check to be sure that it's actually the global context.
31
- // istanbul ignore next
32
- var globalContext = typeof global !== 'undefined' && global . Object === Object ? global : this ;
33
-
34
- // Is the environment node?
35
- var hasExports = typeof module !== 'undefined' && module . exports ;
29
+ var globalContext = getGlobal ( ) ;
36
30
37
31
// Whether object instance methods can be mapped to the prototype.
38
32
var allowObjectPrototype = false ;
@@ -53,6 +47,19 @@ var DefaultChainable = getNewChainableClass('Chainable');
53
47
54
48
// Global methods
55
49
50
+ function getGlobal ( ) {
51
+ // Get global context by keyword here to avoid issues with libraries
52
+ // that can potentially alter this script's context object.
53
+ return testGlobal ( typeof global !== 'undefined' && global ) ||
54
+ testGlobal ( typeof window !== 'undefined' && window ) ;
55
+ }
56
+
57
+ function testGlobal ( obj ) {
58
+ // Note that Rhino uses a different "global" keyword so perform an
59
+ // extra check here to ensure that it's actually the global object.
60
+ return obj && obj . Object === Object ? obj : null ;
61
+ }
62
+
56
63
function setupGlobal ( ) {
57
64
Sugar = globalContext [ SUGAR_GLOBAL ] ;
58
65
// istanbul ignore if
@@ -73,9 +80,11 @@ function setupGlobal() {
73
80
return Sugar ;
74
81
} ;
75
82
// istanbul ignore else
76
- if ( hasExports ) {
83
+ if ( typeof module !== 'undefined' && module . exports ) {
84
+ // Node or webpack environment
77
85
module . exports = Sugar ;
78
86
} else {
87
+ // Unwrapped browser environment
79
88
try {
80
89
globalContext [ SUGAR_GLOBAL ] = Sugar ;
81
90
} catch ( e ) {
0 commit comments