-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@schematics/angular): infer app component name and path in server schematic #30028
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
Merged
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions
8
...s/angular/server/files/application-builder/ngmodule-src/app/app.module.server.ts.template
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,12 +1,12 @@ | ||
| import { NgModule } from '@angular/core'; | ||
| import { provideServerRendering, withRoutes } from '@angular/ssr'; | ||
| import { App } from './app'; | ||
| import { AppModule } from './app.module'; | ||
| import { <%= appComponentName %> } from '<%= appComponentPath %>'; | ||
| import { <%= appModuleName %> } from '<%= appModulePath %>'; | ||
| import { serverRoutes } from './app.routes.server'; | ||
|
|
||
| @NgModule({ | ||
| imports: [AppModule], | ||
| imports: [<%= appModuleName %>], | ||
| providers: [provideServerRendering(withRoutes(serverRoutes))], | ||
| bootstrap: [App], | ||
| bootstrap: [<%= appComponentName %>], | ||
| }) | ||
| export class AppServerModule {} |
4 changes: 2 additions & 2 deletions
4
...chematics/angular/server/files/application-builder/standalone-src/main.server.ts.template
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,7 +1,7 @@ | ||
| import { bootstrapApplication } from '@angular/platform-browser'; | ||
| import { App } from './app/app'; | ||
| import { <%= appComponentName %> } from '<%= appComponentPath %>'; | ||
| import { config } from './app/app.config.server'; | ||
|
|
||
| const bootstrap = () => bootstrapApplication(App, config); | ||
| const bootstrap = () => bootstrapApplication(<%= appComponentName %>, config); | ||
|
|
||
| export default bootstrap; | ||
8 changes: 4 additions & 4 deletions
8
...matics/angular/server/files/server-builder/ngmodule-src/app/app.module.server.ts.template
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,14 +1,14 @@ | ||
| import { NgModule } from '@angular/core'; | ||
| import { ServerModule } from '@angular/platform-server'; | ||
|
|
||
| import { AppModule } from './app.module'; | ||
| import { App } from './app'; | ||
| import { <%= appModuleName %> } from '<%= appModulePath %>'; | ||
| import { <%= appComponentName %> } from '<%= appComponentPath %>'; | ||
|
|
||
| @NgModule({ | ||
| imports: [ | ||
| AppModule, | ||
| <%= appModuleName %>, | ||
| ServerModule, | ||
| ], | ||
| bootstrap: [App], | ||
| bootstrap: [<%= appComponentName %>], | ||
| }) | ||
| export class AppServerModule {} |
4 changes: 2 additions & 2 deletions
4
...ges/schematics/angular/server/files/server-builder/standalone-src/main.server.ts.template
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,7 +1,7 @@ | ||
| import { bootstrapApplication } from '@angular/platform-browser'; | ||
| import { App } from './app/app'; | ||
| import { <%= appComponentName %> } from '<%= appComponentPath %>'; | ||
| import { config } from './app/app.config.server'; | ||
|
|
||
| const bootstrap = () => bootstrapApplication(App, config); | ||
| const bootstrap = () => bootstrapApplication(<%= appComponentName %>, config); | ||
|
|
||
| export default bootstrap; |
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
148 changes: 148 additions & 0 deletions
148
packages/schematics/angular/utility/standalone/app_component.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 |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { SchematicsException, Tree } from '@angular-devkit/schematics'; | ||
| import ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; | ||
| import { getDecoratorMetadata, getMetadataField } from '../ast-utils'; | ||
| import { findBootstrapModuleCall, getAppModulePath } from '../ng-ast-utils'; | ||
| import { findBootstrapApplicationCall, getSourceFile } from './util'; | ||
|
|
||
| /** Data resolved for a bootstrapped component. */ | ||
| interface BootstrappedComponentData { | ||
| /** Original name of the component class. */ | ||
| componentName: string; | ||
|
|
||
| /** Path under which the component was imported in the main entrypoint. */ | ||
| componentImportPathInSameFile: string; | ||
|
|
||
| /** Original name of the NgModule being bootstrapped, null if the app isn't module-based. */ | ||
| moduleName: string | null; | ||
|
|
||
| /** | ||
| * Path under which the module was imported in the main entrypoint, | ||
| * null if the app isn't module-based. | ||
| */ | ||
| moduleImportPathInSameFile: string | null; | ||
| } | ||
|
|
||
| /** | ||
| * Finds the original name and path relative to the `main.ts` of the bootrstrapped app component. | ||
| * @param tree File tree in which to look for the component. | ||
| * @param mainFilePath Path of the `main` file. | ||
| */ | ||
| export function resolveBootstrappedComponentData( | ||
| tree: Tree, | ||
| mainFilePath: string, | ||
| ): BootstrappedComponentData | null { | ||
| // First try to resolve for a standalone app. | ||
| try { | ||
| const call = findBootstrapApplicationCall(tree, mainFilePath); | ||
|
|
||
| if (call.arguments.length > 0 && ts.isIdentifier(call.arguments[0])) { | ||
| const resolved = resolveIdentifier(call.arguments[0]); | ||
|
|
||
| if (resolved) { | ||
| return { | ||
| componentName: resolved.name, | ||
| componentImportPathInSameFile: resolved.path, | ||
| moduleName: null, | ||
| moduleImportPathInSameFile: null, | ||
| }; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| // `findBootstrapApplicationCall` will throw if it can't find the `bootrstrapApplication` call. | ||
| // Catch so we can continue to the fallback logic. | ||
| if (!(e instanceof SchematicsException)) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| // Otherwise fall back to resolving an NgModule-based app. | ||
| return resolveNgModuleBasedData(tree, mainFilePath); | ||
| } | ||
|
|
||
| /** Resolves the bootstrap data for a NgModule-based app. */ | ||
| function resolveNgModuleBasedData( | ||
| tree: Tree, | ||
| mainFilePath: string, | ||
| ): BootstrappedComponentData | null { | ||
| const appModulePath = getAppModulePath(tree, mainFilePath); | ||
| const appModuleFile = getSourceFile(tree, appModulePath); | ||
| const metadataNodes = getDecoratorMetadata(appModuleFile, 'NgModule', '@angular/core'); | ||
|
|
||
| for (const node of metadataNodes) { | ||
| if (!ts.isObjectLiteralExpression(node)) { | ||
| continue; | ||
| } | ||
|
|
||
| const bootstrapProp = getMetadataField(node, 'bootstrap').find((prop) => { | ||
| return ( | ||
| ts.isArrayLiteralExpression(prop.initializer) && | ||
| prop.initializer.elements.length > 0 && | ||
| ts.isIdentifier(prop.initializer.elements[0]) | ||
| ); | ||
| }); | ||
|
|
||
| const componentIdentifier = (bootstrapProp?.initializer as ts.ArrayLiteralExpression) | ||
| .elements[0] as ts.Identifier | undefined; | ||
| const componentResult = componentIdentifier ? resolveIdentifier(componentIdentifier) : null; | ||
| const bootstrapCall = findBootstrapModuleCall(tree, mainFilePath); | ||
|
|
||
| if ( | ||
| componentResult && | ||
| bootstrapCall && | ||
| bootstrapCall.arguments.length > 0 && | ||
| ts.isIdentifier(bootstrapCall.arguments[0]) | ||
| ) { | ||
| const moduleResult = resolveIdentifier(bootstrapCall.arguments[0]); | ||
|
|
||
| if (moduleResult) { | ||
| return { | ||
| componentName: componentResult.name, | ||
| componentImportPathInSameFile: componentResult.path, | ||
| moduleName: moduleResult.name, | ||
| moduleImportPathInSameFile: moduleResult.path, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** Resolves an identifier to its original name and path that it was imported from. */ | ||
| function resolveIdentifier(identifier: ts.Identifier): { name: string; path: string } | null { | ||
| const sourceFile = identifier.getSourceFile(); | ||
|
|
||
| // Try to resolve the import path by looking at the top-level named imports of the file. | ||
| for (const node of sourceFile.statements) { | ||
| if ( | ||
| !ts.isImportDeclaration(node) || | ||
| !ts.isStringLiteral(node.moduleSpecifier) || | ||
| !node.importClause || | ||
| !node.importClause.namedBindings || | ||
| !ts.isNamedImports(node.importClause.namedBindings) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| for (const element of node.importClause.namedBindings.elements) { | ||
| if (element.name.text === identifier.text) { | ||
| return { | ||
| // Note that we use `propertyName` if available, because it contains | ||
| // the real name in the case where the import is aliased. | ||
| name: (element.propertyName || element.name).text, | ||
| path: node.moduleSpecifier.text, | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } |
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.