Skip to content
This repository was archived by the owner on Sep 20, 2019. It is now read-only.

Commit f8b627e

Browse files
committed
custom-elements-es5-adapter.js
1 parent 3b3bd26 commit f8b627e

10 files changed

+136
-116
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,22 @@ elements, etc.). Here's an example:
6060
</script>
6161
```
6262

63-
## `webcomponents-es5-loader.js`
63+
## `custom-elements-es5-adapter.js`
6464
According to the spec, Custom Elements must be ES6 classes (https://html.spec.whatwg.org/multipage/scripting.html#custom-element-conformance). Since most projects need to support a wide range of browsers that don't necessary support ES6, it may make sense to compile your project to ES5. However, ES5-style custom element classes will **not** work with native Custom Elements because ES5-style classes cannot properly extend ES6 classes, like `HTMLElement`.
6565

66-
To work around this, `webcomponents-es5-loader.js` first loads an extra ES5 compatibility [shim](https://github.com/webcomponents/custom-elements/blob/master/src/native-shim.js) before
67-
loading the minimum polyfill bundle, like `webcomponents-loader.js` does.
66+
To work around this, load `custom-elements-es5-adapter.js` before declaring new Custom Elements. **The adapter must NOT be compiled.**
6867

69-
The reason for having two different loaders (`webcomponents-loader.js` for ES6 deployed code and `webcomponents-es5-loader.js` for ES5) is that the polyfill can't know if you're deploying ES5 or ES6 code -- it runs before your code, and only _you_ as an author of the code know what you're going to deploy. This means that _you_ have to make the decision about which loader to use.
70-
**TL; DR: ** Use `webcomponents-es5-loader.js` only if you want to deploy ES5 code to _all_ browsers (both with and without native custom elements support) -- if this isn't a requirement, then use
71-
`webcomponents-loader.js` instead.
68+
```html
69+
<!-- Load Custom Elements es5 adapter -->
70+
<script src="bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>
71+
<!-- Load polyfills; note that "loader" will load these async -->
72+
<script src="bower_components/webcomponentsjs/webcomponents-loader.js"></script>
73+
<!-- Load the es5 compiled custom element definition -->
74+
<link rel="import" href="my-es5-element.html">
75+
76+
<!-- Use the custom element -->
77+
<my-es5-element></my-es5-element>
78+
```
7279

7380
## Browser Support
7481

webcomponents-ce-es5.js renamed to custom-elements-es5-adapter.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
(()=>{'use strict';if(!window.customElements)return;const a=window.HTMLElement,b=window.customElements.define,c=window.customElements.get,d=new Map,e=new Map;let f=!1,g=!1;window.HTMLElement=function(){if(!f){const h=d.get(this.constructor),i=c.call(window.customElements,h);g=!0;const j=new i;return j}f=!1;},window.HTMLElement.prototype=a.prototype,window.customElements.define=(h,i)=>{const j=i.prototype,k=class extends a{constructor(){super(),Object.setPrototypeOf(this,j),g||(f=!0,i.call(this)),g=!1;}},l=k.prototype;k.observedAttributes=i.observedAttributes,l.connectedCallback=j.connectedCallback,l.disconnectedCallback=j.disconnectedCallback,l.attributeChangedCallback=j.attributeChangedCallback,l.adoptedCallback=j.adoptedCallback,d.set(i,h),e.set(h,i),b.call(window.customElements,h,k);},window.customElements.get=(h)=>e.get(h);})();
55

6-
(function(){'use strict';var a=document.createElement('style');a.textContent='body {transition: opacity ease-in 0.2s; } \\nbody[unresolved] {opacity: 0; display: block; overflow: hidden; position: relative; } \\n';var b=document.querySelector('head');b.insertBefore(a,b.firstChild);})();
7-
86
/**
97
@license
108
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
@@ -13,6 +11,6 @@ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
1311
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
1412
Code distributed by Google as part of the polymer project is also
1513
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
16-
*/requestAnimationFrame(()=>{window.dispatchEvent(new CustomEvent('WebComponentsReady'));});
14+
*/
1715

1816
}());

entrypoints/webcomponents-ce-es5-index.js renamed to entrypoints/custom-elements-es5-adapter-index.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
1111

1212
/*
1313
* Polyfills loaded: Custom Elements ES5 Shim
14-
* Used in: Chrome
1514
*/
1615

1716
import '../bower_components/custom-elements/src/native-shim.js'
18-
19-
import '../src/unresolved.js'
20-
21-
requestAnimationFrame(() => {
22-
window.dispatchEvent(new CustomEvent('WebComponentsReady'));
23-
});

entrypoints/webcomponents-hi-ce-es5-index.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

gulpfile.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,10 @@ const babelOptions = {
162162
shouldPrintComment: singleLicenseComment()
163163
};
164164

165-
gulp.task('debugify-ce-es5', () => {
166-
return debugify('webcomponents-ce-es5', '', {plugins: [babel(babelOptions)]});
165+
gulp.task('debugify-ce-es5-adapter', () => {
166+
return debugify('custom-elements-es5-adapter', '', {plugins: [babel(babelOptions)]});
167167
});
168168

169-
gulp.task('debugify-hi-ce-es5', () => {
170-
return debugify('webcomponents-hi-ce-es5', '', {plugins: [babel(babelOptions)]});
171-
})
172-
173169
gulp.task('refresh-bower', () => {
174170
return del('bower_components').then(() => {
175171
let resolve, reject;
@@ -184,7 +180,7 @@ gulp.task('default', (cb) => {
184180
});
185181

186182
gulp.task('clean-builds', () => {
187-
return del(['webcomponents*.js{,.map}', '!webcomponents-{es5-,}loader.js']);
183+
return del(['custom-elements-es5-adapter.js{,.map}', 'webcomponents*.js{,.map}', '!webcomponents-loader.js']);
188184
});
189185

190186
gulp.task('debug', (cb) => {
@@ -194,8 +190,7 @@ gulp.task('debug', (cb) => {
194190
'debugify-hi-sd-ce',
195191
'debugify-hi-sd-ce-pf',
196192
'debugify-sd-ce',
197-
'debugify-ce-es5',
198-
'debugify-hi-ce-es5'
193+
'debugify-ce-es5-adapter'
199194
];
200195
runseq('clean-builds', tasks, cb);
201196
});
@@ -207,8 +202,7 @@ gulp.task('closure', (cb) => {
207202
'closurify-hi-sd-ce',
208203
'closurify-hi-sd-ce-pf',
209204
'closurify-sd-ce',
210-
'debugify-ce-es5',
211-
'debugify-hi-ce-es5'
205+
'debugify-ce-es5-adapter'
212206
];
213207
runseq('clean-builds', ...tasks, cb);
214208
});

tests/imports/simple-element-es5.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!--
2+
@license
3+
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
11+
<script>
12+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
14+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
15+
16+
// Kick off shady CSS.
17+
var template = document.createElement('template');
18+
template.innerHTML =
19+
`
20+
<style>:host {color: blue;} .red-text {color: red;} </style>
21+
<p class="red-text">Shadow DOM</p>
22+
<slot id="slot"></slot>
23+
`;
24+
if (template) {
25+
if (window.ShadyCSS) {
26+
window.ShadyCSS.prepareTemplate(template, 'simple-element');
27+
}
28+
}
29+
30+
var SimpleElement = function (_HTMLElement) {
31+
_inherits(SimpleElement, _HTMLElement);
32+
33+
function SimpleElement() {
34+
_classCallCheck(this, SimpleElement);
35+
36+
var _this = _possibleConstructorReturn(this, (SimpleElement.__proto__ || Object.getPrototypeOf(SimpleElement)).call(this));
37+
38+
_this.bestName = 'batman';
39+
if (window.ShadyCSS) {
40+
window.ShadyCSS.styleElement(_this);
41+
}
42+
43+
if (template && !_this.shadowRoot) {
44+
_this.attachShadow({ mode: 'open' });
45+
_this.shadowRoot.appendChild(document.importNode(template.content, true));
46+
}
47+
return _this;
48+
}
49+
50+
return SimpleElement;
51+
}(HTMLElement);
52+
53+
window.customElements.define('simple-element', SimpleElement);
54+
</script>

tests/integration-es5.html

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!doctype html>
2+
<!--
3+
@license
4+
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
5+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8+
Code distributed by Google as part of the polymer project is also
9+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10+
-->
11+
<html>
12+
<head>
13+
<title>Integration Test</title>
14+
<script src="../custom-elements-es5-adapter.js"></script>
15+
<script src="../webcomponents-loader.js"></script>
16+
<script src="../../web-component-tester/browser.js"></script>
17+
<link rel="import" href="imports/simple-element-es5.html">
18+
</head>
19+
<body>
20+
21+
<!-- Don't break this into multiple lines or you'll have to count text nodes -->
22+
<simple-element id="basic"><span>Light DOM</span></simple-element>
23+
24+
<script>
25+
suite('integration es5', function() {
26+
var el;
27+
28+
setup(function() {
29+
el = document.querySelector('#basic');
30+
});
31+
32+
test('element is imported & upgraded', function() {
33+
assert.equal(el.bestName, 'batman',
34+
'doesn\'t have property set in constructor');
35+
});
36+
37+
test('element has shadow DOM content', function() {
38+
var shadowRoot = el.shadowRoot;
39+
40+
assert.ok(shadowRoot, 'does not have a shadow root');
41+
assert.equal(shadowRoot.querySelector('p').textContent, 'Shadow DOM',
42+
'does not have <p> in the shadow dom');
43+
assert.equal(getComputedStyle(shadowRoot.querySelector('p')).color, 'rgb(255, 0, 0)',
44+
'does not style <p> in the shadow dom');
45+
});
46+
47+
test('element has distributed content', function() {
48+
var slot = el.shadowRoot.querySelector('slot');
49+
assert.ok(slot, 'does not have a slot');
50+
51+
var distributedNodes = slot.assignedNodes()
52+
assert.equal(distributedNodes.length, 1,
53+
'does not have exactly one element distributed');
54+
assert.equal(distributedNodes[0].textContent, 'Light DOM',
55+
'does not have the right content distributed');
56+
assert.equal(getComputedStyle(distributedNodes[0]).color, 'rgb(0, 0, 255)',
57+
'does not style light dom');
58+
});
59+
});
60+
</script>
61+
</body>
62+
</html>

tests/runner.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'template-and-CE.html',
2020
// 'template-and-imports.html',
2121
'integration.html',
22+
'integration-es5.html',
2223
'load.html',
2324
'dev-loader.html',
2425
'dev-loader-swizzled.html',

webcomponents-es5-loader.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)