Nx.dev 기반개발에서 Angular 모듈을 Router기반으로 Lazy Loading하기와 Router를 사용하지 않고 Lazy Loading하는 방법에 대해 알아보자. 알단 Lazy Loading 설정을 하게되면 Angular 컴파일시에 자동으로 모듈단위 Code Splitting하여 번들링한다. 따라서 필요한 시점에 필요한 파일만 다운로드를 받기때문에 TTI(Time ot interactive)시간 즉, 애플리케이션 응답성능을 높일 수 있다. 참고로 Webpack 5 (핸재 beta) 에서 Federation Module 개념을 구현하였는데, Angular에서 이와 비슷한 기능을 Angular Module단위 개발로 제공한다.
NX 환경 설정
- NodeJS v12.* 사용
- @angular/cli와 @nwrl/ci 설치
- create-nx-workspace 이용하여 nx 기반 환경 생성
- yarn통한 node modules 설치
// NVM 설치 (https://github.com/nvm-sh/nvm)
$ nvm install 12.16.2
$ nvm alias default 12.16.2
$ nvm use 12.16.2
// 최신버전들 설치
$ npm i -g @angular/cli@latest
$ npm i -g @nrwl/cli@latest
$ npm i -g yarn@latest
$ npm install typescript@3.8.3 --save-dev --save-exact
// create-nx-workspace
$ npx create-nx-workspace@latest
? Worspace name demo
...
$ cd demo
$ yarn install
Router 를 통한 Lazy Loading
- nx 명령을 통해 Lazy loading할 library를 생성한다.
- nx 명령을 통해 Lazy loading되어 보여질 component를 생성한다.
- libs/lazyview/src/lib/lazyview.module.ts 에 RouterModule.forChild 설정
- lazyview모듈을 로딩하는 "app-container" 애플리케이션의 tsconfig.app.json에 "include" path 설정
- apps/app-container/src/app/app-routing.module.ts 안에 loadChildren 을 통해 lazyview 모듈을 import 한다.
// 1) libs/lazyview 라이브러리 생성 - 라이브러리는 모듈단위
$ nx g lib --routing --lazy lazyview
// 2) 컴포넌트 생성
$ nx g c lazypage --project=lazyview
// 3) libs/lazyview/src/lib/lazyview.module.ts에 forChild 설정
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { LazypageComponent } from './lazypage/lazypage.component';
@NgModule({
imports: [
CommonModule,
RouterModule.forChild([
{ path: '', pathMatch: 'full', component: LazypageComponent }
]),
],
declarations: [LazypageComponent],
})
export class LazyviewModule {}
// 4) apps/app-container/src/tsconfig.app.json 에 include/exclude 설정
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": []
},
"include": ["**/*.ts", "../../libs/lazyview/src/index.ts"],
"exclude": ["src/test-setup.ts", "**/*.spec.ts", "src/environments/*.ts"]
}
// 5) app-routing.module.ts 에 loadChildren으로 '@micro-demo/lazyview' 설정
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', pathMatch: 'full', redirectTo: '/welcome' },
{ path: 'lazyview', loadChildren: () => import('@micro-demo/lazyview').then((m) => m.LazyviewModule) },
{ path: 'welcome', loadChildren: () => import('./pages/welcome/welcome.module').then((m) => m.WelcomeModule) },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
"nx serve" 수행한 후 "http://localhost:4200/lazyview" 로 호출한다.
소스: https://github.com/ysyun/blog-5730-micro-demo
ysyun/blog-5730-micro-demo
https://mobicon.tistory.com/573 블로그 소스. Contribute to ysyun/blog-5730-micro-demo development by creating an account on GitHub.
github.com
참조
Lazy load Angular Components
Or better..how to lazy load Angular Modules. Learn about the state of lazy loading and lazy loading on steroids with Angular Elements
juristr.com
https://juristr.com/blog/2019/10/lazyload-module-ivy-viewengine/
Manually Lazy Load an Angular Module with ViewEngine and Ivy
Find out how to lazy load an NgModule compatible with ViewEngine and Ivy
juristr.com
https://juristr.com/blog/2019/08/ngperf-route-level-code-splitting/
Angular Performance: Route Level Code Splitting
Learn about route-level code splitting and lazy loading with Angular
juristr.com
https://medium.com/angular-in-depth/lazy-load-components-in-angular-596357ab05d8
Lazy load components in Angular
Lazy load Angular components with Ivy and Angular 9
medium.com
https://indepth.dev/webpack-5-module-federation-a-game-changer-in-javascript-architecture/
Webpack 5 Federation. A Game-changer to Javascript architecture.
Module federation — The Javascript equivalent of what Apollo did with GraphQL. Multiple Webpack builds work together, sharing the dependency graph at runtime. Multiple bundles working as an SPA
indepth.dev
'Angular > Concept' 카테고리의 다른 글
[NX] Lazy loading with Router 또는 without Router - 2 (0) | 2021.08.05 |
---|---|
[Angular Schematics] NX기반 Custom Schematic 개발하기 - 1 (0) | 2020.05.22 |
[Angular] Docker에서 수행하기 (0) | 2017.04.23 |
[Typescript] NodeJS에서 Typescript 사용하기 (0) | 2017.03.29 |
[PWA] Progressive Web Apps 만들기 (0) | 2016.12.20 |