output-targets: bug: react-output-target not wrapper Ionic components since upgrade 0.6.0

Prerequisites

Stencil Version

4.22.3

Stencil Framework Output Target

React

Stencil Framework Output Target Version

0.7.4

Current Behavior

I have DS in Web Components using @ionic/core to provide wrappers to React and Vue. I am using react-ouput-target 0.5.3 and for now everything works but when I upgrade to 0.7.4 I am facing with some problems in components that use @ionic/core

image

Components that not uses @ionic/core is working fine

Expected Behavior

I need to keep the same current behavior

Steps to Reproduce

  • Upgrade react-output-target to 0.7.4
  • Update stencil.config to new approach
  • Change tsconfig to
"module": "esnext",
"moduleResolution": "bundler",
  • Generate just esm modules

Code Reproduction URL

Additional Information

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 14
  • Comments: 25 (10 by maintainers)

Most upvoted comments

@christian-bromann I’ve discovered something quite interesting that might point us in the right direction.

Looking at ionic-button, I noticed it has two distinct stylesheets -> button.md and button.ios https://github.com/ionic-team/ionic-framework/blob/2df6d2ce6ec7a16ddbeecdf748d6621b7607bc14/core/src/components/button/button.tsx#L23-L30

From what I understand, the function responsible for determining which CSS file to use is the one that gets the default Ionic “mode”: https://github.com/stenciljs/core/blob/af2acd44802b7fdc846e50846af2243d4905823c/src/runtime/mode.ts#L10 https://github.com/ionic-team/ionic-framework/blob/2df6d2ce6ec7a16ddbeecdf748d6621b7607bc14/core/src/global/ionic-global.ts#L12-L15

In the current version of react-output-target, for some reason, this default mode isn’t being set properly, which affects how component styles are selected and applied. I’ve confirmed this in the browser debugger with the new version.

New Version React Output Target New Version React Output Target

When compared to the previous version’s behavior:

Old Version React Output Target Old Version React Output Target Old Version React Output Target

I’ve checked that setting the mode via stencil/core resolves this issue and restores the expected behavior.

import { Component, Host, h, setMode } from "@stencil/core";

setMode(() => "md");

@Component({
  tag: "my-ionic-component",
  shadow: true,
})
export class MyIonicComponent {
  render() {
    return (
      <Host>
        <ion-button>
          This is a wrapper of ion-button and it's not working
        </ion-button>
      </Host>
    );
  }
}
Workaround

If you can provide a minimal reproducible example, I am happy to take a look.

@christian-bromann

I’ve forked the designsystem-boilerplate and created a minimal example to reproduce the issue.

Basically, I just created a global.ts to import the "@ionic/core", added a new component my-ionic-component that uses Ionic components inside the render method and import it inside the React app.

Image

As you can see, the component is not rendered correctly in the React app, the wrapper of ion-button is completely ignored and if check the network tab, the ion-button component is not loaded at all.

This is exactly the same issue I have in my real project, where I use Ionic components inside a Stencil component and then import it into a React app. It just work well if I use a pure Stencil component without Ionic components.

But this behavior was working in the react-output-target before upgrade of 0.7.4, how you can check here

It’s enough to help you to check this issue?

@christian-bromann After further investigation, I’ve found some more important details about this issue:

In version 0.5.3, the setMode function is indeed executed by an Ionic script, as shown in the screenshots

Image Image Image

Custom function in global script to test

import "@ionic/core";

export default function () {
  console.debug("From global.ts");
}
Image

The absence of defined mode and styles not being applied stems from the fact that Ionic’s global initialization function isn’t being executed in the newer version, Or more precisely, no global scripts are executed at all in the new version of the react output target.

As you can see in the new version, when I run the same setup, no global scripts isn’t executed.

Image Image Image

In the new version, I noticed components are being defined this way:

import { p as proxyCustomElement, H, h, c as Host } from "/designsystem-boilerplate/packages/core/components/p-D_VKWJ14.js";
import { d as defineCustomElement$3 } from "/designsystem-boilerplate/packages/core/components/p-CcMhn3NU.js";
import { d as defineCustomElement$2 } from "/designsystem-boilerplate/packages/core/components/p-CxQc7pn0.js";

