@@ -435,6 +435,83 @@ function _diagnoseDeps(reasons: ModuleReason[], plugin: AotPlugin, checked: Set<
435435}
436436
437437
438+ export function _getModuleExports ( plugin : AotPlugin ,
439+ refactor : TypeScriptFileRefactor ) : ts . Identifier [ ] {
440+ const exports = refactor
441+ . findAstNodes ( refactor . sourceFile , ts . SyntaxKind . ExportDeclaration , true ) ;
442+
443+ return exports
444+ . filter ( node => {
445+
446+ const identifiers = refactor . findAstNodes ( node , ts . SyntaxKind . Identifier , false ) ;
447+
448+ identifiers
449+ . filter ( node => node . getText ( ) === plugin . entryModule . className ) ;
450+
451+ return identifiers . length > 0 ;
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 )
461+ . forEach ( node => {
462+ const factoryPath = _getNgFactoryPath ( plugin , refactor ) ;
463+ const factoryClassName = plugin . entryModule . className + 'NgFactory' ;
464+ const exportStatement = `export \{ ${ factoryClassName } \} from '${ factoryPath } '` ;
465+ refactor . appendAfter ( node , exportStatement ) ;
466+ } ) ;
467+ }
468+
469+
470+ export function _exportModuleMap ( plugin : AotPlugin , refactor : TypeScriptFileRefactor ) {
471+ if ( ! plugin . replaceExport ) {
472+ return ;
473+ }
474+
475+ const dirName = path . normalize ( path . dirname ( refactor . fileName ) ) ;
476+ const classNameAppend = plugin . skipCodeGeneration ? '' : 'NgFactory' ;
477+ const modulePathAppend = plugin . skipCodeGeneration ? '' : '.ngfactory' ;
478+
479+ _getModuleExports ( plugin , refactor )
480+ . forEach ( node => {
481+ const modules = Object . keys ( plugin . discoveredLazyRoutes )
482+ . map ( ( loadChildrenString ) => {
483+ let [ lazyRouteKey , moduleName ] = loadChildrenString . split ( '#' ) ;
484+
485+ if ( ! lazyRouteKey || ! moduleName ) {
486+ throw new Error ( `${ loadChildrenString } was not a proper loadChildren string` ) ;
487+ }
488+
489+ moduleName += classNameAppend ;
490+ lazyRouteKey += modulePathAppend ;
491+ const modulePath = plugin . lazyRoutes [ lazyRouteKey ] ;
492+
493+ return {
494+ modulePath,
495+ moduleName,
496+ loadChildrenString
497+ } ;
498+ } ) ;
499+
500+ modules . forEach ( ( module , index ) => {
501+ const relativePath = path . relative ( dirName , module . modulePath ) . replace ( / \\ / g, '/' ) ;
502+ refactor . prependBefore ( node , `import * as __lazy_${ index } __ from './${ relativePath } '` ) ;
503+ } ) ;
504+
505+ const jsonContent : string = modules
506+ . map ( ( module , index ) =>
507+ `"${ module . loadChildrenString } ": __lazy_${ index } __.${ module . moduleName } ` )
508+ . join ( ) ;
509+
510+ refactor . appendAfter ( node , `export const LAZY_MODULE_MAP = {${ jsonContent } };` ) ;
511+ } ) ;
512+ }
513+
514+
438515// Super simple TS transpiler loader for testing / isolated usage. does not type check!
439516export function ngcLoader ( this : LoaderContext & { _compilation : any } , source : string | null ) {
440517 const cb = this . async ( ) ;
@@ -464,11 +541,14 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
464541 if ( ! plugin . skipCodeGeneration ) {
465542 return Promise . resolve ( )
466543 . then ( ( ) => _removeDecorators ( refactor ) )
467- . then ( ( ) => _refactorBootstrap ( plugin , refactor ) ) ;
544+ . then ( ( ) => _refactorBootstrap ( plugin , refactor ) )
545+ . then ( ( ) => _replaceExport ( plugin , refactor ) )
546+ . then ( ( ) => _exportModuleMap ( plugin , refactor ) ) ;
468547 } else {
469548 return Promise . resolve ( )
470549 . then ( ( ) => _replaceResources ( refactor ) )
471- . then ( ( ) => _removeModuleId ( refactor ) ) ;
550+ . then ( ( ) => _removeModuleId ( refactor ) )
551+ . then ( ( ) => _exportModuleMap ( plugin , refactor ) ) ;
472552 }
473553 } )
474554 . then ( ( ) => {
0 commit comments