기본 콘텐츠로 건너뛰기

Angular 라이브러리 배포

Angular 라이브러리 배포

# Angular 라이브러리

Angular를 사용하다 보면 다른 사람이 만든 컴포넌트를 npm으로 설치해 사용하거나 하는 일들이 종종 있는데, 이 컴포넌트를 배포하는 방식이 바로 Angular Library를 만들어서 배포하는 것

컴포넌트, 모듈 등을 라이브러리화 해서 배포할 수 있도록 한다

# Angular 프로젝트 만들기

Angular 라이브러리를 생성하기 위해선 우선 Angular 프로젝트를 생성해줘야 한다

ng new {project-name}

# Angular 라이브러리 만들기

ng generate library {library-name}

위 명령어를 이용하면 Angular 라이브러리 생성 완료

/projects/{library-name} 폴더 안에 라이브러리가 생성 된다

만약 ng new 명령어로 프로젝트를 생성하지 않은 상태에서 위 명령어를 실행하면 angular.json 파일이 없기 때문에 에러가 발생한다

아래는 ng new sample-app, ng generate library sample-library 명령어로 라이브러리를 생성했을 때의 폴더 구조

# angular.json

angular.json 파일을 열어보면 projects 필드에 "sample-library"가 추가된 것을 확인할 수 있다

여기서 library에 관한 설정들을 할 수 있지만 기본적으로는 건드릴 필요가 없음

라이브러리 루트 폴더가 변경 될 때나 prefix 수정이 필요할 때 바꿔주면 된다

"sample-library": { "projectType": "library", "root": "projects/sample-library", "sourceRoot": "projects/sample-library/src", "prefix": "lib", "architect": { "build": { "builder": "@angular-devkit/build-ng-packagr:build", "options": { "tsConfig": "projects/sample-library/tsconfig.lib.json", "project": "projects/sample-library/ng-package.json" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "main": "projects/sample-library/src/test.ts", "tsConfig": "projects/sample-library/tsconfig.spec.json", "karmaConfig": "projects/sample-library/karma.conf.js" } }, "lint": { "builder": "@angular-devkit/build-angular:tslint", "options": { "tsConfig": [ "projects/sample-library/tsconfig.lib.json", "projects/sample-library/tsconfig.spec.json" ], "exclude": [ "**/node_modules/**" ] } } } }

그리고 위에서 root와 sourceRoot 필드를 보면 projects 폴더 내에 있는 요소들만 바라보도록 되어있기 때문에 라이브러리에서 외부 모듈을 사용해야 한다면 projects/sample-library/src/package.json 파일과 projects/sample-library/src/ng-package.json 파일을 수정해줘야 한다

# 외부 모듈 추가

우선 Angular 프로젝트 루트 폴더에서 npm 명령어를 이용해 패키지를 설치해준다

여기선 예시로 d3 패키지를 설치해보자

d3와 @types/d3를 모두 설치한다

npm intsall d3 npm install --save-dev @types/d3

그리고 projects/sample-library/src/package.json 파일의 peerDependencies 부분에 두 모듈을 추가해준다

{ "name": "sample-library", "version": "0.0.1", "peerDependencies": { "@angular/common": "^8.2.0", "@angular/core": "^8.2.0", "d3": "^5.12.0", "@types/d3": "^5.7.2" } }

이 부분이 약간 이상한게 .. 지금 이 글을 쓸 때 사용한 Angular 버전은 8이고, 처음 라이브러리를 배포해 본 건 Angular 6 버전인데 적용법이 조금 다른 것 같다

Angular 6에선 위와 같이 했더니 외부 모듈은 명시적으로 whitelist를 작성해줘야 한다 - 정확히는 기억 안 나지만 비슷한 문구 - 는 에러가 발생했었고, 이에 따라 projects/sample-library/src/package.json 파일이 아래와 같이 작성되도록 projects/sample-library/src 폴더 내에서 npm install 명령어를 실행하고,

{ "name": "sample-library", "version": "0.0.1", "peerDependencies": { "@angular/common": "^8.2.0", "@angular/core": "^8.2.0" }, "dependencies": { "d3": "^5.12.0" }, "devDependencies": { "@types/d3": "^5.7.2" } }

projects/sample-library/src/ng-package.json 파일을 아래와 같이 수정 했었다

{ "$schema": "../../node_modules/ng-packagr/ng-package.schema.json", "dest": "../../dist/sample-library", "lib": { "entryFile": "src/public-api.ts" }, "whitelistedNonPeerDependencies": [ "d3", "@types/d3" ] }

