-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[FRONT COMPONENTS] Declare command menu items in front components #18047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
charlesBochet
merged 8 commits into
main
from
r--declare-command-menu-items-in-front-components
Feb 19, 2026
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3bcf7e7
define command menu items in front components
bosiraphael e44d8a0
use ast parsing in unwrap-define-front-component-to-direct-export
bosiraphael ec1118f
Revert "use ast parsing in unwrap-define-front-component-to-direct-ex…
bosiraphael a9d81bc
undo changes to unwrap define front component
bosiraphael 04fe7e8
commandMenuItems are included in frontComponents manifest
bosiraphael 6caddd4
delete type file
bosiraphael 62188d3
fix regex
bosiraphael cd4abaa
Merge branch 'main' into r--declare-command-menu-items-in-front-compo…
bosiraphael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 147 additions & 30 deletions
177
...uild/common/front-component-build/utils/unwrap-define-front-component-to-direct-export.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,160 @@ | ||
| const DEFINE_FRONT_COMPONENT_IMPORT_PATTERN = | ||
| /import\s*\{\s*defineFrontComponent\s*\}\s*from\s*['"][^'"]+['"];?\n?/g; | ||
| import { Node, Project, type SourceFile, ts } from 'ts-morph'; | ||
|
|
||
| const DEFINE_FRONT_COMPONENT_EXPORT_PATTERN = | ||
| /export\s+default\s+defineFrontComponent\s*\(\s*\{[^}]*component\s*:\s*(\w+)[^}]*\}\s*\)\s*;?/s; | ||
| const project = new Project({ | ||
| useInMemoryFileSystem: true, | ||
| compilerOptions: { jsx: ts.JsxEmit.ReactJSX }, | ||
| }); | ||
bosiraphael marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const removeDefineFrontComponentFromImports = ( | ||
| sourceFile: SourceFile, | ||
| ): void => { | ||
| for (const importDeclaration of sourceFile.getImportDeclarations()) { | ||
| const defineFrontComponentSpecifier = importDeclaration | ||
| .getNamedImports() | ||
| .find((specifier) => specifier.getName() === 'defineFrontComponent'); | ||
|
|
||
| if (!defineFrontComponentSpecifier) { | ||
| continue; | ||
| } | ||
|
|
||
| defineFrontComponentSpecifier.remove(); | ||
|
|
||
| if (importDeclaration.getNamedImports().length === 0) { | ||
| importDeclaration.remove(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const extractComponentNameFromDefaultExport = ( | ||
bosiraphael marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sourceFile: SourceFile, | ||
| ): string | null => { | ||
| for (const statement of sourceFile.getStatements()) { | ||
| if (!Node.isExportAssignment(statement) || statement.isExportEquals()) { | ||
| continue; | ||
| } | ||
|
|
||
| const expression = statement.getExpression(); | ||
|
|
||
| if ( | ||
| !Node.isCallExpression(expression) || | ||
| expression.getExpression().getText() !== 'defineFrontComponent' | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| const [configArg] = expression.getArguments(); | ||
|
|
||
| if (!configArg || !Node.isObjectLiteralExpression(configArg)) { | ||
| throw new Error( | ||
| 'defineFrontComponent must be called with an object literal argument', | ||
| ); | ||
| } | ||
|
|
||
| const componentProperty = configArg.getProperty('component'); | ||
|
|
||
| if (!componentProperty) { | ||
| throw new Error( | ||
| 'defineFrontComponent config must include a "component" property', | ||
| ); | ||
| } | ||
|
|
||
| if (Node.isShorthandPropertyAssignment(componentProperty)) { | ||
| return componentProperty.getName(); | ||
| } | ||
|
|
||
| if (!Node.isPropertyAssignment(componentProperty)) { | ||
| throw new Error( | ||
| 'Unexpected syntax for "component" property in defineFrontComponent config', | ||
| ); | ||
| } | ||
|
|
||
| const initializer = componentProperty.getInitializer(); | ||
|
|
||
| if (!initializer) { | ||
| throw new Error('"component" property must have a value'); | ||
| } | ||
|
|
||
| return initializer.getText(); | ||
| } | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| const removeExportFromComponentDeclaration = ( | ||
| sourceFile: SourceFile, | ||
| componentName: string, | ||
| ): void => { | ||
| for (const statement of sourceFile.getStatements()) { | ||
| if ( | ||
| Node.isVariableStatement(statement) && | ||
| statement.isExported() && | ||
| statement | ||
| .getDeclarationList() | ||
| .getDeclarations() | ||
| .some((declaration) => declaration.getName() === componentName) | ||
| ) { | ||
| statement.setIsExported(false); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| Node.isFunctionDeclaration(statement) && | ||
| statement.isExported() && | ||
| !statement.isDefaultExport() && | ||
| statement.getName() === componentName | ||
| ) { | ||
| statement.setIsExported(false); | ||
|
|
||
| return; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const replaceDefaultExportWithRenderFunction = ( | ||
| sourceFile: SourceFile, | ||
| componentName: string, | ||
| ): void => { | ||
| for (const statement of sourceFile.getStatements()) { | ||
| if (!Node.isExportAssignment(statement) || statement.isExportEquals()) { | ||
| continue; | ||
| } | ||
|
|
||
| statement.replaceWithText( | ||
| `export default function __renderFrontComponent(__container) {` + | ||
| ` __createRoot(__container).render(` + | ||
| `__frontComponentJsx(${componentName}, {})); }`, | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
| }; | ||
|
|
||
| export const unwrapDefineFrontComponentToDirectExport = ( | ||
| sourceCode: string, | ||
| ): string => { | ||
| let transformedSource = sourceCode.replace( | ||
| DEFINE_FRONT_COMPONENT_IMPORT_PATTERN, | ||
| '', | ||
| ); | ||
| const existingFile = project.getSourceFile('component.tsx'); | ||
|
|
||
| const defineFrontComponentMatch = transformedSource.match( | ||
| DEFINE_FRONT_COMPONENT_EXPORT_PATTERN, | ||
| ); | ||
| if (existingFile) { | ||
| project.removeSourceFile(existingFile); | ||
| } | ||
|
|
||
| if (defineFrontComponentMatch) { | ||
| const wrappedComponentName = defineFrontComponentMatch[1]; | ||
| const sourceFile = project.createSourceFile('component.tsx', sourceCode); | ||
|
|
||
| const exportedComponentDeclarationPattern = new RegExp( | ||
| `export\\s+(const|function)\\s+${wrappedComponentName}\\b`, | ||
| ); | ||
| const componentName = extractComponentNameFromDefaultExport(sourceFile); | ||
|
|
||
| transformedSource = transformedSource.replace( | ||
| exportedComponentDeclarationPattern, | ||
| `$1 ${wrappedComponentName}`, | ||
| ); | ||
| if (!componentName) { | ||
| return sourceCode; | ||
| } | ||
|
|
||
| transformedSource = | ||
| `import { createRoot as __createRoot } from 'react-dom/client';\n` + | ||
| `import { jsx as __frontComponentJsx } from 'react/jsx-runtime';\n` + | ||
| transformedSource; | ||
| removeDefineFrontComponentFromImports(sourceFile); | ||
| removeExportFromComponentDeclaration(sourceFile, componentName); | ||
| replaceDefaultExportWithRenderFunction(sourceFile, componentName); | ||
|
|
||
| transformedSource = transformedSource.replace( | ||
| DEFINE_FRONT_COMPONENT_EXPORT_PATTERN, | ||
| `export default function __renderFrontComponent(__container) { __createRoot(__container).render(__frontComponentJsx(${wrappedComponentName}, {})); }`, | ||
| ); | ||
| } | ||
| sourceFile.insertStatements(0, [ | ||
| `import { createRoot as __createRoot } from 'react-dom/client';`, | ||
| `import { jsx as __frontComponentJsx } from 'react/jsx-runtime';`, | ||
| ]); | ||
|
|
||
| return transformedSource; | ||
| return sourceFile.getFullText(); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.