ng build my-lib
cd dist/my-lib
@@ -108,27 +179,43 @@ npm publish
If you've never published a package in npm before, you must create a user account. Read more in [Publishing npm Packages](https://docs.npmjs.com/getting-started/publishing-npm-packages).
+如果你之前从未在 npm 中发布过包,就必须创建一个用户帐号。[点此阅读发布 npm 包](https://docs.npmjs.com/getting-started/publishing-npm-packages)的更多信息。
+
## Linked libraries
+## 链接库
+
While working on a published library, you can use [npm link](https://docs.npmjs.com/cli/link) to avoid reinstalling the library on every build.
+在开发要发布的库时,可以使用 [npm link](https://docs.npmjs.com/cli/link) 来避免每次构建时都被迫重新安装库。
+
The library must be rebuilt on every change.
When linking a library, make sure that the build step runs in watch mode, and that the library's `package.json` configuration points at the correct entry points.
For example, `main` should point at a JavaScript file, not a TypeScript file.
+必须在每次修改时都重新构建这个库。在链接库时,确保构建步骤在监视模式下运行,并且该库的 `package.json` 配置指向了正确的入口点。例如,`main` 应该指向一个 JavaScript 文件,而不是一个 TypeScript 文件。
+
## Use TypeScript path mapping for peer dependencies
+## 对同级依赖使用 TypeScript 路径映射
+
Angular libraries should list all `@angular/*` dependencies as peer dependencies.
This insures that when modules ask for Angular, they all get the exact same module.
If a library lists `@angular/core` in `dependencies` instead of `peerDependencies`, it might get a different Angular module instead, which would cause your application to break.
+Angular 库应该把所有 `@angular/*` 依赖项都列为同级依赖。这确保了当各个模块请求 Angular 时,都会得到完全相同的模块。如果某个库在 `dependencies` 列出 `@angular/core` 而不是用 `peerDependencies` ,它可能会得到一个不同的 Angular 模块,这会破坏你的应用。
+
While developing a library, you must install all peer dependencies through `devDependencies` to ensure that the library compiles properly.
A linked library will then have its own set of Angular libraries that it uses for building, located in its `node_modules` folder.
However, this can cause problems while building or running your application.
+在开发库的过程中,你必须通过 `devDependencies` 安装所有的同级依赖,以确保库能够正确编译。这样,一个链接过的库就会拥有自己的一组用于构建的 Angular 库,它们位于 `node_modules` 文件夹中。但是,这会在构建或运行应用程序时引发问题。
+
To get around this problem you can use TypeScript path mapping to tell TypeScript that it should load some modules from a specific location.
List all the peer dependencies that your library uses in the TypeScript configuration file `./tsconfig.json`, and point them at the local copy in the app's `node_modules` folder.
+为了解决此问题,你可以使用 TypeScript 路径映射来告诉 TypeScript 它应该从指定的位置加载某些模块。在 TypeScript 配置文件`./tsconfig.json` 中列出该库使用的所有同级依赖,并把它们指向应用的 `node_modules` 文件夹中的本地副本。
+
```
{
"compilerOptions": {
@@ -145,45 +232,72 @@ List all the peer dependencies that your library uses in the TypeScript configur
This mapping ensures that your library always loads the local copies of the modules it needs.
+此映射可确保你的库始终加载所需模块的本地副本。
+
## Using your own library in apps
+## 在应用中使用自己的库
+
You don't have to publish your library to the npm package manager in order to use it in your own apps, but you do have to build it first.
+你不必把库发布到 npm 包管理器上就可以在自己的应用中使用它,但必须先构建它。
+
To use your own library in an app:
+要想在应用中使用你自己的库:
+
* Build the library. You cannot use a library before it is built.
+
+ 构建该库。在构建之前,无法使用库。
+
ng build my-lib
* In your apps, import from the library by name:
+
+ 在你的应用中,按名字从库中导入:
+
```
import { my-export } from 'my-lib';
```
-
### Building and rebuilding your library
+### 构建和重建你的库
+
The build step is important if you haven't published your library as an npm package and then installed the package back into your app from npm.
For instance, if you clone your git repository and run `npm install`, your editor will show the `my-lib` imports as missing if you haven't yet built your library.
+如果你没有把库发布为 npm 包,然后把它从 npm 安装到你的应用中,那么构建步骤就是必要的。例如,如果你克隆了 git 仓库并运行了 `npm install` ,编辑器就会把 `my-lib` 的导入显示为缺失状态(如果你还没有构建过该库)。
+
When you import something from a library in an Angular app, Angular looks for a mapping between the library name and a location on disk.
When you install a library package, the mapping is in the `node_modules` folder. When you build your own library, it has to find the mapping in your `tsconfig` paths.
+当你在 Angular 应用中从某个库导入一些东西时,Angular 就会寻找库名和磁盘上某个位置之间的映射关系。当你用 npm 包安装该库时,它就映射到 `node_modules` 目录下。当你自己构建库时,它就会在 `tsconfig` 路径中查找这个映射。
+
Generating a library with the Angular CLI automatically adds its path to the `tsconfig` file.
The Angular CLI uses the `tsconfig` paths to tell the build system where to find the library.
+用 Angular CLI 生成库时,会自动把它的路径添加到 `tsconfig` 文件中。 Angular CLI 使用 `tsconfig` 路径告诉构建系统在哪里寻找这个库。
+
If you find that changes to your library are not reflected in your app, your app is probably using an old build of the library.
+如果你发现库中的更改没有反映到应用中,那么你的应用很可能正在使用这个库的旧版本。
+
You can rebuild your library whenever you make changes to it, but this extra step takes time.
*Incremental builds* functionality improves the library-development experience.
Every time a file is changed a partial build is performed that emits the amended files.
+每当你对它进行修改时,都可以重建你的库,但这个额外的步骤需要时间。*增量构建*功能可以改善库的开发体验。每当文件发生变化时,都会执行局部构建,并修补一些文件。
+
Incremental builds can be run as a backround process in your dev environment. To take advantage of this feature add the `--watch` flag to the build command:
+增量构建可以作为开发环境中的后台进程运行。要启用这个特性,可以在构建命令中加入 `--watch` 标志:
+
ng build my-lib --watch