From d72db4d05b41bdbe6a8d0f73183e95f5c2b14d29 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Tue, 21 May 2019 18:50:04 -0400 Subject: [PATCH] feat(nx): use dynamic imports for lazy-loading --- .../src/schematics/library/library.spec.ts | 25 ++++++++++++++----- .../angular/src/schematics/library/library.ts | 6 ++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/angular/src/schematics/library/library.spec.ts b/packages/angular/src/schematics/library/library.spec.ts index e3f316f8bdcca..d78b460dc0e94 100644 --- a/packages/angular/src/schematics/library/library.spec.ts +++ b/packages/angular/src/schematics/library/library.spec.ts @@ -483,7 +483,8 @@ describe('lib', () => { expect(moduleContents).toContain(` { path: 'my-dir-my-lib', - loadChildren: '@proj/my-dir/my-lib#MyDirMyLibModule' + loadChildren: () => + import('@proj/my-dir/my-lib').then(module => module.MyDirMyLibModule) }`); const tsConfigAppJson = JSON.parse( @@ -516,12 +517,16 @@ describe('lib', () => { expect(moduleContents2).toContain(` { path: 'my-dir-my-lib', - loadChildren: '@proj/my-dir/my-lib#MyDirMyLibModule' + loadChildren: () => + import('@proj/my-dir/my-lib').then(module => module.MyDirMyLibModule) }`); expect(moduleContents2).toContain(` { path: 'my-dir-my-lib2', - loadChildren: '@proj/my-dir/my-lib2#MyDirMyLib2Module' + loadChildren: () => + import('@proj/my-dir/my-lib2').then( + module => module.MyDirMyLib2Module + ) }`); const tsConfigAppJson2 = JSON.parse( @@ -556,15 +561,23 @@ describe('lib', () => { expect(moduleContents3).toContain(` { path: 'my-dir-my-lib', - loadChildren: '@proj/my-dir/my-lib#MyDirMyLibModule' + loadChildren: () => + import('@proj/my-dir/my-lib').then(module => module.MyDirMyLibModule) }`); expect(moduleContents3).toContain(` { path: 'my-dir-my-lib2', - loadChildren: '@proj/my-dir/my-lib2#MyDirMyLib2Module' + loadChildren: () => + import('@proj/my-dir/my-lib2').then( + module => module.MyDirMyLib2Module + ) }`); expect(moduleContents3).toContain(` - { path: 'my-lib3', loadChildren: '@proj/my-dir/my-lib3#MyLib3Module' }`); + { + path: 'my-lib3', + loadChildren: () => + import('@proj/my-dir/my-lib3').then(module => module.MyLib3Module) + }`); const tsConfigAppJson3 = JSON.parse( stripJsonComments( diff --git a/packages/angular/src/schematics/library/library.ts b/packages/angular/src/schematics/library/library.ts index 2542e0d278415..f342eb7d3eae0 100644 --- a/packages/angular/src/schematics/library/library.ts +++ b/packages/angular/src/schematics/library/library.ts @@ -131,9 +131,9 @@ function addLoadChildren(options: NormalizedSchema): Rule { sourceFile, `{path: '${toFileName( options.fileName - )}', loadChildren: '@${npmScope}/${options.projectDirectory}#${ - options.moduleName - }'}` + )}', loadChildren: () => import('@${npmScope}/${ + options.projectDirectory + }').then(module => module.${options.moduleName})}` ) ]);