이 부분이 버전에 따른 차이인지 아니면 이전에 내가 잘못한 부분인지는 추가 확인이 필요할 듯

만약 잘못했다면 .. 수상과 멀어지는 소리가 들린다 ...

# public-api.ts

외부, 즉 다른 Angular 프로젝트에서 불러올 수 있도록 하려는 모듈, 컴포넌트, 서비스 등은 public-api.ts 파일에 작성해준다

/* * Public API Surface of sample-library */ export * from './lib/sample-library.service'; export * from './lib/sample-library.component'; export * from './lib/sample-library.module';

# Build

ng bulid {library-name}

빌드는 간단하게 위와 같이 할 수 있다

정상적으로 빌드가 되면 dist/{library-name} 폴더가 생성되고, 해당 폴더를 npm에 배포하거나 다른 프로젝트에 설치해서 사용할 수 있다

from http://devs-diary.tistory.com/39 by ccl(A) rewrite - 2020-03-06 03:54:28

댓글

이 블로그의 인기 게시물

[Debugging] AngularJS2 - Can't bind to 'ngModel' since it isn't a...

[Debugging] AngularJS2 - Can't bind to 'ngModel' since it isn't a... - 좋아요 하이 .. !! Angular2 로 개발을 하다가 아래와 같은 에러를 만났다. 흠 .. 이게 뭘까 열심히 구글링을 해봤다. 간단한 내용이다. 모듈을 추가해주기만 하면 된다. app.module.ts를 열어보자. 여기에다가 FormsModule 과 ReactiveFormsModule을 추가해주면 문제가 해결된다 ! 다들 즐거운 코딩하자. from http://devkingdom.tistory.com/106 by ccl(A) rewrite - 2020-03-18 00:54:15

[aws] deploy Angular app with s3 | AWS S3로 angular 앱 배포하기

[aws] deploy Angular app with s3 | AWS S3로 angular 앱 배포하기 angular project를 빌드한다 ng build --prod 그러면 dist 폴더가 생긴다. dist 폴더 안에 있는 아이들을 사용한다. 아마존 s3 콘솔로 이동 https://s3.console.aws.amazon.com/s3/home?region=ap-northeast-2 새로운 Bucket 을 생성한다(Create bucket). 버킷 이름은 고유하게 짓는다. 버킷 생성후 properties tab > static website hosting을 클릭한다. index document는 index.html은 쓴다. properties > static website hosting Permission tab 에서 권한을 수정한다. overview tab 에서 필요한 파일 업로드 dist 폴더 안에 있는 파일들을 업로드 한다. bucket policy 설정 properties > static website hosting > endpoint 클릭하면 서버에 올라간 앱을 확인 할 수 있다 일단 angular 앱을 올리긴 했는데.. 이걸로는 아무것도 할 수 었다. django로 만든 서버를 올리고 database를 연결하고 그것을 지금 이 angular 앱과 연결해야한다. 아직 어떻게 해야 할지는 모르겠음 계속 삽질 중. 그래도 angular app 하나 올라갔는데 재밌네 from http://paigeblog.tistory.com/18 by ccl(A) rewrite - 2020-03-25 16:20:22

Angular2 시작하기

Angular2 시작하기 Angular2 시작하기 1. NodeJS 설치 - https://nodejs.org/ko/ NodeJS 공식 홈페이지 접속하여 Node를 다운 후 설치. 2. NPM 을 통한 Angular-Cli 설치 Window Command 를 통하여 npm install -g @angular/cli 명령어를 실행한다. 위와 같이 정상적으로 angular-cli 가 설치되었다면 Project 가 위치할 폴더를 생성. 참고 사이트 : https://cli.angular.io/ 3. Angular-cli 를 통한 Angular Project 생성. Window Command 를 통한 ng new [폴더명] 위와 같이 Angular 기본 프로젝트가 생성됨. 해당 프로젝트로 폴더로 이동하여 ng serve 명령어 실행 Node 를 통해 Angular 프로젝트 실행. http://localhost:4200 접속하게되면 위와같이 Angular 프로젝트 실행된다. 앞으로 Angular2 의 개념들을 포스팅하면서 Spring-Boot , Spring Project의 Angular-cli 를 이용하여 ng build 하여 포팅하는 글을 올리겠습니다. from http://overclassbysin.tistory.com/3 by ccl(A) rewrite - 2020-03-07 07:55:13