mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
1 parent
847a6f0a09
commit
0a7bb7ac7a
2 files changed
+52
-62
No files matched your search
@@ -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<t.Program>,
|
||||
@@ -129,24 +129,44 @@ export function updateExistingReactImportDeclaration(
|
||||
return didInsertUseMemoCache;
|
||||
}
|
||||
|
||||
export function insertUseMemoCacheImportDeclaration(
|
||||
program: NodePath<t.Program>
|
||||
export function updateUseMemoCacheImport(
|
||||
program: NodePath<t.Program>,
|
||||
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<t.Program>,
|
||||
moduleName: string
|
||||
): void {
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user