const MyIonicComponent$1 = /*@__PURE__*/ proxyCustomElement(class MyIonicComponent extends H {
    constructor() {
        super();
        this.__registerHost();
        this.__attachShadow();
    }
    render() {
        return (h(Host, { key: '38b2e85ff675b1dd35ad61062b587299f5240316' }, h("ion-button", { key: '60c42751f5450acac8cad16eb96366dc4d8c72ee' }, "This is a wrapper of ion-button and it's not working")));
    }
}, [1, "my-ionic-component"]);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["my-ionic-component", "ion-button", "ion-ripple-effect"];
    components.forEach(tagName => { switch (tagName) {
        case "my-ionic-component":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, MyIonicComponent$1);
            }
            break;
        case "ion-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "ion-ripple-effect":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}

const MyIonicComponent = MyIonicComponent$1;
const defineCustomElement = defineCustomElement$1;

export { MyIonicComponent, defineCustomElement };

while in the old version, the index.js from react-output-target executed the global script configured in the loader

import { b as bootstrapLazy } from './index-DK9UIEbD.js';
export { s as setNonce } from './index-DK9UIEbD.js';
import { g as globalScripts } from './app-globals-Be5sV2vT.js';
import './ionic-global-vVi_O9W_.js';
import './index-DMSY4CrR.js';

const defineCustomElements = async (win, options) => {
  if (typeof window === 'undefined') return undefined;
  await globalScripts();
  return bootstrapLazy(JSON.parse

Thanks for verifying. This nails it down to this list of changes that may caused this issue. I am not entirely sure which one did cause it but it narrows down the origin of the problem.

@christian-bromann I’ve tried run with 0.6.0 and I received the same error

Image

But if I downgraded @stencil/react-output-target to 0.5.3 it’s working well

Image

Another question, when trying to run my Next.js project, I encountered the following error:

Collecting page data .[Error: Cannot find module '@stencil/react-output-target/runtime'

Upon reviewing your designsystem-boilerplate repository, I noticed that @stencil/react-output-target is explicitly added within the Next.js project’s dependencies.

This leads me to a question regarding best practices for distributed design systems:

Is this an ideal approach for distributed systems, given that it necessitates adding an external dependency to what is intended to be a self-contained design system?

Hi @christian-bromann, I just opened a pull request to make it easier to check the changes

Thanks!

@felipefialho thanks for providing the reproduction case. It doesn’t seem to be straightforward to mix Ionic components into Stencil components. I am seeing that Ionic components are just embedded into the projects runtime which I am not sure if that is a good thing. The biggest issue I see is that the component styles aren’t properly applied to the shadow root.

This is my current diff:

diff --git a/apps/next/src/app/page.tsx b/apps/next/src/app/page.tsx
index ea31b9c..05df043 100644
--- a/apps/next/src/app/page.tsx
+++ b/apps/next/src/app/page.tsx
@@ -1,4 +1,4 @@
-import { MyComponent, MyCounter } from '@placid/react'
+import { MyComponent, MyCounter, MyIonicComponent } from '@placid/react'
 
 export default function Home() {
   return (
@@ -15,6 +15,7 @@ export default function Home() {
           </a>
         </header>
         <MyComponent first="Stencil" last="'Don't call me a framework' JS"></MyComponent>
+        <MyIonicComponent />
         <div>
           <MyCounter startCount={42}></MyCounter>
         </div>
diff --git a/packages/core/package.json b/packages/core/package.json
index 89a6008..c04d2a6 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -64,7 +64,7 @@
     "@stencil/angular-output-target": "^0.10.2",
     "@stencil/react-output-target": "^1.0.2",
     "@stencil/vue-output-target": "^0.10.7",
-    "@stencil/core": "^4.29.3",
+    "@stencil/core": "^4.33.0",
     "@types/jest": "^29.5.6",
     "@types/node": "^16.18.98",
     "@types/react": "^18.3.3",
diff --git a/packages/core/src/components/my-ionic-component/my-ionic-component.tsx b/packages/core/src/components/my-ionic-component/my-ionic-component.tsx
index 17d7704..e3a7ef5 100644
--- a/packages/core/src/components/my-ionic-component/my-ionic-component.tsx
+++ b/packages/core/src/components/my-ionic-component/my-ionic-component.tsx
@@ -1,14 +1,26 @@
 import { Component, Host, h } from "@stencil/core";
+import { defineCustomElement as defineIonButton } from "@ionic/core/components/ion-button";
+
+// Set iOS mode
+// @ts-expect-error
+window.Ionic = {
+  config: {
+    mode: 'ios'
+  }
+}
+
+defineIonButton()
 
 @Component({
   tag: "my-ionic-component",
+  styleUrl: "my-ionic-component.css",
   shadow: true,
 })
 export class MyIonicComponent {
   render() {
     return (
       <Host>
-        <ion-button>This is a wrapper of ion-button and it's not working</ion-button>
+        <ion-button mode="md">This is a wrapper of IonButton and it's not working</ion-button>
       </Host>
     );
   }
diff --git a/packages/core/src/components/my-ionic-component/readme.md b/packages/core/src/components/my-ionic-component/readme.md
index 10eaacd..012f050 100644
--- a/packages/core/src/components/my-ionic-component/readme.md
+++ b/packages/core/src/components/my-ionic-component/readme.md
@@ -5,20 +5,6 @@
 <!-- Auto Generated Below -->
 
 
-## Dependencies
-
-### Depends on
-
-- ion-button
-
-### Graph
-```mermaid
-graph TD;
-  my-ionic-component --> ion-button
-  ion-button --> ion-ripple-effect
-  style my-ionic-component fill:#f9f,stroke:#333,stroke-width:4px
-```
-
 ----------------------------------------------
 
 *Built with [StencilJS](https://stenciljs.com/)*
diff --git a/packages/core/src/global/global.css b/packages/core/src/global/global.css
index e69de29..41917f9 100644
--- a/packages/core/src/global/global.css
+++ b/packages/core/src/global/global.css
@@ -0,0 +1,6 @@
+@import url('../../node_modules/@ionic/core/css/ionic.bundle.css');
+@import url('../../node_modules/@ionic/core/css/palettes/dark.class.css');
+
+ion-button {
+  color: red;
+}
\ No newline at end of file
diff --git a/packages/core/src/global/global.ts b/packages/core/src/global/global.ts
index 4dbeef7..22c2465 100644
--- a/packages/core/src/global/global.ts
+++ b/packages/core/src/global/global.ts
@@ -1 +1,2 @@
-import "@ionic/core";
+// import "@ionic/core";
+//
\ No newline at end of file
diff --git a/packages/core/stencil.config.ts b/packages/core/stencil.config.ts
index a6c5405..b48a3bd 100644
--- a/packages/core/stencil.config.ts
+++ b/packages/core/stencil.config.ts
@@ -109,6 +109,7 @@ const excludeComponents = [
 export const config: Config = {
   namespace: "core",
   globalScript: "src/global/global.ts",
+  globalStyle: "src/global/global.css",
   outputTargets: [
     {
       type: "dist",
@@ -155,6 +156,6 @@ export const config: Config = {
     },
   ],
   testing: {
-    browserHeadless: "new",
+    browserHeadless: true,
   },
 };
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bfbeafc..c9f7954 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -158,7 +158,7 @@ importers:
         version: link:../../packages/react
       '@stencil/react-output-target':
         specifier: 0.7.1
-        version: 0.7.1(@stencil/core@4.32.0)(@types/react@18.3.23)(react@18.3.1)
+        version: 0.7.1(@stencil/core@4.33.0)(@types/react@18.3.23)(react@18.3.1)
       next:
         specifier: 14.2.5
         version: 14.2.5(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(sass@1.89.1)
@@ -465,16 +465,16 @@ importers:
     devDependencies:
       '@stencil/angular-output-target':
         specifier: ^0.10.2
-        version: 0.10.2(@stencil/core@4.32.0)
+        version: 0.10.2(@stencil/core@4.33.0)
       '@stencil/core':
-        specifier: ^4.29.3
-        version: 4.32.0
+        specifier: ^4.33.0
+        version: 4.33.0
       '@stencil/react-output-target':
         specifier: ^1.0.2
-        version: 1.0.3(@stencil/core@4.32.0)(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
+        version: 1.0.3(@stencil/core@4.33.0)(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
       '@stencil/vue-output-target':
         specifier: ^0.10.7
-        version: 0.10.7(@stencil/core@4.32.0)(vue-router@4.5.1(vue@3.5.16(typescript@5.8.3)))(vue@3.5.16(typescript@5.8.3))
+        version: 0.10.7(@stencil/core@4.33.0)(vue-router@4.5.1(vue@3.5.16(typescript@5.8.3)))(vue@3.5.16(typescript@5.8.3))
       '@types/jest':
         specifier: ^29.5.6
         version: 29.5.14
@@ -513,7 +513,7 @@ importers:
         version: link:../core
       '@stencil/react-output-target':
         specifier: ^1.0.2
-        version: 1.0.3(@stencil/core@4.32.0)(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
+        version: 1.0.3(@stencil/core@4.33.0)(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
       react:
         specifier: ^18.3.1
         version: 18.3.1
@@ -541,7 +541,7 @@ importers:
         version: link:../core
       '@stencil/vue-output-target':
         specifier: ^0.10.7
-        version: 0.10.7(@stencil/core@4.32.0)(vue-router@4.5.1(vue@3.5.16(typescript@5.8.3)))(vue@3.5.16(typescript@5.8.3))
+        version: 0.10.7(@stencil/core@4.33.0)(vue-router@4.5.1(vue@3.5.16(typescript@5.8.3)))(vue@3.5.16(typescript@5.8.3))
     devDependencies:
       typescript:
         specifier: ^5.7.3
@@ -3962,6 +3962,11 @@ packages:
     engines: {node: '>=16.0.0', npm: '>=7.10.0'}
     hasBin: true
 
+  '@stencil/core@4.33.0':
+    resolution: {integrity: sha512-3Xdn5mh+qz7/vgMAy5Ps+U37Y7qikhNT9PKNmd6ZlzBxomDazsX+ORClE+jswQo2j7/VX8i38nAqwB71i1IA7Q==}
+    engines: {node: '>=16.0.0', npm: '>=7.10.0'}
+    hasBin: true
+
   '@stencil/react-output-target@0.7.1':
     resolution: {integrity: sha512-KSwX453qTEinzOPU0JrWTFDau7mP/UlaQSc79ADQExx3pct1y0BgfA/SIFLovzTwpSWM75gegd6eUtrX+RE1dA==}
     peerDependencies:
@@ -16214,9 +16219,9 @@ snapshots:
 
   '@speed-highlight/core@1.2.7': {}
 
-  '@stencil/angular-output-target@0.10.2(@stencil/core@4.32.0)':
+  '@stencil/angular-output-target@0.10.2(@stencil/core@4.33.0)':
     dependencies:
-      '@stencil/core': 4.32.0
+      '@stencil/core': 4.33.0
 
   '@stencil/core@4.20.0': {}
 
@@ -16231,10 +16236,21 @@ snapshots:
       '@rollup/rollup-win32-arm64-msvc': 4.34.9
       '@rollup/rollup-win32-x64-msvc': 4.34.9
 
-  '@stencil/react-output-target@0.7.1(@stencil/core@4.32.0)(@types/react@18.3.23)(react@18.3.1)':
+  '@stencil/core@4.33.0':
+    optionalDependencies:
+      '@rollup/rollup-darwin-arm64': 4.34.9
+      '@rollup/rollup-darwin-x64': 4.34.9
+      '@rollup/rollup-linux-arm64-gnu': 4.34.9
+      '@rollup/rollup-linux-arm64-musl': 4.34.9
+      '@rollup/rollup-linux-x64-gnu': 4.34.9
+      '@rollup/rollup-linux-x64-musl': 4.34.9
+      '@rollup/rollup-win32-arm64-msvc': 4.34.9
+      '@rollup/rollup-win32-x64-msvc': 4.34.9
+
+  '@stencil/react-output-target@0.7.1(@stencil/core@4.33.0)(@types/react@18.3.23)(react@18.3.1)':
     dependencies:
       '@lit/react': 1.0.7(@types/react@18.3.23)
-      '@stencil/core': 4.32.0
+      '@stencil/core': 4.33.0
       html-react-parser: 5.2.5(@types/react@18.3.23)(react@18.3.1)
       react-dom: 18.3.1(react@18.3.1)
       ts-morph: 22.0.0
@@ -16242,10 +16258,10 @@ snapshots:
       - '@types/react'
       - react
 
-  '@stencil/react-output-target@1.0.3(@stencil/core@4.32.0)(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)':
+  '@stencil/react-output-target@1.0.3(@stencil/core@4.33.0)(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)':
     dependencies:
       '@lit/react': 1.0.7(@types/react@18.3.23)
-      '@stencil/core': 4.32.0
+      '@stencil/core': 4.33.0
       html-react-parser: 5.2.5(@types/react@18.3.23)(react@18.3.1)
       react: 18.3.1
       react-dom: 18.3.1(react@18.3.1)
@@ -16254,11 +16270,11 @@ snapshots:
     transitivePeerDependencies:
       - '@types/react'
 
-  '@stencil/vue-output-target@0.10.7(@stencil/core@4.32.0)(vue-router@4.5.1(vue@3.5.16(typescript@5.8.3)))(vue@3.5.16(typescript@5.8.3))':
+  '@stencil/vue-output-target@0.10.7(@stencil/core@4.33.0)(vue-router@4.5.1(vue@3.5.16(typescript@5.8.3)))(vue@3.5.16(typescript@5.8.3))':
     dependencies:
       vue: 3.5.16(typescript@5.8.3)
     optionalDependencies:
-      '@stencil/core': 4.32.0
+      '@stencil/core': 4.33.0
       vue-router: 4.5.1(vue@3.5.16(typescript@5.8.3))
 
   '@storybook/addon-actions@8.6.14(storybook@8.6.14)':

As you can see, the component is not rendered correctly in the React app, the wrapper of ion-button is completely ignored and if check the network tab, the ion-button component is not loaded at all.

The component is loaded correctly. A shadow root was created. You can see that the Ionic component is embedded in packages/core/components/my-ionic-component.js. The issue is that the styles aren’t applied properly.

It makes sense?

If you can provide a minimal reproducible example, I am happy to take a look.

Is this an ideal approach for distributed systems, given that it necessitates adding an external dependency to what is intended to be a self-contained design system?

You are making a good point here. Mind raising a separate issue for that?

@felipefialho It would be always helpful to make the reproduction case as small as possible but what you provided works to inspect the issue. I personally haven’t had the time to look into it.