Looking Ahead to Social Media Marketing Changes in 2019

2018 has proven to be a pivotal year for social media, and although there are trends that come and go, here are some ways that social media is changing and some key directives on how to incorporate…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to build REAL Web Components with Angular 6

Tough journey through failures to success

If you found this story, desperately searching for the guideline how to simply and painlessly build a web component out of Angular 6 application, then you are at the right place. Probably.

As you know, the new version of Angular has brought us a really interesting, but reliably fresh feature, called custom elements. According to different sources, developers could write a normal Angular application and then build it as a web component, where all fancy Angular features are supported. But it is not as simple as one could think. I am not going really in detail in this story, how custom elements work. I’d rather want to give a step by step guideline how to build a working web component.

My first thought about building a web component using Angular 6 was: “easy task, I just have to dive into the docs, read some best practises and ready”. So I started my investigation in internet and found out really great article, which explains roughly what custom elements are and how we possibly could build a simple web component. So let’s begin.

This will generate an Angular application, skipping tests and with inline template and styles. Angular will also add a polyfill for us. If you were following all 5 steps in the article, I mentioned above, you may end up like this.

main.ts
app.module.ts
app.component.ts

Looks not too complicated and was fairly easy to implement, wasn’t it?

Now we could proudly build our web component and run it in browser. Hit the road Jack!

Then we go to the dist folder, find the index.html file. It will look like this.

index.html

But we would like to use our web component as a one simple javascript file. So we replace all those 5 imports with only one.

index.html

Ok, we are ready for the first try. Open index.html in browser. What do we see? An empty screen, at least I see an empty screen. Open up a developer console.

You know why? Because we haven’t imported appropriate polyfill of course. Emergency fix.

polyfills.ts

Rebuild. Run. That’s it, it’s working!

Success, or not yet?

I found it strange, because according to Angular message in console, the polyfill should be added to scripts.js, but I had to import the polyfill additionally to make it work.

So you might think that the first.component.js is our final masterpiece and we could include it everywhere and it will work with other Angular based web components. Let us make some tests. Suppose we’ve built exactly the same component, only selector’s name changed.

After a successful build, let us import two components inside index.html. We should expect first and second components to be up and running simultaneously, independent from each other. But the reality does not fulfil our expectations.

Import of two web components
Another failure, again…

This error just says, that both of our components are importing Zone.js, but Zone.js should be imported only once. Unfortunately Angular will not bootstrap a component without Zone.js. We are facing a vicious circle.

I found out at least three possible solutions for this particular problem. To be or not to be, that is the question?

I, personally, prefer the easiest way, the implementation looks like this.

polyfills.ts
index.html

So, it seems, that we fixed Zone issue. Rebuild. Run. Fail… Again…

For the first time, I’ve seen this error message I didn’t believe my eyes. Importing second component source causes redefining of the first (!!!) component. How is it possible at all? What the magic happens here? That’s really a funny issue. But everything is simple, the problem is inside our webpack build.

To be more specific, the problem causes CommonChunkPlugin in webpack, which is enabled by default inside Angular. This article helped me to find out the solution. The second component calls common chunks from the first component (simple Array indexing) and it causes a strange error, described above.

But that is not really a good solution, moreover it is temporarily disabled.

Trying to eject

And the ngw.config.ts file should look like this.

ngw.config.ts

Ok, let’s try one more time. Build. Run. SUCCESS!

Success!

And that’s it! It works, finally, without any errors and the components seem to be really independent and almost standalone (Zone.js dependency is still needed). And it’s working in all browsers, including IE11.

In my personal opinion, it was not the simplest and easiest way to build a web component. So Angular team has work to do. To be honest, the custom elements feature was released in order to use components inside an Angular application and not outside of it in an independent project. The size of generated source file is also not as small as I would wish. Possibly the 7 version of Angular will offer us something new, at least Ivy engine looks really promising.

I did not use the ViewEncapsulation.Native inside the components for the sake of compatibility with all browser. If you really need it, be prepared that it won’t work in Edge, IE and FireFox too.

I hope, this article was helpful. I would appreciate any new ideas, suggestions or solutions.

Add a comment

Related posts:

Virtual Reality Check

Virtual reality is experiencing a resurgence in interest, including renewed attention toward medical applications. New VR research shows promise for applications like physical therapy, pain control…

Palmetto Hearts

Leo smushed a patty on the grill, eliciting a little sizzle and a whisp of white smoke. He took the bottle of beer Ray was holding for him. “As I was saying, it was so quiet, I could hear the…

Exploring the different types of escorts available in Mumbai

Mumbai is one of the largest cities in India, and it has a vibrant and diverse escort industry. As a client, it can be overwhelming to choose from the different types of escorts available in Mumbai…