forked from webcomponents/webcomponentsjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcomponents-loader.js
61 lines (57 loc) · 2.31 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
/**
* @license
* Copyright (c) 2017 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() {
// Feature detect which polyfill needs to be imported.
var polyfills = [];
if (!('import' in document.createElement('link'))) {
polyfills.push('hi');
}
if (!('attachShadow' in Element.prototype) ||
(window.ShadyDOM && window.ShadyDOM.force)) {
polyfills.push('sd');
}
if (!window.customElements || window.customElements.forcePolyfill) {
polyfills.push('ce');
}
if (!('content' in document.createElement('template')) || !window.Promise ||
// Edge has broken fragment cloning which means you cannot clone template.content
!(document.createDocumentFragment().cloneNode() instanceof DocumentFragment)) {
polyfills.push('pf');
}
if (polyfills.length === 4) { // hi-ce-sd-pf is actually called lite.
polyfills = ['lite'];
}
if (polyfills.length) {
var script = document.querySelector('script[src*="webcomponents-loader.js"]');
var newScript = document.createElement('script');
// Load it from the right place.
var url = script.src.replace(
'webcomponents-loader.js', 'webcomponents-' + polyfills.join('-') + '.js');
newScript.src = url;
var readyIssued = false;
window.addEventListener('WebComponentsReady', function() {
readyIssued = true;
});
newScript.addEventListener('load', function() {
requestAnimationFrame(function() {
if (!readyIssued) {
// Ensure `WebComponentsReady` is fired also when polyfills did not fire it.
window.dispatchEvent(new CustomEvent('WebComponentsReady'));
}
});
});
document.head.appendChild(newScript);
} else {
// Ensure `WebComponentsReady` is fired also when there are no polyfills loaded.
requestAnimationFrame(function() {
window.dispatchEvent(new CustomEvent('WebComponentsReady'));
});
}
})();