Bundler.setup vs. Bundler.require

本文详细解释了Bundler在Rails 3中的自动加载机制,并对比了Bundler.setup与Bundler.require的区别,指导开发者正确使用Bundler进行Gem管理。


December 20, 2010

TL;DR Use Bundler.require instead of Bundler.setup

I think pretty much everyone knows that when you use Bundler with Rails 3, you don’t have to require your gems manually; it’s just all taken care of when you define them in the Gemfile. But I think a lot of people don’t understand exactly how this works, because when I see Bundler used outside of Rails, I consistently see gems being defined in the Gemfile and then manually required in the app.

I’m guessing this is because in the Bundler docs the most prominent example tells you to use Bundler.setup, and so people assume the auto-requiring is part of Rails and do this:

# Gemfile
source 'http://rubygems.org'

gem 'sinatra'
gem 'haml'

# config.ru
require 'rubygems'
require 'bundler'
Bundler.setup(:default)

require 'sinatra'
require 'sinatra/base'
require 'haml'

But actually it’s all Bundler. Instead of Bundler.setup you can call Bundler.require, which will not only set up the load paths but also require the gems as well. This is how Rails automatically requires the gems you specify. So the best way to do it (in most situations) is like this:

# Gemfile
source 'http://rubygems.org'

gem 'sinatra', :require => 'sinatra/base'
gem 'haml'

# config.ru
require 'rubygems'
require 'bundler'
Bundler.require(:default)

Note that you can specify a different name to require a gem by if the file name happens to be different from the gem name. And, as an added bonus, you can pass in multiple groups to Bundler.require, so you could also include gems specific to the current environment like this:

Bundler.require(:default, ENV['RACK_ENV'].to_sym)

