output-targets: bug: Tree shaking is not working with React output target

Prerequisites

Stencil Version

2.9

Stencil Framework Output Target

React

Stencil Framework Output Target Version

0.3.1

Current Behavior

Hi, I am using react output target to generate a tree shakable bundle of the React components. Stencil config file below.

      componentCorePackage: `@freshworks/${packageName}`, // name in the package.json should be used
      proxiesFile: './crayons-react/components.ts',
      customElementsDir: 'dist/components',
      includeImportCustomElements: true,
    }) 

Usage in the consuming app, import { FwButton } from “@freshworks/crayons/react”;

when I run npm run build, in the main script that gets bundled, I see code for all the components added and tree shaking is not working.

Expected Behavior

Tree shaking should work fine.

Steps to Reproduce

Stencil config file below.

      componentCorePackage: `@freshworks/${packageName}`, // name in the package.json should be used
      proxiesFile: './crayons-react/components.ts',
      customElementsDir: 'dist/components',
      includeImportCustomElements: true,
    })

Usage in the consuming CRA app, import { FwButton } from “@freshworks/crayons/react”;

when I run npm run build, in the main script that gets bundled, I see code for all the components added and tree shaking is not working.

Code Reproduction URL

https://github.com/freshworks/crayons/

Additional Information

The issue is because all the components are imported and exported from the same file.

export const FwAccordionBody = /*@__PURE__*/createReactComponent<JSX.FwAccordionBody, HTMLFwAccordionBodyElement>('fw-accordion-body', undefined, undefined, defineFwAccordionBody);

Fix - https://github.com/arvindanta/stencil-react-wrapper/blob/master/src/output-react.ts#L155

I had solved this by forking this repo and exported each component in a separate file and they are just imported here. Can we please check on this ?

component.ts

import { FwAccordion } from './FwAccordion'
import { FwAccordionBody } from './FwAccordionBody'

FwAccordion file

// @ts-nocheck
/* eslint-disable */
/* tslint:disable */
/* auto-generated react proxies */
import { createReactComponent } from './react-component-lib';

import type { JSX } from '@freshworks/crayons/dist/components';

import { FwAccordion as FwAccordionCmp, defineCustomElement as defineCustomElementFwAccordion } from '@freshworks/crayons/dist/components/fw-accordion.js';
export const FwAccordion = /*@__PURE__*/createReactComponent<JSX.FwAccordion, HTMLFwAccordionElement>('fw-accordion', undefined, undefined, FwAccordionCmp, defineCustomElementFwAccordion);

No response

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 3
  • Comments: 15 (4 by maintainers)

Most upvoted comments

@arvindanta thanks for opening this issue and confirming that splitting the generated components into separate files resolves the issue.

I’ll capture as a bug and prioritize 👍