-
Notifications
You must be signed in to change notification settings - Fork 25
/
bootstrap.js
397 lines (341 loc) · 13.6 KB
/
bootstrap.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*global module: false, define, callbackApplication */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define('mr', [], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require, exports, module);
} else {
// Browser globals (root is window)
root.Montage = factory({}, {}, {});
}
}(this, function (require, exports, module) {
"use strict";
// reassigning causes eval to not use lexical scope.
var globalEval = eval,
global = globalEval('this');
//
//
//
function upperCaseChar(_, c) {
return c.toUpperCase();
}
var paramsCache,
dataAttrPattern = /^data-(.*)$/,
boostrapPattern = /^(.*)bootstrap.js(?:[\?\.]|$)/i,
letterAfterDashPattern = /-([a-z])/g;
function getParams() {
var i, j,
match, script, scripts,
mrLocation, attr, name;
if (!paramsCache) {
paramsCache = {};
// Find the <script> that loads us, so we can divine our
// parameters from its attributes.
scripts = document.getElementsByTagName("script");
for (i = 0; i < scripts.length; i++) {
script = scripts[i];
if (script.src && (match = script.src.match(boostrapPattern))) {
mrLocation = match[1];
}
if (script.hasAttribute("data-mr-location")) {
mrLocation = resolve(window.location, script.getAttribute("data-mr-location"));
}
if (mrLocation) {
if (script.dataset) {
for (name in script.dataset) {
if (script.dataset.hasOwnProperty(name)) {
paramsCache[name] = script.dataset[name];
}
}
} else if (script.attributes) {
for (j = 0; j < script.attributes.length; j++) {
attr = script.attributes[j];
match = attr.name.match(dataAttrPattern);
if (match) {
paramsCache[match[1].replace(letterAfterDashPattern, upperCaseChar)] = attr.value;
}
}
}
// Permits multiple bootstrap.js <scripts>; by
// removing as they are discovered, next one
// finds itself.
script.parentNode.removeChild(script);
paramsCache.mrLocation = mrLocation;
break;
}
}
}
return paramsCache;
}
function load(location, loadCallback, errorCallback, finallyCallback) {
var script;
function finallyHandler() {
if (finallyCallback) {
finallyCallback(script);
}
// remove clutter
if (script.parentNode) {
script.parentNode.removeChild(script);
}
}
if (typeof document !== "undefined") {
script = document.createElement("script");
script.setAttribute('async', '');
script.setAttribute('src', location);
script.onload = function () {
if (loadCallback) {
loadCallback(script);
}
finallyHandler();
};
script.onerror = function (err) {
if (errorCallback) {
errorCallback(err, script);
}
finallyHandler();
};
document.querySelector("head").appendChild(script);
} else {
errorCallback(new Error("document not supported"));
finallyHandler();
}
}
// mini-url library
var isAbsolutePattern = /^[\w\-]+:/;
function makeResolve() {
var baseElement = document.querySelector("base"),
existingBaseElement = baseElement;
if (!existingBaseElement) {
baseElement = document.createElement("base");
baseElement.href = "";
}
return function (base, relative) {
base = String(base);
var resolved, restore,
head = document.querySelector("head"),
relativeElement = document.createElement("a");
if (!existingBaseElement) {
head.appendChild(baseElement);
}
if (!isAbsolutePattern.test(base)) {
throw new Error("Can't resolve " + JSON.stringify(relative) + " relative to " + JSON.stringify(base));
}
restore = baseElement.href;
baseElement.href = base;
relativeElement.href = relative;
resolved = relativeElement.href;
baseElement.href = restore;
if (!existingBaseElement) {
head.removeChild(baseElement);
}
return resolved;
};
}
var resolve = makeResolve();
//
//
//
var readyStatePattern = /interactive|complete/;
var bootstrap = function (callback) {
callback = callback || callbackApplication;
var domLoaded, Require, Promise, URL,
params = getParams();
function callbackIfReady() {
if (domLoaded && Require) {
callback(Require, Promise, URL);
}
}
// execute bootstrap scripts
function allModulesLoaded() {
Promise = bootRequire("promise");
Require = bootRequire("require");
URL = bootRequire("mini-url");
callbackIfReady();
}
// observe dom loading and load scripts in parallel
function domLoad() {
// observe dom loaded
document.removeEventListener("DOMContentLoaded", domLoad, true);
domLoaded = true;
callbackIfReady();
}
// miniature module system
var bootModules = {};
var definitions = {};
function bootRequire(id) {
if (!bootModules[id] && definitions[id]) {
var exports = bootModules[id] = {};
bootModules[id] = definitions[id](bootRequire, exports) || exports;
}
return bootModules[id];
}
// this permits bootstrap.js to be injected after DOMContentLoaded
// http://jsperf.com/readystate-boolean-vs-regex/2
if (readyStatePattern.test(document.readyState)) {
domLoad();
} else {
document.addEventListener("DOMContentLoaded", domLoad, true);
}
// determine which scripts to load
var pending = {
"promise": "node_modules/bluebird/js/browser/bluebird.min.js",
"require": "require.js",
"require/browser": "browser.js",
};
// Handle preload
// TODO rename to MontagePreload
if (!global.preload) {
var mrLocation = resolve(window.location, params.mrLocation);
// Special Case bluebird for now:
var onLoadBluebird = function () {
//global.bootstrap cleans itself from window once all known are loaded. "bluebird" is not known, so needs to do it first
global.bootstrap("bluebird", function (mrRequire, exports) {
return window.Promise;
});
global.bootstrap("promise", function (mrRequire, exports) {
return window.Promise;
});
};
if (params.promiseLocation) {
load(params.promiseLocation, onLoadBluebird);
} else {
// First assume the app was installed with npm 3+ and without
// --legacy-bundling, in which case bluebird should be under
// the app's node_modules
load(resolve(window.location, pending.promise), onLoadBluebird, function () {
// If that failed, it's possible the app was installed with
// npm 2 or --legacy-bundling, in which case bluebird should
// be under mr's node_modules
load(resolve(mrLocation, pending.promise), onLoadBluebird);
});
}
// Load other module and skip promise
for (var id in pending) {
if (pending.hasOwnProperty(id)) {
if (id !== 'promise') {
load(resolve(mrLocation, pending[id]));
}
}
}
}
// register module definitions for deferred, serial execution
global.bootstrap = function (id, factory) {
definitions[id] = factory;
delete pending[id];
for (id in pending) {
if (pending.hasOwnProperty(id)) {
// this causes the function to exit if there are any remaining
// scripts loading, on the first iteration. consider it
// equivalent to an array length check
return;
}
}
// if we get past the for loop, bootstrapping is complete. get rid
// of the bootstrap function and proceed.
delete global.bootstrap;
allModulesLoaded();
};
// one module loaded for free, for use in require.js, browser.js
global.bootstrap("mini-url", function (mrRequire, exports) {
exports.resolve = resolve;
});
};
var browser = {
getParams: getParams,
bootstrap: bootstrap
};
// Bootstrapping for multiple-platforms
exports.getPlatform = function() {
if (typeof window !== "undefined" && window && window.document) {
return browser;
} else if (typeof process !== "undefined") {
return require("./node.js");
} else {
throw new Error("Platform not supported.");
}
};
exports.Require = null;
/**
* Initializes Montage and creates the application singleton if
* necessary.
*/
exports.initMontageRequire = function() {
var platform = exports.getPlatform();
// Platform dependent
return platform.bootstrap(function(mrRequire, Promise, URL) {
var config = {},
params = platform.getParams(),
applicationModuleId = params.module || "",
applicationLocation = URL.resolve(mrRequire.getLocation(), params.package || ".");
// execute the preloading plan and stall the fallback module loader
// until it has finished
if (global.preload) {
var bundleDefinitions = {};
var getDefinition = function (name) {
return bundleDefinitions[name] =
bundleDefinitions[name] ||
Promise.resolve();
};
global.bundleLoaded = function (name) {
return getDefinition(name).resolve();
};
var preloading = {
promise: new Promise(function (resolve, reject) {
preloading.resolve = resolve;
preloading.reject = reject;
})
};
config.preloaded = preloading.promise;
// preload bundles sequentially
var preloaded = Promise.resolve();
global.preload.forEach(function (bundleLocations) {
preloaded = preloaded.then(function () {
return Promise.all(bundleLocations.map(function (bundleLocation) {
load(bundleLocation);
return getDefinition(bundleLocation).promise;
}));
});
});
// then release the module loader to run normally
preloading.resolve(preloaded.then(function () {
delete global.preload;
delete global.bundleLoaded;
}));
}
mrRequire.loadPackage({
location: params.mrLocation,
hash: params.mrHash
}, config).then(function (mrRequire) {
mrRequire.inject("mini-url", URL);
mrRequire.inject("promise", Promise);
mrRequire.inject("require", mrRequire);
if ("autoPackage" in params) {
mrRequire.injectPackageDescription(applicationLocation, {});
}
return mrRequire.loadPackage({
location: applicationLocation,
hash: params.applicationHash
}).then(function (pkg) {
// Expose global require and mr
global.require = global.mr = pkg;
return pkg.async(applicationModuleId);
});
});
});
};
if (typeof window !== "undefined") {
if (global.__MONTAGE_REQUIRE_LOADED__) {
console.warn("MontageRequire already loaded!");
} else {
global.__MONTAGE_REQUIRE_LOADED__ = true;
exports.initMontageRequire();
}
} else {
// may cause additional exports to be injected:
exports.getPlatform();
}
}));