使用npm安装依赖项的子集 [英] Install subset of dependencies with npm

查看:244
本文介绍了使用npm安装依赖项的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用别名或功能标签"在npm中指定依赖项的子集?也就是说,如果有人知道他们只会使用我软件包的某些功能子集,那么他们可以指定那些功能,并在npm install上仅下载与那些功能相关的依赖项?

Is there a way to specify subsets of dependencies in npm, with an alias or "feature tag"? That is, if someone knows that they will only be using some limited subset of the features of my package, they can specify those features and, on npm install, only download the dependencies relevant to those features?

我的软件包有很多依赖关系,并且安装需要将近半小时,但是大多数用户只需要一部分功能.我在考虑类似如何将依赖项划分为devDependencies和依赖项,但使用n个组而不是仅使用这两个组.例如:

My package has a very large number of dependencies and takes nearly half an hour to install, but most users only need a subset of their functionality. I'm thinking of something like how dependencies can be divided into devDependencies and dependencies, but with n groups instead of just those two. For example:

npm install --feature feature1 --feature feature2

通过阅读文档,我认为这里的答案是否",但是您对此案的建议是什么?将程序包拆分为较小的插件程序包,并让用户安装所需的插件程序吗?我不想让用户配置太复杂的东西.

From reading the docs, I think the answer here is "no", but what would be your suggestion for this case? Split the package into smaller plugin packages and have users install the plugins that they want? I don't want something that is too complicated for users to configure.

推荐答案

最简单的答案是不,npm并不是为此而设计的,主要是因为依赖树非常大,对于大多数用户而言,这种用例确实会使事情变得复杂.话虽如此,我写了一个程序包来做到这一点.

The short answer is no, npm was not designed for this mostly because dependency trees are incredibly large, and this use case could really just complicate things for most users. That being said, I wrote a package to do it.

我的软件包为install-subset,可以与npm install -g install-subset

My package is install-subset, and can be installed globally with npm install -g install-subset

https://www.npmjs.com/package/install-subset

基本上,您可以像这样在package.json中为命名子集构建包含列表和排除列表:

Essentially you build inclusion lists and exclusion lists for named subsets in your package.json like this:

"subsets": {
    "build": {
        "include": [
            "babel-cli",
            "dotenv"
        ]
    },
    "test": {
        "exclude": [
            "eslint",
            "lint-rules",
            "prettier"
        ]
    }
}

然后用例如install-subset test

这将临时重写您的package.json,以不安装那些排除的软件包,然后将其还原,这取决于软件包,可以节省大量的时间和带宽.

This will temporarily rewrite your package.json to not install those packages excluded, then restore it, which depending on the packages can save a lot of time and bandwidth.

还可以处理纱线,它是开源的,欢迎发布问题/PR.

Also works with yarn, is open source and issues/PRs are welcome.

这篇关于使用npm安装依赖项的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