ionic-team / stencil

A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
https://stenciljs.com
Other
12.6k stars 791 forks source link

bug: dist-custom-elements issues when used inside www output target app #3206

Open MarkChrisLevy opened 2 years ago

MarkChrisLevy commented 2 years ago

Prerequisites

Stencil Version

2.12.1

Current Behavior

If stencil is used as a standalone app compiler (www output target) there are two issues with components compiled with dist-custom-elements output target:

Expected Behavior

Components compiled with dist-custom-elements should be loaded and life-cycle methods should be executed.

Steps to Reproduce

Code Reproduction URL

https://github.com/MarkChrisLevy/stencil-www-issues1

Additional Information

This issue was already reported in #2531, but because it was reported for Stencil 1.x I decided to open new issue.

In my comment to #2531 I described few changes, that I made in order to fix loading issues (and some other stuff related to custom-elements-bundle output target) - all changes are available here: https://github.com/CODE-AND-MORE/stencil/tree/codeandmore-2.12.1

I would like to make a PR to fix those issues, but I need some guidance from Stencil team, to make sure that my solution is acceptable by Stencil.

Life-cycle methods issue is easy to fix, but... it touches the very basic idea how Stencil runtime works. Right now, when project is built, Stencil writes different kind of flags, those flags decides whether some functionality should be run in the runtime or not... and when in a project there is a life-cycle method e.g. componentDidLoad, Stencil writes a flag, that runtime should run componentDidLoad, if there were no such a method in a project it will not be called in the runtime which applies to external components as well. Because of the "flagging" there can be other issues, so I think that Stencil team must look at this issue from much wider point of view.

rwaskiewicz commented 2 years ago

👋

First, thanks for the detailed report! It was super helpful 🙂

I confirmed that this is indeed an issue - specifically the issue appears to be that Stencil doesn't assign an internal ID to components are compiled using dist-custom-elements with an externalized runtime. So when we try to use the lazy load runtime on such components, the loading fails due to that lack of ID.

A potential workaround here (depending on one's use case) is to not externalize the runtime (by setting externalRuntime: false) in the component library using dist-custom-elements.

I haven't taken a close look at the changes made in https://github.com/CODE-AND-MORE/stencil/tree/codeandmore-2.12.1 too closely yet. In the mean time, I'll label this issue to get it ingested and discuss with the team some of our options here.

MarkChrisLevy commented 2 years ago

Setting externalRuntime = true doesn't fix the loading issue - in the example component (https://github.com/MarkChrisLevy/stencil-www-issues2/blob/0d2c42bffd81cb1bd2052464bcf253a99f8187f2/stencil.config.ts#L10) it was already set to true.

My changes, that fixes loading issue were made without wider knowledge of Stencil guts, so it is very likely that it will not be helpful at all...

alexabreu commented 3 months ago

This is still an ongoing issue. Any updates?