如何将两个 npm 包合并为一个 [英] How to merge two npm packages as one
问题描述
我创建了一个新的 npm 包.假设我的新包名称是 my-package.我需要将 bootstrap npm 包导入到我的包中.如何将 Bootstrap 包导入到 my-package 中?此外,我需要更改 bootstrap 包中的一些颜色变量以匹配 my-package.当我执行 npm install my-package
时,它应该将 my-package 和 bootstarp 包安装为一个包.我不想分别安装 my-package 和 bootstrap 包,而是将这两个包安装或合并为 my-package.任何建议和感谢.
I have created a new npm package. Let's say my new package name is my-package. I need to import bootstrap npm package into my-package. How to import Bootstrap package into my-package? Also I need to change some color variables in bootstrap package to match my-package. When I do npm install my-package
it should install my-package and bootstarp package as one package. I don't want to install my-package and bootstrap package separately but install or merge both packages as my-package. Any suggestions and thanks.
推荐答案
这个问题听起来像是植根于 dependencies 和 peerDependencies.
This question sounds like it is rooted in the difference between dependencies and peerDependencies.
在 my-package
包的 package.json
中,您可以将 bootstrap
定义为 dependency
> 或 peerDependency
.
In the package.json
for the my-package
package, you can define bootstrap
as either a dependency
or peerDependency
.
如果 bootstrap
作为 peerDependency
包含在内,那么它需要任何使用你的包的人也安装 bootstrap.这将导致他们的包树看起来像这样:
If bootstrap
is included as a peerDependency
, then it will require anyone who uses your package to also install bootstrap as well. This will result in their package tree looking like this:
➜ consumer npm ls
consumer@1.0.0 /private/tmp/consumer
├── bootstrap@4.5.2
├── jquery@3.5.1
├── my-package@^1.0.0
└── popper.js@1.16.1
注意消费项目不仅需要依赖 my-package
,还需要引导程序和所有引导程序对等依赖项.
Note how the consuming project is required to have a dependency for not only my-package
, but also bootstrap and all the bootstrap peer dependencies as well.
要完成您想要的操作,my-package
库的 package.json
应包含这些作为其自己的依赖项.例如
To accomplish what you want, the package.json
for the my-package
lib should include those as its own dependencies. e.g.
{
"name": "my-package",
"dependencies": {
"bootstrap": "^4.5.2",
"jquery": "^3.5.1",
"popper.js": "^1.16.1"
}
...
}
通过这样做,任何使用 my-package
的项目将只能指定 my-package
作为依赖项,并且所有嵌套的依赖项也将被抓取.下面是依赖树的样子:
By doing this, any project that consumes my-package
will be able to specify only my-package
as a dependency and all nested dependencies will be grabbed as well. Here is what the dependency tree looks like with the above:
➜ consumer npm ls
consumer@1.0.0 /private/tmp/consumer
└─┬ my-package@1.0.0 -> /private/tmp/my-package
├── bootstrap@4.5.2
├── jquery@3.5.1
└── popper.js@1.16.1
至于更改用于引导程序的颜色,您可以按照您使用的版本的主题文档来完成此操作:https://getbootstrap.com/docs/4.5/getting-started/theming/
这篇关于如何将两个 npm 包合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!