From c5e676bb8715bcde42e56eb08a41cc1ba5c95f91 Mon Sep 17 00:00:00 2001 From: Enea Jahollari Date: Fri, 1 Nov 2024 20:42:05 +0100 Subject: [PATCH] fix(migrations): flip the default standalone flag in route-lazy-loading migration (#58474) Before v19, the default value of the standalone flag was false, this code change flips the logic in the migration to make it true by default. PR Close #58474 --- .../ng-generate/route-lazy-loading/util.ts | 6 +++++- .../core/schematics/test/standalone_routes_spec.ts | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/core/schematics/ng-generate/route-lazy-loading/util.ts b/packages/core/schematics/ng-generate/route-lazy-loading/util.ts index e85e77bb576..b3cd9f480d4 100644 --- a/packages/core/schematics/ng-generate/route-lazy-loading/util.ts +++ b/packages/core/schematics/ng-generate/route-lazy-loading/util.ts @@ -23,7 +23,11 @@ export function isStandaloneComponent(node: ts.ClassDeclaration): boolean { const arg = decorator.expression.arguments[0]; if (ts.isObjectLiteralExpression(arg)) { const property = findLiteralProperty(arg, 'standalone') as ts.PropertyAssignment; - return property ? property.initializer.getText() === 'true' : false; + if (property) { + return property.initializer.getText() === 'true'; + } else { + return true; // standalone is true by default in v19 + } } } diff --git a/packages/core/schematics/test/standalone_routes_spec.ts b/packages/core/schematics/test/standalone_routes_spec.ts index 13190c78b7a..2db99553a8a 100644 --- a/packages/core/schematics/test/standalone_routes_spec.ts +++ b/packages/core/schematics/test/standalone_routes_spec.ts @@ -335,11 +335,13 @@ describe('route lazy loading migration', () => { import {NgModule} from '@angular/core'; import {provideRoutes} from '@angular/router'; import {TestComponent} from './test'; + import {StandaloneByDefaultComponent} from './standalone-by-default'; import {NotStandaloneComponent} from './not-standalone'; const routes = [ {path: 'test', component: TestComponent}, {path: 'test1', component: NotStandaloneComponent}, + {path: 'test2', component: StandaloneByDefaultComponent}, ]; @NgModule({ @@ -358,11 +360,20 @@ describe('route lazy loading migration', () => { `, ); + writeFile( + 'standalone-by-default.ts', + ` + import {Component} from '@angular/core'; + @Component({template: 'hello'}) + export class StandaloneByDefaultComponent {} + `, + ); + writeFile( 'not-standalone.ts', ` import {Component, NgModule} from '@angular/core'; - @Component({template: 'hello'}) + @Component({template: 'hello', standalone: false}) export class NotStandaloneComponent {} @NgModule({declarations: [NotStandaloneComponent], exports: [NotStandaloneComponent]}) @@ -381,6 +392,7 @@ describe('route lazy loading migration', () => { const routes = [ {path: 'test', loadComponent: () => import('./test').then(m => m.TestComponent)}, {path: 'test1', component: NotStandaloneComponent}, + {path: 'test2', loadComponent: () => import('./standalone-by-default').then(m => m.StandaloneByDefaultComponent)}, ]; @NgModule({