-
Notifications
You must be signed in to change notification settings - Fork 5.4k
build: Enable React Compiler for Webpack builds #38007
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
base: main
Are you sure you want to change the base?
Conversation
…n/build/251103-enable-react-compiler
…n/build/251103-enable-react-compiler
Builds ready [30f49be]
UI Startup Metrics (1165 ± 96 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
|
…n/build/enable-react-compiler-webpack
Builds ready [88afa9e]
UI Startup Metrics (1295 ± 129 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
|
…n/build/enable-react-compiler-webpack
Builds ready [7266050]
UI Startup Metrics (1393 ± 133 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
|
…er/react-compiler` ESLint rule violations (#37480) <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## Motivation This commit enables React Compiler for Babel-transformed Browserify builds, and for ESLint (via the `react-compiler/react-compiler` ruleset). We can expect long-term UI performance benefits and developer time savings from this change. The Compiler transparently adds memoization code for React components and hooks, among other optimizations, and enforces React best practices. ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> Because enabling React Compiler requires addressing all violations of the `react-compiler/react-compiler` rule, this commit contains a large number of formatting changes and renames that do not require close review, alongside refactors and logical changes that do. I would suggest that reviewers primarily focus on whether a given change is problematic, rather than on whether it is necessary. If the motivation for any change is unclear, it's safe to assume that it fixes an error thrown in CI. The objective of this commit is to ship fixes that unblock React Compiler without introducing regressions in functionality or performance. Suggestions on implementing React best practices will be catalogued and addressed in follow-up tickets. The 10% bundle size increase is due to new memoizations being applied as React Compiler transforms files during the build process, which is the tradeoff for improvements in runtime interaction and responsiveness metrics. Despite this change, the UI startup and pageLoad benchmark results show no evidence of degradations in initial load time. [](https://codespaces.new/MetaMask/metamask-extension/pull/37480?quickstart=1) ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: null (non-user facing but regressions possible) ## **Related issues** - Epic: #33059 - Previous: #37383 - Next: #38007 ## **Manual testing steps** 1. Create a Browserify dev build by running `yarn start`. 2. Inspect `./dist/(chrome|firefox)/ui-[3-17].js`. 3. Search for "`_reactCompilerRuntime.c`". 4. There should be many examples of React Compiler-optimized code that uses a `$` variable e.g. ```js const $ = (0, _reactCompilerRuntime.c)(22); const { pathname } = (0, _reactRouterDomV5Compat.useLocation)(); const dispatch = (0, _reactRedux.useDispatch)(); const t = (0, _useI18nContext.useI18nContext)(); let t0; if ($[0] !== pathname) { t0 = (0, _snaps2.decodeSnapIdFromPathname)(pathname); $[0] = pathname; $[1] = t0; } else { t0 = $[1]; } ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Enables React Compiler in Browserify Babel builds and enforces the react-compiler ESLint rule, with broad hook/memoization refactors, selector renames, tests updates, and dependency/policy changes. > > - **Build/Tooling**: > - Enable React Compiler via Babel override for `ui/**/{components,contexts,hooks,layouts,pages}` with `babel-plugin-react-compiler` (target 17). > - Add `react-compiler-runtime` and `babel-plugin-react-compiler` deps; update `esbuild`, `koa` (v3), and related tooling. > - Enforce `react-compiler/react-compiler` ESLint rule and plugin across JS/TS React configs. > - **Policies/Config**: > - Extend LavaMoat policies and depcheck ignores for React Compiler packages and updated sub-deps. > - **UI/Hook Refactors (compliance + perf)**: > - Replace patterns violating React Compiler (e.g., `useMemo` state updates, unstable callbacks, missing keys) with `useCallback`/`useRef`/guards; fix array checks, keys, debounce handling, and effect deps. > - Improve polling hooks (`usePolling`, `useMultiPolling`) with input change detection and cleanup; stabilize navigation and debounce handlers. > - Add `isNativeAsset` and use in `AssetPage`; compute asset balances immutably. > - **Reliability/Null Safety**: > - Guard optional `request?.metadata?.id` across confirmations/snaps flows; miscellaneous null-safe checks. > - **Selectors/Types**: > - Rename selectors: `useSafeChainsListValidationSelector` → `getUseSafeChainsListValidation`; `use4ByteResolutionSelector` → `getUse4ByteResolution` (and propagate). > - Tighten types (e.g., `ownerId` optional handling, asset type helpers). > - **Tests**: > - Update tests to use `renderHook` for hooks; adjust mocks to renamed selectors and new patterns. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c47e3f6. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: MetaMask Bot <[email protected]>
|
@metamaskbot update-policies |
|
Policies updated. 🧠 Learn how: https://lavamoat.github.io/guides/policy-diff/#what-to-look-for-when-reviewing-a-policy-diff |
|
@metamaskbot update-policies |
|
Policies updated. 🧠 Learn how: https://lavamoat.github.io/guides/policy-diff/#what-to-look-for-when-reviewing-a-policy-diff |
Builds ready [83cf846]
UI Startup Metrics (1297 ± 126 ms)
📊 Page Load Benchmark ResultsCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
| reactCompilerVerbose: { | ||
| array: false, | ||
| default: false, | ||
| description: | ||
| 'Enables/disables React Compiler verbose mode and statistics', | ||
| group: toOrange('Developer assistance:'), | ||
| type: 'boolean', | ||
| }, | ||
| reactCompilerDebug: { | ||
| array: false, | ||
| choices: ['all', 'critical', 'none'], | ||
| default: 'none', | ||
| description: | ||
| 'Sets React Compiler panic threshold that fails the build for all errors or critical errors only. If `none`, the build will not fail.', | ||
| group: toOrange('Developer assistance:'), | ||
| type: 'string', | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should be grouped with the other "Developer assistance:" CLI commands in this object
Description
Enables React Compiler code transformation and optimizations for Webpack builds.
Webpack CLI arguments
--reactCompilerDebugThis sets the
panicThresholdcompiler option for stricter debugging.There are still a large number of React Compiler errors present in the app that do not break functionality but prevent compiler optimizations from being applied. We should aim for eventually being able to create non-production builds with
panicThresholdset tocritical_errors, and then toall_errors.--reactCompilerVerboseThis outputs compilation status and error descriptions per file, as well as compiler run result statistics.
The output distinguishes between actionable errors, which are fixable from our end, and "unsupported" errors that are caused by limitations or unimplemented features in React Compiler itself.
Changelog
CHANGELOG entry: null (build)
Related issues
react-compiler/react-compilerESLint rule violations #37480: Previous PRManual testing steps
yarn webpack../dist/{chrome,firefox}/js-ui_{com,d,p,pages}*react_compiler_runtime__WEBPACK_IMPORTED_MODULE_0__.c"$variable e.g.Screenshots/Recordings
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Enable React Compiler in Webpack builds with new CLI flags, a loader for
ui/**React files, verbose stats, and corresponding policy/dependency updates.utils/loaders/reactCompilerLoader.ts); target React 17; optional verbose stats plugin.utils/cli.ts:--reactCompilerVerboseand--reactCompilerDebug(all|critical|none); included in dry-run output and defaults test.ui/{components,contexts,hooks,layouts,pages}via new rule usingreact-compiler-webpack; addUI_DIR_REmatcher.react-compiler-webpackdependency; update.depcheckrc.ymlignores; refreshyarn.lock.Written by Cursor Bugbot for commit 83cf846. This will update automatically on new commits. Configure here.