Skip to content

[Compiler Bug]: React Compiler does not compile module-level 'use memo' but compiles function-level 'use memo' #35868

@tryasko

Description

@tryasko

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-hooks (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://github.com/tryasko/test-react-compiler

Repro steps

Description

React Compiler compiles a component when 'use memo' is placed inside the function body, but does NOT compile the same component when 'use memo' is placed at the module level.

According to the documentation, module-level directives should apply to all functions in the file:

https://react.dev/reference/react-compiler/directives

"Place directives at the top of a file to affect all functions in that module."

However, in my setup, only function-level directives trigger compilation, while module-level directives are ignored.

Steps to reproduce

  1. Clone the repository: https://github.com/tryasko/test-react-compiler
  2. Install dependencies: npm i
  3. Run the compiler: npm start
  4. Inspect the generated files in the output directory.

Compilation result

Component file Directive level Compilation Expected result
component-a.jsx no directive Not compiled ✅ Expected
component-b.jsx function-level Compiled ✅ Expected
component-c.jsx module-level Not compiled ❌ Not expected

Expected behavior

React Compiler should compile components when 'use memo' is placed at the module level, the same way it does when the directive is placed at the function level.

Specifically, the output for component-c.jsx should include React Compiler transformations, such as:

import { c as _c } from "react/compiler-runtime";

How often does this bug happen?

Every time

What version of React are you using?

19

What version of React Compiler are you using?

1.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugType: Bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions