Skip to content

Commit

Permalink
feat(angular): remove redundant standalone: true from templates for…
Browse files Browse the repository at this point in the history
… angular v19 or higher (#28879)

Upstream change:
angular/angular-cli@8567267

<!-- 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 15, 2024
1 parent ba8e32d commit 4a1e115
Show file tree
Hide file tree
Showing 26 changed files with 426 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ exports[`app --minimal should skip "nx-welcome.component.ts" file and references
import { RouterModule } from '@angular/router';
@Component({
standalone: true,
imports: [RouterModule],
selector: 'app-root',
templateUrl: './app.component.html',
Expand Down Expand Up @@ -172,7 +171,6 @@ exports[`app --minimal should skip "nx-welcome.component.ts" file and references
"import { Component } from '@angular/core';
@Component({
standalone: true,
imports: [],
selector: 'app-root',
templateUrl: './app.component.html',
Expand Down Expand Up @@ -245,7 +243,6 @@ import { RouterModule } from '@angular/router';
import { NxWelcomeComponent } from './nx-welcome.component';
@Component({
standalone: true,
imports: [NxWelcomeComponent, RouterModule],
selector: 'app-root',
templateUrl: './app.component.html',
Expand Down Expand Up @@ -313,7 +310,6 @@ exports[`app --standalone should generate a standalone app correctly without rou
import { NxWelcomeComponent } from './nx-welcome.component';
@Component({
standalone: true,
imports: [NxWelcomeComponent, ],
selector: 'app-root',
templateUrl: './app.component.html',
Expand Down
29 changes: 20 additions & 9 deletions packages/angular/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,14 @@ describe('app', () => {

it('should create Nx specific `nx-welcome.component.ts` file', async () => {
await generateApp(appTree, 'my-dir/my-app');
expect(
appTree.read('my-dir/my-app/src/app/nx-welcome.component.ts', 'utf-8')
).toContain('Hello there');

const nxWelcomeComponentText = appTree.read(
'my-dir/my-app/src/app/nx-welcome.component.ts',
'utf-8'
);
expect(nxWelcomeComponentText).not.toContain('standalone: true');
expect(nxWelcomeComponentText).toContain('standalone: false');
expect(nxWelcomeComponentText).toContain('Hello there');
});

it('should update the AppComponent spec to target Nx content', async () => {
Expand Down Expand Up @@ -956,9 +961,12 @@ describe('app', () => {
appTree.read('standalone/src/app/app.component.spec.ts', 'utf-8')
).toMatchSnapshot();
expect(appTree.exists('standalone/src/app/app.module.ts')).toBeFalsy();
expect(
appTree.read('standalone/src/app/nx-welcome.component.ts', 'utf-8')
).toContain('standalone: true');
const nxWelcomeComponentText = appTree.read(
'standalone/src/app/nx-welcome.component.ts',
'utf-8'
);
expect(nxWelcomeComponentText).not.toContain('standalone: true');
expect(nxWelcomeComponentText).not.toContain('standalone: false');
});

it('should generate a standalone app correctly without routing', async () => {
Expand All @@ -980,9 +988,12 @@ describe('app', () => {
appTree.read('standalone/src/app/app.component.spec.ts', 'utf-8')
).toMatchSnapshot();
expect(appTree.exists('standalone/src/app/app.module.ts')).toBeFalsy();
expect(
appTree.read('standalone/src/app/nx-welcome.component.ts', 'utf-8')
).toContain('standalone: true');
const nxWelcomeComponentText = appTree.read(
'standalone/src/app/nx-welcome.component.ts',
'utf-8'
);
expect(nxWelcomeComponentText).not.toContain('standalone: true');
expect(nxWelcomeComponentText).not.toContain('standalone: false');
});

it('should should not use event coalescing in versions lower than v18', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Component, ViewEncapsulation } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
selector: '<%= nxWelcomeSelector %>',
standalone: true,
selector: '<%= nxWelcomeSelector %>',<% if (setStandaloneTrue) { %>
standalone: true,<% } %>
imports: [CommonModule],
template: `
<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Component, ViewEncapsulation } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
selector: '<%= nxWelcomeSelector %>',
standalone: true,
selector: '<%= nxWelcomeSelector %>',<% if (setStandaloneTrue) { %>
standalone: true,<% } %>
imports: [CommonModule],
template: `
<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Component, ViewEncapsulation } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
selector: '<%= nxWelcomeSelector %>',
standalone: true,
selector: '<%= nxWelcomeSelector %>',<% if (setStandaloneTrue) { %>
standalone: true,<% } %>
imports: [CommonModule],
template: `
<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Component<% if(viewEncapsulation) { %>, ViewEncapsulation<% } %> } from
import { RouterModule } from '@angular/router';<% } %><% if(!minimal) { %>
import { NxWelcomeComponent } from './nx-welcome.component';<% } %>

@Component({
standalone: true,
@Component({<% if (setStandaloneTrue) { %>
standalone: true,<% } %>
imports: [<% if(!minimal) { %>NxWelcomeComponent, <% } %><% if(routing) { %>RouterModule<% } %>],
selector: '<%= rootSelector %>',<% if(!inlineTemplate) { %>
templateUrl: './app.component.html',<% } else { %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export async function createFiles(
disableModernClassFieldsBehavior,
useEventCoalescing: angularMajorVersion >= 18,
useRouterTestingModule: angularMajorVersion < 18,
setStandaloneFalse: angularMajorVersion >= 18,
// Angular v19 or higher defaults to true, while v18 or lower defaults to false
setStandaloneFalse: angularMajorVersion >= 19,
setStandaloneTrue: angularMajorVersion < 19,
connectCloudUrl,
tutorialUrl: options.standalone
? 'https://nx.dev/getting-started/tutorials/angular-standalone-tutorial?utm_source=nx-project'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'example',
standalone: true,
imports: [CommonModule],
templateUrl: './example.component.html',
styleUrl: './example.component.css'
Expand Down Expand Up @@ -146,7 +145,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'example',
standalone: true,
imports: [CommonModule],
templateUrl: './example.component.html',
styleUrl: './example.component.css'
Expand Down
5 changes: 4 additions & 1 deletion packages/angular/src/generators/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export async function componentGenerator(tree: Tree, rawOptions: Schema) {
viewEncapsulation: options.viewEncapsulation,
displayBlock: options.displayBlock,
selector: options.selector,
setStandaloneFalse: angularMajorVersion >= 18,
// Angular v19 or higher defaults to true, while v18 or lower defaults to false
setStandalone:
(angularMajorVersion >= 19 && !options.standalone) ||
(angularMajorVersion < 19 && options.standalone),
angularMajorVersion,
tpl: '',
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%
import { CommonModule } from '@angular/common';<% } %>

@Component({<% if(!skipSelector) {%>
selector: '<%= selector %>',<%}%><% if(standalone || setStandaloneFalse) { %>
selector: '<%= selector %>',<%}%><% if (setStandalone) { %>
standalone: <%= standalone %>,<% } %><% if(standalone) { %>
imports: [CommonModule],<%}%><% if(inlineTemplate) { %>
template: `<p><%= name %> works!</p>`<% } else { %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ exports[`directive generator should generate correctly 1`] = `
@Directive({
selector: '[test]',
standalone: true,
})
export class TestDirective {
constructor() {}
Expand Down
5 changes: 4 additions & 1 deletion packages/angular/src/generators/directive/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export async function directiveGenerator(tree: Tree, schema: Schema) {
symbolName: options.symbolName,
fileName: options.fileName,
standalone: options.standalone,
setStandaloneFalse: angularMajorVersion >= 18,
// Angular v19 or higher defaults to true, while v18 or lower defaults to false
setStandalone:
(angularMajorVersion >= 19 && !options.standalone) ||
(angularMajorVersion < 19 && options.standalone),
tpl: '',
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Directive } from '@angular/core';

@Directive({
selector: '[<%= selector %>]'<% if(standalone || setStandaloneFalse) { %>,
selector: '[<%= selector %>]'<% if (setStandalone) { %>,
standalone: <%= standalone %><% } %>
})
export class <%= symbolName %> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,6 @@ import { RouterModule } from '@angular/router';
import { NxWelcomeComponent } from './nx-welcome.component';
@Component({
standalone: true,
imports: [NxWelcomeComponent, RouterModule],
selector: 'app-root',
templateUrl: './app.component.html',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
templateUrl: './my-lib.component.html',
styleUrl: './my-lib.component.css'
Expand Down Expand Up @@ -54,7 +53,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
templateUrl: './my-lib.component.html',
styleUrl: './my-lib.component.css'
Expand Down Expand Up @@ -106,7 +104,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
templateUrl: './my-lib.component.html',
styleUrl: './my-lib.component.css'
Expand Down Expand Up @@ -148,7 +145,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
template: \`<p>my-lib works!</p>\`,
styles: \`\`,
Expand All @@ -167,7 +163,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
template: \`<p>my-lib works!</p>\`,
styles: \`\`
Expand All @@ -184,7 +179,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
template: \`<p>my-lib works!</p>\`,
styles: \`\`
Expand Down Expand Up @@ -240,7 +234,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
templateUrl: './my-lib.component.html',
styleUrl: './my-lib.component.css'
Expand Down Expand Up @@ -354,7 +347,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
templateUrl: './my-lib.component.html',
styleUrl: './my-lib.component.css'
Expand Down Expand Up @@ -396,7 +388,6 @@ import { CommonModule } from '@angular/common';
@Component({
selector: 'lib-my-lib',
standalone: true,
imports: [CommonModule],
templateUrl: './my-lib.component.html',
styleUrl: './my-lib.component.css'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ exports[`pipe generator should generate correctly 1`] = `
@Pipe({
name: 'test',
standalone: true,
})
export class TestPipe implements PipeTransform {
transform(value: unknown, ...args: unknown[]): unknown {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: '<%= selector %>'<% if(standalone || setStandaloneFalse) { %>,
name: '<%= selector %>'<% if (setStandalone) { %>,
standalone: <%= standalone %><% } %>
})
export class <%= symbolName %> implements PipeTransform {
Expand Down
5 changes: 4 additions & 1 deletion packages/angular/src/generators/pipe/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export async function pipeGenerator(tree: Tree, rawOptions: Schema) {
fileName: options.fileName,
selector: pipeNames.propertyName,
standalone: options.standalone,
setStandaloneFalse: angularMajorVersion >= 18,
// Angular v19 or higher defaults to true, while v18 or lower defaults to false
setStandalone:
(angularMajorVersion >= 19 && !options.standalone) ||
(angularMajorVersion < 19 && options.standalone),
tpl: '',
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,6 @@ import { CommonModule } from '@angular/common';
import { NxWelcomeComponent } from './nx-welcome.component';
@Component({
standalone: true,
imports: [CommonModule, NxWelcomeComponent],
selector: 'app-test-entry',
template: \`<app-nx-welcome></app-nx-welcome>\`,
Expand Down Expand Up @@ -639,7 +638,6 @@ import { CommonModule } from '@angular/common';
import { NxWelcomeComponent } from './nx-welcome.component';
@Component({
standalone: true,
imports: [CommonModule, NxWelcomeComponent],
selector: 'app-test-entry',
template: \`<app-nx-welcome></app-nx-welcome>\`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Node, SourceFile } from 'typescript';
import { Tree } from 'nx/src/generators/tree';
import { joinPathFragments, type Tree } from '@nx/devkit';
import { parse } from 'path';
import { joinPathFragments } from 'nx/src/utils/path';
import type { Node, SourceFile } from 'typescript';
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';

export function convertScamToStandalone(
componentAST: SourceFile,
Expand All @@ -23,11 +23,12 @@ export function convertScamToStandalone(
{ visitAllChildren: true }
)[0];

const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);

newComponentContents = `${componentFileContents.slice(
0,
componentDecoratorMetadataNode.getStart() - 1
)}({
standalone: true,
)}({${angularMajorVersion < 19 ? `\nstandalone: true,` : ''}
imports: [${importsArray.join(',')}],${
providersArray.length > 0 ? `providers: [${providersArray.join(',')}],` : ''
}${componentFileContents.slice(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe('scam-to-standalone', () => {
import { CommonModule } from '@angular/common';
@Component({
standalone: true,
imports: [CommonModule],
selector: 'app-bar',
standalone: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NxWelcomeComponent } from './nx-welcome.component';

@Component({
standalone: true,
@Component({<% if (setStandaloneTrue) { %>
standalone: true,<% } %>
imports: [CommonModule, NxWelcomeComponent],<% if (prefix) { %>
selector: '<%= prefix %>-<%= appName %>-entry',
template: `<<%= prefix %>-nx-welcome></<%= prefix %>-nx-welcome>`<% } else { %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export function addRemoteEntry(
appName,
routing,
prefix,
setStandaloneFalse: angularMajorVersion >= 18,
// Angular v19 or higher defaults to true, while v18 or lower defaults to false
setStandaloneFalse: angularMajorVersion >= 19,
setStandaloneTrue: angularMajorVersion < 19,
}
);

Expand Down
Loading

0 comments on commit 4a1e115

Please sign in to comment.