docs: widen React Compiler include to optimize custom hooks in .ts files#7392
Conversation
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>
Summary of ChangesHello, 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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
Co-authored-by: Rook <rook@rook.is>
| `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 将不会被优化。 | ||
| ::: |
There was a problem hiding this comment.
This is AI-generated translation so I have no idea if this read correctly, sorry 😂
There was a problem hiding this comment.
Thanks! The translation is quite good. 😄
| pluginBabel({ | ||
| include: /\.(?:jsx|tsx)$/, | ||
| include: /\.[jt]sx?$/, | ||
| exclude: [/[\\/]node_modules[\\/]/], |
There was a problem hiding this comment.
I assume this is necessary because we're now likely to match on (compiled) files in node_modules, but perhaps they're automatically excluded?
There was a problem hiding this comment.
Yeah, explicitly excluding node_modules is reasonable and can reduce perf overhead.
…les (#7392) Co-authored-by: Rook <rook@rook.is>
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.tsfiles, so they don't get optimized by the compiler with this pattern.Changes:
includepattern from/\.(?:jsx|tsx)$/to/\.[jt]sx?$/to match all JS/TS source filesexclude: [/node_modules/]since.jsfiles in node_modules would otherwise be processedUpdated both English and Chinese docs.
Context
We discovered this while configuring React Compiler for our project — hooks in
.tsfiles were silently skipped. The React Compiler docs confirm it memoizes "React components and hooks, not every function", so the include pattern should cover.tsfiles to get the full benefit.