diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts index 15796d77a70e..0e2cc3055ac0 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts @@ -151,6 +151,17 @@ export class ProgramContext { ); } + removeMemoCacheImport(): void { + const moduleImports = this.imports.get(this.reactRuntimeModule); + if (moduleImports == null) { + return; + } + moduleImports.delete('c'); + if (moduleImports.size === 0) { + this.imports.delete(this.reactRuntimeModule); + } + } + /** * * @param externalFunction diff --git a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts index 2880e9283c77..e7aa76a3e043 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts @@ -775,6 +775,13 @@ function applyCompiledFunctions( // Forget compiled the component, we need to update existing imports of useMemoCache if (compiledFns.length > 0) { + // Codegen may have registered `_c` for a function that was later discarded. + const anyAppliedUsesMemo = compiledFns.some( + result => result.compiledFn.memoSlotsUsed > 0, + ); + if (!anyAppliedUsesMemo) { + programContext.removeMemoCacheImport(); + } addImportsToProgram(program, programContext); } } diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/use-no-forget-multiple-with-eslint-suppression.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/use-no-forget-multiple-with-eslint-suppression.expect.md index b46d71fdf488..22386c5205af 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/use-no-forget-multiple-with-eslint-suppression.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/use-no-forget-multiple-with-eslint-suppression.expect.md @@ -25,7 +25,6 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { c as _c } from "react/compiler-runtime"; import { useRef } from "react"; const useControllableState = (options) => {};