@@ -17,7 +17,6 @@ import {
17
17
import { dirname , join } from 'node:path/posix' ;
18
18
import ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript' ;
19
19
import {
20
- addImportToModule ,
21
20
addSymbolToNgModuleMetadata ,
22
21
findNode ,
23
22
findNodes ,
@@ -140,19 +139,6 @@ function validateProject(mainPath: string): Rule {
140
139
} ;
141
140
}
142
141
143
- function addRouterModule ( mainPath : string ) : Rule {
144
- return ( host : Tree ) => {
145
- const modulePath = getAppModulePath ( host , mainPath ) ;
146
- const moduleSource = getSourceFile ( host , modulePath ) ;
147
- const changes = addImportToModule ( moduleSource , modulePath , 'RouterModule' , '@angular/router' ) ;
148
- const recorder = host . beginUpdate ( modulePath ) ;
149
- applyToUpdateRecorder ( recorder , changes ) ;
150
- host . commitUpdate ( recorder ) ;
151
-
152
- return host ;
153
- } ;
154
- }
155
-
156
142
function getMetadataProperty ( metadata : ts . Node , propertyName : string ) : ts . PropertyAssignment {
157
143
const properties = ( metadata as ts . ObjectLiteralExpression ) . properties ;
158
144
const property = properties . filter ( ts . isPropertyAssignment ) . filter ( ( prop ) => {
@@ -265,7 +251,7 @@ function addStandaloneServerRoute(options: AppShellOptions): Rule {
265
251
throw new SchematicsException ( `Cannot find "${ configFilePath } ".` ) ;
266
252
}
267
253
268
- let recorder = host . beginUpdate ( configFilePath ) ;
254
+ const recorder = host . beginUpdate ( configFilePath ) ;
269
255
let configSourceFile = getSourceFile ( host , configFilePath ) ;
270
256
if ( ! isImported ( configSourceFile , 'ROUTES' , '@angular/router' ) ) {
271
257
const routesChange = insertImport (
@@ -295,89 +281,74 @@ function addStandaloneServerRoute(options: AppShellOptions): Rule {
295
281
const updatedProvidersString = [
296
282
...providersLiteral . elements . map ( ( element ) => ' ' + element . getText ( ) ) ,
297
283
` {
298
- provide: ROUTES,
299
- multi: true,
300
- useValue: [{
301
- path: '${ APP_SHELL_ROUTE } ',
302
- component: AppShellComponent
303
- }]
304
- }\n ` ,
284
+ provide: ROUTES,
285
+ multi: true,
286
+ useValue: [{
287
+ path: '${ APP_SHELL_ROUTE } ',
288
+ component: AppShellComponent
289
+ }]
290
+ }\n ` ,
305
291
] ;
306
292
307
293
recorder . insertRight ( providersLiteral . getStart ( ) , `[\n${ updatedProvidersString . join ( ',\n' ) } ]` ) ;
308
294
309
- if ( options . serverRouting ) {
310
- host . commitUpdate ( recorder ) ;
311
- configSourceFile = getSourceFile ( host , configFilePath ) ;
312
- const functionCall = findNodes ( configSourceFile , ts . isCallExpression ) . find (
313
- ( n ) =>
314
- ts . isIdentifier ( n . expression ) && n . expression . getText ( ) === 'provideServerRoutesConfig' ,
315
- ) ;
316
-
317
- if ( ! functionCall ) {
318
- throw new SchematicsException (
319
- `Cannot find the "provideServerRoutesConfig" function call in "${ configFilePath } ".` ,
320
- ) ;
321
- }
322
-
323
- recorder = host . beginUpdate ( configFilePath ) ;
324
- recorder . insertLeft ( functionCall . end - 1 , `, { appShellRoute: '${ APP_SHELL_ROUTE } ' }` ) ;
325
- }
326
-
327
- // Add AppShellComponent import
328
- const appShellImportChange = insertImport (
329
- configSourceFile ,
330
- configFilePath ,
331
- 'AppShellComponent' ,
332
- './app-shell/app-shell.component' ,
333
- ) ;
334
-
335
- applyToUpdateRecorder ( recorder , [ appShellImportChange ] ) ;
295
+ applyToUpdateRecorder ( recorder , [
296
+ insertImport (
297
+ configSourceFile ,
298
+ configFilePath ,
299
+ 'AppShellComponent' ,
300
+ './app-shell/app-shell.component' ,
301
+ ) ,
302
+ ] ) ;
336
303
host . commitUpdate ( recorder ) ;
337
304
} ;
338
305
}
339
306
340
- function addServerRoutingConfig ( options : AppShellOptions ) : Rule {
307
+ function addServerRoutingConfig ( options : AppShellOptions , isStandalone : boolean ) : Rule {
341
308
return async ( host : Tree ) => {
342
309
const workspace = await getWorkspace ( host ) ;
343
310
const project = workspace . projects . get ( options . project ) ;
344
311
if ( ! project ) {
345
312
throw new SchematicsException ( `Project name "${ options . project } " doesn't not exist.` ) ;
346
313
}
347
314
348
- const configFilePath = join ( project . sourceRoot ?? 'src' , 'app/app.routes.server.ts' ) ;
349
- if ( ! host . exists ( configFilePath ) ) {
315
+ const configFilePath = isStandalone
316
+ ? join ( project . sourceRoot ?? 'src' , 'app/app.config.server.ts' )
317
+ : getServerModulePath ( host , project . sourceRoot || 'src' , 'main.server.ts' ) ;
318
+
319
+ if ( ! configFilePath || ! host . exists ( configFilePath ) ) {
350
320
throw new SchematicsException ( `Cannot find "${ configFilePath } ".` ) ;
351
321
}
352
322
353
- const sourceFile = getSourceFile ( host , configFilePath ) ;
354
- const nodes = getSourceNodes ( sourceFile ) ;
355
-
356
- // Find the serverRoutes variable declaration
357
- const serverRoutesNode = nodes . find (
358
- ( node ) =>
359
- ts . isVariableDeclaration ( node ) &&
360
- node . initializer &&
361
- ts . isArrayLiteralExpression ( node . initializer ) &&
362
- node . type &&
363
- ts . isArrayTypeNode ( node . type ) &&
364
- node . type . getText ( ) . includes ( 'ServerRoute' ) ,
365
- ) as ts . VariableDeclaration | undefined ;
366
-
367
- if ( ! serverRoutesNode ) {
323
+ let recorder = host . beginUpdate ( configFilePath ) ;
324
+ const configSourceFile = getSourceFile ( host , configFilePath ) ;
325
+ const functionCall = findNodes (
326
+ configSourceFile ,
327
+ ts . isCallExpression ,
328
+ /** max */ undefined ,
329
+ /** recursive */ true ,
330
+ ) . find (
331
+ ( n ) => ts . isIdentifier ( n . expression ) && n . expression . getText ( ) === 'provideServerRouting' ,
332
+ ) ;
333
+
334
+ if ( ! functionCall ) {
368
335
throw new SchematicsException (
369
- `Cannot find the "ServerRoute" configuration in "${ configFilePath } ".` ,
336
+ `Cannot find the "provideServerRouting" function call in "${ configFilePath } ".` ,
370
337
) ;
371
338
}
372
- const recorder = host . beginUpdate ( configFilePath ) ;
373
- const arrayLiteral = serverRoutesNode . initializer as ts . ArrayLiteralExpression ;
374
- const firstElementPosition =
375
- arrayLiteral . elements [ 0 ] ?. getStart ( ) ?? arrayLiteral . getStart ( ) + 1 ;
376
- const newRouteString = `{
377
- path: '${ APP_SHELL_ROUTE } ',
378
- renderMode: RenderMode.AppShell
379
- },\n` ;
380
- recorder . insertLeft ( firstElementPosition , newRouteString ) ;
339
+
340
+ recorder = host . beginUpdate ( configFilePath ) ;
341
+ recorder . insertLeft ( functionCall . end - 1 , `, withAppShell(AppShellComponent)` ) ;
342
+
343
+ applyToUpdateRecorder ( recorder , [
344
+ insertImport ( configSourceFile , configFilePath , 'withAppShell' , '@angular/ssr' ) ,
345
+ insertImport (
346
+ configSourceFile ,
347
+ configFilePath ,
348
+ 'AppShellComponent' ,
349
+ './app-shell/app-shell.component' ,
350
+ ) ,
351
+ ] ) ;
381
352
382
353
host . commitUpdate ( recorder ) ;
383
354
} ;
@@ -391,10 +362,14 @@ export default function (options: AppShellOptions): Rule {
391
362
return chain ( [
392
363
validateProject ( browserEntryPoint ) ,
393
364
schematic ( 'server' , options ) ,
394
- ...( isStandalone
395
- ? [ addStandaloneServerRoute ( options ) ]
396
- : [ addRouterModule ( browserEntryPoint ) , addServerRoutes ( options ) ] ) ,
397
- options . serverRouting ? noop ( ) : addAppShellConfigToWorkspace ( options ) ,
365
+ ...( options . serverRouting
366
+ ? [ noop ( ) ]
367
+ : isStandalone
368
+ ? [ addStandaloneServerRoute ( options ) ]
369
+ : [ addServerRoutes ( options ) ] ) ,
370
+ options . serverRouting
371
+ ? addServerRoutingConfig ( options , isStandalone )
372
+ : addAppShellConfigToWorkspace ( options ) ,
398
373
schematic ( 'component' , {
399
374
name : 'app-shell' ,
400
375
module : 'app.module.server.ts' ,
0 commit comments