Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';

@Component({<% if(!skipSelector) {%>
@Component({<% if (standalone) {%>
standalone: true,<%}%><% if(!skipSelector) {%>
selector: '<%= selector %>',<%}%><% if(inlineTemplate) { %>
template: `
<p>
Expand Down
35 changes: 35 additions & 0 deletions packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,41 @@ describe('Component Schematic', () => {
);
});

it('should create a standalone component', async () => {
const options = { ...defaultOptions, standalone: true };

const fooModule = '/projects/bar/src/app/foo/foo.module.ts';
appTree.create(
fooModule,
`
import { NgModule } from '@angular/core';

@NgModule({
imports: [],
declarations: []
})
export class FooModule { }
`,
);

const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
'/projects/bar/src/app/foo/foo.component.css',
'/projects/bar/src/app/foo/foo.component.html',
'/projects/bar/src/app/foo/foo.component.spec.ts',
'/projects/bar/src/app/foo/foo.component.ts',
]),
);

const fooComponentContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(fooComponentContent).toContain('standalone: true,');

const fooModuleContent = tree.readContent(fooModule);
expect(fooModuleContent).not.toContain('FooComponent');
});

it('should find the closest module', async () => {
const options = { ...defaultOptions };
const fooModule = '/projects/bar/src/app/foo/foo.module.ts';
Expand Down
7 changes: 7 additions & 0 deletions packages/schematics/angular/component/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
"alias": "t",
"x-user-analytics": 10
},
"standalone": {
"description": "Generate a standalone component",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Drive by comment, can you please add a full stop at the end of the sentence?

"type": "boolean",
"default": false,
"alias": "sa",
"x-user-analytics": 100
},
"viewEncapsulation": {
"description": "The view encapsulation strategy to use in the new component.",
"enum": ["Emulated", "None", "ShadowDom"],
Expand Down
6 changes: 6 additions & 0 deletions packages/schematics/angular/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ModuleOptions {
skipImport?: boolean;
moduleExt?: string;
routingModuleExt?: string;
standalone?: boolean;
}

export const MODULE_EXT = '.module.ts';
Expand All @@ -31,6 +32,11 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
return undefined;
}

// eslint-disable-next-line no-prototype-builtins
if (options.hasOwnProperty('standalone') && options.standalone) {
return undefined;
}

const moduleExt = options.moduleExt || MODULE_EXT;
const routingModuleExt = options.routingModuleExt || ROUTING_MODULE_EXT;

Expand Down