Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 92cfd97

Browse files
JLHwungfilipesilva
authored andcommitted
feat(@schematics/angular): support entryComponent
Fix angular/angular-cli#7749
1 parent f05698f commit 92cfd97

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

packages/schematics/angular/component/index.ts

+34-13
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,27 @@ import {
2222
url,
2323
} from '@angular-devkit/schematics';
2424
import * as ts from 'typescript';
25-
import { addDeclarationToModule, addExportToModule } from '../utility/ast-utils';
25+
import {
26+
addDeclarationToModule,
27+
addEntryComponentToModule,
28+
addExportToModule,
29+
} from '../utility/ast-utils';
2630
import { InsertChange } from '../utility/change';
2731
import { getWorkspace } from '../utility/config';
2832
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
2933
import { parseName } from '../utility/parse-name';
3034
import { validateHtmlSelector, validateName } from '../utility/validation';
3135
import { Schema as ComponentOptions } from './schema';
3236

37+
function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile {
38+
const text = host.read(modulePath);
39+
if (text === null) {
40+
throw new SchematicsException(`File ${modulePath} does not exist.`);
41+
}
42+
const sourceText = text.toString('utf-8');
43+
44+
return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
45+
}
3346

3447
function addDeclarationToNgModule(options: ComponentOptions): Rule {
3548
return (host: Tree) => {
@@ -38,12 +51,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
3851
}
3952

4053
const modulePath = options.module;
41-
const text = host.read(modulePath);
42-
if (text === null) {
43-
throw new SchematicsException(`File ${modulePath} does not exist.`);
44-
}
45-
const sourceText = text.toString('utf-8');
46-
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
54+
const source = readIntoSourceFile(host, modulePath);
4755

4856
const componentPath = `/${options.path}/`
4957
+ (options.flat ? '' : strings.dasherize(options.name) + '/')
@@ -66,12 +74,7 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
6674

6775
if (options.export) {
6876
// Need to refresh the AST because we overwrote the file in the host.
69-
const text = host.read(modulePath);
70-
if (text === null) {
71-
throw new SchematicsException(`File ${modulePath} does not exist.`);
72-
}
73-
const sourceText = text.toString('utf-8');
74-
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
77+
const source = readIntoSourceFile(host, modulePath);
7578

7679
const exportRecorder = host.beginUpdate(modulePath);
7780
const exportChanges = addExportToModule(source, modulePath,
@@ -86,6 +89,24 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
8689
host.commitUpdate(exportRecorder);
8790
}
8891

92+
if (options.entryComponent) {
93+
// Need to refresh the AST because we overwrote the file in the host.
94+
const source = readIntoSourceFile(host, modulePath);
95+
96+
const entryComponentRecorder = host.beginUpdate(modulePath);
97+
const entryComponentChanges = addEntryComponentToModule(
98+
source, modulePath,
99+
strings.classify(`${options.name}Component`),
100+
relativePath);
101+
102+
for (const change of entryComponentChanges) {
103+
if (change instanceof InsertChange) {
104+
entryComponentRecorder.insertLeft(change.pos, change.toAdd);
105+
}
106+
}
107+
host.commitUpdate(entryComponentRecorder);
108+
}
109+
89110

90111
return host;
91112
};

packages/schematics/angular/component/index_spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ describe('Component Schematic', () => {
135135
expect(appModuleContent).toMatch(/exports: \[FooComponent\]/);
136136
});
137137

138+
it('should set the entry component', () => {
139+
const options = { ...defaultOptions, entryComponent: true };
140+
141+
const tree = schematicRunner.runSchematic('component', options, appTree);
142+
const appModuleContent = tree.readContent('/projects/bar/src/app/app.module.ts');
143+
expect(appModuleContent).toMatch(/entryComponents: \[FooComponent\]/);
144+
});
145+
138146
it('should import into a specified module', () => {
139147
const options = { ...defaultOptions, module: 'app.module.ts' };
140148

packages/schematics/angular/component/schema.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,8 @@ export interface Schema {
6767
* Specifies if declaring module exports the component.
6868
*/
6969
export?: boolean;
70+
/**
71+
* Specifies if the component is an entry component of declaring module.
72+
*/
73+
entryComponent?: boolean;
7074
}

packages/schematics/angular/component/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
"type": "boolean",
9999
"default": false,
100100
"description": "Specifies if declaring module exports the component."
101+
},
102+
"entryComponent": {
103+
"type": "boolean",
104+
"default": false,
105+
"description": "Specifies if the component is an entry component of declaring module."
101106
}
102107
},
103108
"required": []

packages/schematics/angular/utility/ast-utils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,18 @@ export function addBootstrapToModule(source: ts.SourceFile,
531531
return addSymbolToNgModuleMetadata(source, modulePath, 'bootstrap', classifiedName, importPath);
532532
}
533533

534+
/**
535+
* Custom function to insert an entryComponent into NgModule. It also imports it.
536+
*/
537+
export function addEntryComponentToModule(source: ts.SourceFile,
538+
modulePath: string, classifiedName: string,
539+
importPath: string): Change[] {
540+
return addSymbolToNgModuleMetadata(
541+
source, modulePath,
542+
'entryComponents', classifiedName, importPath,
543+
);
544+
}
545+
534546
/**
535547
* Determine if an import already exists.
536548
*/

0 commit comments

Comments
 (0)