addImportsToProgram() — react Function Reference
Architecture documentation for the addImportsToProgram() function in Imports.ts from the react codebase.
Entity Profile
Dependency Diagram
graph TD e19f561e_cb2d_6161_2ab6_1c6533ed4958["addImportsToProgram()"] 2a7e50cd_6171_085d_277c_6ced6ddd7148["Imports.ts"] e19f561e_cb2d_6161_2ab6_1c6533ed4958 -->|defined in| 2a7e50cd_6171_085d_277c_6ced6ddd7148 11c511ca_2971_a23f_cfd6_2897f2fe1b13["applyCompiledFunctions()"] 11c511ca_2971_a23f_cfd6_2897f2fe1b13 -->|calls| e19f561e_cb2d_6161_2ab6_1c6533ed4958 9863ad63_8ce7_66b3_fe68_e73a382edd72["getExistingImports()"] e19f561e_cb2d_6161_2ab6_1c6533ed4958 -->|calls| 9863ad63_8ce7_66b3_fe68_e73a382edd72 041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"] e19f561e_cb2d_6161_2ab6_1c6533ed4958 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e 073c81a5_c389_d108_5b8f_4d6dc6eece83["push()"] e19f561e_cb2d_6161_2ab6_1c6533ed4958 -->|calls| 073c81a5_c389_d108_5b8f_4d6dc6eece83 8e8b7ee8_d3c2_f98d_17e1_c5ff5fff1940["map()"] e19f561e_cb2d_6161_2ab6_1c6533ed4958 -->|calls| 8e8b7ee8_d3c2_f98d_17e1_c5ff5fff1940 style e19f561e_cb2d_6161_2ab6_1c6533ed4958 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts lines 238–319
export function addImportsToProgram(
path: NodePath<t.Program>,
programContext: ProgramContext,
): void {
const existingImports = getExistingImports(path);
const stmts: Array<t.ImportDeclaration | t.VariableDeclaration> = [];
const sortedModules = [...programContext.imports.entries()].sort(([a], [b]) =>
a.localeCompare(b),
);
for (const [moduleName, importsMap] of sortedModules) {
for (const [specifierName, loweredImport] of importsMap) {
/**
* Assert that the import identifier hasn't already be declared in the program.
* Note: we use getBinding here since `Scope.hasBinding` pessimistically returns true
* for all allocated uids (from `Scope.getUid`)
*/
CompilerError.invariant(
path.scope.getBinding(loweredImport.name) == null,
{
reason:
'Encountered conflicting import specifiers in generated program',
description: `Conflict from import ${loweredImport.module}:(${loweredImport.imported} as ${loweredImport.name})`,
loc: GeneratedSource,
},
);
CompilerError.invariant(
loweredImport.module === moduleName &&
loweredImport.imported === specifierName,
{
reason:
'Found inconsistent import specifier. This is an internal bug.',
description: `Expected import ${moduleName}:${specifierName} but found ${loweredImport.module}:${loweredImport.imported}`,
loc: GeneratedSource,
},
);
}
const sortedImport: Array<NonLocalImportSpecifier> = [
...importsMap.values(),
].sort(({imported: a}, {imported: b}) => a.localeCompare(b));
const importSpecifiers = sortedImport.map(specifier => {
return t.importSpecifier(
t.identifier(specifier.name),
t.identifier(specifier.imported),
);
});
/**
* If an existing import of this module exists (ie `import { ... } from
* '<moduleName>'`), inject new imported specifiers into the list of
* destructured variables.
*/
const maybeExistingImports = existingImports.get(moduleName);
if (maybeExistingImports != null) {
maybeExistingImports.pushContainer('specifiers', importSpecifiers);
} else {
if (path.node.sourceType === 'module') {
stmts.push(
t.importDeclaration(importSpecifiers, t.stringLiteral(moduleName)),
);
} else {
stmts.push(
t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern(
sortedImport.map(specifier => {
return t.objectProperty(
t.identifier(specifier.imported),
t.identifier(specifier.name),
);
}),
),
t.callExpression(t.identifier('require'), [
t.stringLiteral(moduleName),
]),
),
]),
);
}
}
}
path.unshiftContainer('body', stmts);
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does addImportsToProgram() do?
addImportsToProgram() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts.
Where is addImportsToProgram() defined?
addImportsToProgram() is defined in compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts at line 238.
What does addImportsToProgram() call?
addImportsToProgram() calls 4 function(s): getExistingImports, invariant, map, push.
What calls addImportsToProgram()?
addImportsToProgram() is called by 1 function(s): applyCompiledFunctions.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free