Repository navigation
Description
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already include this feature request, without success.
Describe the Feature Request
It should be possible to set a suffix to the component names that are registered via customElements.define to be better hardened when 2 custom elements are "living" on the same page in different versions with the same name.
Describe the Use Case
We have our main landing page that is organized with a content management system. And we have several apps, written with angular, react, vue - all using our stencil lib. We have 2 Apps that are loaded at this main page. These 2 Apps are using different versions of our stencil lib that can cause issues on that page. Depending on which app is loaded first on that page and registered "their" customElements first. If app1 uses lib-v-1 with the old, blue button and app2 uses lib-v-2 with the new red one and app1 is loaded first, all the buttons of app2 are blue.
Describe Preferred Solution
Example for mylib version 1.1.0.
At build time:
Add a customElements.define with <mylib-button> and <mylib-button-1-1-0>.
(optional feature) At build time of a consumer of that mylib:
Add a 3. define: <mylib-button-consumername>
Describe Alternatives
Waiting for html standard to implement something like customElements.define('mylib-button', MyButton, { version: 1.1.0 }).
Related Code
No response
Additional Information
I found a solution of how it could work of a lit-element based lib:
https://github.com/axa-ch-webhub-cloud/pattern-library/blob/develop/COMPONENT_VERSIONING.md
Activity
danyball commentedon Nov 17, 2021
Another approach could be https://open-wc.org/docs/development/scoped-elements/
JSMike commentedon Nov 19, 2021
Hi All, I just spoke with some on the Stencil team about versioning and was pointed to this issue. My team has a similar scenario where we need to support design system components that could potentially be used in multiple applications that are present on the same page, and there's potential that each application could be using a different version of the design system's components.
Especially for the tooling that wraps Stencil components for other frameworks (Angular/React/Vue) It would be great if we were able to keep the framework selectors the same so that each version of the library wouldn't result in a breaking change for consuming applications. IMO it would be best if Stencil could keep track of a generated hash (or a value from config) during build time and append that hash to the web component selector, but keep the original selector for the wrapped framework libraries. Under this example the selector would be defined as
<mylib-button>, the Angular/React/Vue components would behave as if<mylib-button>was unchanged as the selector, but would be configured to point to use<mylib-button-ba583>.I do believe that the scoped elements option is the proper long-term solution, but until the spec is finalized and implemented in enough browsers we'll need a pattern to manage versioning. Since the listed frameworks already hook/scope onto an element this would provide a path forward for those using wrapped stencil components without having to introduce breaking changes to those framework specific libraries with every update.
splitinfinities commentedon Dec 2, 2021
Question, how would #3155 relate to this? I feel like, based on the behavior of the compiler's results, either this issue or that one may need to happen first. Or are they effectively accomplishing the same objectives, for the versioning feature? Super interested in forming a strategy around this versioning and bundling behavior so I want to dig deeper. Any advice is super helpful!
danyball commentedon Dec 3, 2021
3155 describes a solution for the "collections" folder and a use case where 2 stencil projects working together. I dont have experience with that case but it sounds that this issue here is solving 3155. If the version of a component is being reflect to its tagname at build time, another stencil project could consume that tag. But an app could also use tag version-tag.
danyball commentedon Dec 3, 2021
My optional feature request could be covered by "tagNameTransform" but that comes with some points which should be noticed when writing the stencil lib (if a component uses another):
<mylib-button>because that tagname is probably transformed at runtime::slotted(mylib-button)Its possible to solve that but maybe we could have a look at that if we/you design a solution for my optional request.
This guy describes how to solve this: https://dev.to/sanderand/running-multiple-versions-of-a-stencil-design-system-without-conflicts-2f46
danyball commentedon Apr 4, 2022
Referring to my last post and the optionally requested feature: There is another open issue now: #3269
8 remaining items
fcano-ut commentedon Mar 13, 2024
For anyone looking for a workaround, we managed to make it working doing something like this:
That works without any change in Stencyl, but it's not pretty. I'm hoping they can add library support for this soon as @mayerraphael suggested, to stop using this kind of "hacky" workaround
mayerraphael commentedon Mar 13, 2024
This only works in older versions of Stencil. In newer versions defineCustomElements is async.
If you have other registrations in between it breaks.
See: https://github.com/ionic-team/stencil/blob/41f877ec48200dee0483691b4e5e519073d392dd/src/compiler/output-targets/dist-lazy/lazy-output.ts#L189
FabioGimmillaro commentedon Mar 13, 2024
I wrote a script which manipulates the generates dist/esm sources to also accept a registry as an input parameter:
I wrote the script for StencilJS 2.22.3 and the "dist" output target so I don't know if this also works for StencilJS versions >= 3.
I'm currently in my testing phase so I don't know if this is a viable solution for most use-cases
The call to defineCustomElements should look like this:
defineCustomElements(window, { registry: $newRegistry });mayerraphael commentedon Mar 14, 2024
Again, defineCustomElements is async in newer Stencil Versions.
Imagine you have multiple microfrontends each requiring a custom registry, overwriting the global window.customElements Registry in an async initialization process will be pure chaos. I highly recommend not doing that.
The only viable option is that the internal define call in defineCustomElements has the correct registry so it works nicely with the async nature of JS.
fcano-ut commentedon Mar 14, 2024
@mayerraphael I agree, the only "proper" solution is support at the Stencil level, but we need workarounds in case that takes a lot...
If
defineCustomElementsis a promise, something like this should work and be safe I guess? We haven't tested it, but we'll figure it out when we update Stencilmayerraphael commentedon Mar 14, 2024
@fcano-ut
Lets say you have a shell and a microfrontend app, both having different Versions of your component library.
It could happen that the shell is not done regsitering while you override the global window.customElements and also registering other versioned components in between => Chaos.
The only thing helping is synchronizing, but this blocks your frontend (one after the other => slow)
But i dont wannt to know what other Stencil processes running async this messes with. So just don't do it.
It would be easier to patch the Stencil runtime (index.js) by providing the registry in the options than guaranteeing that overriding the global registry works in a correct way.
fcano-ut commentedon Mar 14, 2024
I get your point, @mayerraphael, you're right. This totally breaks if the
defineCustomElementsfunction has async steps inside and if you register multiple micro-frontends at once, since the order of execution will not be guaranteed. Thanks for thinking this through. We'll abstain for updating stencil for now I guess 😐FabioGimmillaro commentedon Mar 14, 2024
@fcano-ut
Wouldn't it be enough if StencilJS provided an API to add a new CustomElementRegistry when calling defineCustomElements?
e.g.
or return a customElementsRegistry when calling defineCustomElements?
Whether a new customElementsRegistry is created could be configured in the stencil.config.ts by adding a flag in the extras section
mayerraphael commentedon Mar 14, 2024
Exactly, i just patched the runtime.
AndreBarr commentedon Mar 14, 2024
@mayerraphael Isn't the script @FabioGimmillaro is running here, the same as the patch you are doing? The global window.customElements is not being overritten.
johnjenkins commentedon Mar 19, 2025
Hey all - I recently had to tackle this myself - here's a demo of my full solution, hopefully it's helpful :)
(unfortunately this only works when using the
dist/ lazy-loader output - not individual components viadist-custom-elements:( )https://github.com/johnjenkins/stencil-prefix-demo/
Centrally registering a namespace, then using that register to augment Stencil's rendering
h()function means all internal JSX is handled / transformed during render.christian-bromann commentedon Mar 19, 2025
@johnjenkins this is awesome! Do you think there is something we can do to bring this into Stencil?
johnjenkins commentedon Mar 19, 2025
@christian-bromann yes for sure - could be quite simple:
setTagTransformer()/getTagTransformer()/transformTag()< they just set / get / use an internal variableh()runs any incoming tags throughtransformTag()customElements.define(transformTag('....'))wherever it needssetTagTransformer()from theirsrc/index.tsif it's something they want to exposetransformTag()within their codebase with any dom api (e.g.this.host.querySelector(transformTag('...')))Think that's about it
Only question is if that's something Stencil wants to do vs (or in addition to) better integration with Scoped Custom Element Registries
christian-bromann commentedon Mar 19, 2025
It seems like the API is available in Canary behind a flag and under development in Firefox. So I assume we won't see this stable within the next year or so but there are polyfills available we could ship. I think it makes sense to look into implementing it and later integrate it into Scoped Custom Element Registries.