Angular CLI 6:库依赖项放在哪里 [英] Angular CLI 6: Where to put library dependencies
问题描述
我正在转换一个库 (ng-app-state) 以使用angular cli,现在 v6 支持库(耶!).
I'm converting a library (ng-app-state) to use the angular cli, now that v6 supports libraries (yay!).
在脚手架和复制一些代码之后,这是我的第一个问题:
After scaffolding and copying in some code, here is my first question:
如何/在哪里添加第 3 方依赖项?
How/where do I add 3rd party dependencies?
到package.json
,还是到projects/ng-app-state/package.json
?
推荐答案
结果是两者兼而有之".理解答案来自于此:
Turns out the answer is kind of "both". Understanding the answer comes from this:
package.json
将在开发过程中使用.您实际上在这里安装了所有库供您自己使用,包括用户也需要的库.您应该在项目的根目录中只有一个node_modules/
目录,而不是在库的目录中(所以只运行npm install
和类似的在这里).projects/ng-app-state/package.json
是将部署到 npm 的内容(在构建过程中添加了一些额外的字段).因此,请复制您的库用户需要的dependencies
和/或peerDependencies
.将devDependencies
放在这里是没有意义的.
package.json
is what will be used during development. You actually install all your libraries here for your own use, including the ones that users will also need. You should only have anode_modules/
directory in the root of your project, not within the library's directory (so only runnpm install
and similar here).projects/ng-app-state/package.json
is what will be deployed to npm (with some additional fields added by the build process). So copy in thedependencies
and/orpeerDependencies
that users of your library will need. There is no point puttingdevDependencies
here.
这就是完整的答案.继续阅读以查看示例.
That is the full answer. Read on to see an example.
在我的例子中 package.json
有一长串 dependencies
和 devDependencies
(你可以看到它这里),但所有这些只会影响我(以及任何想要贡献的人)ng-app-state
).projects/ng-app-state/package.json
小得多,这就是影响我图书馆用户的原因:
In my case package.json
has a long list of many dependencies
and devDependencies
(you can see it here), but all of this only effects me (and anyone who wants to contribute to ng-app-state
). projects/ng-app-state/package.json
is much smaller, and this is what affects users of my library:
{
"name": "ng-app-state",
"version": "8.0.0",
"author": "Simonton Software",
"license": "MIT",
"repository": "simontonsoftware/ng-app-state",
"peerDependencies": {
"@angular/common": ">=6.0.0 <7.0.0",
"@angular/core": ">=6.0.0 <7.0.0",
"@ngrx/store": ">=6.0.0 <7.0.0",
"micro-dash": ">=3.5.0 <4.0.0"
}
}
在运行 ng build np-app-state --prod
以生成将发布到 npm 的内容后,这就是 dist/ng-app-state/
代码>(这是应该发布的):
After running ng build np-app-state --prod
to generate what will be released to npm, this is what ends up in dist/ng-app-state/
(which is what should be published):
{
"name": "ng-app-state",
"version": "8.0.0",
"author": "Simonton Software",
"license": "MIT",
"repository": "simontonsoftware/ng-app-state",
"peerDependencies": {
"@angular/common": ">=6.0.0 <7.0.0",
"@angular/core": ">=6.0.0 <7.0.0",
"@ngrx/store": ">=6.0.0 <7.0.0",
"micro-dash": ">=3.5.0 <4.0.0"
},
"main": "bundles/ng-app-state.umd.js",
"module": "fesm5/ng-app-state.js",
"es2015": "fesm2015/ng-app-state.js",
"esm5": "esm5/ng-app-state.js",
"esm2015": "esm2015/ng-app-state.js",
"fesm5": "fesm5/ng-app-state.js",
"fesm2015": "fesm2015/ng-app-state.js",
"typings": "ng-app-state.d.ts",
"metadata": "ng-app-state.metadata.json",
"sideEffects": false,
"dependencies": {
"tslib": "^1.9.0"
}
}
这篇关于Angular CLI 6:库依赖项放在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!