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

build: Fix Preact support by externalizing JSX runtime #2076

Merged
merged 1 commit into from Apr 16, 2024

Conversation

pwolfert
Copy link
Contributor

@pwolfert pwolfert commented Mar 26, 2024

Description

When the new jsx transform was introduced, a copy of the JSX runtime started getting included in the index.esm.js bundle. Including this copy meant that we could no longer swap out that module when building our own downstream Preact project. In effect, it broke Preact support.

To fix this, I've updated the rollup config to treat react/jsx-runtime as an external dependency. By externalizing the JSX runtime in this project, it 1) reduces the library bundle size by 32% and 2) allows downstream projects to use whatever JSX runtime they want.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Could possibly be considered a breaking change, but I don't know the landscape of dependent projects well enough to know if that's true.

Checklist

Before submitting your pull request, please make sure the following is done:

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • I have followed the coding guidelines in this project
  • I have tested my changes on different browsers and screen sizes

Linked Issues

If this PR addresses any existing issues, please link them here. Example: Fixes #123

Test Plan

You can test that it removes the runtime by searching for the jsxDEV function in dist/index.esm.js before and after the change. You can also see that the file size changed from 144KB to 97KB.

I can also create a branch in my downstream project for testing upon request.

@gpbl gpbl merged commit 7c37f40 into gpbl:main Apr 16, 2024
14 checks passed
@gpbl
Copy link
Owner

gpbl commented Apr 16, 2024

Thanks @pwolfert !

@pwolfert
Copy link
Contributor Author

Thank you, @gpbl!

@eps1lon
Copy link

eps1lon commented May 20, 2024

FYI this is also the correct fix if only react/jsx-runtime would exist. Otherwise library consumers would need to use the React version that was used when publishing this library. Either you declare React as a peer dependency, or inline all its packages. Externalizing some entrypoints while declaring others as peer dependencies only worked incidentally.

@pwolfert
Copy link
Contributor Author

@eps1lon, maybe I'm not understanding, but it looks like react is a peer dependency of this library, which should mean that react/jsx-runtime should exist, right? Or are you saying it does exist, and so therefore this is the correct fix?

@eps1lon
Copy link

eps1lon commented May 20, 2024

Or are you saying it does exist, and so therefore this is the correct fix?

This. Previously you were inlining react/jsx-runtime. Either you inline all entrypoints or none. Just inlining a few (here: just the JSX entrypoint), is bad.

@pwolfert
Copy link
Contributor Author

Okay cool 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants