Zeitwerk

Rails 6.0 から標準で導入された定数の自動読み込み機構。

参考:

fxn/zeitwerk: Efficient and thread-safe code loader for Ruby – GitHub

ドキュメント

参考:

Documentation for zeitwerk | RubyDoc.info

概要

参考:

Rails 6 Beta2 時点の Zeitwerk 情報 要訳 | TechRacho

Zeitwerk integration in Rails 6 (Beta 2) | Riding Rails

A new code loader for Ruby | Xavier Noria

Zeitwerk::Loader.eager_load_all

登録されている全てのローダーで eager_load を実行する。

参考:

Eager loading – fxn/zeitwerk: Efficient and thread-safe code loader for Ruby – GitHub

Method: Zeitwerk::Loader.eager_load_all — Documentation for zeitwerk | RubyDoc.info

zeitwerk/loader.rb at v2.1.5 · fxn/zeitwerk – GitHub

do_not_eager_load

Zeitwerk の eager_load による読み込みから除外する。

参考:

Method: Zeitwerk::Loader#do_not_eager_load — Documentation for zeitwerk | RubyDoc.info

Bring back the do_not_eager_load feature by casperisfine · Pull Request #22 · fxn/zeitwerk – GitHub

fixes eager loading edge case in :zeitwerk mode · rails/rails@278f0df – GitHub

eager_load から除外する

eager_load: true を指定したディレクトリ下で除外するディレクトリをexclude に指定する。

paths.add "app",
  eager_load: true,
  glob:       "{*,*/concerns}",
  exclude:    %w[assets javascript frontend]

参考:

paths.add “app” – rails/configuration.rb at v6.0.0.rc1 · rails/rails – GitHub

イニシャライザにおける定数読み込み

DEPRECATION WARNING が表示される。

DEPRECATION WARNING: Initialization autoloaded the constants ...

Being able to do this is deprecated. Autoloading during initialization is going to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during initialization does not run again. So, if you reload ColorHelper, for example, the expected changes won't be reflected in that stale Module object.

These autoloaded constants have been unloaded.

Please, check the "Autoloading and Reloading Constants" guide for solutions.

参考:

自動読み込みとイニシャライザ | Rails ガイド [公式]

Autoloading and Reloading Constants (Zeitwerk Mode) | Ruby on Rails Guides (Edge) [Official]

take_over – rails/zeitwerk_integration.rb at v6.0.0.rc1 · rails/rails – GitHub

let_zeitwerk_take_over – rails/finisher.rb at v6.0.0.rc1 · rails/rails – GitHub

warn_if_autoloaded – rails/finisher.rb at v6.0.0.rc1 · rails/rails – GitHub

Register features loaded by the classic autoloader in $LOADED_FEATURES when switching to Zeitwerk by casperisfine · Pull Request #35745 · rails/rails – GitHub

Unexpected deprecation warning in Rails 6: ‘Initialization autoloaded the constant Example’ · Issue #36363 · rails/rails – GitHub

How can I preload concerns in a rails initializer using Rails 6/Zeitwerk? – Stack Overflow

Rails6 で従来の自動読み込みを使用する

config/application.rbconfig.autoloader:classic を指定する。

config.autoloader = :classic

参考:

Classes or modules in explicit namespaces are not found · Issue #35475 · rails/rails – GitHub

‘<class:InboundEmail>’: uninitialized constant #::Incineratable (NameError) · Issue #35452 · rails/rails – GitHub

Non autoloaded directories with zeitwerk by casperisfine · Pull Request #35682 · rails/rails – GitHub

ソースコード

参考:

rails/zeitwerk_integration.rb at v6.0.0.rc1 · rails/rails – GitHub

main – rails/autoloaders.rb at v6.0.0.rc1 · rails/rails – GitHub

once – rails/autoloaders.rb at v6.0.0.rc1 · rails/rails – GitHub

inflector – zeitwerk/loader.rb at v2.1.8 · fxn/zeitwerk – GitHub

記事をシェアする:
タグ:

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Protected by reCAPTCHA