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

examples: Inject react runtime for ui package #371

Merged

Conversation

tknickman
Copy link
Member

What has been changed?

  1. Update build and dev command for kitchen-sink examples ui package to support components that don't explicitly import React.

Why?

Starting from React 17, React shipped with the new JSX Transform, allowing components to include JSX without React being in scope (For example, the Button component included below). Because of this, it is now very common to write react components without explicitly importing React at all.

However, in the current kitchen-sink example, doing this will break, and throw:
ReferenceError: React is not defined.

How?

In order to support this common use case, we need to instruct tsup to inject the react runtime into the build. Because tsup uses esbuild, we can leverage --inject to automatically replace a global variable with an import from another file. tsup has supported this argument (although poorly documented) since 5.3.0 (commit)

Other

Fixing this will hopefully avoid frustration from new users who want to use an example as a starting point, with minimal changes to the build configuration.

@vercel
Copy link

vercel bot commented Dec 20, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/vercel/turbo-site/DiTtxhRZdUoaVtajvxQ2W34exFq2
✅ Preview: https://turbo-site-git-fork-tknickman-tekkitchen-sinkui-react-im-5eee46.vercel.sh

Copy link
Contributor

@jaredpalmer jaredpalmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just let's get rid of index.tsx's / barrel files aside from the root in ui and also move tests back to where they were

@tknickman
Copy link
Member Author

@jaredpalmer good call, updated with your suggestions! Thanks!

@jaredpalmer jaredpalmer changed the title fix(examples): inject react runtime for ui package examples: Inject react runtime for ui package Dec 23, 2021
@jaredpalmer jaredpalmer merged commit 6e2f4a7 into vercel:main Dec 23, 2021
@tknickman tknickman deleted the TEK_kitchen-sink_ui-react-improvements branch December 23, 2021 15:56
nareshbhatia added a commit to nareshbhatia/custom-react-stack that referenced this pull request Apr 9, 2022
Without this tsup builds do not work with apps (see vercel/turborepo#371)
sokra added a commit that referenced this pull request Oct 25, 2022
This allows us to reference the source maps for individual chunk items when delivering them for HMR updates.

We currently send down the update via a `JSON.stringify()` of the updated module's code. We then `eval` this code to generate the new "module", and call registered hot handlers with the new module to update their scopes. Thankfully, we can include a `sourceMappingUrl` comment in the evaled code, so we can get away with deferring creating the source map's content until dev tools is opened.

This is a first pass, it really needs to be cleaned up. But at least it's working.


Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>
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.

2 participants