9

我正在寻找简单但很好的方法来清理 gemfile 并使 rails 启动更快。如何获取所有必需 gem 与所有已加载 gem 的列表。

4

4 回答 4

22

bundle clean --force将删除以前安装但当前未在当前 Gemfile.lock 清单中使用的旧 gem(或当前使用的旧版本的 gem)。

于 2013-10-03T20:25:11.257 回答
3

首先,如果你想查看你的项目使用了哪些 gems,我邀请你gem server在你的项目文件夹根目录下运行,然后去http://0.0.0.0:8808/

您将能够了解您的项目正在使用的所有 gem 的依赖关系。它还将向您显示同一 gem 的所有版本

要删除旧版本的 gem,您可以像 @changerainbows 提到的那样运行 bundle clean --force

在此步骤之后,再次运行您的 gem 服务器并观察结果,一个干净且易于理解的 gem 列表,其中包含所有依赖项。

于 2019-07-19T12:00:50.607 回答
2

It depends what you're after here.

If you're looking to remove old, unused gem versions, then bundle clean.

If you've been adding gems as you develop and have lost track of the ones you actually use, and have good test coverage, then try this answer.

If you want to reduce the number of gems rails pulls in at startup to the bare minimum, try gem_bench.

于 2015-02-20T12:40:05.603 回答
0

I think it is impossible. When your APP starts it loads gems from Gemfile.lock but it does not know if they (gems) are needed in your code or not. The APP inform you by raising an exception When something calls a class or method that is undefined if some needed gem is missed (if you remove it from Gemfile), but this can happen at any moment (not during starting your APP).

So if you are looking the way to clean up your gem list I think the best way to do it manually (I know it is not easy way). Analyse each gem to find out what functionality it provides and decide (or find in your code) if it is needed or not. Additionally tests (if you have them) should help you a lot.

于 2013-10-03T20:02:19.727 回答