Skip to content

Commit

Permalink
feat(angular): add option to export component as default (#28883)
Browse files Browse the repository at this point in the history
Upstream change:
angular/angular-cli@a381a3d

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
leosvelperez committed Nov 12, 2024
1 parent caa8eb6 commit 4a76f00
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/generated/packages/angular/generators/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
"default": false,
"x-priority": "important"
},
"exportDefault": {
"type": "boolean",
"default": false,
"description": "Use default export for the component instead of a named export."
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
Expand Down
19 changes: 19 additions & 0 deletions packages/angular/src/generators/component/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ describe('component Generator', () => {
);
});

it('should export the component as default when exportDefault is true', async () => {
const tree = createTreeWithEmptyWorkspace({});
addProjectConfiguration(tree, 'lib1', {
projectType: 'library',
sourceRoot: 'libs/lib1/src',
root: 'libs/lib1',
});
tree.write('libs/lib1/src/index.ts', '');

await componentGenerator(tree, {
path: 'libs/lib1/src/lib/example/example',
exportDefault: true,
});

expect(
tree.read('libs/lib1/src/lib/example/example.component.ts', 'utf-8')
).toContain('export default class ExampleComponent {}');
});

it('should not generate test file when --skip-tests=true', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function componentGenerator(tree: Tree, rawOptions: Schema) {
name: options.name,
fileName: options.fileName,
symbolName: options.symbolName,
exportDefault: options.exportDefault,
style: options.style,
inlineStyle: options.inlineStyle,
inlineTemplate: options.inlineTemplate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ import { CommonModule } from '@angular/common';<% } %>
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
})
export class <%= symbolName %> {}
export <% if (exportDefault) {%>default <%}%>class <%= symbolName %> {}
1 change: 1 addition & 0 deletions packages/angular/src/generators/component/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Schema {
module?: string;
skipSelector?: boolean;
export?: boolean;
exportDefault?: boolean;
prefix?: string;
skipFormat?: boolean;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/angular/src/generators/component/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
"default": false,
"x-priority": "important"
},
"exportDefault": {
"type": "boolean",
"default": false,
"description": "Use default export for the component instead of a named export."
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
Expand Down

0 comments on commit 4a76f00

Please sign in to comment.