forked from LinkedInAttic/inject
/
context.js
279 lines (259 loc) · 8.28 KB
/
context.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*global context:true */
/*
Inject
Copyright 2011 LinkedIn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
/**
This file defines the public interface for Inject
many functions in this collection pass through via proxy
to internal methods
@file public interface for Inject
*/
var globalRequire = new RequireContext();
/**
This object contains the public interface for Inject.
@class
@type {object}
@global
*/
context.Inject = {
/**
This object and its properties are meant solely for consumption
@private
*/
INTERNAL: {
// used by the executor. these let inject know the module that is currently running
defineExecutingModuleAs: proxy(Executor.defineExecutingModuleAs, Executor),
undefineExecutingModule: proxy(Executor.undefineExecutingModule, Executor),
createModule: proxy(Executor.createModule, Executor),
setModuleExports: function () {},
// a hash of publicly reachable module sandboxes ie exec0, exec1...
execute: {},
// a hash of publicly reachable module objects ie exec0's modules, exec1's modules...
modules: {},
// a hash of publicly reachable executor scopes ie exec0's __exe function
execs: {},
// a globally available require() call for the window and base page
globalRequire: globalRequire,
// creates require and define methods as passthrough
createRequire: proxy(InjectCore.createRequire, InjectCore),
createDefine: proxy(InjectCore.createDefine, InjectCore)
},
/**
Exposes easyXDM API for doing cross-domain messaging.
@see <a href="http://www.easyxdm.net">easyXDM</a>
@public
*/
easyXDM: easyXDM,
/**
Clears any locally cached modules, downloads and local storage.
@see InjectCore.reset
@method
@public
*/
reset: proxy(InjectCore.reset, InjectCore),
/**
Enables debugging options.
@see InjectCore.enableDebug
@method
@public
*/
enableDebug: function () {
InjectCore.enableDebug.apply(this, arguments);
},
/**
Enables AMD Plugins if that's your thing
Adds a special rule to make AMD plugins go round
@method
@public
*/
enableAMDPlugins: function () {
RulesEngine.addRule(/^.+?\!.+$/, {
last: true,
useSuffix: false,
path: function () {
return ''; // no path, no fetch!
},
pointcuts: {
afterFetch: function (next, text, moduleName, requestorName, requestorUrl) {
var pieces = moduleName.split('!');
var pluginId = RulesEngine.resolveIdentifier(pieces[0], requestorName);
var pluginUrl = RulesEngine.resolveUrl(pluginId, requestorUrl);
var identifier = pieces[1];
var rq = InjectCore.createRequire(moduleName, requestorUrl);
rq.ensure([pluginId], function (pluginRequire) {
// the plugin must come from the contextual require
// any subsequent fetching depends on the resolved plugin's location
var plugin = pluginRequire(pluginId);
var remappedRequire = InjectCore.createRequire(pluginId, pluginUrl);
var resolveIdentifier = function (name) {
return RulesEngine.resolveIdentifier(name, requestorName);
};
var normalized = (plugin.normalize) ? plugin.normalize(identifier, resolveIdentifier) : resolveIdentifier(identifier);
var complete = function (contents) {
if (typeof(contents) === 'string') {
contents = ['module.exports = decodeURIComponent("', encodeURIComponent(contents), '");'].join('');
}
next(null, contents);
};
complete.fromText = function (ftModname, body) {
if (!body) {
body = ftModname;
ftModname = null;
}
// use the executor
Executor.runModule(ftModname, body, pluginUrl);
next(null, body);
};
plugin.load(normalized, remappedRequire, complete, {});
});
}
}
});
},
/**
Sets base path for all module includes.
@see InjectCore.setModuleRoot
@method
@public
*/
setModuleRoot: function () {
InjectCore.setModuleRoot.apply(this, arguments);
},
/**
Set a time for how long to preserve items in cache.
The default time is 300 seconds.
@see InjectCore.setExpires
@method
@public
*/
setExpires: function () {
InjectCore.setExpires.apply(this, arguments);
},
/**
Sets unique cache identifier for Inject. This allows the parent page
to "bust" the cache by invoking setCacheKey with a different value.
@see InjectCore.setCacheKey
@method
@public
*/
setCacheKey: function () {
InjectCore.setCacheKey.apply(this, arguments);
},
/**
Sets the cross-domain configuration. The cross-domain configuration
is an object consisting of two properties: relayHtml and relaySwf. The
HTML and SWF file should be located on the remote server (for example
the CDN).
@see InjectCore.setCrossDomain
@method
@public
*/
setCrossDomain: function () {
InjectCore.setCrossDomain.apply(this, arguments);
},
/**
Sets the useSuffix user config. The useSuffix config tells the RulesEngine
not to auto-append a .js extension. This is highly helpful in concatenated
environments or environments with JS being generated programatically.
*/
setUseSuffix: function (val) {
InjectCore.setUseSuffix(val);
},
/**
* Set the global AMD property. Setting this to "true" can disable
* the global AMD detection. This is really useful in scenarios where
* you anticipate mixing script tags with your loader framework
*/
disableGlobalAMD: function (disable) {
if (disable) {
context.define = Inject.INTERNAL.createDefine(null, null, true);
}
else {
context.define = Inject.INTERNAL.createDefine();
}
},
/**
Clears the local storage caches.
@see InjectCore.clearCache
@method
@public
*/
clearCache: proxy(InjectCore.clearCache, InjectCore),
/**
@see RulesEngine.manifest
@method
@public
*/
manifest: function () {
RulesEngine.manifest.apply(RulesEngine, arguments);
},
/**
@see RulesEngine.addRule
@method
@public
*/
addRule: function () {
RulesEngine.addRule.apply(RulesEngine, arguments);
},
/**
* Add a plugin to Inject, registering a rule and global functions
* @see InjectCore.plugin
* @method
* @public
*/
plugin: function () {
var args = [].slice.call(arguments, 0);
args.push(context.Inject);
InjectCore.plugin.apply(InjectCore, args);
},
/**
CommonJS and AMD require()
@see InjectCore.createRequire
@see <a href="http://wiki.commonjs.org/wiki/Modules/1.1">CommonJS require()</a>
@see <a href="https://github.com/amdjs/amdjs-api/wiki/require">AMD require()</a>
@method
@public
*/
require: InjectCore.createRequire(),
/**
AMD define()
@see InjectCore.createDefine
@see <a href="https://github.com/amdjs/amdjs-api/wiki/AMD">AMD define()</a>
@method
@public
*/
define: InjectCore.createDefine(),
/**
The version of Inject.
@type {String}
@public
*/
version: 'undefined'
};
/**
CommonJS and AMD require()
@see InjectCore.createRequire
@see <a href="http://wiki.commonjs.org/wiki/Modules/1.1">CommonJS require()</a>
@see <a href="https://github.com/amdjs/amdjs-api/wiki/require">AMD require()</a>
@method
@public
*/
context.require = context.Inject.INTERNAL.createRequire();
/**
AMD define()
@see InjectCore.createDefine
@see <a href="https://github.com/amdjs/amdjs-api/wiki/AMD">AMD define()</a>
@method
@public
*/
context.define = context.Inject.INTERNAL.createDefine();