Skip to content

Commit

Permalink
fix(@schematics/angular): remove CommonModule import from standalone …
Browse files Browse the repository at this point in the history
…components

`ng g c` no longer generates a component with the `CommonModule` import.
This import is not useful if developers adopt the control flow syntax in their templates.
Instead, developers are encouraged to import the individual directives/pipes if needed.

(cherry picked from commit 571722d)
  • Loading branch information
cexbrayat authored and alan-agius4 committed Nov 23, 2023
1 parent 7747495 commit 7a28230
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';<% if(standalone) {%>
import { CommonModule } from '@angular/common';<% } %>
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';

@Component({<% if(!skipSelector) {%>
selector: '<%= selector %>',<%}%><% if(standalone) {%>
standalone: true,
imports: [CommonModule],<%}%><% if(inlineTemplate) { %>
imports: [],<%}%><% if(inlineTemplate) { %>
template: `
<p>
<%= dasherize(name) %> works!
Expand Down
2 changes: 0 additions & 2 deletions packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,9 @@ describe('Component Schematic', () => {
const tree = await schematicRunner.runSchematic('component', options, appTree);
const moduleContent = tree.readContent('/projects/bar/src/app/app.module.ts');
const componentContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(componentContent).toContain('@angular/common');
expect(componentContent).toContain('class FooComponent');
expect(moduleContent).not.toContain('FooComponent');
expect(componentContent).toContain('standalone: true');
expect(componentContent).toContain('imports: [CommonModule]');
});

it('should declare standalone components in the `imports` of a test', async () => {
Expand Down

0 comments on commit 7a28230

Please sign in to comment.