如何使ViewComponent与Importmap Rails一起工作



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

我认为它会很简单:

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

然后我执行了这个命令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。我需要告诉刺激轨道在";app/view_components";文件夹

我的第一个想法是将CCD_;app/javascript/controllers/index.js";并将importmap中的配置从pin_all_from "app/components", preload: true更新为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

可变路径包含应用程序的所有资产路径,例如:

["/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中创建一个控制器,那么logical_path变量将是controllers/foo_controller.js。结果:

CCD_ 16。完美的道路。这就是它工作的原因。

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

您可以自己为组件控制器创建引脚:

# 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

@混合这是个好主意:

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

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

最新更新