premailer_bundled_asset_strategy.rb 636 B

1234567891011121314151617181920212223
  1. # frozen_string_literal: true
  2. module PremailerBundledAssetStrategy
  3. def load(url)
  4. asset_host = ENV['CDN_HOST'] || ENV['WEB_DOMAIN'] || ENV.fetch('LOCAL_DOMAIN', nil)
  5. if Webpacker.dev_server.running?
  6. asset_host = "#{Webpacker.dev_server.protocol}://#{Webpacker.dev_server.host_with_port}"
  7. url = File.join(asset_host, url)
  8. end
  9. css = if url.start_with?('http')
  10. HTTP.get(url).to_s
  11. else
  12. url = url[1..] if url.start_with?('/')
  13. Rails.public_path.join(url).read
  14. end
  15. css.gsub(%r{url\(/}, "url(#{asset_host}/")
  16. end
  17. module_function :load
  18. end