简体   繁体   English

处理 npm 包依赖关系的最佳方法?

[英]Best way to handle an npm packages dependencies?

I am trying to breakout a couple of Vue.js components from a primary application into an npm package stored on a repository.我正在尝试将几个 Vue.js 组件从主应用程序拆分为存储在存储库中的 npm package。 This package can then be imported and used across two separate sites.然后可以导入此 package 并在两个单独的站点上使用。 I am using Webpack to bundle everything but have had a few queries about the layout that should be used.我正在使用 Webpack 来捆绑所有内容,但对应该使用的布局有一些疑问。

Seeing that things like Vue.js and Vuex will installed as dependencies on the main application, I know that the package will have access to these once installed on the two main applications.看到 Vue.js 和 Vuex 之类的东西将作为依赖项安装在主应用程序上,我知道 package 一旦安装在两个主应用程序上就可以访问这些内容。

My primary confusion is how do I deal with dependencies that belong to the package only, do I bundle this as part of the webpack and will running npm install on the applications automatically install the 'dependencies dependencies' if you will?我的主要困惑是如何处理仅属于 package 的依赖项,我是否将其捆绑为 webpack 的一部分并将运行npm install在应用程序上是否会自动安装依赖项? Is there a general standard for these things?这些东西有通用标准吗?

The simplest thing to do is list them in your dependencies section in package.json .最简单的做法是在package.jsondependencies项部分列出它们。 The package manager tool you're using (usually either npm or 'yarn`) will take responsibility for deduping the dependences, so if your parent app and your subproject both have the same dependencies, you'll only end up with one copy in the final bundle.您正在使用的 package 管理器工具(通常是npm或 'yarn`)将负责删除依赖项,因此如果您的父应用程序和子项目都具有相同的依赖项,那么您最终只会在最终捆绑。

I would strongly suggest that you be looser on version numbers in the component projects then you are in the parent project.我强烈建议您在组件项目中的版本号比在父项目中更宽松。 So in the parent maybe you say the dependency is "vue": "1.2.3" and in the component project it's "vue": "^1.1.0" or something along those lines.因此,在父项中,您可能会说依赖项是"vue": "1.2.3" ,而在组件项目中,它是"vue": "^1.1.0"或类似的东西。 This way the parent can control the specific versions and your component picks up the actual version from the parent.这样,父级可以控制特定版本,并且您的组件从父级获取实际版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM