Skip to content

Importing Stencil-generated custom elements in another Stencil project fails #2531

Open

Description

@lxg

Stencil version:

I'm submitting a:

[x ] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior:

When I create a set of custom elements with Stencil using the dist-custom-elements-bundle target and try to use them in another Stencil project by registering the custom element with customElements.define, I get the following errors in the console:

my-component.entry.js:2673 stencil Trying to lazily load component <ui-button> with style mode "undefined", but it does not exist.

my-component.entry.js:1906 Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
    at initializeComponent (my-component.entry.js:1906)
    at my-component.entry.js:2059

When I import the component in the same way in a project that isn’t Stencil, it works fine. (#2516 aside.)

Steps to reproduce:

I have created a demo here: https://github.com/lxg/stencil-elements-poc

  • The elements dir contains the elements library, currently containing only the ui-button element.
  • The component dir contains another Stencil project which demonstrates the issue.
  • The site dir contains an 11ty example site where importing the ui-button element works.

Activity

  1. jainanuj0812 commented on Oct 29, 2020

    @jainanuj0812

    Facing the same problem,

    Any update?

  2. Paitum commented on Nov 17, 2020

    @Paitum

    I am facing a similar problem. Is there clear guidance on how to use a Stencil Component Library's "custom elements" output within another Stencil Component Project?

    The use-case here is a set of shared-base-components being used within multiple application-web-components. This is analogous to Angular Material components being used within many Angular Applications.

    A consumer of one of the application would be responsible for loading the application web components and NOT be responsible for loading the shared components.

  3. Paitum commented on Nov 17, 2020

    @Paitum

    It appears only using dist-custom-elements-bundle was affecting my outcome. Once using BOTH dist and dist-custom-elements-bundle it started working. This could also be due to how my package.json file is configured to reference many undefined files within dist/.

    Clear documentation on this use-case would be a huge benefit. I assume StencilJS projects consuming other StencilJS components would be a use-case worth documenting, for both build-time linking and runtime lazyloading scenarios.

  4. SheepFromHeaven commented on Mar 22, 2021

    @SheepFromHeaven

    Any updates here? This is really keeping stencil from being scalable. I had a look into the stencil runtime and it seems the initializeComponent function does not care about the source of a component. It just looks if the build is lazy. There I currently no way to do this, as far as I see it!

  5. SheepFromHeaven commented on Mar 22, 2021

    @SheepFromHeaven

    BTW: Still a problem in 2.4.0

  6. MarkChrisLevy commented on Mar 26, 2021

    @MarkChrisLevy

    I made appropriate changes in stencil compiler and runtime and it now works - you may check it https://github.com/CODE-AND-MORE/stencil/tree/codeandmore (build and test or npm install github:CODE-AND-MORE/stencil-core-build). With those changes it is now possible to import components generated only as dist-custom-elements-bundle and use them with other stencil project that build lazy components. I'm not making a PUSH request yet, cause there are other things to be done to improve dist-custom-elements-bundle.

    Quick summary of changes:

    • component generated with dist-custom-elements-bundle can be used inside other stencil project, that builds lazy components
    • config of "dist-custom-elements-bundle" output target may now take "external" option, which is an array of string or RegExp - this will tell to rollup and stencil, that code and components of given modules should not be included in the bundle
    • if stencil config's outputTarget has "dist-custom-elements-bundle" and "dist" target is not present, types are also generated (you can set "typesDir" for "dist-custom-elements-bundle" target to decide where to save types, by default it is subdir "types" in output dir)
    • "includeGlobalScripts" in "dist-custom-elements-bundle" target config should be set to false if your component uses ionic or other collection of components that use global script (now stencil's default is true, but imho it should be false for dist-custom-elements-bundle)
    • config of "dist-custom-elements-bundle" output target may now take "defineFunctionName" option, which is the name of the function that will define custom elements from the collection, by default stencil uses defineCustomElements. It is very unconvienient when you must load components from multiple external collections and each collection uses the same name to define its elements, you will end up in making named imports import {defineCustomElements as defineMyCollectionElements} from ..., with that option you have nice and clean imports: import {defineMyElements} from "..."; import {defineOtherElements} from "..."
  7. MarkChrisLevy commented on Mar 27, 2021

    @MarkChrisLevy

    @lxg @SheepFromHeaven Can you test my fork and check it for your use case?

  8. lxg commented on Mar 28, 2021

    @lxg
    Author

    Hi, sorry, I’m not using Stencil in current projects anymore … switched to pure TSX for web components.

  9. SheepFromHeaven commented on Apr 8, 2021

    @SheepFromHeaven

    @MarkChrisLevy was on vacation. Will try 👍 thx!

  10. SheepFromHeaven commented on Apr 28, 2021

    @SheepFromHeaven

    Hey @MarkChrisLevy. I tried, but it did not seem to work. I installed your version in both of the projects. As soon as I import one of the components in my lazy bundle, the whole library is being imported 🙁

  11. MarkChrisLevy commented on Apr 29, 2021

    @MarkChrisLevy

    @SheepFromHeaven Hmm, I'm using it without any issues. Can you show me your stencil conf for building a component? See how I'm doing it: ionx.

  12. MarkChrisLevy commented on Jul 7, 2021

    @MarkChrisLevy

    As far I know Stencil team plans to improve CustomElements output target and would be great to hear more about it, any word @rwaskiewicz in this topic? Thanks.

  13. rwaskiewicz commented on Jul 8, 2021

    @rwaskiewicz
    Member

    Hey all 👋 I don't have anything concrete to share at the moment as far as timelines are concerned on specific improvements.

    @MarkChrisLevy did you have any specific thoughts on how to improve the CustomElements output target?

  14. added
    Awaiting Reply
    and removed on Jul 25, 2021
  15. MarkChrisLevy commented on Aug 18, 2021

    @MarkChrisLevy

    @rwaskiewicz Uhh, sorry for not answering, I didn't see your reply. There are several issues with current implementation of CustomElements, the main issue is that it cannot be used alongside with LazyLoad build - CustomElements components just doesn't load. Please have a look what I did to make it work and some nice improvements: #2531 (comment)

  16. added and removed
    Awaiting Reply
    on Sep 3, 2021
  17. danemesis commented on Jan 10, 2022

    @danemesis

    It does work for me on the latest stencil generated by npm init stencil

    package.json

    {
      "name": "core",
      "version": "0.0.1",
      "description": "Core set of material components",
      "main": "dist/index.cjs.js",
      "module": "dist/index.js",
      "es2015": "dist/esm/index.mjs",
      "es2017": "dist/esm/index.mjs",
      "types": "dist/types/components.d.ts",
      "collection": "dist/collection/collection-manifest.json",
      "collection:main": "dist/collection/index.js",
      "unpkg": "dist/core/core.esm.js",
      "repository": {
        "type": "git",
        "url": "https://github.com/ionic-team/stencil-component-starter.git"
      },
      "files": [
        "dist/",
        "loader/"
      ],
      "scripts": {
        "build": "stencil build --docs",
        "start": "stencil build --dev --watch --serve",
        "test": "stencil test --spec --e2e",
        "test.watch": "stencil test --spec --e2e --watchAll",
        "generate": "stencil generate"
      },
      "dependencies": {
        "@stencil/core": "^2.11.0",
        "@types/hammerjs": "^2.0.41",
        "hammerjs": "^2.0.8",
        "lozad": "^1.16.0"
      },
      "devDependencies": {
        "@stencil/postcss": "^2.1.0",
        "@stencil/sass": "^1.5.2",
        "@types/jest": "^26.0.24",
        "autoprefixer": "^10.4.2",
        "jest": "^26.6.3",
        "jest-cli": "^26.6.3",
        "puppeteer": "^10.0.0"
      },
      "license": "MIT"
    }
    

    stencil.config.ts

    import { Config } from '@stencil/core';
    import { sass } from '@stencil/sass';
    import { postcss } from '@stencil/postcss';
    import autoprefixer from 'autoprefixer';
    
    export const config: Config = {
      namespace: 'core',
      outputTargets: [
        {
          type: 'dist',
          esmLoaderPath: '../loader',
        },
        {
          type: 'dist-custom-elements',
        },
        {
          type: 'docs-readme',
        },
        {
          type: 'www',
          serviceWorker: null, // disable service workers
        },
      ],
      plugins: [
        sass(),
        postcss({
          plugins: [autoprefixer()],
        }),
      ],
    };
    

    stencil elements are just normal elements.
    in a project you want to reference you just add package as dependency
    and in global.ts you add import 'core';

  18. orl99 commented on Feb 25, 2022

    @orl99

    is there any update???

  19. added
    Resolution: Needs Investigation
    on Mar 25, 2022
  20. thibert commented on Apr 22, 2022

    @thibert

    +1 !!!
    I can't make it work in my project also.
    Too sad to not be able to build a WebComponent with Stencil which embed few components from my Stencil Library...
    It will work if you embed your entire collection but not acceptable in terms of weight and good practices.

    Hope it will be fixed soon :)

  21. thibert commented on May 17, 2022

    @thibert

    @rwaskiewicz do you have any news to share ? Thank you

  22. duncanhunter commented on Jul 3, 2023

    @duncanhunter

    Does Ionic/Stencil have any advice on how to write unit or e2e tests in a stencil project that depends on other stencil projects' compiled components?

    We are considering running these tests in Playwright or Cypress, as we have a mono repo with a base library of stencil components and another library of stencil components that use the base library. Ideally, we would prefer just to use the stencil testing setup.

    Before stencil v4 we just did a hacky direct path to the other library's component, but now it is not working and trying to find a better solution.

    Ideally, it would be better to have some guidance on registering custom elements from any source in the stencil unit or e2e tests.

    Or, if this is not possible, some reply to this thread would be much appreciated.

  23. duncanhunter commented on Dec 4, 2023

    @duncanhunter

    We reverted from having stencil project that depends on other stencil projects and avoid the headaches.

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

    Resolution: Needs Investigation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions