@@ -435,15 +435,12 @@ function _diagnoseDeps(reasons: ModuleReason[], plugin: AotPlugin, checked: Set<
435435}
436436
437437
438- export function _replaceExport ( plugin : AotPlugin , refactor : TypeScriptFileRefactor ) {
439- if ( ! plugin . replaceExport ) {
440- return ;
441- }
442-
438+ export function _getModuleExports ( plugin : AotPlugin ,
439+ refactor : TypeScriptFileRefactor ) : ts . Identifier [ ] {
443440 const exports = refactor
444441 . findAstNodes ( refactor . sourceFile , ts . SyntaxKind . ExportDeclaration , true ) ;
445442
446- exports
443+ return exports
447444 . filter ( node => {
448445
449446 const identifiers = refactor . findAstNodes ( node , ts . SyntaxKind . Identifier , false ) ;
@@ -452,7 +449,15 @@ export function _replaceExport(plugin: AotPlugin, refactor: TypeScriptFileRefact
452449 . filter ( node => node . getText ( ) === plugin . entryModule . className ) ;
453450
454451 return identifiers . length > 0 ;
455- } )
452+ } ) as ts . Identifier [ ] ;
453+ }
454+
455+
456+ export function _replaceExport ( plugin : AotPlugin , refactor : TypeScriptFileRefactor ) {
457+ if ( ! plugin . replaceExport ) {
458+ return ;
459+ }
460+ _getModuleExports ( plugin , refactor )
456461 . forEach ( node => {
457462 const factoryPath = _getNgFactoryPath ( plugin , refactor ) ;
458463 const factoryClassName = plugin . entryModule . className + 'NgFactory' ;
@@ -462,6 +467,45 @@ export function _replaceExport(plugin: AotPlugin, refactor: TypeScriptFileRefact
462467}
463468
464469
470+ export function _exportModuleMap ( plugin : AotPlugin , refactor : TypeScriptFileRefactor ) {
471+ if ( ! plugin . replaceExport ) {
472+ return ;
473+ }
474+
475+ _getModuleExports ( plugin , refactor )
476+ . forEach ( node => {
477+ const routes : any [ ] = Object . keys ( plugin . discoveredLazyRoutes )
478+ . map ( routeKey => {
479+ let lazyRouteKey = routeKey . split ( '#' ) [ 0 ] ;
480+ if ( ! plugin . skipCodeGeneration ) {
481+ lazyRouteKey += '.ngfactory' ;
482+ }
483+ return {
484+ loadChildrenString : routeKey ,
485+ path : plugin . lazyRoutes [ lazyRouteKey ] ,
486+ name : routeKey . split ( '#' ) [ 1 ]
487+ } ;
488+ } ) ;
489+ let map : { [ key : string ] : string } = { } ;
490+ routes . forEach ( module => {
491+ const append = plugin . skipCodeGeneration ? '' : 'NgFactory' ;
492+ map [ module . loadChildrenString ] = module . name + append ;
493+ } ) ;
494+
495+ const dirName = path . normalize ( path . dirname ( refactor . fileName ) ) ;
496+ let exportStatement = `export const moduleMap = ${ JSON . stringify ( map ) } ;` ;
497+ routes . forEach ( ( module , index ) => {
498+ const moduleName = map [ module . loadChildrenString ] ;
499+ const relativePath = path . relative ( dirName , module . path ) . replace ( / \\ / g, '/' ) ;
500+ exportStatement = exportStatement . replace (
501+ `"${ moduleName } "` , `__lazy_${ index } __.${ moduleName } ` ) ;
502+ refactor . prependBefore ( node , `import * as __lazy_${ index } __ from './${ relativePath } '` ) ;
503+ } ) ;
504+ refactor . appendAfter ( node , exportStatement ) ;
505+ } ) ;
506+ }
507+
508+
465509// Super simple TS transpiler loader for testing / isolated usage. does not type check!
466510export function ngcLoader ( this : LoaderContext & { _compilation : any } , source : string | null ) {
467511 const cb = this . async ( ) ;
@@ -492,11 +536,13 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
492536 return Promise . resolve ( )
493537 . then ( ( ) => _removeDecorators ( refactor ) )
494538 . then ( ( ) => _refactorBootstrap ( plugin , refactor ) )
495- . then ( ( ) => _replaceExport ( plugin , refactor ) ) ;
539+ . then ( ( ) => _replaceExport ( plugin , refactor ) )
540+ . then ( ( ) => _exportModuleMap ( plugin , refactor ) ) ;
496541 } else {
497542 return Promise . resolve ( )
498543 . then ( ( ) => _replaceResources ( refactor ) )
499- . then ( ( ) => _removeModuleId ( refactor ) ) ;
544+ . then ( ( ) => _removeModuleId ( refactor ) )
545+ . then ( ( ) => _exportModuleMap ( plugin , refactor ) ) ;
500546 }
501547 } )
502548 . then ( ( ) => {
0 commit comments