Comments (13)
Maybe a solution would be to (re)write the native-shim.js in syntactically valid ES5, so no:
- arrow functions
- let/const
- class
I've done this locally, only 'problem' was the StandInElement, which realy needs to be an ES6 class that extends NativeHTMLElement and has a constructor that calls super. I've solved that one using an eval:
var StandInElement = eval('var x = class extends NativeHTMLElement {constructor() {super();Object.setPrototypeOf(this, elementProto);if (!userConstruction) { browserConstruction = true;elementClass.call(this);} userConstruction = false;}};x')
This way the native shim can be loaded as is without errors on non-es6 browsers and still does it's thing properly on ES6 browsers with Custom Element support.
Only downside of this approach is that is uses eval which I guess means browsers cannot properly do their JS optimizations, so it might have a bit of a performance penalty
Thoughts? I could create aPR with my ES5-ified version
from custom-elements.
I've created this ugly work-around for IE11 but it works like a charm.
The string is the base64 encoded, minified version of native-shim.js and you can do it yourself it if you don't want to copy/paste such dodgy code.
if (window.customElements) eval(atob('KCgpPT57J3VzZSBzdHJpY3QnO2lmKHdpbmRvdy5jdXN0b21FbGVtZW50cyl7Y29uc3QgYT13aW5kb3cuSFRNTEVsZW1lbnQsYj13aW5kb3cuY3VzdG9tRWxlbWVudHMuZGVmaW5lLGM9d2luZG93LmN1c3RvbUVsZW1lbnRzLmdldCxkPW5ldyBNYXAsZT1uZXcgTWFwO2xldCBmPSExLGc9ITE7d2luZG93LkhUTUxFbGVtZW50PWZ1bmN0aW9uKCl7aWYoIWYpe2NvbnN0IGo9ZC5nZXQodGhpcy5jb25zdHJ1Y3Rvciksaz1jLmNhbGwod2luZG93LmN1c3RvbUVsZW1lbnRzLGopO2c9ITA7Y29uc3QgbD1uZXcgaztyZXR1cm4gbH1mPSExfSx3aW5kb3cuSFRNTEVsZW1lbnQucHJvdG90eXBlPWEucHJvdG90eXBlO09iamVjdC5kZWZpbmVQcm9wZXJ0eSh3aW5kb3csJ2N1c3RvbUVsZW1lbnRzJyx7dmFsdWU6d2luZG93LmN1c3RvbUVsZW1lbnRzLGNvbmZpZ3VyYWJsZTohMCx3cml0YWJsZTohMH0pLE9iamVjdC5kZWZpbmVQcm9wZXJ0eSh3aW5kb3cuY3VzdG9tRWxlbWVudHMsJ2RlZmluZScse3ZhbHVlOihqLGspPT57Y29uc3QgbD1rLnByb3RvdHlwZSxtPWNsYXNzIGV4dGVuZHMgYXtjb25zdHJ1Y3Rvcigpe3N1cGVyKCksT2JqZWN0LnNldFByb3RvdHlwZU9mKHRoaXMsbCksZ3x8KGY9ITAsay5jYWxsKHRoaXMpKSxnPSExfX0sbj1tLnByb3RvdHlwZTttLm9ic2VydmVkQXR0cmlidXRlcz1rLm9ic2VydmVkQXR0cmlidXRlcyxuLmNvbm5lY3RlZENhbGxiYWNrPWwuY29ubmVjdGVkQ2FsbGJhY2ssbi5kaXNjb25uZWN0ZWRDYWxsYmFjaz1sLmRpc2Nvbm5lY3RlZENhbGxiYWNrLG4uYXR0cmlidXRlQ2hhbmdlZENhbGxiYWNrPWwuYXR0cmlidXRlQ2hhbmdlZENhbGxiYWNrLG4uYWRvcHRlZENhbGxiYWNrPWwuYWRvcHRlZENhbGxiYWNrLGQuc2V0KGssaiksZS5zZXQoaixrKSxiLmNhbGwod2luZG93LmN1c3RvbUVsZW1lbnRzLGosbSl9LGNvbmZpZ3VyYWJsZTohMCx3cml0YWJsZTohMH0pLE9iamVjdC5kZWZpbmVQcm9wZXJ0eSh3aW5kb3cuY3VzdG9tRWxlbWVudHMsJ2dldCcse3ZhbHVlOmo9PmUuZ2V0KGopLGNvbmZpZ3VyYWJsZTohMCx3cml0YWJsZTohMH0pfX0pKCk7'));
from custom-elements.
If you npm install it, the main
field points to the transpiled ES5 source. In other words, you shouldn't be consuming src/
directly. If you don't want to use NPM, you can also get it at https://unpkg.com/@webcomponents/custom-elements.
from custom-elements.
Sorry, I may be missing something but this shim is not included in the NPM of custom-elements. My question here is that I want to use the 'native-shim.js' to allow my ES5 classes to work with custom-elements v1 in browsers that do not support ES6. Converting this to ES5 results in the same error that this is trying to fix.
from custom-elements.
You're not supposed to transpile the native-shim; it needs to be in es2015 in order to work properly. The intent is for you to only include it in supported browsers and if you're transpiling your definitions to ES5.
from custom-elements.
Related to #25.
from custom-elements.
Thanks, the intention of this shim is different to what i thought. V0 custom elements is the way forward for me for the time being.
from custom-elements.
@treshugart I'm also confused by this. So lets say I have a webpack config where I am bundling the CustomElements and native-shim..
entry: [
'@webcomponents/custom-elements/src/native-shim',
'@webcomponents/custom-elements',
'ModuleThatUsesCustomElements',
'./src/MyAppEntrypoint',
],
this works great in modern ES6-ready browsers, but makes it impossible to use custom elements on older devices ( ie iOS 8).
If I just conditionally import the shim in my app code, then anything injected higher in the load order (ie ModuleThatUsesCustomElements
above) will blow up because we're not properly shimmed at the point it executes / registers the custom elements.
Can we transpile native-shim
to es5? How would I get this working with older browsers that don't support ES2015 or CustomElements natively?
from custom-elements.
Hi @tconroy
I feel your problems, we spent a long time creating our Plugin framework using web components, we wanted to use all the new features in the v1 spec, however as an enterprise application we do support browsers that don't support ES6 :( When submitting this issue i thought the native-shim's purpose was to help me in the situation, however that isn't the case the shim works differently than you naturally feel, i transpired it and made changes however it is not the solution and will not solve the problems you want to overcome.
In the end after a lot of hard work we had to go back to the v0 spec. I do get that this is very much a technology at the bleeding edge and for progress to be made you cant always support older tech, it is a shame and makes it difficult to use in the enterprise world, but i will keep trying!
from custom-elements.
Hey @MrMcGibblets,
thanks for sharing. I'm in a similar scenario -- enterprise environment where we still have a high % of ES5-only devices so transpiling is a must-have.
I'm curious how others are approaching this problem. Surely not everyone is just sticking with v0 to maintain ES5 compatibility.. Is there really no way to get v1 working with ES5 browsers? That seems quite crazy to me. Would any of the maintainers like to chime in with how they've observed this issue being solved in the wild?
Any input much appreciated.
from custom-elements.
Just to leave a link here for anyone in the future.
http://www.milezero.org/index.php/tech/web/classless_components.html i found this really useful when looking at this issue.
FYI due to this we ended up creating native mobile apps for now, I still love WebComponents but it just wasn't to be for this project.
from custom-elements.
@p-bakker I'm not a contributor yet, but I'll be really glad to see your PR. I was looking for hours how to get the native shim to work on IE11 without success.
from custom-elements.
from my point of view this is a problem still: https://stackoverflow.com/questions/50617479/angular-6-custom-elements-fail-on-ie11-and-firefox-with-syntax-and-shadow-dom-er
from custom-elements.
Related Issues (20)
- Node.append() should be monkeypatched by custom-elements/src/Patch/Node.js HOT 2
- Use webcomponents-loader in webpack HOT 2
- Native ES6 classes do not work when the native-shim is in use. HOT 7
- Transpiling CEs to ES5 and loading them correctly HOT 1
- Object doesn't support property or method ... HOT 1
- Add support for `customElements.upgrade`.
- Constructor called twice in some cases HOT 2
- Duplicate declaration exception differs from original HOT 1
- CE polyfill is incompatible with babelHelpers class adapter HOT 4
- Modifying DOM in connectedCallback leads to wrong callback order for children. HOT 1
- No callbacks when using range.insertNode HOT 1
- Compiled version of custom-elements.min.js in latest version 1.1.2 does not exist in npm HOT 2
- Question: why use __ce_shadowRoot instead of shadowRoot HOT 7
- Error compiling m-datepicker HOT 1
- IE11/Edge errors SCRIPT28 and SCRIPT2343 (es5) HOT 2
- appreciate your feedback for custom element diagram HOT 7
- Can this polyfill be considered dead? HOT 2
- A browser extension overwrites HTMLElement.prototype.appendChild HOT 1
- Setting textContent to `null` or `undefined` incorrectly creates text with the string `null` or `undefined`
- Some errors thrown during define are squelched
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from custom-elements.