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
This commit is contained in:
aparzi 2024-08-23 23:22:29 +02:00 committed by Alan Agius
parent ac102aa9c6
commit a381a3db18
3 changed files with 22 additions and 1 deletions

View File

@ -19,6 +19,6 @@ import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
})
export class <%= classify(name) %><%= classify(type) %> {
export <% if(exportDefault) {%>default <%}%>class <%= classify(name) %><%= classify(type) %> {
}

View File

@ -496,4 +496,20 @@ describe('Component Schematic', () => {
await expectAsync(schematicRunner.runSchematic('component', options, appTree)).toBeRejected();
});
});
it('should export the component as default when exportDefault is true', async () => {
const options = { ...defaultOptions, exportDefault: true };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).toContain('export default class FooComponent');
});
it('should export the component as a named export when exportDefault is false', async () => {
const options = { ...defaultOptions, exportDefault: false };
const tree = await schematicRunner.runSchematic('component', options, appTree);
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
expect(tsContent).toContain('export class FooComponent');
});
});

View File

@ -130,6 +130,11 @@
"type": "boolean",
"default": false,
"description": "The declaring NgModule exports this component."
},
"exportDefault": {
"type": "boolean",
"default": false,
"description": "Use default export for the component instead of a named export."
}
},
"required": ["name", "project"]