How to write a reusable private library for Angular 2+

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 18:09:19

This question boils down to two independent tasks: Creating the library package and publishing it internally.

Create the library package

In order to create a library with AOT support, you need to compile it using ngc -p src/tsconfig-aot.json.

tsconfig-aot.json is a copy of tsconfig.json with an additional section:

 "files": [
   "./app/index.ts"
 ],
 "angularCompilerOptions": {
   "annotateForClosureCompiler": true,
   "genDir": "../dist/out-lib-tsc",
   "skipMetadataEmit" : false,
   "skipTemplateCodegen": true,
   "strictMetadataEmit": true,
   "flatModuleOutFile": "libname.js",
   "flatModuleId": "libname"
 }

I need to fix the directory structure by moving the files from src/app to the root of the output directory. Then you copy src/app and src/asserts to the output directory.

There are several detailed guides out there. For example Distributing an Angular Library - The Brief Guide

Publish the library package

There are several options to publish private libraries:

  • reference a branch in a git repository Note: It is probably a good idea to use different repositories for developing (without compiler output) and publishing
  • npm offers private repositories for a fee
  • you can setup a local registry (for example Artifactory or Sinopia)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!