EightfoldAI / octuple

The Octuple component library
MIT License
16 stars 45 forks source link

feat: support tree shaking #829

Closed gclark-eightfold closed 1 week ago

gclark-eightfold commented 1 month ago

SUMMARY:

Adds tree-shaking support for apps using Octuple. Core changes:

  1. Add explicit sideEffects to package.json for CJS tree shaking.
  2. Switch from Webpack to Rollup to support exporting ESM.

For the Webpack -> Rollup change, it may be possible to export ESM from webpack, but most sources I found said exporting ESM from webpack was not supported. Additionally, after examining quite a number of specifically react component libraries, it seemed like Rollup was the most common tool for this job.

Testing on Eightfold for the careerHub entry point only yielded a -20.9% decrease in the bundle size for the octuple chunk compared to v2.51.1

Here is the bundle analysis diff in Blindfold.ai after following the steps outlined in the Test Plan

Before

Before

After

After

GITHUB ISSUE (Open Source Contributors)

NA

JIRA TASK (Eightfold Employees Only):

ENG-55139

CHANGE TYPE:

TEST COVERAGE:

TEST PLAN:

This test plan relies on running Blindfold locally, but can be done in isolation if desired

  1. Clone the https://github.com/EightfoldAI/blindfold repo locally
  2. In the root of the repo, add the following .env file. (If you have one already, just add ANALYZE_BUNDLES=1
    ANALYZE_BUNDLES=1
    BACKEND_BASE_URL="https://app.eightfold.ai"
    NEXT_PUBLIC_HOST_URL="https://localhost:8000"
    NEXTAUTH_URL="https://localhost:8000"
  3. Run git checkout gclark/octuple-bundle-test to pull these changes.
  4. Install deps with pnpm install. (Run npm install -g pnpm if you do not have pnpm installed)
  5. Run pnpm build --filter nextjs to build the Blindfold app.
  6. Once complete open the file located in apps/nextjs/.next/analyze/client.html in your web browser.
    • [x] The bundle analysis does not show the entire octuple.js bundle present.
  7. Run pnpm start --filter nextjs and go to http://localhost:8000/octuple
    • [x] Verify the Octuple primary button is present on the page and styled correctly.
  8. Remove the patch with pnpm patch-remove @eightfold.ai/octuple
  9. Run pnpm build --filter nextjs again.
  10. Once complete open the file located in apps/nextjs/.next/analyze/client.html in your web browser.
    • [x] The bundle analysis does show the entire octuple.js bundle present.
dkilgore-eightfold commented 1 month ago

Need to bump node in .codesanbox dir

dkilgore-eightfold commented 1 month ago

@gclark-eightfold You may be correct:

https://github.com/webpack/webpack/issues/2933

codesandbox-ci[bot] commented 1 month ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 84.70%. Comparing base (4eae06e) to head (bf7e072).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #829 +/- ## ======================================= Coverage 84.70% 84.70% ======================================= Files 1098 1098 Lines 20422 20422 Branches 7719 7719 ======================================= + Hits 17298 17299 +1 + Misses 3040 3039 -1 Partials 84 84 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

dkilgore-eightfold commented 1 month ago

Also removed this line and its import to fix the circular dep issue accross bundles:

https://github.com/EightfoldAI/octuple/blob/main/src/shared/utilities/flexGapSupported.ts#L7