This repository has been archived by the owner on Sep 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 490
/
webcomponents-loader.js
185 lines (170 loc) · 6.13 KB
/
webcomponents-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
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
/**
* @license
* Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
(function() {
'use strict';
/**
* Basic flow of the loader process
*
* There are 4 flows the loader can take when booting up
*
* - Synchronous script, no polyfills needed
* - wait for `DOMContentLoaded`
* - fire WCR event, as there could not be any callbacks passed to `waitFor`
*
* - Synchronous script, polyfills needed
* - document.write the polyfill bundle
* - wait on the `load` event of the bundle to batch Custom Element upgrades
* - wait for `DOMContentLoaded`
* - run callbacks passed to `waitFor`
* - fire WCR event
*
* - Asynchronous script, no polyfills needed
* - wait for `DOMContentLoaded`
* - run callbacks passed to `waitFor`
* - fire WCR event
*
* - Asynchronous script, polyfills needed
* - Append the polyfill bundle script
* - wait for `load` event of the bundle
* - batch Custom Element Upgrades
* - run callbacks pass to `waitFor`
* - fire WCR event
*/
var polyfillsLoaded = false;
var whenLoadedFns = [];
var allowUpgrades = false;
var flushFn;
function fireEvent() {
window.WebComponents.ready = true;
document.dispatchEvent(new CustomEvent('WebComponentsReady', { bubbles: true }));
}
function batchCustomElements() {
if (window.customElements && customElements.polyfillWrapFlushCallback) {
customElements.polyfillWrapFlushCallback(function (flushCallback) {
flushFn = flushCallback;
if (allowUpgrades) {
flushFn();
}
});
}
}
function asyncReady() {
batchCustomElements();
ready();
}
function ready() {
// bootstrap <template> elements before custom elements
if (window.HTMLTemplateElement && HTMLTemplateElement.bootstrap) {
HTMLTemplateElement.bootstrap(window.document);
}
polyfillsLoaded = true;
runWhenLoadedFns().then(fireEvent);
}
function runWhenLoadedFns() {
allowUpgrades = false;
var fnsMap = whenLoadedFns.map(function(fn) {
return fn instanceof Function ? fn() : fn;
});
whenLoadedFns = [];
return Promise.all(fnsMap).then(function() {
allowUpgrades = true;
flushFn && flushFn();
}).catch(function(err) {
console.error(err);
});
}
window.WebComponents = window.WebComponents || {};
window.WebComponents.ready = window.WebComponents.ready || false;
window.WebComponents.waitFor = window.WebComponents.waitFor || function(waitFn) {
if (!waitFn) {
return;
}
whenLoadedFns.push(waitFn);
if (polyfillsLoaded) {
runWhenLoadedFns();
}
};
window.WebComponents._batchCustomElements = batchCustomElements;
var name = 'webcomponents-loader.js';
// Feature detect which polyfill needs to be imported.
var polyfills = [];
if (!('attachShadow' in Element.prototype && 'getRootNode' in Element.prototype) ||
(window.ShadyDOM && window.ShadyDOM.force)) {
polyfills.push('sd');
}
if (!window.customElements || window.customElements.forcePolyfill) {
polyfills.push('ce');
}
var needsTemplate = (function() {
// no real <template> because no `content` property (IE and older browsers)
var t = document.createElement('template');
if (!('content' in t)) {
return true;
}
// broken doc fragment (older Edge)
if (!(t.content.cloneNode() instanceof DocumentFragment)) {
return true;
}
// broken <template> cloning (Edge up to at least version 17)
var t2 = document.createElement('template');
t2.content.appendChild(document.createElement('div'));
t.content.appendChild(t2);
var clone = t.cloneNode(true);
return (clone.content.childNodes.length === 0 ||
clone.content.firstChild.content.childNodes.length === 0);
})();
// NOTE: any browser that does not have template or ES6 features
// must load the full suite of polyfills.
if (!window.Promise || !Array.from || !window.URL || !window.Symbol || needsTemplate) {
polyfills = ['sd-ce-pf'];
}
if (polyfills.length) {
var url;
var polyfillFile = 'bundles/webcomponents-' + polyfills.join('-') + '.js';
// Load it from the right place.
if (window.WebComponents.root) {
url = window.WebComponents.root + polyfillFile;
} else {
var script = document.querySelector('script[src*="' + name +'"]');
// Load it from the right place.
url = script.src.replace(name, polyfillFile);
}
var newScript = document.createElement('script');
newScript.src = url;
// if readyState is 'loading', this script is synchronous
if (document.readyState === 'loading') {
// make sure custom elements are batched whenever parser gets to the injected script
newScript.setAttribute('onload', 'window.WebComponents._batchCustomElements()');
document.write(newScript.outerHTML);
document.addEventListener('DOMContentLoaded', ready);
} else {
newScript.addEventListener('load', function () {
asyncReady();
});
newScript.addEventListener('error', function () {
throw new Error('Could not load polyfill bundle' + url);
});
document.head.appendChild(newScript);
}
} else {
// if readyState is 'complete', script is loaded imperatively on a spec-compliant browser, so just fire WCR
if (document.readyState === 'complete') {
polyfillsLoaded = true;
fireEvent();
} else {
// this script may come between DCL and load, so listen for both, and cancel load listener if DCL fires
window.addEventListener('load', ready);
window.addEventListener('DOMContentLoaded', function() {
window.removeEventListener('load', ready);
ready();
})
}
}
})();