Skip to content

perf(react): improve react compiler preset so that slightly more modules are filtered out#1138

Open
sapphi-red wants to merge 1 commit intomainfrom
perf/react-filter-out-more-modules-that-doesnt-contain-hooks
Open

perf(react): improve react compiler preset so that slightly more modules are filtered out#1138
sapphi-red wants to merge 1 commit intomainfrom
perf/react-filter-out-more-modules-that-doesnt-contain-hooks

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red added p2-nice-to-have 🍰 Not breaking anything but nice to have (priority) plugin: react labels Mar 5, 2026
options.compilationMode === 'annotation'
? /['"]use memo['"]/
: /\b[A-Z]|\buse/,
: /\b[A-Z]|\buse[A-Z0-9]/,
Copy link
Member

@ArnaudBarre ArnaudBarre Mar 5, 2026

Choose a reason for hiding this comment

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

I think the only way to define a function without a whitespace before is const A,B= ... which I don't know why you would do that in anyworld for defining hooks or components

Suggested change
: /\b[A-Z]|\buse[A-Z0-9]/,
: /\s[A-Z]|\suse[A-Z0-9]/,

But I think the better tradeoff is always running on jsx|tsx|mdx (in a normal codebase, you have like 98/99% of jsx files that contrains components, so not sure trying to get a 2% improvement with a regex is worth it) and run on js|ts only with when code matches \suse[A-Z0-9] (Vite always ban using JSX inside JS, nobody writes manual jsx calls and plugin should not rename file ids once transform from jsx to js

The only issue with the approach if for people having custom DSL that compile to React, but I think these frameworks should provide their own react compiler preset

Also can you put the link from the PR into the source code? I think it's good to have it as a reference if someone challenges it later

Copy link
Member Author

Choose a reason for hiding this comment

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

If it's possible to have , before the name, I think we can use [\s,]. I guess \s and [\s,] won't have much perf difference.

I think the better tradeoff is always running on jsx|tsx|mdx (in a normal codebase, you have like 98/99% of jsx files that contrains components, so not sure trying to get a 2% improvement with a regex is worth it) and run on js|ts only with when code matches \suse[A-Z0-9] (Vite always ban using JSX inside JS, nobody writes manual jsx calls and plugin should not rename file ids once transform from jsx to js

It's currently difficult to achieve, but it would be easier to do once Vite implements composable filters (vitejs/rolldown-vite#605).

Copy link
Member

@ArnaudBarre ArnaudBarre Mar 5, 2026

Choose a reason for hiding this comment

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

After some AI checking, I think this is pretty safe and code not matching this regex will certainly break a lot a React rules:
(?:const|let|var|function)\s+(?:[A-Z]|use[A-Z0-9])

(The way to break it is using new Function or putting your component in a class method, both sounds horrible)

Speaking of classes, I checked and the playground doesn't touch class component, so no need to match this

Edit: Ok no actually this would be valid IMO but the playground doesn't touch it:

import { useState } from "react";

const hooks = {
  useTest: () => {
    const [state, setState] = useState(0);
    if (state === 0) return <div>Nothing</div>
    else return <div>{state}</div>
  }
}

I think the React compiler is only looking for top-level functions, and this sounds reasonable, so we can make an more aggressive regex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p2-nice-to-have 🍰 Not breaking anything but nice to have (priority) plugin: react

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants