forked from systemjs/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal-helpers.js
136 lines (117 loc) · 3.59 KB
/
global-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(function(__global) {
var loader = $__System;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var indexOf = Array.prototype.indexOf || function(item) {
for (var i = 0, l = this.length; i < l; i++)
if (this[i] === item)
return i;
return -1;
}
function readMemberExpression(p, value) {
var pParts = p.split('.');
while (pParts.length)
value = value[pParts.shift()];
return value;
}
function getGlobalValue(exports) {
if (typeof exports == 'string')
return readMemberExpression(exports, __global);
if (!(exports instanceof Array))
throw new Error('Global exports must be a string or array.');
var globalValue = {};
var first = true;
for (var i = 0; i < exports.length; i++) {
var val = readMemberExpression(exports[i], __global);
if (first) {
globalValue['default'] = val;
first = false;
}
globalValue[exports[i].split('.').pop()] = val;
}
return globalValue;
}
// bare minimum ignores
var ignoredGlobalProps = ['_g', 'sessionStorage', 'localStorage', 'clipboardData', 'frames', 'frameElement', 'external',
'mozAnimationStartTime', 'webkitStorageInfo', 'webkitIndexedDB', 'mozInnerScreenY', 'mozInnerScreenX'];
var globalSnapshot;
function forEachGlobal(callback) {
if (Object.keys)
Object.keys(__global).forEach(callback);
else
for (var g in __global) {
if (!hasOwnProperty.call(__global, g))
continue;
callback(g);
}
}
function forEachGlobalValue(callback) {
forEachGlobal(function(globalName) {
if (indexOf.call(ignoredGlobalProps, globalName) != -1)
return;
try {
var value = __global[globalName];
}
catch (e) {
ignoredGlobalProps.push(globalName);
}
callback(globalName, value);
});
}
loader.set('@@global-helpers', loader.newModule({
prepareGlobal: function(moduleName, exports, globals) {
// disable module detection
var curDefine = __global.define;
__global.define = undefined;
// set globals
var oldGlobals;
if (globals) {
oldGlobals = {};
for (var g in globals) {
oldGlobals[g] = __global[g];
__global[g] = globals[g];
}
}
// store a complete copy of the global object in order to detect changes
if (!exports) {
globalSnapshot = {};
forEachGlobalValue(function(name, value) {
globalSnapshot[name] = value;
});
}
// return function to retrieve global
return function() {
var globalValue;
if (exports) {
globalValue = getGlobalValue(exports);
}
else {
globalValue = {};
var singleGlobal;
var multipleExports;
forEachGlobalValue(function(name, value) {
if (globalSnapshot[name] === value)
return;
if (typeof value == 'undefined')
return;
globalValue[name] = value;
if (typeof singleGlobal != 'undefined') {
if (!multipleExports && singleGlobal !== value)
multipleExports = true;
}
else {
singleGlobal = value;
}
});
globalValue = multipleExports ? globalValue : singleGlobal;
}
// revert globals
if (oldGlobals) {
for (var g in oldGlobals)
__global[g] = oldGlobals[g];
}
__global.define = curDefine;
return globalValue;
};
}
}));
})(typeof self != 'undefined' ? self : global);