ruby-on-rails - rails 6 中的 asset_pack_path 和 image_pack_tag 有什么区别?

标签 ruby-on-rails webpack webpacker

根据我的观点,image_pack_tag 是我们在 View 中用来将图像放置在 HTML 表单中的助手。但是在 webpack 的文档中,我也看到了 asset_pack_path。现在我对此感到困惑。这里的任何人都知道确切的区别吗?

最佳答案

asset_pack_path 只返回路径。 image_pack_tag 返回一个包含 <img> 的字符串html 元素。

module Webpacker::Helper
  ...

  # Computes the relative path for a given Webpacker asset.
  # Returns the relative path using manifest.json and passes it to path_to_asset helper.
  # This will use path_to_asset internally, so most of their behaviors will be the same.
  #
  # Example:
  #
  #   # When extract_css is false in webpacker.yml and the file is a css:
  #   <%= asset_pack_path 'calendar.css' %>  # => nil
  #
  #   # When extract_css is true in webpacker.yml or the file is not a css:
  #   <%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
  def asset_pack_path(name, **options)
    if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
      path_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
    end
  end

  # Computes the absolute path for a given Webpacker asset.
  # Returns the absolute path using manifest.json and passes it to url_to_asset helper.
  # This will use url_to_asset internally, so most of their behaviors will be the same.
  #
  # Example:
  #
  #   # When extract_css is false in webpacker.yml and the file is a css:
  #   <%= asset_pack_url 'calendar.css' %> # => nil
  #
  #   # When extract_css is true in webpacker.yml or the file is not a css:
  #   <%= asset_pack_url 'calendar.css' %> # => "http://example.com/packs/calendar-1016838bab065ae1e122.css"
  def asset_pack_url(name, **options)
    if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
      url_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
    end
  end

  # Creates an image tag that references the named pack file.
  #
  # Example:
  #
  #  <%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
  #  <img alt='Edit Entry' src='/packs/application-k344a6d59eef8632c9d1.png' width='16' height='10' />
  #
  #  <%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>
  #  <img srcset= "/packs/picture-2x-7cca48e6cae66ec07b8e.png 2x" src="/packs/picture-c38deda30895059837cf.png" >
  def image_pack_tag(name, **options)
    if options[:srcset] && !options[:srcset].is_a?(String)
      options[:srcset] = options[:srcset].map do |src_name, size|
        "#{resolve_path_to_image(src_name)} #{size}"
      end.join(", ")
    end

    image_tag(resolve_path_to_image(name), options)
  end
end

关于ruby-on-rails - rails 6 中的 asset_pack_path 和 image_pack_tag 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63718529/

相关文章:

javascript - Karma 不运行测试用例

javascript - 如何从 Rails 中的 JS 文件中引用已编译的 Webpack 包文件的路径?

ruby-on-rails - 我似乎无法 grep 处理纯字符串

javascript - 在 vendor/lib 目录中包含 Rails 中的 js/css 模块

sql - Ruby ActiveRecord 通过关联进行多重联接

ruby-on-rails - 如何使用元搜索和 Rails 3 通过单个文本字段搜索多个字段?

webpack - 配置 webpack 使用绝对路径而不是相对路径

javascript - bundle.js 在 webpack 项目中太大

ruby-on-rails - Rails 6 + Webpacker 添加和使用 yaml-loader

ruby-on-rails-5 - 如何在 Webpacker 中使用 Rails Url helper/Rails 5.1 中的 React-rails