helper_extensions.rb 1.2 KB

123456789101112131415161718192021222324252627
  1. # frozen_string_literal: true
  2. module Webpacker::HelperExtensions
  3. def javascript_pack_tag(name, **options)
  4. src, integrity = current_webpacker_instance.manifest.lookup!(name, type: :javascript, with_integrity: true)
  5. javascript_include_tag(src, options.merge(integrity: integrity))
  6. end
  7. def stylesheet_pack_tag(name, **options)
  8. src, integrity = current_webpacker_instance.manifest.lookup!(name, type: :stylesheet, with_integrity: true)
  9. stylesheet_link_tag(src, options.merge(integrity: integrity))
  10. end
  11. def preload_pack_asset(name, **options)
  12. src, integrity = current_webpacker_instance.manifest.lookup!(name, with_integrity: true)
  13. # This attribute will only work if the assets are on a different domain.
  14. # And Webpack will (correctly) only add it in this case, so we need to conditionally set it here
  15. # otherwise the preloaded request and the real request will have different crossorigin values
  16. # and the preloaded file wont be loaded
  17. crossorigin = 'anonymous' if Rails.configuration.action_controller.asset_host.present?
  18. preload_link_tag(src, options.merge(integrity: integrity, crossorigin: crossorigin))
  19. end
  20. end
  21. Webpacker::Helper.prepend(Webpacker::HelperExtensions)