Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions packages/schematics/angular/server/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,78 @@ describe('Server Schematic', () => {
expect(contents).toContain(`bootstrap: [MyCustomApp],`);
});

it('should skip an NgModule without bootstrap that is declared before the root module', async () => {
appTree.create(
'/projects/bar/src/app/my-custom-module.ts',
`
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MyCustomApp } from './foo/bar/baz/app.foo';

@NgModule({
exports: [],
})
export class SharedModule {}

@NgModule({
declarations: [MyCustomApp],
imports: [BrowserModule, SharedModule],
bootstrap: [MyCustomApp]
})
export class MyCustomModule {}
`,
);

appTree.overwrite(
'/projects/bar/src/main.ts',
`
import { platformBrowser } from '@angular/platform-browser';
import { MyCustomModule } from './app/my-custom-module';

platformBrowser().bootstrapModule(MyCustomModule)
.catch(err => console.error(err));
`,
);

const tree = await schematicRunner.runSchematic('server', defaultOptions, appTree);
const contents = tree.readContent('/projects/bar/src/app/app.module.server.ts');

expect(contents).toContain(`import { MyCustomApp } from './foo/bar/baz/app.foo';`);
expect(contents).toContain(`imports: [MyCustomModule],`);
expect(contents).toContain(`bootstrap: [MyCustomApp],`);
});

it('should not fail when the root NgModule uses ngDoBootstrap', async () => {
appTree.create(
'/projects/bar/src/app/my-custom-module.ts',
`
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

@NgModule({
imports: [BrowserModule],
})
export class MyCustomModule {
ngDoBootstrap() {}
}
`,
);

appTree.overwrite(
'/projects/bar/src/main.ts',
`
import { platformBrowser } from '@angular/platform-browser';
import { MyCustomModule } from './app/my-custom-module';

platformBrowser().bootstrapModule(MyCustomModule)
.catch(err => console.error(err));
`,
);

const tree = await schematicRunner.runSchematic('server', defaultOptions, appTree);
expect(tree.exists('/projects/bar/src/app/app.module.server.ts')).toBeTrue();
});

it('should account for renamed app component and module that have been aliased', async () => {
appTree.create(
'/projects/bar/src/app/my-custom-module.ts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ function resolveNgModuleBasedData(
);
});

const componentIdentifier = (bootstrapProp?.initializer as ts.ArrayLiteralExpression)
if (!bootstrapProp) {
continue;
}

const componentIdentifier = (bootstrapProp.initializer as ts.ArrayLiteralExpression)
.elements[0] as ts.Identifier | undefined;
const componentResult = componentIdentifier ? resolveIdentifier(componentIdentifier) : null;
const bootstrapCall = findBootstrapModuleCall(tree, mainFilePath);
Expand Down