diff --git a/src/app/_shared/api/articles/index.ts b/src/app/_shared/api/articles/index.ts
index 7d3e517..1c6828d 100644
--- a/src/app/_shared/api/articles/index.ts
+++ b/src/app/_shared/api/articles/index.ts
@@ -3,6 +3,15 @@ import {Article} from '../../models/article';
import {Observable} from 'rxjs/Observable';
const items: Article[] = [
+ {
+ id: '从 Angluar-CLI 到 NPM',
+ title: '从 Angluar-CLI 到 NPM',
+ summary: '使用 Angular-CLI 开发的 module 要如何发布到 NPM 上呢?',
+ content: require('./timliu/ng-cli-to-npm/_index.md'),
+ first: true,
+ tags: ['Angular', 'npm'],
+ authors: ['timliu'],
+ },
{
id: 'Angular2中的StructuralDirective',
title: 'Angular 2 中的 Sturctural Directive',
diff --git a/src/app/_shared/api/articles/timliu/ng-cli-to-npm/_index.md b/src/app/_shared/api/articles/timliu/ng-cli-to-npm/_index.md
new file mode 100644
index 0000000..96b9dab
--- /dev/null
+++ b/src/app/_shared/api/articles/timliu/ng-cli-to-npm/_index.md
@@ -0,0 +1,202 @@
+# 从 Angluar-CLI 到 NPM
+
+## 写作目的
+使用 Angluar-CLI 写好了一个 module,需要重复使用,能不能发到 NPM 上去呢?
+本文将用试错的方式来制作一个 `tl-ui` package(里面有一个 `tl-ui` module),然后发布到 NPM。(`tl-ui` 只是个随意起的名字。)
+
+## 读者指引
+- 本文所述方法是笔者通过试错摸索出来的,过程中大量参考了 [@ng-bootstrap/ng-bootstrap][]。
+- 试错固然啰嗦,需要干货的同学请移步“总结”部分。
+- 本文在测试安装本地 package 的过程中,没有使用 `npm link` -- 笔者试验多次均以 Error 告终。
+- 阅读本文,需要读者简单了解以下知识点:
+ - [`tsconfig.json` 的配置](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)
+ - [Angular-CLI 指令](https://github.com/angular/angular-cli)
+ - [npm 指令](https://docs.npmjs.com/)
+
+## 制作 tl-ui package (里面有个 tl-ui module)
+敲指令:
+```
+npm i angular-cli -g
+npm i typescript -g
+ng new tl-ui // 这个是 package
+cd tl-ui
+ng g module tl-ui // 这个是 module
+```
+### 更改标签前缀设置
+看一下 `src\app\tl-ui\tl-ui.component.ts`,selector 是 `app-tl-ui`,不想要那个 app 前缀?删掉不就行了?
+selector 改成 `tl-ui`之后,tslint 又报错,“你得有 app 前缀啊”。可以这样:
+- 修改 `angular-cli.json`:`"apps": [{ "prefix": "app"}]` => `"apps": [{ "prefix": ""}]`,这样 `ng g` 的时候就不会有自动前缀了。
+- 修改 `tslint.json`:`directive/component-selector-prefix": [true, "app"]` => `directive/component-selector-prefix": [true, "app", "tl"]`,这样 tslint 就接受 tl 这个前缀了。
+
+### serve 起来
+然后,我们在 app.module 里使用 tl-component,看一下效果:
+
+```ts
+// `tl-ui\src\app\tl-ui\tl-ui.module.ts`
+exports: [TlUiComponent]
+// 'tl-ui\src\app\app.module.ts'
+imports: [TlUiModule]
+```
+```html
+
+
tl-ui works!
`, + styles: [] +}) +``` +重新编译,`tsc -p tsconfig.json`;重新安装,`npm i ../tl-ui`(Administator 也会报错,重装两遍通过);(双到了见证奇迹的时刻)serve 起来,`npm start`。 + +奇 迹 终 于 出 现 了 -- `tl-ui works!`。 + +## 发布 tl-ui package 至 npm,并从 npm 安装 +发布很简单,`npm publish` 就好,刚才我们每次更改 package 以后都要编译,可以把编译工作放到 npm script 里: + +```json +// tl-ui\package.json +"scripts": { "prepublish": "tsc -p tsconfig.json" }, +``` +然后,`npm publish`。报错:`This package has been marked as private`。 +改 `package.json`,`"private": false`;重新 publish。报错:`auth required for publishing`。额,还没注册用户... +根据提示运行 `npm adduser`,添加用户名、密码、email,登上了。重新 publish。 +`+ tl-ui@0.0.0`,一个加号,几个意思?先 `npm show` 一下: +``` +> npm show tl-ui time +{ modified: '2016-12-20T15:35:57.084Z', + created: '2016-12-20T15:35:57.084Z', + '0.0.0': '2016-12-20T15:35:57.084Z' } +``` +好像有戏。我们回到 another-project,卸载之前安装的 tl-ui,然后,(见证奇迹的时刻叒到了): +``` +> (at another-project) npm uni tl-ui +> npm i tl-ui -S +``` +安 装 成 功!serve 起来,`npm start`,看到`tl-ui works!`。It works! + +别着急庆祝,我们看看,如果在 another-project 里不删除 tl-ui,重新发布 tl-ui 以后,安装时是否报错。 +首先,更改 `tl-ui` package,随便改,template 里加两个叹号吧;然后发布; +报错,版本号没改;来到 `package.json`,改 ` "version": "0.0.1",`;重新发布,通过; +然后新开一个 cmd 窗口,不用 Administrator 身份,来到 `another-project` 目录,不卸载 `tl-ui`,运行 `npm i tl-ui`,通过; +`npm start`,`tl-ui works!!!`。 + +## 给我们的 package 在 github 上安个家 +将 `tl-ui` package **push** 到 github 上。另外,考虑在 package 里加上 examples(README.md 不够生动),告诉别人如何使用 `tl-ui` module(我们的 app.module 其实就是 examples)。 +然后运行 `ng github-pages:deploy`,之后就可以通过 `https://user.github.io/package-name` 来访问 examples了。 + +## 这才只是个开始 +拿 `tl-ui\package.json` 跟 [@ng-bootstrap/ng-bootstrap][] 的比较一下,就知道,这才只是个开始,做成一个 npm package ... 的路还很长。翻滚翻滚... + +## 总结 +要把 Angular-CLI 做好的 module 发到 NPM 上,需要(步骤很多,但都很简单): +- `ng new package awesome-package` +- `ng g module awesome-module` +- 为了标签前缀,稍微修改 `angular-cli.json` 和 `tslint.json` +- 完成 awesome-module 的开发(template 和 styles 直接写在 component 里) +- 根目录新建 `index.ts`,并 re-export `awesome-module` +- 根目录新建 `tsconfig.json`,设置 declaration 为 true +- 在 `package.json` 里: + - 转移 dependencies 至 devDependencies (也许要保留一些在 dependencies,也许要添加 peerDependencies) + - 将 private 改为 false + - 添加 script: `"prepublish": "tsc -p tsconfig.json"` + - 设定版本号 +- 在本地测试安装,先运行 `npm run prepublish`(就是 tsc),再移步其他 project ,以相对路径来安装 +- 通过本地测试以后,运行 `npm adduser`,`npm publish`;现在可以 `npm i awesome-package`了,然后 `import { awsome-module } from 'awesome-package'`。 +- `git commit`、`git push`。 +- 制作 examples,然后 `ng github-pages:deploy`,让大家看看 awesome package 的使用效果。 +- 不是最后的最后,按照 npm docs 的说法,我们还需要:Brag about it. Send emails, write blogs, blab in IRC. Tell the world how easy it is to install your program! 比如这样: + > [tl-ui](https://rxjs-space.github.io/tl-ui/) -- UI components made easy. + +## 参考 +- [@ng-bootstrap/ng-bootstrap][] +- [NPM Developer Guide](https://docs.npmjs.com/misc/developers) + +[@ng-bootstrap/ng-bootstrap]: https://github.com/ng-bootstrap/ng-bootstrap