如何为 Angular 2+ 编写可重用的私有库 [英] How to write a reusable private library for Angular 2+

查看:17
本文介绍了如何为 Angular 2+ 编写可重用的私有库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Angular-CLI 创建了一个用于创建由多个模块组成的仪表板的通用框架.

I have created a generic framework for creating dashboards which consists of multiple modules using Angular-CLI.

有些模块是完全独立的.使用此库的开发人员可以根据需要在他们的项目中添加模块.我有一个在 Angular Js 1.0 中创建的该框架的先前版本,在此我已作为 javascript min 文件提供.

Some modules are completely independent. Developers who are using this library can add the modules on their project on demand. I had a previous version of this framework created in Angular Js 1.0, in this I have delivered as javascript min files.

我需要注意哪些事项才能将这个库创建为私有而不是公开的,或者有什么方法可以将我的模块打包成单独的并在没有 NPM 的情况下交付?

What are the things I have to take care to create this Library as private not for public or is there any way to package my modules as separate and deliver without NPM?

推荐答案

这个问题归结为两个独立的任务:创建库包并在内部发布它.

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

为了创建一个支持 AOT 的库,你需要使用 ngc -p src/tsconfig-aot.json 编译它.

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

tsconfig-aot.json 是带有附加部分的 tsconfig.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"
 }

我需要通过将文件从 src/app 移动到输出目录的根来修复目录结构.然后将 src/app 和 src/asserts 复制到输出目录.

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.

有几个详细的指南.例如 分发 Angular 库 -简要指南

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

发布私有库有多种选择:

There are several options to publish private libraries:

  • 引用 git 存储库中的一个分支注意:使用不同的存储库进行开发(没有编译器输出)和发布可能是个好主意
  • npm 提供私有存储库,收取费用
  • 您可以设置本地注册表(例如 ArtifactorySinopia)
  • 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)

这篇关于如何为 Angular 2+ 编写可重用的私有库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