You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Importing Stencil-generated custom elements in another Stencil project fails #2531
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.)
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.
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.
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!
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 "..."
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 🙁
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.
@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)
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';
+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.
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.
Activity
jainanuj0812 commentedon Oct 29, 2020
Facing the same problem,
Any update?
Paitum commentedon Nov 17, 2020
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.
Paitum commentedon Nov 17, 2020
It appears only using
dist-custom-elements-bundlewas affecting my outcome. Once using BOTHdistanddist-custom-elements-bundleit started working. This could also be due to how mypackage.jsonfile is configured to reference many undefined files withindist/.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.
SheepFromHeaven commentedon Mar 22, 2021
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!
SheepFromHeaven commentedon Mar 22, 2021
BTW: Still a problem in 2.4.0
MarkChrisLevy commentedon Mar 26, 2021
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:
import {defineCustomElements as defineMyCollectionElements} from ..., with that option you have nice and clean imports:import {defineMyElements} from "..."; import {defineOtherElements} from "..."MarkChrisLevy commentedon Mar 27, 2021
@lxg @SheepFromHeaven Can you test my fork and check it for your use case?
lxg commentedon Mar 28, 2021
Hi, sorry, I’m not using Stencil in current projects anymore … switched to pure TSX for web components.
SheepFromHeaven commentedon Apr 8, 2021
@MarkChrisLevy was on vacation. Will try 👍 thx!
SheepFromHeaven commentedon Apr 28, 2021
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 🙁
MarkChrisLevy commentedon Apr 29, 2021
@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.
MarkChrisLevy commentedon Jul 7, 2021
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.
rwaskiewicz commentedon Jul 8, 2021
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?
MarkChrisLevy commentedon Aug 18, 2021
@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)
danemesis commentedon Jan 10, 2022
It does work for me on the latest stencil generated by
npm init stencilpackage.json
stencil.config.ts
stencil elements are just normal elements.
in a project you want to reference you just add package as dependency
and in
global.tsyou addimport 'core';orl99 commentedon Feb 25, 2022
is there any update???
thibert commentedon Apr 22, 2022
+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 :)
thibert commentedon May 17, 2022
@rwaskiewicz do you have any news to share ? Thank you
duncanhunter commentedon Jul 3, 2023
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.
duncanhunter commentedon Dec 4, 2023
We reverted from having stencil project that depends on other stencil projects and avoid the headaches.