Skip to content

docs: widen React Compiler include to optimize custom hooks in .ts files#7392

Merged
chenjiahan merged 2 commits intoweb-infra-dev:mainfrom
RobHannay:rook/docs-react-compiler-hooks
Mar 26, 2026
Merged

docs: widen React Compiler include to optimize custom hooks in .ts files#7392
chenjiahan merged 2 commits intoweb-infra-dev:mainfrom
RobHannay:rook/docs-react-compiler-hooks

Conversation

@RobHannay
Copy link
Copy Markdown
Contributor

Summary

React Compiler optimizes both components and custom hooks, but the current docs example uses include: /\.(?:jsx|tsx)$/ which only matches JSX/TSX files. Custom hooks are typically defined in plain .ts files, so they don't get optimized by the compiler with this pattern.

Changes:

  • Updated the include pattern from /\.(?:jsx|tsx)$/ to /\.[jt]sx?$/ to match all JS/TS source files
  • Added exclude: [/node_modules/] since .js files in node_modules would otherwise be processed
  • Added a tip explaining the rationale and offering the narrower pattern as an alternative

Updated both English and Chinese docs.

Context

We discovered this while configuring React Compiler for our project — hooks in .ts files were silently skipped. The React Compiler docs confirm it memoizes "React components and hooks, not every function", so the include pattern should cover .ts files to get the full benefit.

React Compiler optimizes both components and custom hooks, but custom
hooks are typically defined in plain .ts files. The previous include
pattern /\.(?:jsx|tsx)$/ only matched JSX/TSX files, meaning hooks
in .ts files were not optimized.

Updated the pattern to /\.[jt]sx?$/ with an explicit node_modules
exclude, and added a tip explaining the rationale.

Co-authored-by: Rook <rook@rook.is>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the integration of the React Compiler by refining its configuration to more comprehensively optimize React applications. It addresses a critical oversight where custom hooks in plain TypeScript files were not being processed, ensuring that the compiler's benefits extend to a wider range of code. The changes also introduce an exclusion for node_modules to streamline the compilation process, all while providing clear, updated documentation for users.

Highlights

  • Expanded React Compiler Scope: The include pattern for the React Compiler Babel plugin was broadened to /\.[jt]sx?$/ to ensure optimization of custom hooks defined in .ts files, in addition to existing .jsx and .tsx files.
  • Improved Performance/Exclusion: An exclude: [/node_modules/] rule was added to prevent the React Compiler from processing third-party dependencies, enhancing build performance and avoiding potential issues.
  • Updated Documentation: The English and Chinese documentation for React Compiler integration was updated with the new configuration and a detailed tip explaining the rationale behind the changes and their impact on custom hook optimization.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the pluginBabel configuration in the React framework guide documentation (both English and Chinese versions). The include regex is broadened to /\[jt\]sx?$/ to cover .js, .jsx, .ts, and .tsx files, and an exclude for node_modules is added. The documentation is also updated with a new tip explaining these changes. The feedback suggests improving the exclude regex to /[\\/]node_modules[\\/]/ for better robustness and consistency, preventing potential false positives.

Comment on lines +119 to +122
`include` 使用 `/\.[jt]sx?$/` 来匹配 `.js`、`.jsx`、`.ts` 和 `.tsx` 文件。这样可以确保 React Compiler 能够优化组件和[自定义 Hooks](https://zh-hans.react.dev/learn/reusing-logic-with-custom-hooks),因为自定义 Hooks 通常定义在 `.ts` 文件中。`exclude` 中的 `node_modules` 可以防止编译器处理第三方依赖。

如果你只想编译 JSX/TSX 文件,可以使用 `include: /\.(?:jsx|tsx)$/`,但 `.ts` 文件中的自定义 Hooks 将不会被优化。
:::
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is AI-generated translation so I have no idea if this read correctly, sorry 😂

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! The translation is quite good. 😄

pluginBabel({
include: /\.(?:jsx|tsx)$/,
include: /\.[jt]sx?$/,
exclude: [/[\\/]node_modules[\\/]/],
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I assume this is necessary because we're now likely to match on (compiled) files in node_modules, but perhaps they're automatically excluded?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, explicitly excluding node_modules is reasonable and can reduce perf overhead.

Copy link
Copy Markdown
Member

@chenjiahan chenjiahan left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@chenjiahan chenjiahan merged commit ddac630 into web-infra-dev:main Mar 26, 2026
5 checks passed
chenjiahan pushed a commit that referenced this pull request Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants