From 0ce1f379438ecaaa96b27481a69a1efb1c997c11 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Wed, 19 Apr 2023 09:45:36 +0100 Subject: [PATCH] fix(angular): add component import path correctly to NgModule when flat=false (#16364) --- .../__snapshots__/component.spec.ts.snap | 12 ++++++ .../generators/component/component.spec.ts | 40 +++++++++++++++++++ .../src/generators/component/component.ts | 7 +--- .../component/lib/normalize-options.ts | 1 + 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/packages/angular/src/generators/component/__snapshots__/component.spec.ts.snap b/packages/angular/src/generators/component/__snapshots__/component.spec.ts.snap index 254b2d0251781..91b46b3b15688 100644 --- a/packages/angular/src/generators/component/__snapshots__/component.spec.ts.snap +++ b/packages/angular/src/generators/component/__snapshots__/component.spec.ts.snap @@ -24,6 +24,18 @@ export class ExampleComponent {} " `; +exports[`component Generator --module should import the component correctly to the module file when flat is false 1`] = ` +"import { NgModule } from '@angular/core'; +import { ExampleComponent } from './example/example.component'; + +@NgModule({ + declarations: [ExampleComponent], + exports: [ExampleComponent], +}) +export class LibModule {} +" +`; + exports[`component Generator --path should create the component correctly and export it in the entry point 1`] = ` "import { Component } from '@angular/core'; diff --git a/packages/angular/src/generators/component/component.spec.ts b/packages/angular/src/generators/component/component.spec.ts index 09ec5bc1e4231..1cae01a176f34 100644 --- a/packages/angular/src/generators/component/component.spec.ts +++ b/packages/angular/src/generators/component/component.spec.ts @@ -648,6 +648,46 @@ describe('component Generator', () => { } ); + it('should import the component correctly to the module file when flat is false', async () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + addProjectConfiguration(tree, 'shared-ui', { + projectType: 'library', + sourceRoot: 'libs/shared/ui/src', + root: 'libs/shared/ui', + }); + tree.write( + 'libs/shared/ui/src/lib/lib.module.ts', + ` + import { NgModule } from '@angular/core'; + + @NgModule({ + declarations: [], + exports: [] + }) + export class LibModule {}` + ); + tree.write( + 'libs/shared/ui/src/index.ts', + 'export * from "./lib/lib.module";' + ); + + // ACT + await componentGenerator(tree, { + name: 'example', + project: 'shared-ui', + export: true, + flat: false, + }); + + // ASSERT + const moduleSource = tree.read( + 'libs/shared/ui/src/lib/lib.module.ts', + 'utf-8' + ); + expect(moduleSource).toMatchSnapshot(); + }); + it('should not export it in the entry point when the module it belong to is not exported', async () => { // ARRANGE const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); diff --git a/packages/angular/src/generators/component/component.ts b/packages/angular/src/generators/component/component.ts index 07db7fbec5f35..15b9e98fcf968 100644 --- a/packages/angular/src/generators/component/component.ts +++ b/packages/angular/src/generators/component/component.ts @@ -82,12 +82,7 @@ export async function componentGenerator(tree: Tree, rawOptions: Schema) { modulePath, componentNames.fileName, `${componentNames.className}${typeNames.className}`, - options.flat - ? `${componentNames.fileName}.${typeNames.fileName}` - : joinPathFragments( - componentNames.fileName, - `${componentNames.fileName}.${typeNames.fileName}` - ), + `${componentNames.fileName}.${typeNames.fileName}`, 'declarations', options.flat, options.export diff --git a/packages/angular/src/generators/component/lib/normalize-options.ts b/packages/angular/src/generators/component/lib/normalize-options.ts index 1e11940ba705e..5ca348798e115 100644 --- a/packages/angular/src/generators/component/lib/normalize-options.ts +++ b/packages/angular/src/generators/component/lib/normalize-options.ts @@ -27,6 +27,7 @@ export function normalizeOptions( name, changeDetection: options.changeDetection ?? 'Default', style: options.style ?? 'css', + flat: options.flat ?? false, directory, filePath, path,