Skip to content

Commit a381a3d

Browse files
aparzialan-agius4
authored andcommitted
feat(@schematics/angular): add option to export component as default
Introduces option `--export-default` to control whether the generated component uses a default export instead of a named export. Closes: #25023
1 parent ac102aa commit a381a3d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.ts.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%
1919
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
2020
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
2121
})
22-
export class <%= classify(name) %><%= classify(type) %> {
22+
export <% if(exportDefault) {%>default <%}%>class <%= classify(name) %><%= classify(type) %> {
2323

2424
}

packages/schematics/angular/component/index_spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -496,4 +496,20 @@ describe('Component Schematic', () => {
496496
await expectAsync(schematicRunner.runSchematic('component', options, appTree)).toBeRejected();
497497
});
498498
});
499+
500+
it('should export the component as default when exportDefault is true', async () => {
501+
const options = { ...defaultOptions, exportDefault: true };
502+
503+
const tree = await schematicRunner.runSchematic('component', options, appTree);
504+
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
505+
expect(tsContent).toContain('export default class FooComponent');
506+
});
507+
508+
it('should export the component as a named export when exportDefault is false', async () => {
509+
const options = { ...defaultOptions, exportDefault: false };
510+
511+
const tree = await schematicRunner.runSchematic('component', options, appTree);
512+
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
513+
expect(tsContent).toContain('export class FooComponent');
514+
});
499515
});

packages/schematics/angular/component/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
"type": "boolean",
131131
"default": false,
132132
"description": "The declaring NgModule exports this component."
133+
},
134+
"exportDefault": {
135+
"type": "boolean",
136+
"default": false,
137+
"description": "Use default export for the component instead of a named export."
133138
}
134139
},
135140
"required": ["name", "project"]

0 commit comments

Comments
 (0)