Uncaught (in promise) ReferenceError: Swiper is not defined at setup (index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/databaseinfo/WeiXinCode.vue?vue&type=script&setup=true&lang=js:8:20) at callWithErrorHandling (runtime-core.esm-bundler.js:350:19) at setupStatefulComponent (runtime-core.esm-bundler.js:8018:25) at setupComponent (runtime-core.esm-bundler.js:7979:36) at mountComponent (runtime-core.esm-bundler.js:5329:7) at processComponent (runtime-core.esm-bundler.js:5295:9) at patch (runtime-core.esm-bundler.js:4826:11) at ReactiveEffect.componentUpdateFn [as fn] (runtime-core.esm-bundler.js:5519:9) at ReactiveEffect.run (reactivity.esm-bundler.js:279:19) at ReactiveEffect.runIfDirty (reactivity.esm-bundler.js:317:12) setup @ index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/databaseinfo/WeiXinCode.vue?vue&type=script&setup=true&lang=js:8 callWithErrorHandling @ runtime-core.esm-bundler.js:350 setupStatefulComponent @ runtime-core.esm-bundler.js:8018 setupComponent @ runtime-core.esm-bundler.js:7979 mountComponent @ runtime-core.esm-bundler.js:5329 processComponent @ runtime-core.esm-bundler.js:5295 patch @ runtime-core.esm-bundler.js:4826 componentUpdateFn @ runtime-core.esm-bundler.js:5519 run @ reactivity.esm-bundler.js:279 runIfDirty @ reactivity.esm-bundler.js:317 callWithErrorHandling @ runtime-core.esm-bundler.js:350 flushJobs @ runtime-core.esm-bundler.js:555 Promise.then queueFlush @ runtime-core.esm-bundler.js:469 queueJob @ runtime-core.esm-bundler.js:464 baseWatchOptions.scheduler @ runtime-core.esm-bundler.js:6318 effect.scheduler @ reactivity.esm-bundler.js:1874 trigger @ reactivity.esm-bundler.js:307 endBatch @ reactivity.esm-bundler.js:365 notify @ reactivity.esm-bundler.js:651 trigger @ reactivity.esm-bundler.js:625 set value @ reactivity.esm-bundler.js:1496 finalizeNavigation @ vue-router.mjs:3519 eval @ vue-router.mjs:3384 Promise.then pushWithRedirect @ vue-router.mjs:3351 push @ vue-router.mjs:3276 install @ vue-router.mjs:3720 use @ runtime-core.esm-bundler.js:3997 eval @ main.js:15 ./src/main.js @ app.js:2896 __webpack_require__ @ app.js:4484 (匿名) @ app.js:5596 __webpack_require__.O @ app.js:4526 (匿名) @ app.js:5597 (匿名) @ app.js:5599
07-01
client:733 [vite] connecting... ChatRoom.vue:109 socket连接失败: server error Error: server error at Socket._onPacket (socket.js:253:33) at Emitter.emit (index.js:136:20) at XHR.onPacket (transport.js:99:15) at callback (polling.js:79:18) at Array.forEach (<anonymous>) at XHR.onData (polling.js:82:53) at Emitter.emit (index.js:136:20) at _Request._onLoad (polling-xhr.js:193:18) at xhr.onreadystatechange (polling-xhr.js:129:26) (匿名) @ ChatRoom.vue:109 Emitter.emit @ index.js:136 onerror @ socket.js:439 Emitter.emit @ index.js:136 onError @ manager.js:124 Emitter.emit @ index.js:136 _onError @ socket.js:495 _onPacket @ socket.js:256 Emitter.emit @ index.js:136 onPacket @ transport.js:99 callback @ polling.js:79 onData @ polling.js:82 Emitter.emit @ index.js:136 _onLoad @ polling-xhr.js:193 xhr.onreadystatechange @ polling-xhr.js:129 XMLHttpRequest.send _create @ polling-xhr.js:139 _Request @ polling-xhr.js:75 request @ polling-xhr.js:253 doPoll @ polling-xhr.js:52 _poll @ polling.js:59 doOpen @ polling.js:19 open @ transport.js:47 _open @ socket.js:197 _SocketWithoutUpgrade @ socket.js:150 SocketWithUpgrade @ socket.js:565 Socket @ socket.js:725 open @ manager.js:111 Manager @ manager.js:39 lookup2 @ index.js:29 setup @ ChatRoom.vue:95 callWithErrorHandling @ runtime-core.esm-bundler.js:199 setupStatefulComponent @ runtime-core.esm-bundler.js:7996 setupComponent @ runtime-core.esm-bundler.js:7957 mountComponent @ runtime-core.esm-bundler.js:5285 processComponent @ runtime-core.esm-bundler.js:5251 patch @ runtime-core.esm-bundler.js:4767 componentUpdateFn @ runtime-core.esm-bundler.js:5477 run @ reactivity.esm-bundler.js:237 runIfDirty @ reactivity.esm-bundler.js:275 callWithErrorHandling @ runtime-core.esm-bundler.js:199 flushJobs @ runtime-core.esm-bundler.js:408 Promise.then queueFlush @ runtime-core.esm-bundler.js:322 queuePostFlushCb @ runtime-core.esm-bundler.js:336 queueEffectWithSuspense @ runtime-core.esm-bundler.js:7409 baseWatchOptions.scheduler @ runtime-core.esm-bundler.js:6284 effect2.scheduler @ reactivity.esm-bundler.js:1854 trigger @ reactivity.esm-bundler.js:265 endBatch @ reactivity.esm-bundler.js:323 notify @ reactivity.esm-bundler.js:614 trigger @ reactivity.esm-bundler.js:588 set value @ reactivity.esm-bundler.js:1472 finalizeNavigation @ vue-router.mjs:1388 (匿名) @ vue-router.mjs:1316 Promise.then pushWithRedirect @ vue-router.mjs:1304 push @ vue-router.mjs:1257 install @ vue-router.mjs:1504 use @ runtime-core.esm-bundler.js:3928 (匿名) @ main.js:13 client:827 [vite] connected.
11-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值