forked from pinf/pinf-loader-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.stripped.js
382 lines (318 loc) · 12.9 KB
/
loader.stripped.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// WARNING: DO NOT EDIT THIS FILE! IT IS AUTO-GENERATED FROM ./loader.js BY STRIPPING '/*DEBUG*/' LINES.
/**
* Copyright: 2011 Christoph Dorn <christoph@christophdorn.com>
* License: MIT
*/
// Ignore all globals except for `require`, `exports` and `sourcemint`.
// Declare `require` global but ignore if it already exists.
var require;
// Set `sourcemint` global no matter what.
var sourcemint = null;
// Combat pollution if used via <script> tag.
(function (global, document) {
var loadedBundles = [],
// @see https://github.com/unscriptable/curl/blob/62caf808a8fd358ec782693399670be6806f1845/src/curl.js#L69
readyStates = { 'loaded': 1, 'interactive': 1, 'complete': 1 };
// A set of modules working together.
var Sandbox = function(sandboxIdentifier, loadedCallback, sandboxOptions) {
var moduleInitializers = {},
initializedModules = {},
packages = {},
headTag,
loadingBundles = {};
var sandbox = {
id: sandboxIdentifier
};
// @credit https://github.com/unscriptable/curl/blob/62caf808a8fd358ec782693399670be6806f1845/src/curl.js#L319-360
function loadInBrowser(uri, loadedCallback) {
// See if we are in a web worker.
if (typeof importScripts !== "undefined") {
importScripts(uri.replace(/^\/?\{host\}/, ""));
loadedCallback();
return;
}
if (/^\/?\{host\}\//.test(uri)) {
uri = document.location.protocol + "//" + document.location.host + uri.replace(/^\/?\{host\}/, "");
} else
if (/^\//.test(uri)) {
uri = document.location.protocol + "/" + uri;
}
if (!headTag) {
headTag = document.getElementsByTagName("head")[0];
}
var element = document.createElement("script");
element.type = "text/javascript";
element.onload = element.onreadystatechange = function(ev) {
ev = ev || global.event;
if (ev.type === "load" || readyStates[this.readyState]) {
this.onload = this.onreadystatechange = this.onerror = null;
loadedCallback(function() {
element.parentNode.removeChild(element);
});
}
}
element.onerror = function(e) {
}
element.charset = "utf-8";
element.async = true;
element.src = uri;
element = headTag.insertBefore(element, headTag.firstChild);
}
function load(bundleIdentifier, packageIdentifier, loadedCallback) {
if (packageIdentifier !== "") {
bundleIdentifier = ("/" + packageIdentifier + "/" + bundleIdentifier).replace(/\/+/g, "/");
}
if (initializedModules[bundleIdentifier]) {
// Module is already loaded and initialized.
loadedCallback(sandbox);
} else {
// Module is not initialized.
if (loadingBundles[bundleIdentifier]) {
// Module is already loading.
loadingBundles[bundleIdentifier].push(loadedCallback);
} else {
// Module is not already loading.
loadingBundles[bundleIdentifier] = [];
bundleIdentifier = sandboxIdentifier + bundleIdentifier;
// Default to our script-injection browser loader.
(sandboxOptions.rootBundleLoader || sandboxOptions.load || loadInBrowser)(bundleIdentifier, function(cleanupCallback) {
// The rootBundleLoader is only applicable for the first load.
delete sandboxOptions.rootBundleLoader;
finalizeLoad(bundleIdentifier, packageIdentifier);
loadedCallback(sandbox);
if (cleanupCallback) {
cleanupCallback();
}
});
}
}
}
// Called after a bundle has been loaded. Takes the top bundle off the *loading* stack
// and makes the new modules available to the sandbox.
// If a `packageIdentifier` is supplied we prefix it to all module identifiers anchored
// at the root of the bundle (starting with `/`).
function finalizeLoad(bundleIdentifier, packageIdentifier)
{
// Assume a consistent statically linked set of modules has been memoized.
var key;
for (key in loadedBundles[0][1]) {
// Only add modules that don't already exist!
// TODO: Log warning in debug mode if module already exists.
if (typeof moduleInitializers[key] === "undefined") {
moduleInitializers[packageIdentifier + key] = loadedBundles[0][1][key];
}
}
loadedBundles.shift();
}
var Package = function(packageIdentifier) {
if (packages[packageIdentifier]) {
return packages[packageIdentifier];
}
var descriptor = moduleInitializers[packageIdentifier + "/package.json"] || {
main: "/main.js"
},
mappings = descriptor.mappings || {},
directories = descriptor.directories || {},
libPath = (typeof directories.lib !== "undefined" && directories.lib != "")?directories.lib + "/":"";
var pkg = {
id: packageIdentifier,
main: descriptor.main
};
var Module = function(moduleIdentifier) {
var moduleIdentifierSegment = moduleIdentifier.replace(/\/[^\/]*$/, "").split("/"),
module = {
id: moduleIdentifier,
exports: {}
};
function normalizeIdentifier(identifier) {
// If we have a period (".") in the basename we want an absolute path from
// the root of the package. Otherwise a relative path to the "lib" directory.
if (identifier.split("/").pop().indexOf(".") === -1) {
// We have a module relative to the "lib" directory of the package.
identifier = identifier + ".js";
} else
if (!/^\//.test(identifier)) {
// We want an absolute path for the module from the root of the package.
identifier = "/" + identifier;
}
return identifier;
}
function resolveIdentifier(identifier) {
// Check for relative module path to module within same package.
if (/^\./.test(identifier)) {
var segments = identifier.replace(/^\.\//, "").split("../");
identifier = "/" + moduleIdentifierSegment.slice(1, moduleIdentifierSegment.length-segments.length+1).concat(segments[segments.length-1]).join("/");
return [pkg, normalizeIdentifier(identifier)];
} else
// Check for mapped module path to module within mapped package.
{
identifier = identifier.split("/");
return [Package(mappings[identifier[0]]), normalizeIdentifier(identifier.slice(1).join("/"))];
}
}
// Statically link a module and its dependencies
module.require = function(identifier) {
// HACK: RequireJS compatibility.
// TODO: Move this to a plugin.
if (typeof identifier !== "string") {
return module.require.async.call(null, identifier[0], arguments[1]);
}
identifier = resolveIdentifier(identifier);
return identifier[0].require(identifier[1]).exports;
};
module.require.supports = [
"ucjs2-pinf-0"
];
module.require.id = function(identifier) {
identifier = resolveIdentifier(identifier);
return identifier[0].require.id(identifier[1]);
};
module.require.async = function(identifier, loadedCallback) {
identifier = resolveIdentifier(identifier);
identifier[0].load(identifier[1], loadedCallback);
};
module.require.sandbox = function() {
if (arguments.length === 3)
{
arguments[2].load = arguments[2].load || sandboxOptions.load;
}
// If the `programIdentifier` (first argument) is relative it is resolved against the URI of the owning sandbox (not the owning page).
if (/^\./.test(arguments[0]))
{
arguments[0] = sandboxIdentifier + "/" + arguments[0];
// HACK: Temporary hack as zombie (https://github.com/assaf/zombie) does not normalize path before sending to server.
arguments[0] = arguments[0].replace(/\/\.\//g, "/");
}
return sourcemint.sandbox.apply(null, arguments);
}
module.require.sandbox.id = sandboxIdentifier;
// HACK: RequireJS compatibility.
// TODO: Move this to a plugin.
module.require.nameToUrl = function(identifier)
{
return sandboxIdentifier + module.require.id(identifier);
}
module.load = function() {
if (typeof moduleInitializers[moduleIdentifier] === "function") {
var moduleInterface = {
id: module.id,
exports: undefined
}
// If embedded in bundle `isMain` will be set to `true` if bundle was `require.main`.
if (packageIdentifier === "" && pkg.main === moduleIdentifier && sandboxOptions.isMain === true) {
module.require.main = moduleInterface;
}
if (sandboxOptions.onInitModule) {
sandboxOptions.onInitModule(moduleInterface, module, pkg, sandbox);
}
var exports = moduleInitializers[moduleIdentifier](module.require, module.exports, moduleInterface);
if (typeof moduleInterface.exports !== "undefined") {
module.exports = moduleInterface.exports;
} else
if (typeof exports !== "undefined") {
module.exports = exports;
}
} else
if (typeof moduleInitializers[moduleIdentifier] === "string") {
// TODO: Use more optimal string encoding algorythm to reduce payload size?
module.exports = decodeURIComponent(moduleInitializers[moduleIdentifier]);
} else {
module.exports = moduleInitializers[moduleIdentifier];
}
};
return module;
};
pkg.load = function(moduleIdentifier, loadedCallback) {
load(((!/^\//.test(moduleIdentifier))?"/"+libPath:"") + moduleIdentifier, packageIdentifier, function() {
loadedCallback(pkg.require(moduleIdentifier).exports);
});
}
pkg.require = function(moduleIdentifier) {
var loadingBundlesCallbacks;
if (!/^\//.test(moduleIdentifier)) {
moduleIdentifier = "/" + libPath + moduleIdentifier;
}
moduleIdentifier = packageIdentifier + moduleIdentifier;
if (!initializedModules[moduleIdentifier]) {
(initializedModules[moduleIdentifier] = Module(moduleIdentifier)).load();
}
if (loadingBundles[moduleIdentifier]) {
loadingBundlesCallbacks = loadingBundles[moduleIdentifier];
delete loadingBundles[moduleIdentifier];
for (var i=0 ; i<loadingBundlesCallbacks.length ; i++) {
loadingBundlesCallbacks[i](sandbox);
}
}
return initializedModules[moduleIdentifier];
}
pkg.require.id = function(moduleIdentifier) {
if (!/^\//.test(moduleIdentifier)) {
moduleIdentifier = "/" + libPath + moduleIdentifier;
}
return (((packageIdentifier !== "")?"/"+packageIdentifier+"/":"") + moduleIdentifier).replace(/\/+/g, "/");
}
if (sandboxOptions.onInitPackage) {
sandboxOptions.onInitPackage(pkg, sandbox, {
finalizeLoad: finalizeLoad,
moduleInitializers: moduleInitializers
});
}
packages[packageIdentifier] = pkg;
return pkg;
}
// Get a module and initialize it (statically link its dependencies) if it is not already so
sandbox.require = function(moduleIdentifier) {
return Package("").require(moduleIdentifier);
}
// Call the 'main' module of the program
sandbox.boot = function() {
return sandbox.require(Package("").main).exports;
};
// Call the 'main' exported function of the main' module of the program
sandbox.main = function() {
var exports = sandbox.boot();
return ((exports.main)?exports.main.apply(null, arguments):undefined);
};
load(".js", "", loadedCallback);
return sandbox;
};
// The global `require` for the 'external' (to the loader) environment.
var Loader = function() {
var
sandboxes = {};
var Require = function(bundle) {
// Address a specific sandbox or currently loading sandbox if initial load.
this.bundle = function(uid, callback) {
var moduleInitializers = {},
req = new Require(uid);
// Store raw module in loading bundle
req.memoize = function(moduleIdentifier, moduleInitializer) {
moduleInitializers[moduleIdentifier] = moduleInitializer;
}
callback(req);
loadedBundles.push([uid, moduleInitializers]);
}
};
var require = new Require();
// TODO: @see URL_TO_SPEC
require.supports = [
"ucjs2-pinf-0"
];
// Create a new environment to memoize modules to.
// If relative, the `programIdentifier` is resolved against the URI of the owning page (this is only for the global require).
require.sandbox = function(programIdentifier, loadedCallback, options) {
var sandboxIdentifier = programIdentifier.replace(/\.js$/, "");
return sandboxes[sandboxIdentifier] = Sandbox(sandboxIdentifier, loadedCallback, options || {});
}
return require;
}
sourcemint = Loader();
// Ignore `require` global if already exists.
if (!require) {
require = sourcemint;
}
// Export `require` for CommonJS if `exports` global exists.
if (typeof exports === "object") {
exports.require = sourcemint;
}
}(this, (typeof document !== "undefined")?document:null));