@@ -7,7 +7,7 @@ import { DependencyResolver } from "./../services/DependencyResolver";
77import { LiveEditingFile , SAMPLE_APP_FOLDER , SAMPLE_SRC_FOLDER } from "./misc/LiveEditingFile" ;
88import { TsImportsService } from "../services/TsImportsService" ;
99import { componentPaths , appRouting } from "../services/TsRoutingPathService" ;
10- import { IConfigGenerator , Config , ILiveEditingOptions } from "../public" ;
10+ import { IConfigGenerator , Config , ILiveEditingOptions , RoutesConfig } from "../public" ;
1111import { SampleDefinitionFile } from "./misc/SampleDefinitionFile" ;
1212
1313const APP_MODULE_TEMPLATE_PATH = path . join ( __dirname , "../templates/app.module.ts.template" ) ;
@@ -145,7 +145,7 @@ export class SampleAssetsGenerator {
145145 ) ) ;
146146 sampleFiles . push ( new LiveEditingFile (
147147 SAMPLE_APP_FOLDER + "app.component.html" ,
148- this . _getAppComponentHtml ( componentTsContent , config . usesRouting ) ) ) ;
148+ this . _getAppComponentHtml ( componentTsContent , config . appConfig . routesConfig ) ) ) ;
149149 sampleFiles . push ( new LiveEditingFile (
150150 `${ SAMPLE_APP_FOLDER } app.config.ts` ,
151151 this . _getAppConfig ( config , configImports , configAdditionalImports ) ,
@@ -251,16 +251,19 @@ export class SampleAssetsGenerator {
251251 private _getAppComponentTs ( config : Config , sampleFiles : LiveEditingFile [ ] ) {
252252 let appComponentTemplate = fs . readFileSync ( APP_COMPONENT_TEMPLATE_PATH , "utf8" ) ;
253253 const mainSampleTsPath = sampleFiles . filter ( f => f . isMain && f . fileExtension === "ts" ) [ 0 ] . path ;
254+ const importRouter = config . appConfig . routesConfig ?. router ?? false ;
254255 return appComponentTemplate
255256 . replace ( / \{ s a m p l e A p p C o m p o n e n t \} / g, config . component )
256257 . replace ( / \{ a p p I m p o r t \} / g, `.\/${ mainSampleTsPath . substring ( mainSampleTsPath . indexOf ( "app/" ) + 4 ) } ` )
258+ . replace ( / \{ r o u t e r O u t l e t \} / g, importRouter ? ", RouterOutlet" : "" )
259+ . replace ( / \{ r o u t e r O u t l e t I m p o r t \} / g, importRouter ? "import { RouterOutlet } from '@angular/router';" : "" )
257260 . replace ( ".ts" , "" ) ;
258261 }
259262
260- private _getAppComponentHtml ( componentTsContent , usesRouting ) {
263+ private _getAppComponentHtml ( componentTsContent , routesConfig ?: RoutesConfig ) {
261264 let selectorRegex = / s e l e c t o r : [ \s ] * [ " ' ] ( [ a - z A - Z 0 - 9 - ] + ) [ " ' ] / g;
262265 let selectorComponent = selectorRegex . exec ( componentTsContent ) [ 1 ] ;
263- let appComponentHtml = usesRouting ? "<router-outlet></router-outlet>" :
266+ let appComponentHtml = routesConfig ?. router ? "<router-outlet></router-outlet>" :
264267 "<" + selectorComponent + "></" + selectorComponent + ">" ;
265268 return appComponentHtml ;
266269 }
@@ -314,8 +317,9 @@ export class SampleAssetsGenerator {
314317 }
315318 } ) ;
316319 }
317- if ( appConfig . router ) {
320+ if ( this . _shouldConfigureRouting ( config ) ) {
318321 importMap . set ( '@angular/router' , [ 'provideRouter' , 'withComponentInputBinding' ] ) ;
322+ importMap . set ( `${ appConfig . routesConfig . routesImportPath } ` , [ 'routes' ] ) ;
319323 }
320324 return importMap ;
321325 }
@@ -351,15 +355,20 @@ export class SampleAssetsGenerator {
351355 formatted += ` ${ provider } ${ i < providers . length - 1 ? ',\n' : '' } ` ;
352356 } ) ;
353357 }
354- if ( config . appConfig . router ) {
358+ if ( this . _shouldConfigureRouting ( config ) ) {
355359 if ( formatted . length > 0 ) {
356360 formatted += ',\n' ;
357361 }
358- formatted += ` provideRouter([] , withComponentInputBinding())` ;
362+ formatted += ` provideRouter(routes , withComponentInputBinding())` ;
359363 }
360364 return formatted ;
361365 }
362366
367+ private _shouldConfigureRouting ( config : Config ) : boolean {
368+ const routesConfig = config ?. appConfig ?. routesConfig ;
369+ return ! ! ( routesConfig ?. router && routesConfig ?. routesImportPath ) ;
370+ }
371+
363372 private _getAppModuleConfig ( config : Config , configImports , configAdditionalImports ?) {
364373 let defaultNgDeclarations = [ "AppComponent" ] ;
365374 let defaultNgImports = [ "BrowserModule" , "BrowserAnimationsModule" , "FormsModule" ] ;
0 commit comments