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

bqf10yzr  于 2022-12-28  发布在  Java
关注(0)|答案(1)|浏览(194)

我尝试在rails 7中使用importmap-rails添加view_component。
我以为会很简单:
1.将config/initializers/assets.rb更新为:Rails.application.config.assets.paths << Rails.root.join('app', 'components')
1.将app/assets/config/manifest.js更新为://= link_tree ../../components .js.
1.将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"文件夹中注册所有控制器。
我的第一个想法是将eagerLoadControllersFrom("components", application)添加到"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_paths变量将是controllers/foo_controller.js
/home/pedro/tempo-livre/importmap-view-component-stimulus/app/javascript/controllers/foo_controller.js。完美的路径。这就是它工作的原因。
我被困住了。似乎有必要做出贡献来使这个工作正常进行,我很乐意这样做,但我想知道是否有人找到了其他方法。也许我错过了一些东西。我可能是错的,因为我对ViewComponent + SprocketRails + ImportmapRails + StimulusRails了解不多。

7vhp5slm

7vhp5slm1#

您可以自己为组件控制器创建端号:

# 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

相关问题