diff --git a/packages/schematics/angular/app-shell/index.ts b/packages/schematics/angular/app-shell/index.ts index 868205182de6..a2d661abf565 100644 --- a/packages/schematics/angular/app-shell/index.ts +++ b/packages/schematics/angular/app-shell/index.ts @@ -46,17 +46,17 @@ function getSourceFile(host: Tree, path: string): ts.SourceFile { function getServerModulePath( host: Tree, - projectRoot: string, + sourceRoot: string, mainPath: string, ): string | null { - const mainSource = getSourceFile(host, mainPath); + const mainSource = getSourceFile(host, join(normalize(sourceRoot), mainPath)); const allNodes = getSourceNodes(mainSource); - const expNode = allNodes.filter(node => node.kind === ts.SyntaxKind.ExportDeclaration)[0]; + const expNode = allNodes.find(node => ts.isExportDeclaration(node)); if (!expNode) { return null; } const relativePath = (expNode as ts.ExportDeclaration).moduleSpecifier as ts.StringLiteral; - const modulePath = normalize(`/${projectRoot}/src/${relativePath.text}.ts`); + const modulePath = normalize(`/${sourceRoot}/${relativePath.text}.ts`); return modulePath; } @@ -243,7 +243,7 @@ function addServerRoutes(options: AppShellOptions): Rule { if (!clientServerOptions) { throw new SchematicsException('Server target does not contain options.'); } - const modulePath = getServerModulePath(host, clientProject.root, clientServerOptions.main); + const modulePath = getServerModulePath(host, clientProject.sourceRoot || 'src', options.main as string); if (modulePath === null) { throw new SchematicsException('Universal/server module not found.'); } diff --git a/packages/schematics/angular/app-shell/index_spec.ts b/packages/schematics/angular/app-shell/index_spec.ts index 2161a7c0ffd0..4bf8efeb159d 100644 --- a/packages/schematics/angular/app-shell/index_spec.ts +++ b/packages/schematics/angular/app-shell/index_spec.ts @@ -157,6 +157,22 @@ describe('App Shell Schematic', () => { expect(content).toMatch(/import { Routes, RouterModule } from \'@angular\/router\';/); }); + it('should work after adding nguniversal', async () => { + let tree = await schematicRunner.runSchematicAsync('universal', defaultOptions, appTree) + .toPromise(); + + // change main tsconfig to mimic ng add for nguniveral + const workspace = JSON.parse(appTree.readContent('/angular.json')); + workspace.projects.bar.architect.server.options.main = 'server.ts'; + appTree.overwrite('angular.json', JSON.stringify(workspace, undefined, 2)); + + tree = await schematicRunner.runSchematicAsync('appShell', defaultOptions, tree) + .toPromise(); + const filePath = '/projects/bar/src/app/app.server.module.ts'; + const content = tree.readContent(filePath); + expect(content).toMatch(/import { Routes, RouterModule } from \'@angular\/router\';/); + }); + it('should define a server route', async () => { const tree = await schematicRunner.runSchematicAsync('appShell', defaultOptions, appTree) .toPromise();