可以在代码中导入`devDependency`吗? [英] Can you import a `devDependency` in your code?

查看:142
本文介绍了可以在代码中导入`devDependency`吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mobx DevTool的自述文件指导您将其安装为dev依赖项,然后导入它到您的代码.在我看来这是个问题,因为如此SO答案所述,devDependencies是:

Mobx DevTool's README guides you to install it as dev dependency, and then import it into your code. That seems like a problem to me, because devDependencies, as explained by this SO answer, are:

...用于构建过程,可为您提供帮助的工具 管理最终代码的最终方式,第三方测试模块,(例如 webpack的东西)

... used for the build process, tools that help you manage how the end code will end up, third party test modules, (ex. webpack stuff)

如果是这样,那您不应该将devDependency导入代码中是否正确?

If that's true, then is it a correct deduction that you shouldn't import a devDependency into your code?

推荐答案

这取决于导入依赖项的代码的使用方式.如果仅在开发环境中使用它,则可以,它应该是devDependency,并且代码可以使用它.

It depends on how the code that will import the dependency will be used. If it will only be used in a development context, then yes, it should be a devDependency and it's fine for the code to use it.

如果使用该代码的代码将由您的包/模块/库的使用者使用,则它是直接依赖项,应这样注释.

If the code that uses it is going to be used by a consumer of your package/module/library, then it is a direct dependency and should be annotated as such.

这是一种查看使用依赖项的源文件的意图的方式.如果我使用npm install your-cool-package进行安装,则不需要,也不需要安装devDependencies,因为我可能不会从源代码,基准测试或测试中构建您的模块.我只是要食用它.

It's kind of how you view the intention of the source file that consumes the dependency. If I install with npm install your-cool-package, I don't want, and shouldn't need the devDependencies installed because I'm likely not going to be building your module from source, benchmarking, or testing. I'm just going to consume it.

如果我需要依赖项来消耗您的模块,那么它不是devDependencies,而是直接的dependencies(或者可能是peerDependencies).

If I need the dependency to consume your module, then it isn't a devDependencies, it is just a straight up dependencies (or maybe a peerDependencies).

问一下自己:当我使用您的模块时,要使它起作用,是否需要依赖项才能起作用?如果是这样,那就是依赖性.如果不是,那是开发人员的依赖.

Ask yourself this: When I use your module, for it to function, does the dependency need to be there for it to function? If it does, it's a dependency. If not, it's a dev dependency.

如果您为某个东西创作一个插件,则取决于它所插入的东西.如果您编写了一个可以帮助人们进行开发的插件,那么该插件仍然取决于插件所插入的内容,但是安装插件的人员会认为您的插件属于开发依赖项,因为他们不需要开发时就不需要它.但是,您的插件仍然直接取决于它要插入的东西.

If you author a plugin for something, it depends on the thing it plugs in to. If you author a plugin that helps people develop things, the plugin still depends on what it plugs in to, but the person installing your plugin would consider your plugin a development dependency because they don't need it when they aren't developing. However, your plugin still depends directly on the thing that it plugs in to.

听起来您正在为模块( Mobx )编写插件(简称为 cool-plugin ).我不使用 Mobx ,但这听起来像是一种开发工具.由于听起来 cool-plugin 需要 Mobx 来执行任何操作,所以 Mobx 依赖项 cool-plugin ,但消费者会认为 cool-plugin 是他们自己的假设 consumer-module devDependencies ,因为他们没有在生产中不需要它.

It sounds like you are writing a plugin (let's call it cool-plugin) for a module (Mobx). I don't use Mobx, but it sounds like a development tool. Since it sounds like cool-plugin needs Mobx in order to do anything Mobx is a dependency of cool-plugin, but a consumer would consider cool-plugin a devDependencies of their own hypothetical consumer-module because they don't need it in production.

因此,您应该将 Mobx 视为dependencies,因为没有它,模块就没有意义.

Because of that, you should consider Mobx a dependencies because your module doesn't make sense without it.

在某些情况下,您实际上应该将其视为 ,因为消费者模块可能不希望您安装自己的 Mobx 版本,而是希望您与他们应该已经使用的版本进行交互

There is a case to be made that you should actually consider it a peerDependencies because consumer-module probably doesn't want you to install your own version of Mobx but instead wants you to interface with the one they should already be using.

这篇关于可以在代码中导入`devDependency`吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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