Skip to content

Commit

Permalink
fix(angular): add missing skipImport option to the component generator (
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed May 6, 2022
1 parent 7da1913 commit 5c94d62
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/generated/packages/angular.json
Expand Up @@ -319,6 +319,11 @@
"description": "Create the new files at the top level of the current project.",
"default": false
},
"skipImport": {
"type": "boolean",
"description": "Do not import this component into the owning NgModule.",
"default": false
},
"selector": {
"type": "string",
"format": "html-selector",
Expand Down
Expand Up @@ -100,6 +100,25 @@ export class ExampleComponent implements OnInit {
"
`;

exports[`component Generator should create the component correctly and not export it when "--skip-import=true" 1`] = `
"import { Component, OnInit } from '@angular/core';
@Component({
selector: 'example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
"
`;

exports[`component Generator should create the component correctly but not export it when no entry point exists 1`] = `
"import { Component, OnInit } from '@angular/core';
Expand Down
41 changes: 41 additions & 0 deletions packages/angular/src/generators/component/component.spec.ts
Expand Up @@ -83,6 +83,47 @@ describe('component Generator', () => {
);
});

it('should create the component correctly and not export it when "--skip-import=true"', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace(2);
addProjectConfiguration(tree, 'lib1', {
projectType: 'library',
sourceRoot: 'libs/lib1/src',
root: 'libs/lib1',
});
tree.write(
'libs/lib1/src/lib/lib.module.ts',
`
import { NgModule } from '@angular/core';
@NgModule({
declarations: [],
exports: []
})
export class LibModule {}`
);
tree.write('libs/lib1/src/index.ts', '');

// ACT
await componentGenerator(tree, {
name: 'example',
project: 'lib1',
skipImport: true,
});

// ASSERT
const componentSource = tree.read(
'libs/lib1/src/lib/example/example.component.ts',
'utf-8'
);
expect(componentSource).toMatchSnapshot();

const indexSource = tree.read('libs/lib1/src/index.ts', 'utf-8');
expect(indexSource).not.toContain(
`export * from "./lib/example/example.component"`
);
});

it('should create the component correctly but not export it when no entry point exists', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace(2);
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/component/component.ts
Expand Up @@ -47,7 +47,7 @@ function checkPathUnderProjectRoot(tree: Tree, schema: Partial<Schema>) {
}

function exportComponent(tree: Tree, schema: Schema) {
if (!schema.export) {
if (!schema.export || schema.skipImport) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/component/schema.d.ts
Expand Up @@ -11,6 +11,7 @@ export interface Schema {
skipTests?: boolean;
type?: string;
flat?: boolean;
skipImport?: boolean;
selector?: string;
module?: string;
skipSelector?: boolean;
Expand Down
5 changes: 5 additions & 0 deletions packages/angular/src/generators/component/schema.json
Expand Up @@ -81,6 +81,11 @@
"description": "Create the new files at the top level of the current project.",
"default": false
},
"skipImport": {
"type": "boolean",
"description": "Do not import this component into the owning NgModule.",
"default": false
},
"selector": {
"type": "string",
"format": "html-selector",
Expand Down

1 comment on commit 5c94d62

@vercel
Copy link

@vercel vercel bot commented on 5c94d62 May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.