-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
101 lines (73 loc) · 2.1 KB
/
loader.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
/*
* Copyright 2013 The Polymer Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
(function() {
var scope = window.PolymerLoader = {};
var flags = {};
// convert url arguments to flags
if (!flags.noOpts) {
location.search.slice(1).split('&').forEach(function(o) {
o = o.split('=');
o[0] && (flags[o[0]] = o[1] || true);
});
}
// process global logFlags
parseLogFlags(flags);
function load(scopeName) {
// imports
var scope = window[scopeName];
var entryPointName = scope.entryPointName;
var processFlags = scope.processFlags;
// acquire attributes and base path from entry point
var entryPoint = findScript(entryPointName);
var base = entryPoint.basePath;
// acquire common flags
var flags = scope.flags;
// convert attributes to flags
var flags = PolymerLoader.flags;
for (var i=0, a; (a=entryPoint.attributes[i]); i++) {
if (a.name !== 'src') {
flags[a.name] = a.value || true;
}
}
// parse log flags into global
parseLogFlags(flags);
// exports
scope.basePath = base;
scope.flags = flags;
// process flags for dynamic dependencies
if (processFlags) {
processFlags.call(scope, flags);
}
// post-process imports
var modules = scope.modules || [];
var sheets = scope.sheets || [];
// write script tags for dependencies
modules.forEach(function(src) {
document.write('<script src="' + base + src + '"></script>');
});
// write link tags for styles
sheets.forEach(function(src) {
document.write('<link rel="stylesheet" href="' + base + src + '">');
});
}
// utility method
function findScript(fileName) {
var script = document.querySelector('script[src*="' + fileName + '"]');
var src = script.attributes.src.value;
script.basePath = src.slice(0, src.indexOf(fileName));
return script;
}
function parseLogFlags(flags) {
var logFlags = window.logFlags = window.logFlags || {};
if (flags.log) {
flags.log.split(',').forEach(function(f) {
logFlags[f] = true;
});
}
}
scope.flags = flags;
scope.load = load;
})();