javascript - 如何使 ViewComponent 与 Importmap-Rails 一起工作?

标签 javascript ruby-on-rails ruby

我尝试使用 importmap-rails 在 rails 7 中添加 view_component。

我认为这很简单:

  1. 更新config/initializers/assets.rb与:Rails.application.config.assets.paths << Rails.root.join('app', 'components')
  2. 更新app/assets/config/manifest.js与://= link_tree ../../components .js .
  3. 更新config/importmap.rb与:pin_all_from "app/components", preload: true .

然后我执行了这个命令,bin/importmap json , 检查是否一切正常:

{
  "imports": {
    "application": "/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js",
    "@hotwired/turbo-rails": "/assets/turbo.min-e5023178542f05fc063cd1dc5865457259cc01f3fba76a28454060d33de6f429.js",
    "@hotwired/stimulus": "/assets/stimulus.min-b8a9738499c7a8362910cd545375417370d72a9776fb4e766df7671484e2beb7.js",
    "@hotwired/stimulus-loading": "/assets/stimulus-loading-e6261367928db8327c9ed7b698df4a65be8e60f1e405dd2831e4fab49f716e56.js",
    "@hotwired/stimulus-importmap-autoloader": "/assets/stimulus-importmap-autoloader-b2f78229539fa8488bcc30c90ec212a3c2558a7ad04cbc9d43f3ecd85c5671f3.js",
    "controllers/application": "/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js",
    "controllers/foo_controller": "/assets/controllers/foo_controller-45f660adade47dc60929737489aaf6a096ec0bdefa5dc52e509d79ee66982a6c.js",
    "controllers": "/assets/controllers/index-c3026cd9f10d126c4d910db40cdee4112b916f0b357ed0d0489c4c493627d462.js",
    "foo/bar_component_controller": "/assets/foo/bar_component_controller-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.js",
    "foo_component_controller": "/assets/foo_component_controller-0e1379f58b8281a0e5ac54ad8748d2bce7b5da5ddbcb72d91895cf97e47060f2.js"
  }
}

我在最后一行看到了我的 foo_component_controller.js,这是我为 view_component 创建的文件。好的,一切似乎都很好,但 UI 上没有任何反应,为什么?

我忘了更新app/javascript/controllers/index.js .我需要告诉 stimulus-rails 在“app/view_components”文件夹中注册所有 Controller 。

我的第一个想法是添加 eagerLoadControllersFrom("components", application)到“app/javascript/controllers/index.js”并从 pin_all_from "app/components", preload: true 更新我在 importmap 中的配置至 pin_all_from "app/components", under: "components", preload: true ,但我注意到,在运行 bin/importmap json 之后,文件丢失了:

{
  "imports": {
    "application": "/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js",
    "@hotwired/turbo-rails": "/assets/turbo.min-e5023178542f05fc063cd1dc5865457259cc01f3fba76a28454060d33de6f429.js",
    "@hotwired/stimulus": "/assets/stimulus.min-b8a9738499c7a8362910cd545375417370d72a9776fb4e766df7671484e2beb7.js",
    "@hotwired/stimulus-loading": "/assets/stimulus-loading-e6261367928db8327c9ed7b698df4a65be8e60f1e405dd2831e4fab49f716e56.js",
    "@hotwired/stimulus-importmap-autoloader": "/assets/stimulus-importmap-autoloader-b2f78229539fa8488bcc30c90ec212a3c2558a7ad04cbc9d43f3ecd85c5671f3.js",
    "controllers/application": "/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js",
    "controllers/foo_controller": "/assets/controllers/foo_controller-45f660adade47dc60929737489aaf6a096ec0bdefa5dc52e509d79ee66982a6c.js",
    "controllers": "/assets/controllers/index-c3026cd9f10d126c4d910db40cdee4112b916f0b357ed0d0489c4c493627d462.js"
  }
}

为什么?让我们谈谈链轮。更准确地说,让我们谈谈尝试查找文件的方法:

