Skip to content

feat: component versioning #3138

Closed

Description

@danyball

Prerequisites

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

  1. added
    Feature: Want this? Upvote it!
    on Nov 13, 2021
  2. JSMike commented on Nov 19, 2021

    @JSMike

    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.

  3. splitinfinities commented on Dec 2, 2021

    @splitinfinities
    Contributor

    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!

  4. danyball commented on Dec 3, 2021

    @danyball
    Author

    Question, how would #3155 relate to this?

    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.

  5. danyball commented on Dec 3, 2021

    @danyball
    Author

    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):

    • at templates we can not use <mylib-button> because that tagname is probably transformed at runtime
    • at css we can not use tagnames at selector like ::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

  6. danyball commented on Apr 4, 2022

    @danyball
    Author

    Referring to my last post and the optionally requested feature: There is another open issue now: #3269

  7. 8 remaining items

  8. fcano-ut commented on Mar 13, 2024

    @fcano-ut

    For anyone looking for a workaround, we managed to make it working doing something like this:

    const backup = window.customElements;
    
    window.customElements = registry; // pass your registry here
    defineCustomElements(...) // call defineCustomElements
    window.customElements = backup; // restore the customElements global to what it was

    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

  9. mayerraphael commented on Mar 13, 2024

    @mayerraphael

    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

  10. FabioGimmillaro commented on Mar 13, 2024

    @FabioGimmillaro

    I wrote a script which manipulates the generates dist/esm sources to also accept a registry as an input parameter:

    const path = require("path");
    const fs = require("fs");
    
    const distPath = path.resolve(__dirname, "../dist/");
    const esmPath = `${distPath}\\esm`;
    
    const indexFileName = fs
      .readdirSync(esmPath)
      .find((fn) => fn.startsWith("index-") && fn.endsWith(".js"));
    
    const indexFile = `${esmPath}\\${indexFileName}`
    const fileContent = fs.readFileSync(indexFile, "utf-8");
    
    console.log("Adjust indexFile", indexFile);
    
    return fs.promises.writeFile(indexFile, fileContent.replace(/const customElements = win.customElements;/, "const customElements = options.registry ?? win.customElements;"), "utf-8")

    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 });

  11. mayerraphael commented on Mar 14, 2024

    @mayerraphael

    I wrote a script which manipulates the generates dist/esm sources to also accept a registry as an input parameter:

    const path = require("path");
    const fs = require("fs");
    
    const distPath = path.resolve(__dirname, "../dist/");
    const esmPath = `${distPath}\\esm`;
    
    const indexFileName = fs
      .readdirSync(esmPath)
      .find((fn) => fn.startsWith("index-") && fn.endsWith(".js"));
    
    const indexFile = `${esmPath}\\${indexFileName}`
    const fileContent = fs.readFileSync(indexFile, "utf-8");
    
    console.log("Adjust indexFile", indexFile);
    
    return fs.promises.writeFile(indexFile, fileContent.replace(/const customElements = win.customElements;/, "const customElements = options.registry ?? win.customElements;"), "utf-8")

    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 });

    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.

  12. fcano-ut commented on Mar 14, 2024

    @fcano-ut

    @mayerraphael I agree, the only "proper" solution is support at the Stencil level, but we need workarounds in case that takes a lot...

    If defineCustomElements is 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 Stencil

    const backup = window.customElements;
    
    new Promise(resolve => {
      window.customElements = registry; // pass your registry here
      resolve(defineCustomElements(...)); // call defineCustomElements
    }).then(() => {
      window.customElements = backup; // restore the customElements global to what it was
    });
  13. mayerraphael commented on Mar 14, 2024

    @mayerraphael

    @fcano-ut

    @mayerraphael I agree, the only "proper" solution is support at the Stencil level, but we need workarounds in case that takes a lot...

    If defineCustomElements is 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 Stencil

    const backup = window.customElements;
    
    new Promise(resolve => {
      window.customElements = registry; // pass your registry here
      resolve(defineCustomElements(...)); // call defineCustomElements
    }).then(() => {
      window.customElements = backup; // restore the customElements global to what it was
    });

    Lets say you have a shell and a microfrontend app, both having different Versions of your component library.

    // Register shell on global window.customElements.
    shell.defineCustomElements();
    
    // Register app.
    backup = window.customElements;
    window.customElements = myAppRegistry;
    microfrontend.defineCustomElements().then(() =>window.customElements = backup);

    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)

    // Register shell on global window.customElements.
    shell.defineCustomElements().then(() => {
      // Register app.
      backup = window.customElements;
      window.customElements = myAppRegistry;
      microfrontend.defineCustomElements().then(() =>window.customElements = backup);
    });

    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.

  14. fcano-ut commented on Mar 14, 2024

    @fcano-ut

    I get your point, @mayerraphael, you're right. This totally breaks if the defineCustomElements function 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 😐

  15. FabioGimmillaro commented on Mar 14, 2024

    @FabioGimmillaro

    @fcano-ut
    Wouldn't it be enough if StencilJS provided an API to add a new CustomElementRegistry when calling defineCustomElements?
    e.g.

    defineCustomElements(window, { customElements: new CustomElementRegistry() });

    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

    const { customElementsRegistry } = defineCustomElements(window);
  16. mayerraphael commented on Mar 14, 2024

    @mayerraphael

    I believe Scoped Custom Elements seems like the best solution, more so considering it's proposed as a web standard.
    Right now it this polyfill is adding support: https://www.npmjs.com/package/@webcomponents/scoped-custom-element-registry?activeTab=versions, but it's unclear if it could be used with Stencil. Has anyone tried this?

    Got it working by passing the custom registry as part of the the defineCustomElements method options.

    // Add polyfill before.
    
    const registryA = new CustomElementRegistry();
    
    class HostElementA extends HTMLElement {
        constructor() {
          super();
          this.attachShadow({
            mode: 'open',
            customElements: registryA,
          }).innerHTML = `
             <style>:host { background: red; }</style>
                Wrapper<br/>
            <stencil-button>Button inside scoped registry.</stencil-button>
          `;
    
         // Pass through registry as options.
          defineCustomElements(window, { registry: registryA }).catch(console.warn);
        }
    }
    
    customElements.define('el-a', HostElementA);

    The registry is just passed through to the define call via the defineCustomElements options.

    // Patch. Check if a custom registry is supplied.
    const registry = options.registry ?? window.customElements;
    if (!exclude.includes(tagName) && !registry.get(tagName)) {
        cmpTags.push(tagName);
        registry.define(tagName, proxyComponent(HostElement, cmpMeta, 1 /* PROXY_FLAGS.isElementConstructor */));
    }

    In html you can then test it. The button inside el-a works, the button outside does not.

    <el-a>dsa</el-a>
    <br/>
     This button should not work:
    <stencil-button>Test</patternlib-button>
    

    image
    @rwaskiewicz Allowing a custom elements registry in the defineCustomElements options object could work?

    @mayerraphael How were you able to add the custom registry as an option to the defineCustomElements method? Did you make changes in a local version of stencil?

    Exactly, i just patched the runtime.

  17. AndreBarr commented on Mar 14, 2024

    @AndreBarr

    I wrote a script which manipulates the generates dist/esm sources to also accept a registry as an input parameter:

    const path = require("path");
    const fs = require("fs");
    
    const distPath = path.resolve(__dirname, "../dist/");
    const esmPath = `${distPath}\\esm`;
    
    const indexFileName = fs
      .readdirSync(esmPath)
      .find((fn) => fn.startsWith("index-") && fn.endsWith(".js"));
    
    const indexFile = `${esmPath}\\${indexFileName}`
    const fileContent = fs.readFileSync(indexFile, "utf-8");
    
    console.log("Adjust indexFile", indexFile);
    
    return fs.promises.writeFile(indexFile, fileContent.replace(/const customElements = win.customElements;/, "const customElements = options.registry ?? win.customElements;"), "utf-8")

    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 });

    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.

    @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.

  18. johnjenkins commented on Mar 19, 2025

    @johnjenkins
    Contributor

    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 via dist-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.

  19. christian-bromann commented on Mar 19, 2025

    @christian-bromann
    Member

    @johnjenkins this is awesome! Do you think there is something we can do to bring this into Stencil?

  20. johnjenkins commented on Mar 19, 2025

    @johnjenkins
    Contributor

    @christian-bromann yes for sure - could be quite simple:

    • Stencil core makes and exports setTagTransformer() / getTagTransformer() / transformTag() < they just set / get / use an internal variable
    • Stencil's h() runs any incoming tags through transformTag()
    • Stencil does customElements.define(transformTag('....')) wherever it needs
    • Users export setTagTransformer() from their src/index.ts if it's something they want to expose
    • Users must use transformTag() 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

  21. christian-bromann commented on Mar 19, 2025

    @christian-bromann
    Member

    Only question is if that's something Stencil wants to do vs (or in addition to) better integration with Scoped Custom Element Registries

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature: Want this? Upvote it!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions