Skip to content

Commit 090bc99

Browse files
0z5aalan-agius4
authored andcommitted
fix(@schematics/angular): skip NgModules without bootstrap when resolving the app component
`resolveNgModuleBasedData` read `.elements` from an undefined `bootstrap` property when an `@NgModule` in the bootstrapped module's file had no `bootstrap` array (for example a root module using `ngDoBootstrap()`, or a shared module declared before the root module). This made the `server` and `ssr` schematics throw a TypeError instead of continuing with the next module or falling back to the default names. Closes #34761
1 parent ed233e0 commit 090bc99

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎packages/schematics/angular/server/index_spec.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,78 @@ describe('Server Schematic', () => {
109109
expect(contents).toContain(`bootstrap: [MyCustomApp],`);
110110
});
111111

112+
it('should skip an NgModule without bootstrap that is declared before the root module', async () => {
113+
appTree.create(
114+
'/projects/bar/src/app/my-custom-module.ts',
115+
`
116+
import { NgModule } from '@angular/core';
117+
import { BrowserModule } from '@angular/platform-browser';
118+
import { MyCustomApp } from './foo/bar/baz/app.foo';
119+
120+
@NgModule({
121+
exports: [],
122+
})
123+
export class SharedModule {}
124+
125+
@NgModule({
126+
declarations: [MyCustomApp],
127+
imports: [BrowserModule, SharedModule],
128+
bootstrap: [MyCustomApp]
129+
})
130+
export class MyCustomModule {}
131+
`,
132+
);
133+
134+
appTree.overwrite(
135+
'/projects/bar/src/main.ts',
136+
`
137+
import { platformBrowser } from '@angular/platform-browser';
138+
import { MyCustomModule } from './app/my-custom-module';
139+
140+
platformBrowser().bootstrapModule(MyCustomModule)
141+
.catch(err => console.error(err));
142+
`,
143+
);
144+
145+
const tree = await schematicRunner.runSchematic('server', defaultOptions, appTree);
146+
const contents = tree.readContent('/projects/bar/src/app/app.module.server.ts');
147+
148+
expect(contents).toContain(`import { MyCustomApp } from './foo/bar/baz/app.foo';`);
149+
expect(contents).toContain(`imports: [MyCustomModule],`);
150+
expect(contents).toContain(`bootstrap: [MyCustomApp],`);
151+
});
152+
153+
it('should not fail when the root NgModule uses ngDoBootstrap', async () => {
154+
appTree.create(
155+
'/projects/bar/src/app/my-custom-module.ts',
156+
`
157+
import { NgModule } from '@angular/core';
158+
import { BrowserModule } from '@angular/platform-browser';
159+
160+
@NgModule({
161+
imports: [BrowserModule],
162+
})
163+
export class MyCustomModule {
164+
ngDoBootstrap() {}
165+
}
166+
`,
167+
);
168+
169+
appTree.overwrite(
170+
'/projects/bar/src/main.ts',
171+
`
172+
import { platformBrowser } from '@angular/platform-browser';
173+
import { MyCustomModule } from './app/my-custom-module';
174+
175+
platformBrowser().bootstrapModule(MyCustomModule)
176+
.catch(err => console.error(err));
177+
`,
178+
);
179+
180+
const tree = await schematicRunner.runSchematic('server', defaultOptions, appTree);
181+
expect(tree.exists('/projects/bar/src/app/app.module.server.ts')).toBeTrue();
182+
});
183+
112184
it('should account for renamed app component and module that have been aliased', async () => {
113185
appTree.create(
114186
'/projects/bar/src/app/my-custom-module.ts',

‎packages/schematics/angular/utility/standalone/app_component.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ function resolveNgModuleBasedData(
8989
);
9090
});
9191

92-
const componentIdentifier = (bootstrapProp?.initializer as ts.ArrayLiteralExpression)
92+
if (!bootstrapProp) {
93+
continue;
94+
}
95+
96+
const componentIdentifier = (bootstrapProp.initializer as ts.ArrayLiteralExpression)
9397
.elements[0] as ts.Identifier | undefined;
9498
const componentResult = componentIdentifier ? resolveIdentifier(componentIdentifier) : null;
9599
const bootstrapCall = findBootstrapModuleCall(tree, mainFilePath);

0 commit comments

Comments
 (0)