如果我很好理解您的问题,您想创建一个组件并将其作为库发布。
1。创建组件
您需要创建 foo.component.ts 文件及其 html 模板。你会更喜欢内联 CSS(在 @Component 的 styles 属性中)。
!!!不要忘记在@Component 中设置moduleId: module.id 以使用相对路径将模板链接到您的组件!!!
2。编译代码(一旦你测试过,测试部分是隐式的)
您需要使用tsc 和tsconfig 编译您的组件,应该如下所示:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"sourceMap": true,
"inlineSources": false,
"declaration": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"stripInternal": true,
"skipLibCheck": true
},
"files": [
"index.ts",
"typings/index.d.ts"
]
}
3。发布到 npm
首先,创建你的 .npmignore 文件,这里是一个例子:
.idea
*.ts
!*.d.ts
/typings.json
/typings/
/node_modules/
/.gitignore
/.npmignore
/.travis.yml
/test
/karma.conf.js
/karma-test-shim.js
/rollup.config.js
/rollup-min.config.js
/systemjs.config.js
/tsconfig.json
/tsconfig-build.json
如果您没有使用 JetBrains IDE,请删除 .idea 条目。
然后在package.json中设置您的发布配置:
"publishConfig": {
"registry": "https://registry.npmjs.org/"
}
一切准备就绪后,您可以npm publish。
示例
angular2 的外部库的一个很好的例子可以在这里找到:
https://github.com/noemi-salaun/ng2-logger/
或者在这里获取更新的,包括 webpack 兼容性:
https://github.com/kaiu-lab/serializer
它不是一个组件,但整个发布配置、捆绑和测试逻辑都做得很好。