Skip to content

bug: Tree shaking is not working with React output target #255

Closed
@arvindanta

Description

@arvindanta

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanteda good issue for the communitypackage: react@stencil/react-output-target packagetype: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions