diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts index 46d111f742..8c8888ecf4 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts @@ -3,7 +3,7 @@ import * as t from "@babel/types"; import { CompilerError } from "../CompilerError"; import { GeneratedSource } from "../HIR"; import { getOrInsertDefault } from "../Utils/utils"; -import { ExternalFunction } from "./Options"; +import { ExternalFunction, PluginOptions } from "./Options"; export function addImportsToProgram( path: NodePath, @@ -129,24 +129,44 @@ export function updateExistingReactImportDeclaration( return didInsertUseMemoCache; } -export function insertUseMemoCacheImportDeclaration( - program: NodePath +export function updateUseMemoCacheImport( + program: NodePath, + options: PluginOptions ): void { - program.unshiftContainer( - "body", - t.importDeclaration( - [ - t.importSpecifier( - t.identifier("useMemoCache"), - t.identifier("unstable_useMemoCache") - ), - ], - t.stringLiteral("react") - ) - ); + // If there isn't already an import of * as React, insert it so useMemoCache doesn't + // throw + const { didInsertUseMemoCache, hasExistingReactImport } = + findExistingImports(program); + + // unstable_useMemoCache wasn't already imported, nothing to do + if (!didInsertUseMemoCache) { + return; + } + + if ( + options.useMemoCacheSource === "react" || + options.useMemoCacheSource === null + ) { + // If Forget did successfully compile inject/update an import of + // `import {unstable_useMemoCache as useMemoCache} from 'react'` and rename + // `React.unstable_useMemoCache(n)` to `useMemoCache(n)`; + if (hasExistingReactImport) { + const didUpdateImport = updateExistingReactImportDeclaration(program); + if (didUpdateImport === false) { + throw new Error( + "Expected an ImportDeclaration of react in order to update ImportSpecifiers with useMemoCache" + ); + } + } else { + addUseMemoCacheImportDeclaration(program, "react"); + } + } else if (typeof options.useMemoCacheSource === "string") { + // import useMemoCache from userspace module + addUseMemoCacheImportDeclaration(program, options.useMemoCacheSource); + } } -export function insertUserspaceUseMemoCacheImportDeclaration( +function addUseMemoCacheImportDeclaration( program: NodePath, moduleName: string ): void { diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts index acd6b1cec8..49e8e65aba 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts @@ -17,13 +17,7 @@ import { CodegenFunction } from "../ReactiveScopes"; import { isComponentDeclaration } from "../Utils/ComponentDeclaration"; import { assertExhaustive } from "../Utils/utils"; import { insertGatedFunctionDeclaration } from "./Gating"; -import { - addImportsToProgram, - findExistingImports, - insertUseMemoCacheImportDeclaration, - insertUserspaceUseMemoCacheImportDeclaration, - updateExistingReactImportDeclaration, -} from "./Imports"; +import { addImportsToProgram, updateUseMemoCacheImport } from "./Imports"; import { addInstrumentForget } from "./Instrumentation"; import { ExternalFunction, PluginOptions, parsePluginOptions } from "./Options"; import { compileFn } from "./Pipeline"; @@ -316,47 +310,23 @@ export function compileProgram( } ); - // If there isn't already an import of * as React, insert it so useMemoCache doesn't - // throw + // Forget compiled the component, we need to update existing imports of unstable_useMemoCache if (hasForgetMutatedOriginalSource) { - const { didInsertUseMemoCache, hasExistingReactImport } = - findExistingImports(program); - - // If Forget did successfully compile inject/update an import of - // `import {unstable_useMemoCache as useMemoCache} from 'react'` and rename - // `React.unstable_useMemoCache(n)` to `useMemoCache(n)`; - if (didInsertUseMemoCache) { - if (options.useMemoCacheSource === null) { - if (hasExistingReactImport) { - const didUpdateImport = updateExistingReactImportDeclaration(program); - if (didUpdateImport === false) { - throw new Error( - "Expected an ImportDeclaration of react in order to update ImportSpecifiers with useMemoCache" - ); - } - } else { - insertUseMemoCacheImportDeclaration(program); - } - } else if (typeof options.useMemoCacheSource === "string") { - insertUserspaceUseMemoCacheImportDeclaration( - program, - options.useMemoCacheSource - ); - } - } - const externalFunctions: ExternalFunction[] = []; - // TODO: check for duplicate import specifiers - if (options.gating != null) { - externalFunctions.push(options.gating); - } - if (options.instrumentForget != null) { - externalFunctions.push(options.instrumentForget); - } - if (options.environment?.enableEmitFreeze != null) { - externalFunctions.push(options.environment.enableEmitFreeze); - } - addImportsToProgram(program, externalFunctions); + updateUseMemoCacheImport(program, options); } + + const externalFunctions: ExternalFunction[] = []; + // TODO: check for duplicate import specifiers + if (options.gating != null) { + externalFunctions.push(options.gating); + } + if (options.instrumentForget != null) { + externalFunctions.push(options.instrumentForget); + } + if (options.environment?.enableEmitFreeze != null) { + externalFunctions.push(options.environment.enableEmitFreeze); + } + addImportsToProgram(program, externalFunctions); } function shouldVisitNode(