https://github.com/rails/sprockets/blob/e36620745d7150fc33eccffeaf79e741a774499c/lib/sprockets/resolve.rb#L142-L161

      def resolve_logical_path(paths, logical_path, accept)
        extname, mime_type = PathUtils.match_path_extname(logical_path, config[:mime_exts])
        logical_name = logical_path.chomp(extname)

        extname, pipeline = PathUtils.match_path_extname(logical_name, config[:pipeline_exts])
        logical_name = logical_name.chomp(extname)

        parsed_accept = parse_accept_options(mime_type, accept)
        transformed_accepts = expand_transform_accepts(parsed_accept)

        filename, mime_type, deps, index_alias = resolve_under_paths(paths, logical_name, transformed_accepts)

        if filename
          deps << build_file_digest_uri(filename)
          type = resolve_transform_type(mime_type, parsed_accept)
          return filename, type, pipeline, deps, index_alias
        else
          return nil, nil, nil, deps
        end
      end

变量路径包含应用程序的所有 Assets 路径,示例:

["/home/pedro/tempo-livre/importmap-view-component-stimulus/app/assets/config",
 "/home/pedro/tempo-livre/importmap-view-component-stimulus/app/assets/images",
 "/home/pedro/tempo-livre/importmap-view-component-stimulus/app/assets/stylesheets",
 "/home/pedro/tempo-livre/importmap-view-component-stimulus/lib/assets/javascript",
 "/home/pedro/.rvm/gems/ruby-3.1.2/gems/view_component-2.59.0/app/assets/vendor",
 "/home/pedro/.rvm/gems/ruby-3.1.2/gems/stimulus-rails-1.1.0/app/assets/javascripts",
 "/home/pedro/.rvm/gems/ruby-3.1.2/gems/turbo-rails-1.1.1/app/assets/javascripts",
 "/home/pedro/.rvm/gems/ruby-3.1.2/gems/actionview-7.0.3.1/lib/assets/compiled",
 "/home/pedro/tempo-livre/importmap-view-component-stimulus/app/components",
 "/home/pedro/tempo-livre/importmap-view-component-stimulus/app/javascript",
 "/home/pedro/tempo-livre/importmap-view-component-stimulus/vendor/javascript"]

在最后几行中,我们可以看到 app/components正如预期的那样,但是“logical_paths”包含“components/foo_component_controller.js”并且它找不到该文件,因为它连接了两个字符串导致:

/home/pedro/tempo-livre/importmap-view-component-stimulus/app/components/components/foo_component_controller.js .

文件夹组件出现两次。

如果我们在 javascript/controllers 中创建一个 Controller ,则 logical_paths 变量将为 controllers/foo_controller.js .结果:

/home/pedro/tempo-livre/importmap-view-component-stimulus/app/javascript/controllers/foo_controller.js .完美的道路。这就是它起作用的原因。

我卡住了。似乎有必要做出贡献以使这项工作正常进行,我很乐意这样做,但我想知道是否有人找到了另一种方法。也许我错过了什么。我可能是错的,因为我不太了解 ViewComponent + SprocketRails + ImportmapRails + StimulusRails。

最佳答案

您可以自己为组件 Controller 创建引脚:

# config/importmap.rb
components_path = Rails.root.join("app/components")
components_path.glob("**/*_controller.js").each do |controller|
  name = controller.relative_path_from(components_path).to_s.remove(/\.js$/)
  pin "components/#{name}", to: name
  #    ^                        ^ 
  #    |                        |
  # match what                  '- with what `sprockets` expects 
  # `eagerLoadControllersFrom`
  # expects                           
end

# app/javascript/controllers/index.js
import { application } from "controllers/application"
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("components", application)

# config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join("app/components")

# app/assets/config/manifest.js
//= link_tree ../../components .js

@mingle 这是个好主意:

# config/environments/development.rb
config.importmap.cache_sweepers << Rails.root.join("app/components") 

有关pinpin_all_from 的一些详细信息:https://stackoverflow.com/a/72855705/207090

关于javascript - 如何使 ViewComponent 与 Importmap-Rails 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73223634/

相关文章:

ruby - 从 has_many 关系中检索记录数

ruby-on-rails - 更新父级时未设置父级 ID

javascript - 如何在图像上制作圆形蒙版

javascript - 在 array.map() 的参数中使用 "=>"的含义是什么?

javascript - Particle.js + Node + Express - 无法加载资源

ruby-on-rails - Rails 的问题包括查询

ruby-on-rails - 使用 Ruby on Rails 缓存登录表单

javascript - 如何让 jquery jScrollPane 和 jscrollHorizo​​ntalPane 插件一起工作?

javascript - Rails 最佳实践 : Add Javascripts manually or use gem?

ruby-on-rails - 使用引用对象树查询结果 - Ruby on Rails