22ndtech-common-lib 中文文档教程
Warning
此快速入门正在积极开发中,尚未达到最终形式。
它可能与当前版本的 Angular 不完全兼容。
22ndtech Common Lib
这是一个简单的库,它定义了 22ndtech 开发工作的通用函数和数据结构,实现了 角度包格式 v4.0。
特点:
- a simple example library
- unit tests for the library
- a demo application that consumes the library in JIT mode and runs in watch mode
- an integration app that consumes the library in JIT and AOT mode and runs e2e tests
常见任务作为 npm 脚本存在:
npm start
to run a live-reload server with the demo appnpm run test
to test in watch mode, ornpm run test:once
to only run oncenpm run build
to build the librarynpm run lint
to lintnpm run clean
to cleannpm run integration
to run the integration e2e testsnpm install ./relative/path/to/lib
afternpm run build
to test locally in another app
如果您需要调试集成应用程序,请检查 ./integration/README.md
。
The 22ndtech Common Library
这个库实现
了警告:只在开始时这样做以避免意外删除你自己的 git 设置!
What's in the 22ndtech Common Lib?
来源/ ├── 演示/ | └── 应用/ | ├── app.component.ts | └── app.module.ts └── 库/ ├── index.ts └── 源码/ ├── 组件/ | └── lib.component.ts ├── 服务/ | └── lib.service.ts └── module.ts
```
每个文件都有不同的用途,并随着应用程序的增长而独立发展。
src/
之外的文件涉及构建、部署和测试您的应用程序。 它们包括配置文件和外部依赖项。
src/lib/
中的文件“属于”您的库,而 src/demo/
包含一个演示应用程序 加载您的图书馆。
库不会自行运行,因此在开发时拥有此“演示”应用程序非常有用 了解您的图书馆对消费者的看法。
当您运行 npm start
时,将提供演示应用程序。
以下都在 src/
File | Purpose |
---|---|
demo/app/app.component.ts | A demo component that renders the library component and a value from the library service. |
demo/app/app.module.ts | A demo NgModule that imports the Library LibModule . |
lib/src/component/app.component.ts | A sample library component that renders an h2 tag. |
lib/src/service/lib.service.ts | A sample library service that exports a value. |
lib/src/module.ts | The library's main NgModule , LibModule . |
lib/index.ts | The public API of your library, where you choose what to export to consumers. |
The build step
中,你可以通过运行 npm run build
来构建库。 这将生成一个包含上述所有入口点的 dist/
目录。
创建构建的所有逻辑都可以在 ./build.js
中找到。 它由大约 5 个步骤组成:
- Compile with the AOT Compiler (AOT compiler or
ngc
) for ES5 and ES2015. - Inline html and css inside the generated JavaScript files.
- Copy typings, metatada, html and css.
- Create each bundle using Rollup.
- Copy
LICENSE
,package.json
andREADME.md
files
Testing libraries
虽然测试始终很重要,但它在图书馆中尤其重要,因为消费者 应用程序可能会由于库中的错误而中断。
但库被另一个应用程序使用的事实也使得它难以测试。
要正确测试库,您需要进行集成测试。 集成测试之于库就像端到端测试之于应用程序。 它测试应用程序如何安装和使用您的库。
22ndtech Common lib 包含一个名为 integration
的目录,其中包含一个独立的 在 AOT 和 JIT 模式下使用您构建的库的应用程序,通过端到端测试来验证 有用。
要运行集成测试,请执行 npm run integration
,它会执行以下操作:
- Build your library.
- Enter the integration app's directory.
- Install dependencies.
- Build the app in AOT mode.
- Test the app in AOT mode.
- Test the app in JIT mode.
运行集成测试让您更加确信您的库已正确构建。
除了集成测试,您还可以通过 npm run test
以监视模式运行单元测试, 或通过 npm run test:once
单次运行。
您还可以通过将库安装在您拥有的另一个本地存储库中来测试该库。 为此,首先通过 npm run build
构建您的库。 然后使用 dist 文件夹的相对路径从您的其他存储库安装它: npm install relative/path/to/library/dist
。
Appendix: Supporting AOT
AOT 在优化 Angular 应用程序方面发挥着重要作用。 因此,第三方库以与 AOT 兼容的格式发布非常重要 汇编。 否则,将无法将库包含在 AOT 编译的应用程序中。
只有使用 TypeScript 编写的代码才能进行 AOT 编译。
在发布库之前,必须先使用 AOT 编译器 (ngc
) 进行编译。 ngc
通过添加扩展来扩展 tsc
编译器以支持 AOT 编译 常规的 TypeScript 编译。
AOT 编译输出三个文件,必须包含这些文件才能与 AOT 兼容。
Transpiled JavaScript
像往常一样,原始 TypeScript 被转译为常规 JavaScript。
类型文件
JavaScript 无法表示类型。 为了保留原始类型,ngc
将生成.d.ts
类型文件。
元数据 JSON 文件
ngc
为每个 Component
和 NgModule
输出一个 metadata.json 文件。 这些元数据文件代表了原始 NgModule
和 Component
中的信息 装饰器。
元数据可能引用外部模板或 css 文件。 这些外部文件必须包含在库中。
NgFactories
ngc
生成一系列带有 .ngfactory
后缀的文件。 这些文件代表 AOT 编译源,但不应包含在已发布的库中。
相反,消费应用程序中的 ngc
编译器将生成基于 .ngfactory
的文件 在库附带的 JavaScript、类型和元数据上。
Warning
This quickstart is under active development and hasn't yet reached its final form.
It may not be fully compatible with current versions of Angular.
22ndtech Common Lib
This is a simple library defining the common functions and data structures for 22ndtech development efforts, implementing the Angular Package Format v4.0.
Features:
- a simple example library
- unit tests for the library
- a demo application that consumes the library in JIT mode and runs in watch mode
- an integration app that consumes the library in JIT and AOT mode and runs e2e tests
Common tasks are present as npm scripts:
npm start
to run a live-reload server with the demo appnpm run test
to test in watch mode, ornpm run test:once
to only run oncenpm run build
to build the librarynpm run lint
to lintnpm run clean
to cleannpm run integration
to run the integration e2e testsnpm install ./relative/path/to/lib
afternpm run build
to test locally in another app
If you need to debug the integration app, please check ./integration/README.md
.
The 22ndtech Common Library
This library implements
Warning: Do this only in the beginning to avoid accidentally deleting your own git setup!
What's in the 22ndtech Common Lib?
src/ ├── demo/ | └── app/ | ├── app.component.ts | └── app.module.ts └── lib/ ├── index.ts └── src/ ├── component/ | └── lib.component.ts ├── service/ | └── lib.service.ts └── module.ts
```
Each file has a distinct purpose and evolves independently as the application grows.
Files outside src/
concern building, deploying, and testing your app. They include configuration files and external dependencies.
Files inside src/lib/
"belong" to your library, while src/demo/
contains a demo application that loads your library.
Libraries do not run by themselves, so it's very useful to have this "demo" app while developing to see how your library would look like to consumers.
When you run npm start
, the demo application is served.
The following are all in src/
File | Purpose |
---|---|
demo/app/app.component.ts | A demo component that renders the library component and a value from the library service. |
demo/app/app.module.ts | A demo NgModule that imports the Library LibModule . |
lib/src/component/app.component.ts | A sample library component that renders an h2 tag. |
lib/src/service/lib.service.ts | A sample library service that exports a value. |
lib/src/module.ts | The library's main NgModule , LibModule . |
lib/index.ts | The public API of your library, where you choose what to export to consumers. |
The build step
You can build the library by running npm run build
. This will generate a dist/
directory with all the entry points described above.
All the logic for creating the build can be found in ./build.js
. It consists of roughly 5 steps:
- Compile with the AOT Compiler (AOT compiler or
ngc
) for ES5 and ES2015. - Inline html and css inside the generated JavaScript files.
- Copy typings, metatada, html and css.
- Create each bundle using Rollup.
- Copy
LICENSE
,package.json
andREADME.md
files
Testing libraries
While testing is always important, it's especially important in libraries because consumer applications might break due to bugs in libraries.
But the fact that a library is consumed by another application is also what makes it hard to test.
To properly test a library, you need to have an integration tests. An integration test is to libraries what an end-to-end test is to applications. It tests how an app would install and use your library.
The 22ndtech Common lib includes a directory called integration
containing a standalone app that consumes your built library in both AOT and JIT modes, with end-to-end tests to verify it works.
To run the integration tests, do npm run integration
which does the following:
- Build your library.
- Enter the integration app's directory.
- Install dependencies.
- Build the app in AOT mode.
- Test the app in AOT mode.
- Test the app in JIT mode.
Running integration tests gives you greater confidence that your library is properly built.
In addition to integration tests, you can also run unit tests in watch mode via npm run test
, or single-run via npm run test:once
.
You can also test the library by installing it in another local repository you have. To do so, first build your lib via npm run build
. Then install it from your other repo using a relative path to the dist folder: npm install relative/path/to/library/dist
.
Appendix: Supporting AOT
AOT plays an important role in optimizing Angular applications. It's therefore important that third party libraries be published in a format compatible with AOT compilation. Otherwise it will not be possible to include the library in an AOT compiled application.
Only code written in TypeScript can be AOT compiled.
Before publishing the library must first be compiled using the AOT compiler (ngc
). ngc
extends the tsc
compiler by adding extensions to support AOT compilation in addition to regular TypeScript compilation.
AOT compilation outputs three files that must be included in order to be compatible with AOT.
Transpiled JavaScript
As usual the original TypeScript is transpiled to regular JavaScript.
Typings files
JavaScript has no way of representing typings. In order to preserve the original typings, ngc
will generate .d.ts
typings files.
Meta Data JSON files
ngc
outputs a metadata.json file for every Component
and NgModule
. These meta data files represent the information in the original NgModule
and Component
decorators.
The meta data may reference external templates or css files. These external files must be included with the library.
NgFactories
ngc
generates a series of files with an .ngfactory
suffix as well. These files represent the AOT compiled source, but should not be included with the published library.
Instead the ngc
compiler in the consuming application will generate .ngfactory
files based on the JavaScript, Typings and meta data shipped with the library.
你可能也喜欢
- 3dhubs-node 中文文档教程
- @0xproject/sol-cov 中文文档教程
- @1023-ventures/darri-pp-navigation 中文文档教程
- @4geit/swg-account-model 中文文档教程
- @9hub/udf-component 中文文档教程
- @_pearofducks/require-extension-hooks-babel-7 中文文档教程
- @aandelenkoning/xsoft 中文文档教程
- @abbiamo/seller-theme-utils 中文文档教程
- @abnerh69/lib-jitsi-meet 中文文档教程
- @abolo/abolo 中文文档教程