Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
Tracked by #242
arvindanta opened this issue Apr 26, 2022 · 11 comments · Fixed by #428
Open
3 tasks done
Tracked by #242

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

arvindanta opened this issue Apr 26, 2022 · 11 comments · Fixed by #428
Labels
package: react @stencil/react-output-target package type: bug Something isn't working

Comments

@arvindanta
Copy link

arvindanta commented Apr 26, 2022

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

@ionitron-bot ionitron-bot bot added the triage label Apr 26, 2022
@arvindanta
Copy link
Author

This issue is similar to #222

@sean-perkins
Copy link
Collaborator

@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 👍

@sean-perkins sean-perkins added type: bug Something isn't working package: react @stencil/react-output-target package labels Apr 26, 2022
@ionitron-bot ionitron-bot bot removed the triage label Apr 26, 2022
@R-Bower
Copy link

R-Bower commented Oct 6, 2022

Is this still being prioritized? I still can't get my React output to tree shake. I'd love to adopt this library and port over my components, but this is a major blocker.

Is there anything that I can do to help?

@arvindanta
Copy link
Author

arvindanta commented Oct 13, 2022

@R-Bower can you try this out -https://github.com/arvindanta/stencil-react-wrapper

I forked the react-output-target and added changes to enable tree shakability.

We are using this in @freshworks/crayons project .

@freshworks/crayons/react gives you treeshakable components.

@R-Bower
Copy link

R-Bower commented Oct 16, 2022

I'll give it a shot, thanks!

@R-Bower
Copy link

R-Bower commented Oct 23, 2022

@R-Bower can you try this out -https://github.com/arvindanta/stencil-react-wrapper

I forked the react-output-target and added changes to enable tree shakability.

We are using this in @freshworks/crayons project .

@freshworks/crayons/react gives you treeshakable components.

The components don't render in my docusaurus application, although it works in NextJS.

@drakegens
Copy link

Is there an official fix for this available via the@stencil/react-output-target package?

@venkatesh-nagineni
Copy link

any update on this, it is unable to get build what i used in my app

@venkatesh-nagineni
Copy link

venkatesh-nagineni commented Jun 14, 2023

@arvindanta hey, i was tried your lib in react with vite bundler which still gives me large bundle with registering all components and i have also tested in angular where you used defineCustomElements() which define all elements and it's end up build all entries even i used button component

Screenshot 2023-06-14 at 12 54 22

Screenshot 2023-06-14 at 12 55 12

@jeffplloyd
Copy link

Is there any movement on this? I see a PR was opened last year but it has not been completed.

@tonyle-geocomply
Copy link

Hi guys any update on @stencil/react-output-target, I had the same issue. I see @sean-perkins had a PR for it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: react @stencil/react-output-target package type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants